1 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
2 |
|
3 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
4 |
|
5 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6 |
|
7 | import { factory } from '../../utils/factory.js';
|
8 | import { deepMap } from '../../utils/collection.js';
|
9 | import { isInteger } from '../../utils/number.js';
|
10 | import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11.js';
|
11 | import { createAlgorithm12 } from '../../type/matrix/utils/algorithm12.js';
|
12 | import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js';
|
13 | import { roundNumber } from '../../plain/number/index.js';
|
14 | var NO_INT = 'Number of decimals in function round must be an integer';
|
15 | var name = 'round';
|
16 | var dependencies = ['typed', 'matrix', 'equalScalar', 'zeros', 'BigNumber', 'DenseMatrix'];
|
17 | export var createRound = factory(name, dependencies, (_ref) => {
|
18 | var {
|
19 | typed,
|
20 | matrix,
|
21 | equalScalar,
|
22 | zeros,
|
23 | BigNumber,
|
24 | DenseMatrix
|
25 | } = _ref;
|
26 | var algorithm11 = createAlgorithm11({
|
27 | typed,
|
28 | equalScalar
|
29 | });
|
30 | var algorithm12 = createAlgorithm12({
|
31 | typed,
|
32 | DenseMatrix
|
33 | });
|
34 | var algorithm14 = createAlgorithm14({
|
35 | typed
|
36 | });
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | return typed(name, _objectSpread(_objectSpread({}, roundNumberSignatures), {}, {
|
74 | Complex: function Complex(x) {
|
75 | return x.round();
|
76 | },
|
77 | 'Complex, number': function ComplexNumber(x, n) {
|
78 | if (n % 1) {
|
79 | throw new TypeError(NO_INT);
|
80 | }
|
81 |
|
82 | return x.round(n);
|
83 | },
|
84 | 'Complex, BigNumber': function ComplexBigNumber(x, n) {
|
85 | if (!n.isInteger()) {
|
86 | throw new TypeError(NO_INT);
|
87 | }
|
88 |
|
89 | var _n = n.toNumber();
|
90 |
|
91 | return x.round(_n);
|
92 | },
|
93 | 'number, BigNumber': function numberBigNumber(x, n) {
|
94 | if (!n.isInteger()) {
|
95 | throw new TypeError(NO_INT);
|
96 | }
|
97 |
|
98 | return new BigNumber(x).toDecimalPlaces(n.toNumber());
|
99 | },
|
100 | BigNumber: function BigNumber(x) {
|
101 | return x.toDecimalPlaces(0);
|
102 | },
|
103 | 'BigNumber, BigNumber': function BigNumberBigNumber(x, n) {
|
104 | if (!n.isInteger()) {
|
105 | throw new TypeError(NO_INT);
|
106 | }
|
107 |
|
108 | return x.toDecimalPlaces(n.toNumber());
|
109 | },
|
110 | Fraction: function Fraction(x) {
|
111 | return x.round();
|
112 | },
|
113 | 'Fraction, number': function FractionNumber(x, n) {
|
114 | if (n % 1) {
|
115 | throw new TypeError(NO_INT);
|
116 | }
|
117 |
|
118 | return x.round(n);
|
119 | },
|
120 | 'Array | Matrix': function ArrayMatrix(x) {
|
121 |
|
122 | return deepMap(x, this, true);
|
123 | },
|
124 | 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) {
|
125 | return algorithm11(x, y, this, false);
|
126 | },
|
127 | 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) {
|
128 | return algorithm14(x, y, this, false);
|
129 | },
|
130 | 'number | Complex | BigNumber, SparseMatrix': function numberComplexBigNumberSparseMatrix(x, y) {
|
131 |
|
132 | if (equalScalar(x, 0)) {
|
133 |
|
134 | return zeros(y.size(), y.storage());
|
135 | }
|
136 |
|
137 | return algorithm12(y, x, this, true);
|
138 | },
|
139 | 'number | Complex | BigNumber, DenseMatrix': function numberComplexBigNumberDenseMatrix(x, y) {
|
140 |
|
141 | if (equalScalar(x, 0)) {
|
142 |
|
143 | return zeros(y.size(), y.storage());
|
144 | }
|
145 |
|
146 | return algorithm14(y, x, this, true);
|
147 | },
|
148 | 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) {
|
149 |
|
150 | return algorithm14(matrix(x), y, this, false).valueOf();
|
151 | },
|
152 | 'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) {
|
153 |
|
154 | return algorithm14(matrix(y), x, this, true).valueOf();
|
155 | }
|
156 | }));
|
157 | });
|
158 | var roundNumberSignatures = {
|
159 | number: roundNumber,
|
160 | 'number, number': function numberNumber(x, n) {
|
161 | if (!isInteger(n)) {
|
162 | throw new TypeError(NO_INT);
|
163 | }
|
164 |
|
165 | if (n < 0 || n > 15) {
|
166 | throw new Error('Number of decimals in function round must be in te range of 0-15');
|
167 | }
|
168 |
|
169 | return roundNumber(x, n);
|
170 | }
|
171 | };
|
172 | export var createRoundNumber = factory(name, ['typed'], (_ref2) => {
|
173 | var {
|
174 | typed
|
175 | } = _ref2;
|
176 | return typed(name, roundNumberSignatures);
|
177 | }); |
\ | No newline at end of file |