mathjs 7.2.0 → 7.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +34 -0
- package/dist/math.js +3421 -2476
- package/dist/math.min.js +5 -5
- package/dist/math.min.map +1 -1
- package/docs/expressions/syntax.md +30 -4
- package/docs/reference/functions/bin.md +38 -0
- package/docs/reference/functions/ceil.md +10 -1
- package/docs/reference/functions/fix.md +10 -2
- package/docs/reference/functions/floor.md +12 -3
- package/docs/reference/functions/hex.md +38 -0
- package/docs/reference/functions/lsolve.md +2 -1
- package/docs/reference/functions/lsolveAll.md +45 -0
- package/docs/reference/functions/oct.md +38 -0
- package/docs/reference/functions/rotationMatrix.md +51 -0
- package/docs/reference/functions/round.md +6 -2
- package/docs/reference/functions/usolve.md +2 -1
- package/docs/reference/functions/usolveAll.md +45 -0
- package/docs/reference/functions.md +8 -2
- package/es/entry/dependenciesAny/dependenciesBin.generated.js +10 -0
- package/es/entry/dependenciesAny/dependenciesCeil.generated.js +4 -0
- package/es/entry/dependenciesAny/dependenciesFix.generated.js +2 -0
- package/es/entry/dependenciesAny/dependenciesFloor.generated.js +4 -0
- package/es/entry/dependenciesAny/dependenciesHex.generated.js +10 -0
- package/es/entry/dependenciesAny/dependenciesLsolveAll.generated.js +22 -0
- package/es/entry/dependenciesAny/dependenciesOct.generated.js +10 -0
- package/es/entry/dependenciesAny/dependenciesRotationMatrix.generated.js +30 -0
- package/es/entry/dependenciesAny/dependenciesUsolveAll.generated.js +22 -0
- package/es/entry/dependenciesAny.generated.js +6 -0
- package/es/entry/impureFunctionsAny.generated.js +63 -57
- package/es/entry/pureFunctionsAny.generated.js +323 -277
- package/es/expression/embeddedDocs/embeddedDocs.js +12 -0
- package/es/expression/embeddedDocs/function/algebra/lsolve.js +2 -2
- package/es/expression/embeddedDocs/function/algebra/lsolveAll.js +8 -0
- package/es/expression/embeddedDocs/function/algebra/usolve.js +2 -2
- package/es/expression/embeddedDocs/function/algebra/usolveAll.js +8 -0
- package/es/expression/embeddedDocs/function/matrix/rotationMatrix.js +8 -0
- package/es/expression/embeddedDocs/function/utils/bin.js +8 -0
- package/es/expression/embeddedDocs/function/utils/hex.js +8 -0
- package/es/expression/embeddedDocs/function/utils/oct.js +8 -0
- package/es/expression/parse.js +28 -1
- package/es/factoriesAny.js +6 -0
- package/es/function/algebra/solver/lsolve.js +42 -69
- package/es/function/algebra/solver/lsolveAll.js +213 -0
- package/es/function/algebra/solver/lusolve.js +12 -27
- package/es/function/algebra/solver/usolve.js +41 -64
- package/es/function/algebra/solver/usolveAll.js +213 -0
- package/es/function/algebra/solver/utils/solveValidation.js +66 -107
- package/es/function/arithmetic/ceil.js +88 -4
- package/es/function/arithmetic/fix.js +43 -6
- package/es/function/arithmetic/floor.js +90 -6
- package/es/function/arithmetic/mod.js +10 -1
- package/es/function/arithmetic/round.js +6 -2
- package/es/function/matrix/rotationMatrix.js +175 -0
- package/es/function/matrix/sqrtm.js +4 -0
- package/es/function/probability/pickRandom.js +2 -6
- package/es/function/statistics/variance.js +4 -4
- package/es/function/string/baseUtils.js +36 -0
- package/es/function/string/bin.js +23 -0
- package/es/function/string/hex.js +23 -0
- package/es/function/string/oct.js +23 -0
- package/es/type/bignumber/BigNumber.js +4 -1
- package/es/type/number.js +10 -0
- package/es/utils/object.js +3 -1
- package/es/version.js +1 -1
- package/examples/advanced/web_server/math_worker.js +1 -1
- package/lib/entry/dependenciesAny/dependenciesBin.generated.js +20 -0
- package/lib/entry/dependenciesAny/dependenciesCeil.generated.js +6 -0
- package/lib/entry/dependenciesAny/dependenciesFix.generated.js +3 -0
- package/lib/entry/dependenciesAny/dependenciesFloor.generated.js +6 -0
- package/lib/entry/dependenciesAny/dependenciesHex.generated.js +20 -0
- package/lib/entry/dependenciesAny/dependenciesLsolveAll.generated.js +38 -0
- package/lib/entry/dependenciesAny/dependenciesOct.generated.js +20 -0
- package/lib/entry/dependenciesAny/dependenciesRotationMatrix.generated.js +50 -0
- package/lib/entry/dependenciesAny/dependenciesUsolveAll.generated.js +38 -0
- package/lib/entry/dependenciesAny.generated.js +48 -0
- package/lib/entry/impureFunctionsAny.generated.js +65 -59
- package/lib/entry/pureFunctionsAny.generated.js +373 -321
- package/lib/expression/embeddedDocs/embeddedDocs.js +18 -0
- package/lib/expression/embeddedDocs/function/algebra/lsolve.js +2 -2
- package/lib/expression/embeddedDocs/function/algebra/lsolveAll.js +15 -0
- package/lib/expression/embeddedDocs/function/algebra/usolve.js +2 -2
- package/lib/expression/embeddedDocs/function/algebra/usolveAll.js +15 -0
- package/lib/expression/embeddedDocs/function/matrix/rotationMatrix.js +15 -0
- package/lib/expression/embeddedDocs/function/utils/bin.js +15 -0
- package/lib/expression/embeddedDocs/function/utils/hex.js +15 -0
- package/lib/expression/embeddedDocs/function/utils/oct.js +15 -0
- package/lib/expression/parse.js +28 -1
- package/lib/factoriesAny.js +48 -0
- package/lib/function/algebra/solver/lsolve.js +42 -69
- package/lib/function/algebra/solver/lsolveAll.js +223 -0
- package/lib/function/algebra/solver/lusolve.js +12 -27
- package/lib/function/algebra/solver/usolve.js +41 -64
- package/lib/function/algebra/solver/usolveAll.js +223 -0
- package/lib/function/algebra/solver/utils/solveValidation.js +65 -106
- package/lib/function/arithmetic/ceil.js +91 -4
- package/lib/function/arithmetic/fix.js +44 -6
- package/lib/function/arithmetic/floor.js +93 -6
- package/lib/function/arithmetic/mod.js +10 -1
- package/lib/function/arithmetic/round.js +6 -2
- package/lib/function/matrix/rotationMatrix.js +185 -0
- package/lib/function/matrix/sqrtm.js +4 -0
- package/lib/function/probability/pickRandom.js +3 -7
- package/lib/function/statistics/variance.js +4 -4
- package/lib/function/string/baseUtils.js +45 -0
- package/lib/function/string/bin.js +31 -0
- package/lib/function/string/hex.js +31 -0
- package/lib/function/string/oct.js +31 -0
- package/lib/header.js +2 -2
- package/lib/type/bignumber/BigNumber.js +3 -1
- package/lib/type/number.js +10 -0
- package/lib/utils/object.js +3 -1
- package/lib/version.js +1 -1
- package/package.json +12 -12
- package/src/entry/dependenciesAny/dependenciesBin.generated.js +11 -0
- package/src/entry/dependenciesAny/dependenciesCeil.generated.js +4 -0
- package/src/entry/dependenciesAny/dependenciesFix.generated.js +2 -0
- package/src/entry/dependenciesAny/dependenciesFloor.generated.js +4 -0
- package/src/entry/dependenciesAny/dependenciesHex.generated.js +11 -0
- package/src/entry/dependenciesAny/dependenciesLsolveAll.generated.js +23 -0
- package/src/entry/dependenciesAny/dependenciesOct.generated.js +11 -0
- package/src/entry/dependenciesAny/dependenciesRotationMatrix.generated.js +31 -0
- package/src/entry/dependenciesAny/dependenciesUsolveAll.generated.js +23 -0
- package/src/entry/dependenciesAny.generated.js +6 -0
- package/src/entry/impureFunctionsAny.generated.js +104 -92
- package/src/entry/pureFunctionsAny.generated.js +94 -82
- package/src/expression/embeddedDocs/embeddedDocs.js +12 -0
- package/src/expression/embeddedDocs/function/algebra/lsolve.js +2 -2
- package/src/expression/embeddedDocs/function/algebra/lsolveAll.js +17 -0
- package/src/expression/embeddedDocs/function/algebra/usolve.js +2 -2
- package/src/expression/embeddedDocs/function/algebra/usolveAll.js +15 -0
- package/src/expression/embeddedDocs/function/matrix/rotationMatrix.js +19 -0
- package/src/expression/embeddedDocs/function/utils/bin.js +12 -0
- package/src/expression/embeddedDocs/function/utils/hex.js +12 -0
- package/src/expression/embeddedDocs/function/utils/oct.js +12 -0
- package/src/expression/parse.js +25 -0
- package/src/factoriesAny.js +6 -0
- package/src/function/algebra/solver/lsolve.js +52 -58
- package/src/function/algebra/solver/lsolveAll.js +197 -0
- package/src/function/algebra/solver/lusolve.js +9 -19
- package/src/function/algebra/solver/usolve.js +52 -55
- package/src/function/algebra/solver/usolveAll.js +199 -0
- package/src/function/algebra/solver/utils/solveValidation.js +78 -86
- package/src/function/arithmetic/ceil.js +63 -3
- package/src/function/arithmetic/fix.js +45 -6
- package/src/function/arithmetic/floor.js +65 -5
- package/src/function/arithmetic/mod.js +8 -1
- package/src/function/arithmetic/round.js +6 -2
- package/src/function/matrix/rotationMatrix.js +185 -0
- package/src/function/matrix/sqrtm.js +4 -0
- package/src/function/probability/pickRandom.js +2 -6
- package/src/function/statistics/variance.js +4 -4
- package/src/function/string/baseUtils.js +29 -0
- package/src/function/string/bin.js +23 -0
- package/src/function/string/hex.js +23 -0
- package/src/function/string/oct.js +24 -0
- package/src/type/bignumber/BigNumber.js +2 -1
- package/src/type/number.js +9 -1
- package/src/utils/object.js +3 -1
- package/src/version.js +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isArray, isDenseMatrix,
|
|
1
|
+
import { isArray, isMatrix, isDenseMatrix, isSparseMatrix } from '../../../../utils/is'
|
|
2
2
|
import { arraySize } from '../../../../utils/array'
|
|
3
3
|
import { format } from '../../../../utils/string'
|
|
4
4
|
|
|
@@ -13,131 +13,123 @@ export function createSolveValidation ({ DenseMatrix }) {
|
|
|
13
13
|
* @return {DenseMatrix} Dense column vector b
|
|
14
14
|
*/
|
|
15
15
|
return function solveValidation (m, b, copy) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
const mSize = m.size()
|
|
17
|
+
|
|
18
|
+
if (mSize.length !== 2) {
|
|
19
|
+
throw new RangeError('Matrix must be two dimensional (size: ' + format(mSize) + ')')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const rows = mSize[0]
|
|
23
|
+
const columns = mSize[1]
|
|
24
|
+
|
|
25
|
+
if (rows !== columns) {
|
|
26
|
+
throw new RangeError('Matrix must be square (size: ' + format(mSize) + ')')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let data = []
|
|
30
|
+
|
|
28
31
|
if (isMatrix(b)) {
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// loop b data
|
|
40
|
-
for (i = 0; i < rows; i++) {
|
|
41
|
-
// row array
|
|
32
|
+
const bSize = b.size()
|
|
33
|
+
const bdata = b._data
|
|
34
|
+
|
|
35
|
+
// 1-dim vector
|
|
36
|
+
if (bSize.length === 1) {
|
|
37
|
+
if (bSize[0] !== rows) {
|
|
38
|
+
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < rows; i++) {
|
|
42
42
|
data[i] = [bdata[i]]
|
|
43
43
|
}
|
|
44
|
-
|
|
44
|
+
|
|
45
45
|
return new DenseMatrix({
|
|
46
46
|
data: data,
|
|
47
47
|
size: [rows, 1],
|
|
48
48
|
datatype: b._datatype
|
|
49
49
|
})
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
51
|
+
|
|
52
|
+
// 2-dim column
|
|
53
|
+
if (bSize.length === 2) {
|
|
54
|
+
if (bSize[0] !== rows || bSize[1] !== 1) {
|
|
55
|
+
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.')
|
|
56
|
+
}
|
|
57
|
+
|
|
56
58
|
if (isDenseMatrix(b)) {
|
|
57
|
-
// check a copy is needed
|
|
58
59
|
if (copy) {
|
|
59
|
-
// create data array
|
|
60
60
|
data = []
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
// loop b data
|
|
64
|
-
for (i = 0; i < rows; i++) {
|
|
65
|
-
// row array
|
|
61
|
+
|
|
62
|
+
for (let i = 0; i < rows; i++) {
|
|
66
63
|
data[i] = [bdata[i][0]]
|
|
67
64
|
}
|
|
68
|
-
|
|
65
|
+
|
|
69
66
|
return new DenseMatrix({
|
|
70
67
|
data: data,
|
|
71
68
|
size: [rows, 1],
|
|
72
69
|
datatype: b._datatype
|
|
73
70
|
})
|
|
74
71
|
}
|
|
75
|
-
|
|
72
|
+
|
|
76
73
|
return b
|
|
77
74
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
75
|
+
|
|
76
|
+
if (isSparseMatrix(b)) {
|
|
77
|
+
for (let i = 0; i < rows; i++) { data[i] = [0] }
|
|
78
|
+
|
|
79
|
+
const values = b._values
|
|
80
|
+
const index = b._index
|
|
81
|
+
const ptr = b._ptr
|
|
82
|
+
|
|
83
|
+
for (let k1 = ptr[1], k = ptr[0]; k < k1; k++) {
|
|
84
|
+
const i = index[k]
|
|
85
|
+
data[i][0] = values[k]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return new DenseMatrix({
|
|
89
|
+
data: data,
|
|
90
|
+
size: [rows, 1],
|
|
91
|
+
datatype: b._datatype
|
|
92
|
+
})
|
|
91
93
|
}
|
|
92
|
-
// return Dense Matrix
|
|
93
|
-
return new DenseMatrix({
|
|
94
|
-
data: data,
|
|
95
|
-
size: [rows, 1],
|
|
96
|
-
datatype: b._datatype
|
|
97
|
-
})
|
|
98
94
|
}
|
|
99
|
-
|
|
100
|
-
throw new RangeError('Dimension mismatch.
|
|
95
|
+
|
|
96
|
+
throw new RangeError('Dimension mismatch. The right side has to be either 1- or 2-dimensional vector.')
|
|
101
97
|
}
|
|
102
|
-
|
|
98
|
+
|
|
103
99
|
if (isArray(b)) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// loop b
|
|
113
|
-
for (i = 0; i < rows; i++) {
|
|
114
|
-
// row array
|
|
100
|
+
const bsize = arraySize(b)
|
|
101
|
+
|
|
102
|
+
if (bsize.length === 1) {
|
|
103
|
+
if (bsize[0] !== rows) {
|
|
104
|
+
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < rows; i++) {
|
|
115
108
|
data[i] = [b[i]]
|
|
116
109
|
}
|
|
117
|
-
|
|
110
|
+
|
|
118
111
|
return new DenseMatrix({
|
|
119
112
|
data: data,
|
|
120
113
|
size: [rows, 1]
|
|
121
114
|
})
|
|
122
115
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
for (i = 0; i < rows; i++) {
|
|
130
|
-
// row array
|
|
116
|
+
|
|
117
|
+
if (bsize.length === 2) {
|
|
118
|
+
if (bsize[0] !== rows || bsize[1] !== 1) {
|
|
119
|
+
throw new RangeError('Dimension mismatch. Matrix columns must match vector length.')
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
for (let i = 0; i < rows; i++) {
|
|
131
123
|
data[i] = [b[i][0]]
|
|
132
124
|
}
|
|
133
|
-
|
|
125
|
+
|
|
134
126
|
return new DenseMatrix({
|
|
135
127
|
data: data,
|
|
136
128
|
size: [rows, 1]
|
|
137
129
|
})
|
|
138
130
|
}
|
|
139
|
-
|
|
140
|
-
throw new RangeError('Dimension mismatch.
|
|
131
|
+
|
|
132
|
+
throw new RangeError('Dimension mismatch. The right side has to be either 1- or 2-dimensional vector.')
|
|
141
133
|
}
|
|
142
134
|
}
|
|
143
135
|
}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
+
import { Decimal } from 'decimal.js'
|
|
1
2
|
import { factory } from '../../utils/factory'
|
|
2
3
|
import { deepMap } from '../../utils/collection'
|
|
3
4
|
import { nearlyEqual } from '../../utils/number'
|
|
4
5
|
import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual'
|
|
5
6
|
import { ceilNumber } from '../../plain/number'
|
|
7
|
+
import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11'
|
|
8
|
+
import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
|
|
6
9
|
|
|
7
10
|
const name = 'ceil'
|
|
8
|
-
const dependencies = ['typed', 'config', 'round']
|
|
11
|
+
const dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar']
|
|
12
|
+
|
|
13
|
+
export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round, matrix, equalScalar }) => {
|
|
14
|
+
const algorithm11 = createAlgorithm11({ typed, equalScalar })
|
|
15
|
+
const algorithm14 = createAlgorithm14({ typed })
|
|
9
16
|
|
|
10
|
-
export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round }) => {
|
|
11
17
|
/**
|
|
12
18
|
* Round a value towards plus infinity
|
|
13
19
|
* If `x` is complex, both real and imaginary part are rounded towards plus infinity.
|
|
@@ -16,6 +22,7 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
16
22
|
* Syntax:
|
|
17
23
|
*
|
|
18
24
|
* math.ceil(x)
|
|
25
|
+
* math.ceil(x, n)
|
|
19
26
|
*
|
|
20
27
|
* Examples:
|
|
21
28
|
*
|
|
@@ -24,16 +31,24 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
24
31
|
* math.ceil(-4.2) // returns number -4
|
|
25
32
|
* math.ceil(-4.7) // returns number -4
|
|
26
33
|
*
|
|
27
|
-
*
|
|
34
|
+
* math.ceil(3.212, 2) // returns number 3.22
|
|
35
|
+
* math.ceil(3.288, 2) // returns number 3.29
|
|
36
|
+
* math.ceil(-4.212, 2) // returns number -4.21
|
|
37
|
+
* math.ceil(-4.782, 2) // returns number -4.78
|
|
38
|
+
*
|
|
39
|
+
* const c = math.complex(3.24, -2.71)
|
|
28
40
|
* math.ceil(c) // returns Complex 4 - 2i
|
|
41
|
+
* math.ceil(c, 1) // returns Complex 3.3 - 2.7i
|
|
29
42
|
*
|
|
30
43
|
* math.ceil([3.2, 3.8, -4.7]) // returns Array [4, 4, -4]
|
|
44
|
+
* math.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.3, 3.9, -4.7]
|
|
31
45
|
*
|
|
32
46
|
* See also:
|
|
33
47
|
*
|
|
34
48
|
* floor, fix, round
|
|
35
49
|
*
|
|
36
50
|
* @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
|
|
51
|
+
* @param {number | BigNumber | Array} [n=0] Number of decimals
|
|
37
52
|
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
|
|
38
53
|
*/
|
|
39
54
|
return typed('ceil', {
|
|
@@ -45,10 +60,25 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
45
60
|
}
|
|
46
61
|
},
|
|
47
62
|
|
|
63
|
+
'number, number': function (x, n) {
|
|
64
|
+
if (nearlyEqual(x, round(x, n), config.epsilon)) {
|
|
65
|
+
return round(x, n)
|
|
66
|
+
} else {
|
|
67
|
+
let [number, exponent] = `${x}e`.split('e')
|
|
68
|
+
const result = Math.ceil(Number(`${number}e${Number(exponent) + n}`));
|
|
69
|
+
[number, exponent] = `${result}e`.split('e')
|
|
70
|
+
return Number(`${number}e${Number(exponent) - n}`)
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
|
|
48
74
|
Complex: function (x) {
|
|
49
75
|
return x.ceil()
|
|
50
76
|
},
|
|
51
77
|
|
|
78
|
+
'Complex, number': function (x, n) {
|
|
79
|
+
return x.ceil(n)
|
|
80
|
+
},
|
|
81
|
+
|
|
52
82
|
BigNumber: function (x) {
|
|
53
83
|
if (bigNearlyEqual(x, round(x), config.epsilon)) {
|
|
54
84
|
return round(x)
|
|
@@ -57,13 +87,43 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
57
87
|
}
|
|
58
88
|
},
|
|
59
89
|
|
|
90
|
+
'BigNumber, BigNumber': function (x, n) {
|
|
91
|
+
if (bigNearlyEqual(x, round(x, n), config.epsilon)) {
|
|
92
|
+
return round(x, n)
|
|
93
|
+
} else {
|
|
94
|
+
return x.toDecimalPlaces(n.toNumber(), Decimal.ROUND_CEIL)
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
|
|
60
98
|
Fraction: function (x) {
|
|
61
99
|
return x.ceil()
|
|
62
100
|
},
|
|
63
101
|
|
|
102
|
+
'Fraction, number': function (x, n) {
|
|
103
|
+
return x.ceil(n)
|
|
104
|
+
},
|
|
105
|
+
|
|
64
106
|
'Array | Matrix': function (x) {
|
|
65
107
|
// deep map collection, skip zeros since ceil(0) = 0
|
|
66
108
|
return deepMap(x, this, true)
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
'Array | Matrix, number': function (x, n) {
|
|
112
|
+
// deep map collection, skip zeros since ceil(0) = 0
|
|
113
|
+
return deepMap(x, i => this(i, n), true)
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
'SparseMatrix, number | BigNumber': function (x, y) {
|
|
117
|
+
return algorithm11(x, y, this, false)
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
'DenseMatrix, number | BigNumber': function (x, y) {
|
|
121
|
+
return algorithm14(x, y, this, false)
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
'number | Complex | BigNumber, Array': function (x, y) {
|
|
125
|
+
// use matrix implementation
|
|
126
|
+
return algorithm14(matrix(y), x, this, true).valueOf()
|
|
67
127
|
}
|
|
68
128
|
})
|
|
69
129
|
})
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { factory } from '../../utils/factory'
|
|
2
2
|
import { deepMap } from '../../utils/collection'
|
|
3
|
+
import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
|
|
3
4
|
|
|
4
5
|
const name = 'fix'
|
|
5
|
-
const dependencies = ['typed', 'Complex', 'ceil', 'floor']
|
|
6
|
+
const dependencies = ['typed', 'Complex', 'matrix', 'ceil', 'floor']
|
|
6
7
|
|
|
7
|
-
export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, Complex, ceil, floor }) => {
|
|
8
|
+
export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, Complex, matrix, ceil, floor }) => {
|
|
9
|
+
const algorithm14 = createAlgorithm14({ typed })
|
|
8
10
|
/**
|
|
9
11
|
* Round a value towards zero.
|
|
10
12
|
* For matrices, the function is evaluated element wise.
|
|
@@ -20,23 +22,35 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C
|
|
|
20
22
|
* math.fix(-4.2) // returns number -4
|
|
21
23
|
* math.fix(-4.7) // returns number -4
|
|
22
24
|
*
|
|
23
|
-
*
|
|
25
|
+
* math.fix(3.12, 1) // returns number 3.1
|
|
26
|
+
* math.fix(3.18, 1) // returns number 3.1
|
|
27
|
+
* math.fix(-4.12, 1) // returns number -4.1
|
|
28
|
+
* math.fix(-4.17, 1) // returns number -4.1
|
|
29
|
+
*
|
|
30
|
+
* const c = math.complex(3.22, -2.78)
|
|
24
31
|
* math.fix(c) // returns Complex 3 - 2i
|
|
32
|
+
* math.fix(c, 1) // returns Complex 3.2 - 2.7i
|
|
25
33
|
*
|
|
26
|
-
* math.fix([3.2, 3.8, -4.7])
|
|
34
|
+
* math.fix([3.2, 3.8, -4.7]) // returns Array [3, 3, -4]
|
|
35
|
+
* math.fix([3.2, 3.8, -4.7], 1) // returns Array [3.2, 3.8, -4.7]
|
|
27
36
|
*
|
|
28
37
|
* See also:
|
|
29
38
|
*
|
|
30
39
|
* ceil, floor, round
|
|
31
40
|
*
|
|
32
|
-
* @param
|
|
33
|
-
* @
|
|
41
|
+
* @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
|
|
42
|
+
* @param {number | BigNumber | Array} [n=0] Number of decimals
|
|
43
|
+
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
|
|
34
44
|
*/
|
|
35
45
|
return typed('fix', {
|
|
36
46
|
number: function (x) {
|
|
37
47
|
return (x > 0) ? floor(x) : ceil(x)
|
|
38
48
|
},
|
|
39
49
|
|
|
50
|
+
'number, number | BigNumber': function (x, n) {
|
|
51
|
+
return (x > 0) ? floor(x, n) : ceil(x, n)
|
|
52
|
+
},
|
|
53
|
+
|
|
40
54
|
Complex: function (x) {
|
|
41
55
|
return new Complex(
|
|
42
56
|
(x.re > 0) ? Math.floor(x.re) : Math.ceil(x.re),
|
|
@@ -44,17 +58,42 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C
|
|
|
44
58
|
)
|
|
45
59
|
},
|
|
46
60
|
|
|
61
|
+
'Complex, number | BigNumber': function (x, n) {
|
|
62
|
+
return new Complex(
|
|
63
|
+
(x.re > 0) ? floor(x.re, n) : ceil(x.re, n),
|
|
64
|
+
(x.im > 0) ? floor(x.im, n) : ceil(x.im, n)
|
|
65
|
+
)
|
|
66
|
+
},
|
|
67
|
+
|
|
47
68
|
BigNumber: function (x) {
|
|
48
69
|
return x.isNegative() ? ceil(x) : floor(x)
|
|
49
70
|
},
|
|
50
71
|
|
|
72
|
+
'BigNumber, number | BigNumber': function (x, n) {
|
|
73
|
+
return x.isNegative() ? ceil(x, n) : floor(x, n)
|
|
74
|
+
},
|
|
75
|
+
|
|
51
76
|
Fraction: function (x) {
|
|
52
77
|
return x.s < 0 ? x.ceil() : x.floor()
|
|
53
78
|
},
|
|
54
79
|
|
|
80
|
+
'Fraction, number | BigNumber': function (x, n) {
|
|
81
|
+
return x.s < 0 ? x.ceil(n) : x.floor(n)
|
|
82
|
+
},
|
|
83
|
+
|
|
55
84
|
'Array | Matrix': function (x) {
|
|
56
85
|
// deep map collection, skip zeros since fix(0) = 0
|
|
57
86
|
return deepMap(x, this, true)
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
'Array | Matrix, number | BigNumber': function (x, n) {
|
|
90
|
+
// deep map collection, skip zeros since fix(0) = 0
|
|
91
|
+
return deepMap(x, i => this(i, n), true)
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
'number | Complex | BigNumber, Array': function (x, y) {
|
|
95
|
+
// use matrix implementation
|
|
96
|
+
return algorithm14(matrix(y), x, this, true).valueOf()
|
|
58
97
|
}
|
|
59
98
|
})
|
|
60
99
|
})
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
import { Decimal } from 'decimal.js'
|
|
1
2
|
import { factory } from '../../utils/factory'
|
|
2
3
|
import { deepMap } from '../../utils/collection'
|
|
3
4
|
import { nearlyEqual } from '../../utils/number'
|
|
4
5
|
import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual'
|
|
6
|
+
import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11'
|
|
7
|
+
import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14'
|
|
5
8
|
|
|
6
9
|
const name = 'floor'
|
|
7
|
-
const dependencies = ['typed', 'config', 'round']
|
|
10
|
+
const dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar']
|
|
11
|
+
|
|
12
|
+
export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round, matrix, equalScalar }) => {
|
|
13
|
+
const algorithm11 = createAlgorithm11({ typed, equalScalar })
|
|
14
|
+
const algorithm14 = createAlgorithm14({ typed })
|
|
8
15
|
|
|
9
|
-
export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round }) => {
|
|
10
16
|
/**
|
|
11
17
|
* Round a value towards minus infinity.
|
|
12
18
|
* For matrices, the function is evaluated element wise.
|
|
@@ -14,6 +20,7 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
14
20
|
* Syntax:
|
|
15
21
|
*
|
|
16
22
|
* math.floor(x)
|
|
23
|
+
* math.floor(x, n)
|
|
17
24
|
*
|
|
18
25
|
* Examples:
|
|
19
26
|
*
|
|
@@ -22,16 +29,24 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
22
29
|
* math.floor(-4.2) // returns number -5
|
|
23
30
|
* math.floor(-4.7) // returns number -5
|
|
24
31
|
*
|
|
25
|
-
*
|
|
26
|
-
* math.floor(
|
|
32
|
+
* math.floor(3.212, 2) // returns number 3.21
|
|
33
|
+
* math.floor(3.288, 2) // returns number 3.28
|
|
34
|
+
* math.floor(-4.212, 2) // returns number -4.22
|
|
35
|
+
* math.floor(-4.782, 2) // returns number -4.79
|
|
36
|
+
*
|
|
37
|
+
* const c = math.complex(3.24, -2.71)
|
|
38
|
+
* math.floor(c) // returns Complex 3 - 3i
|
|
39
|
+
* math.floor(c, 1) // returns Complex 3.2 - 2.8i
|
|
27
40
|
*
|
|
28
|
-
* math.floor([3.2, 3.8, -4.7])
|
|
41
|
+
* math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5]
|
|
42
|
+
* math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8]
|
|
29
43
|
*
|
|
30
44
|
* See also:
|
|
31
45
|
*
|
|
32
46
|
* ceil, fix, round
|
|
33
47
|
*
|
|
34
48
|
* @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
|
|
49
|
+
* @param {number | BigNumber | Array} [n=0] Number of decimals
|
|
35
50
|
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
|
|
36
51
|
*/
|
|
37
52
|
return typed('floor', {
|
|
@@ -43,10 +58,25 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
43
58
|
}
|
|
44
59
|
},
|
|
45
60
|
|
|
61
|
+
'number, number': function (x, n) {
|
|
62
|
+
if (nearlyEqual(x, round(x, n), config.epsilon)) {
|
|
63
|
+
return round(x, n)
|
|
64
|
+
} else {
|
|
65
|
+
let [number, exponent] = `${x}e`.split('e')
|
|
66
|
+
const result = Math.floor(Number(`${number}e${Number(exponent) + n}`));
|
|
67
|
+
[number, exponent] = `${result}e`.split('e')
|
|
68
|
+
return Number(`${number}e${Number(exponent) - n}`)
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
46
72
|
Complex: function (x) {
|
|
47
73
|
return x.floor()
|
|
48
74
|
},
|
|
49
75
|
|
|
76
|
+
'Complex, number': function (x, n) {
|
|
77
|
+
return x.floor(n)
|
|
78
|
+
},
|
|
79
|
+
|
|
50
80
|
BigNumber: function (x) {
|
|
51
81
|
if (bigNearlyEqual(x, round(x), config.epsilon)) {
|
|
52
82
|
return round(x)
|
|
@@ -55,13 +85,43 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
55
85
|
}
|
|
56
86
|
},
|
|
57
87
|
|
|
88
|
+
'BigNumber, BigNumber': function (x, n) {
|
|
89
|
+
if (bigNearlyEqual(x, round(x, n), config.epsilon)) {
|
|
90
|
+
return round(x, n)
|
|
91
|
+
} else {
|
|
92
|
+
return x.toDecimalPlaces(n.toNumber(), Decimal.ROUND_FLOOR)
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
|
|
58
96
|
Fraction: function (x) {
|
|
59
97
|
return x.floor()
|
|
60
98
|
},
|
|
61
99
|
|
|
100
|
+
'Fraction, number': function (x, n) {
|
|
101
|
+
return x.floor(n)
|
|
102
|
+
},
|
|
103
|
+
|
|
62
104
|
'Array | Matrix': function (x) {
|
|
63
105
|
// deep map collection, skip zeros since floor(0) = 0
|
|
64
106
|
return deepMap(x, this, true)
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
'Array | Matrix, number': function (x, n) {
|
|
110
|
+
// deep map collection, skip zeros since ceil(0) = 0
|
|
111
|
+
return deepMap(x, i => this(i, n), true)
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
'SparseMatrix, number | BigNumber': function (x, y) {
|
|
115
|
+
return algorithm11(x, y, this, false)
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
'DenseMatrix, number | BigNumber': function (x, y) {
|
|
119
|
+
return algorithm14(x, y, this, false)
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
'number | Complex | BigNumber, Array': function (x, y) {
|
|
123
|
+
// use matrix implementation
|
|
124
|
+
return algorithm14(matrix(y), x, this, true).valueOf()
|
|
65
125
|
}
|
|
66
126
|
})
|
|
67
127
|
})
|
|
@@ -65,11 +65,18 @@ export const createMod = /* #__PURE__ */ factory(name, dependencies, ({ typed, m
|
|
|
65
65
|
'number, number': modNumber,
|
|
66
66
|
|
|
67
67
|
'BigNumber, BigNumber': function (x, y) {
|
|
68
|
+
if (y.isNeg()) {
|
|
69
|
+
throw new Error('Cannot calculate mod for a negative divisor')
|
|
70
|
+
}
|
|
68
71
|
return y.isZero() ? x : x.mod(y)
|
|
69
72
|
},
|
|
70
73
|
|
|
71
74
|
'Fraction, Fraction': function (x, y) {
|
|
72
|
-
|
|
75
|
+
if (y.compare(0) < 0) {
|
|
76
|
+
throw new Error('Cannot calculate mod for a negative divisor')
|
|
77
|
+
}
|
|
78
|
+
// Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend
|
|
79
|
+
return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y)
|
|
73
80
|
},
|
|
74
81
|
|
|
75
82
|
'SparseMatrix, SparseMatrix': function (x, y) {
|
|
@@ -34,10 +34,14 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
34
34
|
*
|
|
35
35
|
* Examples:
|
|
36
36
|
*
|
|
37
|
-
* math.round(3.
|
|
38
|
-
* math.round(3.
|
|
37
|
+
* math.round(3.22) // returns number 3
|
|
38
|
+
* math.round(3.82) // returns number 4
|
|
39
39
|
* math.round(-4.2) // returns number -4
|
|
40
40
|
* math.round(-4.7) // returns number -5
|
|
41
|
+
* math.round(3.22, 1) // returns number 3.2
|
|
42
|
+
* math.round(3.88, 1) // returns number 3.8
|
|
43
|
+
* math.round(-4.21, 1) // returns number -4.2
|
|
44
|
+
* math.round(-4.71, 1) // returns number -4.7
|
|
41
45
|
* math.round(math.pi, 3) // returns number 3.142
|
|
42
46
|
* math.round(123.45678, 2) // returns number 123.46
|
|
43
47
|
*
|