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
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { isBigNumber } from '../../utils/is'
|
|
2
|
+
import { factory } from '../../utils/factory'
|
|
3
|
+
|
|
4
|
+
const name = 'rotationMatrix'
|
|
5
|
+
const dependencies = [
|
|
6
|
+
'typed',
|
|
7
|
+
'config',
|
|
8
|
+
'multiplyScalar',
|
|
9
|
+
'addScalar',
|
|
10
|
+
'unaryMinus',
|
|
11
|
+
'norm',
|
|
12
|
+
'matrix',
|
|
13
|
+
'BigNumber',
|
|
14
|
+
'DenseMatrix',
|
|
15
|
+
'SparseMatrix',
|
|
16
|
+
'cos',
|
|
17
|
+
'sin'
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
export const createRotationMatrix = /* #__PURE__ */ factory(name, dependencies, (
|
|
21
|
+
{
|
|
22
|
+
typed, config, multiplyScalar,
|
|
23
|
+
addScalar, unaryMinus, norm, BigNumber,
|
|
24
|
+
matrix, DenseMatrix, SparseMatrix, cos, sin
|
|
25
|
+
}) => {
|
|
26
|
+
/**
|
|
27
|
+
* Create a 2-dimensional counter-clockwise rotation matrix (2x2) for a given angle (expressed in radians).
|
|
28
|
+
* Create a 2-dimensional counter-clockwise rotation matrix (3x3) by a given angle (expressed in radians) around a given axis (1x3).
|
|
29
|
+
*
|
|
30
|
+
* Syntax:
|
|
31
|
+
*
|
|
32
|
+
* math.rotationMatrix(theta)
|
|
33
|
+
* math.rotationMatrix(theta, format)
|
|
34
|
+
* math.rotationMatrix(theta, [v])
|
|
35
|
+
* math.rotationMatrix(theta, [v], format)
|
|
36
|
+
*
|
|
37
|
+
* Examples:
|
|
38
|
+
*
|
|
39
|
+
* math.rotationMatrix(math.pi / 2) // returns [[0, -1], [1, 0]]
|
|
40
|
+
* math.rotationMatrix(math.bignumber(45)) // returns [[ bignumber(1 / sqrt(2)), - bignumber(1 / sqrt(2))], [ bignumber(1 / sqrt(2)), bignumber(1 / sqrt(2))]]
|
|
41
|
+
* math.rotationMatrix(math.complex(1 + i)) // returns [[cos(1 + i), -sin(1 + i)], [sin(1 + i), cos(1 + i)]]
|
|
42
|
+
* math.rotationMatrix(math.unit('1rad')) // returns [[cos(1), -sin(1)], [sin(1), cos(1)]]
|
|
43
|
+
*
|
|
44
|
+
* math.rotationMatrix(math.pi / 2, [0, 1, 0]) // returns [[0, 0, 1], [0, 1, 0], [-1, 0, 0]]
|
|
45
|
+
* math.rotationMatrix(math.pi / 2, matrix([0, 1, 0])) // returns matrix([[0, 0, 1], [0, 1, 0], [-1, 0, 0]])
|
|
46
|
+
*
|
|
47
|
+
*
|
|
48
|
+
* See also:
|
|
49
|
+
*
|
|
50
|
+
* matrix, cos, sin
|
|
51
|
+
*
|
|
52
|
+
*
|
|
53
|
+
* @param {number | BigNumber | Complex | Unit} theta Rotation angle
|
|
54
|
+
* @param {Array | Matrix} [v] Rotation axis
|
|
55
|
+
* @param {string} [format] Result Matrix storage format
|
|
56
|
+
* @return {Array | Matrix} Rotation matrix
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
return typed(name, {
|
|
60
|
+
'': function () {
|
|
61
|
+
return (config.matrix === 'Matrix') ? matrix([]) : []
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
string: function (format) {
|
|
65
|
+
return matrix(format)
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
'number | BigNumber | Complex | Unit': function (theta) {
|
|
69
|
+
return _rotationMatrix2x2(theta, config.matrix === 'Matrix' ? 'dense' : undefined)
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
'number | BigNumber | Complex | Unit, string': function (theta, format) {
|
|
73
|
+
return _rotationMatrix2x2(theta, format)
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
'number | BigNumber | Complex | Unit, Array': function (theta, v) {
|
|
77
|
+
const matrixV = matrix(v)
|
|
78
|
+
_validateVector(matrixV)
|
|
79
|
+
return _rotationMatrix3x3(theta, matrixV, config.matrix === 'Matrix' ? 'dense' : undefined)
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
'number | BigNumber | Complex | Unit, Matrix': function (theta, v) {
|
|
83
|
+
_validateVector(v)
|
|
84
|
+
return _rotationMatrix3x3(theta, v, config.matrix === 'Matrix' ? 'dense' : undefined)
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
'number | BigNumber | Complex | Unit, Array, string': function (theta, v, format) {
|
|
88
|
+
const matrixV = matrix(v)
|
|
89
|
+
_validateVector(matrixV)
|
|
90
|
+
return _rotationMatrix3x3(theta, matrixV, format)
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
'number | BigNumber | Complex | Unit, Matrix, string': function (theta, v, format) {
|
|
94
|
+
_validateVector(v)
|
|
95
|
+
return _rotationMatrix3x3(theta, v, format)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns 2x2 matrix of 2D rotation of angle theta
|
|
102
|
+
*
|
|
103
|
+
* @param {number | BigNumber | Complex | Unit} theta The rotation angle
|
|
104
|
+
* @param {string} format The result Matrix storage format
|
|
105
|
+
* @returns {Matrix}
|
|
106
|
+
* @private
|
|
107
|
+
*/
|
|
108
|
+
function _rotationMatrix2x2 (theta, format) {
|
|
109
|
+
const Big = isBigNumber(theta)
|
|
110
|
+
|
|
111
|
+
const minusOne = Big ? new BigNumber(-1) : -1
|
|
112
|
+
const cosTheta = cos(theta)
|
|
113
|
+
const sinTheta = sin(theta)
|
|
114
|
+
const data = [[cosTheta, multiplyScalar(minusOne, sinTheta)], [sinTheta, cosTheta]]
|
|
115
|
+
|
|
116
|
+
return _convertToFormat(data, format)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function _validateVector (v) {
|
|
120
|
+
const size = v.size()
|
|
121
|
+
if (size.length < 1 || size[0] !== 3) {
|
|
122
|
+
throw new RangeError('Vector must be of dimensions 1x3')
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function _mul (array) {
|
|
127
|
+
return array.reduce((p, curr) => multiplyScalar(p, curr))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function _convertToFormat (data, format) {
|
|
131
|
+
if (format) {
|
|
132
|
+
if (format === 'sparse') {
|
|
133
|
+
return new SparseMatrix(data)
|
|
134
|
+
}
|
|
135
|
+
if (format === 'dense') {
|
|
136
|
+
return new DenseMatrix(data)
|
|
137
|
+
}
|
|
138
|
+
throw new TypeError(`Unknown matrix type "${format}"`)
|
|
139
|
+
}
|
|
140
|
+
return data
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Returns a 3x3 matrix of rotation of angle theta around vector v
|
|
145
|
+
*
|
|
146
|
+
* @param {number | BigNumber | Complex | Unit} theta The rotation angle
|
|
147
|
+
* @param {Matrix} v The rotation axis vector
|
|
148
|
+
* @param {string} format The storage format of the resulting matrix
|
|
149
|
+
* @returns {Matrix}
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
152
|
+
function _rotationMatrix3x3 (theta, v, format) {
|
|
153
|
+
const normV = norm(v)
|
|
154
|
+
if (normV === 0) {
|
|
155
|
+
return _convertToFormat([], format)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const Big = isBigNumber(theta) ? BigNumber : null
|
|
159
|
+
|
|
160
|
+
const one = Big ? new Big(1) : 1
|
|
161
|
+
const minusOne = Big ? new Big(-1) : -1
|
|
162
|
+
const vx = Big ? new Big(v.get([0]) / normV) : v.get([0]) / normV
|
|
163
|
+
const vy = Big ? new Big(v.get([1]) / normV) : v.get([1]) / normV
|
|
164
|
+
const vz = Big ? new Big(v.get([2]) / normV) : v.get([2]) / normV
|
|
165
|
+
const c = cos(theta)
|
|
166
|
+
const oneMinusC = addScalar(one, unaryMinus(c))
|
|
167
|
+
const s = sin(theta)
|
|
168
|
+
|
|
169
|
+
const r11 = addScalar(c, _mul([vx, vx, oneMinusC]))
|
|
170
|
+
const r12 = addScalar(_mul([vx, vy, oneMinusC]), _mul([minusOne, vz, s]))
|
|
171
|
+
const r13 = addScalar(_mul([vx, vz, oneMinusC]), _mul([vy, s]))
|
|
172
|
+
|
|
173
|
+
const r21 = addScalar(_mul([vx, vy, oneMinusC]), _mul([vz, s]))
|
|
174
|
+
const r22 = addScalar(c, _mul([vy, vy, oneMinusC]))
|
|
175
|
+
const r23 = addScalar(_mul([vy, vz, oneMinusC]), _mul([minusOne, vx, s]))
|
|
176
|
+
|
|
177
|
+
const r31 = addScalar(_mul([vx, vz, oneMinusC]), _mul([minusOne, vy, s]))
|
|
178
|
+
const r32 = addScalar(_mul([vy, vz, oneMinusC]), _mul([vx, s]))
|
|
179
|
+
const r33 = addScalar(c, _mul([vz, vz, oneMinusC]))
|
|
180
|
+
|
|
181
|
+
const data = [[r11, r12, r13], [r21, r22, r23], [r31, r32, r33]]
|
|
182
|
+
|
|
183
|
+
return _convertToFormat(data, format)
|
|
184
|
+
}
|
|
185
|
+
})
|
|
@@ -87,6 +87,10 @@ export const createSqrtm = /* #__PURE__ */ factory(name, dependencies, ({ typed,
|
|
|
87
87
|
'(size: ' + format(size) + ')')
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
+
default:
|
|
91
|
+
// Multi dimensional array
|
|
92
|
+
throw new RangeError('Matrix must be at most two dimensional ' +
|
|
93
|
+
'(size: ' + format(size) + ')')
|
|
90
94
|
}
|
|
91
95
|
}
|
|
92
96
|
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { factory } from '../../utils/factory'
|
|
2
2
|
import { isNumber } from '../../utils/is'
|
|
3
|
-
import { arraySize } from '../../utils/array'
|
|
4
3
|
import { createRng } from './util/seededRNG'
|
|
4
|
+
import { flatten } from '../../utils/array'
|
|
5
5
|
|
|
6
6
|
const name = 'pickRandom'
|
|
7
7
|
const dependencies = ['typed', 'config', '?on']
|
|
@@ -76,15 +76,11 @@ export const createPickRandom = /* #__PURE__ */ factory(name, dependencies, ({ t
|
|
|
76
76
|
number = 1
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
possibles = possibles.valueOf() // get Array
|
|
79
|
+
possibles = flatten(possibles.valueOf()).valueOf() // get Array
|
|
80
80
|
if (weights) {
|
|
81
81
|
weights = weights.valueOf() // get Array
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
if (arraySize(possibles).length > 1) {
|
|
85
|
-
throw new Error('Only one dimensional vectors supported')
|
|
86
|
-
}
|
|
87
|
-
|
|
88
84
|
let totalWeights = 0
|
|
89
85
|
|
|
90
86
|
if (typeof weights !== 'undefined') {
|
|
@@ -97,7 +97,7 @@ export const createVariance = /* #__PURE__ */ factory(name, dependencies, ({ typ
|
|
|
97
97
|
* @private
|
|
98
98
|
*/
|
|
99
99
|
function _var (array, normalization) {
|
|
100
|
-
let sum
|
|
100
|
+
let sum
|
|
101
101
|
let num = 0
|
|
102
102
|
|
|
103
103
|
if (array.length === 0) {
|
|
@@ -107,7 +107,7 @@ export const createVariance = /* #__PURE__ */ factory(name, dependencies, ({ typ
|
|
|
107
107
|
// calculate the mean and number of elements
|
|
108
108
|
deepForEach(array, function (value) {
|
|
109
109
|
try {
|
|
110
|
-
sum = add(sum, value)
|
|
110
|
+
sum = sum === undefined ? value : add(sum, value)
|
|
111
111
|
num++
|
|
112
112
|
} catch (err) {
|
|
113
113
|
throw improveErrorMessage(err, 'variance', value)
|
|
@@ -118,10 +118,10 @@ export const createVariance = /* #__PURE__ */ factory(name, dependencies, ({ typ
|
|
|
118
118
|
const mean = divide(sum, num)
|
|
119
119
|
|
|
120
120
|
// calculate the variance
|
|
121
|
-
sum =
|
|
121
|
+
sum = undefined
|
|
122
122
|
deepForEach(array, function (value) {
|
|
123
123
|
const diff = subtract(value, mean)
|
|
124
|
-
sum = add(sum, multiply(diff, diff))
|
|
124
|
+
sum = sum === undefined ? multiply(diff, diff) : add(sum, multiply(diff, diff))
|
|
125
125
|
})
|
|
126
126
|
|
|
127
127
|
if (isNaN(sum)) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { factory } from '../../utils/factory'
|
|
2
|
+
import { isInteger } from '../../utils/number'
|
|
3
|
+
|
|
4
|
+
function baseFormatter (base) {
|
|
5
|
+
const prefixes = { 2: '0b', 8: '0o', 16: '0x' }
|
|
6
|
+
const prefix = prefixes[base]
|
|
7
|
+
return function (n) {
|
|
8
|
+
if (n > 2 ** 31 - 1 || n < -(2 ** 31)) {
|
|
9
|
+
throw new Error('Value must be in range [-2^31, 2^31-1]')
|
|
10
|
+
}
|
|
11
|
+
if (!isInteger(n)) {
|
|
12
|
+
throw new Error('Value must be an integer')
|
|
13
|
+
}
|
|
14
|
+
if (n < 0) {
|
|
15
|
+
n = n + 2 ** 32
|
|
16
|
+
}
|
|
17
|
+
return `${prefix}${n.toString(base)}`
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const dependencies = ['typed']
|
|
22
|
+
|
|
23
|
+
export function createBaseFormatterFactory (name, base) {
|
|
24
|
+
return factory(name, dependencies, ({ typed }) => {
|
|
25
|
+
return typed(name, {
|
|
26
|
+
number: baseFormatter(base)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createBaseFormatterFactory } from './baseUtils'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Format a number as binary.
|
|
5
|
+
*
|
|
6
|
+
* Syntax:
|
|
7
|
+
*
|
|
8
|
+
* math.bin(value)
|
|
9
|
+
*
|
|
10
|
+
* Examples:
|
|
11
|
+
*
|
|
12
|
+
* //the following outputs "0b10"
|
|
13
|
+
* math.bin(2)
|
|
14
|
+
*
|
|
15
|
+
* See also:
|
|
16
|
+
*
|
|
17
|
+
* oct
|
|
18
|
+
* hex
|
|
19
|
+
*
|
|
20
|
+
* @param {number} value Value to be stringified
|
|
21
|
+
* @return {string} The formatted value
|
|
22
|
+
*/
|
|
23
|
+
export const createBin = createBaseFormatterFactory('bin', 2)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createBaseFormatterFactory } from './baseUtils'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Format a number as hexadecimal.
|
|
5
|
+
*
|
|
6
|
+
* Syntax:
|
|
7
|
+
*
|
|
8
|
+
* math.hex(value)
|
|
9
|
+
*
|
|
10
|
+
* Examples:
|
|
11
|
+
*
|
|
12
|
+
* //the following outputs "0xF0"
|
|
13
|
+
* math.hex(240)
|
|
14
|
+
*
|
|
15
|
+
* See also:
|
|
16
|
+
*
|
|
17
|
+
* oct
|
|
18
|
+
* bin
|
|
19
|
+
*
|
|
20
|
+
* @param {number} value Value to be stringified
|
|
21
|
+
* @return {string} The formatted value
|
|
22
|
+
*/
|
|
23
|
+
export const createHex = createBaseFormatterFactory('hex', 16)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createBaseFormatterFactory } from './baseUtils'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Format a number as octal.
|
|
5
|
+
*
|
|
6
|
+
* Syntax:
|
|
7
|
+
*
|
|
8
|
+
* math.oct(value)
|
|
9
|
+
*
|
|
10
|
+
* Examples:
|
|
11
|
+
*
|
|
12
|
+
* //the following outputs "0o70"
|
|
13
|
+
* math.oct(56)
|
|
14
|
+
*
|
|
15
|
+
* See also:
|
|
16
|
+
*
|
|
17
|
+
* bin
|
|
18
|
+
* hex
|
|
19
|
+
*
|
|
20
|
+
* @param {number} value Value to be stringified
|
|
21
|
+
* @return {string} The formatted value
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const createOct = createBaseFormatterFactory('oct', 8)
|
|
@@ -5,7 +5,8 @@ const name = 'BigNumber'
|
|
|
5
5
|
const dependencies = ['?on', 'config']
|
|
6
6
|
|
|
7
7
|
export const createBigNumberClass = /* #__PURE__ */ factory(name, dependencies, ({ on, config }) => {
|
|
8
|
-
const
|
|
8
|
+
const EUCLID = 9 // Use euclidian division for mod calculation
|
|
9
|
+
const BigNumber = Decimal.clone({ precision: config.precision, modulo: EUCLID })
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Attach type information
|
package/src/type/number.js
CHANGED
|
@@ -41,10 +41,18 @@ export const createNumber = /* #__PURE__ */ factory(name, dependencies, ({ typed
|
|
|
41
41
|
|
|
42
42
|
string: function (x) {
|
|
43
43
|
if (x === 'NaN') return NaN
|
|
44
|
-
|
|
44
|
+
let num = Number(x)
|
|
45
45
|
if (isNaN(num)) {
|
|
46
46
|
throw new SyntaxError('String "' + x + '" is no valid number')
|
|
47
47
|
}
|
|
48
|
+
if (['0b', '0o', '0x'].includes(x.substring(0, 2))) {
|
|
49
|
+
if (num > 2 ** 32 - 1) {
|
|
50
|
+
throw new SyntaxError(`String "${x}" is out of range`)
|
|
51
|
+
}
|
|
52
|
+
if (num & 0x80000000) {
|
|
53
|
+
num = -1 * ~(num - 1)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
48
56
|
return num
|
|
49
57
|
},
|
|
50
58
|
|
package/src/utils/object.js
CHANGED
|
@@ -86,7 +86,9 @@ export function deepExtend (a, b) {
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
for (const prop in b) {
|
|
89
|
-
|
|
89
|
+
// We check against prop not being in Object.prototype or Function.prototype
|
|
90
|
+
// to prevent polluting for example Object.__proto__.
|
|
91
|
+
if (hasOwnProperty(b, prop) && !(prop in Object.prototype) && !(prop in Function.prototype)) {
|
|
90
92
|
if (b[prop] && b[prop].constructor === Object) {
|
|
91
93
|
if (a[prop] === undefined) {
|
|
92
94
|
a[prop] = {}
|
package/src/version.js
CHANGED