mathjs 5.0.3 → 5.0.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mathjs might be problematic. Click here for more details.
- package/HISTORY.md +9 -0
- package/README.md +2 -1
- package/dist/math.js +974 -958
- package/dist/math.min.js +5 -5
- package/dist/math.min.map +1 -1
- package/docs/reference/functions/typeof.md +13 -13
- package/lib/function/probability/combinations.js +11 -10
- package/lib/function/probability/gamma.js +5 -13
- package/lib/function/probability/permutations.js +2 -12
- package/lib/function/probability/product.js +19 -0
- package/lib/function/utils/typeof.js +13 -13
- package/lib/utils/array.js +27 -19
- package/lib/version.js +1 -1
- package/package.json +2 -1
- package/src/function/probability/combinations.js +10 -8
- package/src/function/probability/gamma.js +5 -13
- package/src/function/probability/permutations.js +2 -11
- package/src/function/probability/product.js +17 -0
- package/src/function/utils/typeof.js +13 -13
- package/src/utils/array.js +31 -23
- package/src/version.js +1 -1
@@ -29,19 +29,19 @@ math.type.Matrix | `'Matrix'` | `math.typeof(math.matrix([[1,2], [3, 4]
|
|
29
29
|
math.type.Range | `'Range'` | `math.typeof(math.range(0, 10))`
|
30
30
|
math.type.ResultSet | `'ResultSet'` | `math.typeof(math.eval('a=2\nb=3'))`
|
31
31
|
math.type.Unit | `'Unit'` | `math.typeof(math.unit('45 deg'))`
|
32
|
-
math.expression.node
|
33
|
-
math.expression.node
|
34
|
-
math.expression.node
|
35
|
-
math.expression.node
|
36
|
-
math.expression.node
|
37
|
-
math.expression.node
|
38
|
-
math.expression.node
|
39
|
-
math.expression.node
|
40
|
-
math.expression.node
|
41
|
-
math.expression.node
|
42
|
-
math.expression.node
|
43
|
-
math.expression.node
|
44
|
-
math.expression.node
|
32
|
+
math.expression.node​.AccessorNode | `'AccessorNode'` | `math.typeof(math.parse('A[2]'))`
|
33
|
+
math.expression.node​.ArrayNode | `'ArrayNode'` | `math.typeof(math.parse('[1,2,3]'))`
|
34
|
+
math.expression.node​.AssignmentNode | `'AssignmentNode'` | `math.typeof(math.parse('x=2'))`
|
35
|
+
math.expression.node​.BlockNode | `'BlockNode'` | `math.typeof(math.parse('a=2; b=3'))`
|
36
|
+
math.expression.node​.ConditionalNode | `'ConditionalNode'` | `math.typeof(math.parse('x<0 ? -x : x'))`
|
37
|
+
math.expression.node​.ConstantNode | `'ConstantNode'` | `math.typeof(math.parse('2.3'))`
|
38
|
+
math.expression.node​.FunctionAssignmentNode | `'FunctionAssignmentNode'` | `math.typeof(math.parse('f(x)=x^2'))`
|
39
|
+
math.expression.node​.FunctionNode | `'FunctionNode'` | `math.typeof(math.parse('sqrt(4)'))`
|
40
|
+
math.expression.node​.IndexNode | `'IndexNode'` | `math.typeof(math.parse('A[2]').index)`
|
41
|
+
math.expression.node​.ObjectNode | `'ObjectNode'` | `math.typeof(math.parse('{a:2}'))`
|
42
|
+
math.expression.node​.ParenthesisNode | `'ParenthesisNode'` | `math.typeof(math.parse('(2+3)'))`
|
43
|
+
math.expression.node​.RangeNode | `'RangeNode'` | `math.typeof(math.parse('1:10'))`
|
44
|
+
math.expression.node​.SymbolNode | `'SymbolNode'` | `math.typeof(math.parse('x'))`
|
45
45
|
|
46
46
|
|
47
47
|
## Syntax
|
@@ -1,7 +1,7 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
3
|
var isInteger = require('../../utils/number').isInteger;
|
4
|
-
|
4
|
+
var product = require('./product');
|
5
5
|
function factory(type, config, load, typed) {
|
6
6
|
/**
|
7
7
|
* Compute the number of ways of picking `k` unordered outcomes from `n`
|
@@ -26,11 +26,11 @@ function factory(type, config, load, typed) {
|
|
26
26
|
* @param {number | BigNumber} k Number of objects in the subset
|
27
27
|
* @return {number | BigNumber} Number of possible combinations.
|
28
28
|
*/
|
29
|
+
|
29
30
|
var combinations = typed('combinations', {
|
30
31
|
'number, number': function numberNumber(n, k) {
|
31
|
-
var
|
32
|
-
|
33
|
-
i = void 0;
|
32
|
+
var prodrange = void 0,
|
33
|
+
nMinusk = void 0;
|
34
34
|
|
35
35
|
if (!isInteger(n) || n < 0) {
|
36
36
|
throw new TypeError('Positive integer value expected in function combinations');
|
@@ -42,13 +42,14 @@ function factory(type, config, load, typed) {
|
|
42
42
|
throw new TypeError('k must be less than or equal to n');
|
43
43
|
}
|
44
44
|
|
45
|
-
|
46
|
-
result = 1;
|
47
|
-
for (i = 1; i <= n - max; i++) {
|
48
|
-
result = result * (max + i) / i;
|
49
|
-
}
|
45
|
+
nMinusk = n - k;
|
50
46
|
|
51
|
-
|
47
|
+
if (k < nMinusk) {
|
48
|
+
prodrange = product(nMinusk + 1, n);
|
49
|
+
return prodrange / product(1, k);
|
50
|
+
}
|
51
|
+
prodrange = product(k + 1, n);
|
52
|
+
return prodrange / product(1, nMinusk);
|
52
53
|
},
|
53
54
|
|
54
55
|
'BigNumber, BigNumber': function BigNumberBigNumber(n, k) {
|
@@ -6,7 +6,7 @@ var isInteger = require('../../utils/number').isInteger;
|
|
6
6
|
function factory(type, config, load, typed) {
|
7
7
|
var multiply = load(require('../arithmetic/multiply'));
|
8
8
|
var pow = load(require('../arithmetic/pow'));
|
9
|
-
|
9
|
+
var product = require('./product');
|
10
10
|
/**
|
11
11
|
* Compute the gamma function of a value using Lanczos approximation for
|
12
12
|
* small values, and an extended Stirling approximation for large values.
|
@@ -30,7 +30,9 @@ function factory(type, config, load, typed) {
|
|
30
30
|
* @param {number | Array | Matrix} n A real or complex number
|
31
31
|
* @return {number | Array | Matrix} The gamma of `n`
|
32
32
|
*/
|
33
|
+
|
33
34
|
var gamma = typed('gamma', {
|
35
|
+
|
34
36
|
'number': function number(n) {
|
35
37
|
var t = void 0,
|
36
38
|
x = void 0;
|
@@ -44,18 +46,7 @@ function factory(type, config, load, typed) {
|
|
44
46
|
return Infinity; // Will overflow
|
45
47
|
}
|
46
48
|
|
47
|
-
|
48
|
-
var res = n - 1;
|
49
|
-
while (value > 1) {
|
50
|
-
res *= value;
|
51
|
-
value--;
|
52
|
-
}
|
53
|
-
|
54
|
-
if (res === 0) {
|
55
|
-
res = 1; // 0! is per definition 1
|
56
|
-
}
|
57
|
-
|
58
|
-
return res;
|
49
|
+
return product(1, n - 1);
|
59
50
|
}
|
60
51
|
|
61
52
|
if (n < 0.5) {
|
@@ -150,6 +141,7 @@ function factory(type, config, load, typed) {
|
|
150
141
|
* @param {BigNumber} n
|
151
142
|
* @returns {BigNumber} Returns the factorial of n
|
152
143
|
*/
|
144
|
+
|
153
145
|
function bigFactorial(n) {
|
154
146
|
if (n.isZero()) {
|
155
147
|
return new type.BigNumber(1); // 0! is per definition 1
|
@@ -4,7 +4,7 @@ var isInteger = require('../../utils/number').isInteger;
|
|
4
4
|
|
5
5
|
function factory(type, config, load, typed) {
|
6
6
|
var factorial = load(require('./factorial'));
|
7
|
-
|
7
|
+
var product = require('./product');
|
8
8
|
/**
|
9
9
|
* Compute the number of ways of obtaining an ordered subset of `k` elements
|
10
10
|
* from a set of `n` elements.
|
@@ -32,11 +32,7 @@ function factory(type, config, load, typed) {
|
|
32
32
|
*/
|
33
33
|
var permutations = typed('permutations', {
|
34
34
|
'number | BigNumber': factorial,
|
35
|
-
|
36
35
|
'number, number': function numberNumber(n, k) {
|
37
|
-
var result = void 0,
|
38
|
-
i = void 0;
|
39
|
-
|
40
36
|
if (!isInteger(n) || n < 0) {
|
41
37
|
throw new TypeError('Positive integer value expected in function permutations');
|
42
38
|
}
|
@@ -46,14 +42,8 @@ function factory(type, config, load, typed) {
|
|
46
42
|
if (k > n) {
|
47
43
|
throw new TypeError('second argument k must be less than or equal to first argument n');
|
48
44
|
}
|
49
|
-
|
50
45
|
// Permute n objects, k at a time
|
51
|
-
|
52
|
-
for (i = n - k + 1; i <= n; i++) {
|
53
|
-
result = result * i;
|
54
|
-
}
|
55
|
-
|
56
|
-
return result;
|
46
|
+
return product(n - k + 1, n);
|
57
47
|
},
|
58
48
|
|
59
49
|
'BigNumber, BigNumber': function BigNumberBigNumber(n, k) {
|
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
/** @param {integer} i
|
4
|
+
* @param {integer} n
|
5
|
+
* @returns : product of i to n
|
6
|
+
*/
|
7
|
+
function product(i, n) {
|
8
|
+
var half = void 0;
|
9
|
+
if (n < i) {
|
10
|
+
return 1;
|
11
|
+
}
|
12
|
+
if (n === i) {
|
13
|
+
return n;
|
14
|
+
}
|
15
|
+
half = n + i >> 1; // divide (n + i) by 2 and truncate to integer
|
16
|
+
return product(i, half) * product(half + 1, n);
|
17
|
+
}
|
18
|
+
|
19
|
+
module.exports = product;
|
@@ -31,19 +31,19 @@ function factory(type, config, load, typed) {
|
|
31
31
|
* math.type.Range | `'Range'` | `math.typeof(math.range(0, 10))`
|
32
32
|
* math.type.ResultSet | `'ResultSet'` | `math.typeof(math.eval('a=2\nb=3'))`
|
33
33
|
* math.type.Unit | `'Unit'` | `math.typeof(math.unit('45 deg'))`
|
34
|
-
* math.expression.node
|
35
|
-
* math.expression.node
|
36
|
-
* math.expression.node
|
37
|
-
* math.expression.node
|
38
|
-
* math.expression.node
|
39
|
-
* math.expression.node
|
40
|
-
* math.expression.node
|
41
|
-
* math.expression.node
|
42
|
-
* math.expression.node
|
43
|
-
* math.expression.node
|
44
|
-
* math.expression.node
|
45
|
-
* math.expression.node
|
46
|
-
* math.expression.node
|
34
|
+
* math.expression.node​.AccessorNode | `'AccessorNode'` | `math.typeof(math.parse('A[2]'))`
|
35
|
+
* math.expression.node​.ArrayNode | `'ArrayNode'` | `math.typeof(math.parse('[1,2,3]'))`
|
36
|
+
* math.expression.node​.AssignmentNode | `'AssignmentNode'` | `math.typeof(math.parse('x=2'))`
|
37
|
+
* math.expression.node​.BlockNode | `'BlockNode'` | `math.typeof(math.parse('a=2; b=3'))`
|
38
|
+
* math.expression.node​.ConditionalNode | `'ConditionalNode'` | `math.typeof(math.parse('x<0 ? -x : x'))`
|
39
|
+
* math.expression.node​.ConstantNode | `'ConstantNode'` | `math.typeof(math.parse('2.3'))`
|
40
|
+
* math.expression.node​.FunctionAssignmentNode | `'FunctionAssignmentNode'` | `math.typeof(math.parse('f(x)=x^2'))`
|
41
|
+
* math.expression.node​.FunctionNode | `'FunctionNode'` | `math.typeof(math.parse('sqrt(4)'))`
|
42
|
+
* math.expression.node​.IndexNode | `'IndexNode'` | `math.typeof(math.parse('A[2]').index)`
|
43
|
+
* math.expression.node​.ObjectNode | `'ObjectNode'` | `math.typeof(math.parse('{a:2}'))`
|
44
|
+
* math.expression.node​.ParenthesisNode | `'ParenthesisNode'` | `math.typeof(math.parse('(2+3)'))`
|
45
|
+
* math.expression.node​.RangeNode | `'RangeNode'` | `math.typeof(math.parse('1:10'))`
|
46
|
+
* math.expression.node​.SymbolNode | `'SymbolNode'` | `math.typeof(math.parse('x'))`
|
47
47
|
*
|
48
48
|
* Syntax:
|
49
49
|
*
|
package/lib/utils/array.js
CHANGED
@@ -247,6 +247,15 @@ function reshape(array, sizes) {
|
|
247
247
|
throw new _DimensionError2.default(0, product(exports.size(array)), '!=');
|
248
248
|
}
|
249
249
|
|
250
|
+
var totalSize = 1;
|
251
|
+
for (var sizeIndex = 0; sizeIndex < sizes.length; sizeIndex++) {
|
252
|
+
totalSize *= sizes[sizeIndex];
|
253
|
+
}
|
254
|
+
|
255
|
+
if (flatArray.length !== totalSize) {
|
256
|
+
throw new _DimensionError2.default(product(sizes), product(exports.size(array)), '!=');
|
257
|
+
}
|
258
|
+
|
250
259
|
try {
|
251
260
|
newArray = _reshape(flatArray, sizes);
|
252
261
|
} catch (e) {
|
@@ -256,37 +265,36 @@ function reshape(array, sizes) {
|
|
256
265
|
throw e;
|
257
266
|
}
|
258
267
|
|
259
|
-
if (flatArray.length > 0) {
|
260
|
-
throw new _DimensionError2.default(product(sizes), product(exports.size(array)), '!=');
|
261
|
-
}
|
262
|
-
|
263
268
|
return newArray;
|
264
269
|
}
|
265
270
|
|
266
271
|
/**
|
267
|
-
*
|
272
|
+
* Iteratively re-shape a multi dimensional array to fit the specified dimensions
|
268
273
|
* @param {Array} array Array to be reshaped
|
269
274
|
* @param {Array.<number>} sizes List of sizes for each dimension
|
270
275
|
* @returns {Array} Array whose data has been formatted to fit the
|
271
276
|
* specified dimensions
|
272
|
-
*
|
273
|
-
* @throws {DimensionError} If the product of the new dimension sizes does
|
274
|
-
* not equal that of the old ones
|
275
277
|
*/
|
276
|
-
function _reshape(array, sizes) {
|
277
|
-
var accumulator = [];
|
278
|
-
var i = void 0;
|
279
278
|
|
280
|
-
|
281
|
-
|
282
|
-
|
279
|
+
function _reshape(array, sizes) {
|
280
|
+
// testing if there are enough elements for the requested shape
|
281
|
+
var tmpArray = array;
|
282
|
+
var tmpArray2;
|
283
|
+
// for each dimensions starting by the last one and ignoring the first one
|
284
|
+
for (var sizeIndex = sizes.length - 1; sizeIndex > 0; sizeIndex--) {
|
285
|
+
var size = sizes[sizeIndex];
|
286
|
+
tmpArray2 = [];
|
287
|
+
|
288
|
+
// aggregate the elements of the current tmpArray in elements of the requested size
|
289
|
+
var length = tmpArray.length / size;
|
290
|
+
for (var i = 0; i < length; i++) {
|
291
|
+
tmpArray2.push(tmpArray.slice(i * size, (i + 1) * size));
|
283
292
|
}
|
284
|
-
return
|
293
|
+
// set it as the new tmpArray for the next loop turn or for return
|
294
|
+
tmpArray = tmpArray2;
|
285
295
|
}
|
286
|
-
|
287
|
-
|
288
|
-
}
|
289
|
-
return accumulator;
|
296
|
+
|
297
|
+
return tmpArray;
|
290
298
|
}
|
291
299
|
|
292
300
|
/**
|
package/lib/version.js
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "mathjs",
|
3
|
-
"version": "5.0.
|
3
|
+
"version": "5.0.4",
|
4
4
|
"description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.",
|
5
5
|
"author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
|
6
6
|
"contributors": [
|
@@ -30,6 +30,7 @@
|
|
30
30
|
"Gary Passero (https://github.com/gap777)",
|
31
31
|
"Harry Sarson (https://github.com/HarrySarson)",
|
32
32
|
"Holman Gao (https://github.com/golmansax)",
|
33
|
+
"Honybar (https://github.com/honeybar)",
|
33
34
|
"Guillermo Indalecio Fernández (https://github.com/guillermobox)",
|
34
35
|
"Gulfaraz (https://github.com/gulfaraz)",
|
35
36
|
"hamadu (https://github.com/hamadu)",
|
@@ -1,7 +1,7 @@
|
|
1
1
|
'use strict'
|
2
2
|
|
3
3
|
const isInteger = require('../../utils/number').isInteger
|
4
|
-
|
4
|
+
const product = require('./product')
|
5
5
|
function factory (type, config, load, typed) {
|
6
6
|
/**
|
7
7
|
* Compute the number of ways of picking `k` unordered outcomes from `n`
|
@@ -26,9 +26,10 @@ function factory (type, config, load, typed) {
|
|
26
26
|
* @param {number | BigNumber} k Number of objects in the subset
|
27
27
|
* @return {number | BigNumber} Number of possible combinations.
|
28
28
|
*/
|
29
|
+
|
29
30
|
const combinations = typed('combinations', {
|
30
31
|
'number, number': function (n, k) {
|
31
|
-
let
|
32
|
+
let prodrange, nMinusk
|
32
33
|
|
33
34
|
if (!isInteger(n) || n < 0) {
|
34
35
|
throw new TypeError('Positive integer value expected in function combinations')
|
@@ -40,13 +41,14 @@ function factory (type, config, load, typed) {
|
|
40
41
|
throw new TypeError('k must be less than or equal to n')
|
41
42
|
}
|
42
43
|
|
43
|
-
|
44
|
-
result = 1
|
45
|
-
for (i = 1; i <= n - max; i++) {
|
46
|
-
result = result * (max + i) / i
|
47
|
-
}
|
44
|
+
nMinusk = n - k
|
48
45
|
|
49
|
-
|
46
|
+
if (k < nMinusk) {
|
47
|
+
prodrange = product(nMinusk + 1, n)
|
48
|
+
return prodrange / product(1, k)
|
49
|
+
}
|
50
|
+
prodrange = product(k + 1, n)
|
51
|
+
return prodrange / product(1, nMinusk)
|
50
52
|
},
|
51
53
|
|
52
54
|
'BigNumber, BigNumber': function (n, k) {
|
@@ -6,7 +6,7 @@ const isInteger = require('../../utils/number').isInteger
|
|
6
6
|
function factory (type, config, load, typed) {
|
7
7
|
const multiply = load(require('../arithmetic/multiply'))
|
8
8
|
const pow = load(require('../arithmetic/pow'))
|
9
|
-
|
9
|
+
const product = require('./product')
|
10
10
|
/**
|
11
11
|
* Compute the gamma function of a value using Lanczos approximation for
|
12
12
|
* small values, and an extended Stirling approximation for large values.
|
@@ -30,7 +30,9 @@ function factory (type, config, load, typed) {
|
|
30
30
|
* @param {number | Array | Matrix} n A real or complex number
|
31
31
|
* @return {number | Array | Matrix} The gamma of `n`
|
32
32
|
*/
|
33
|
+
|
33
34
|
const gamma = typed('gamma', {
|
35
|
+
|
34
36
|
'number': function (n) {
|
35
37
|
let t, x
|
36
38
|
|
@@ -43,18 +45,7 @@ function factory (type, config, load, typed) {
|
|
43
45
|
return Infinity // Will overflow
|
44
46
|
}
|
45
47
|
|
46
|
-
|
47
|
-
let res = n - 1
|
48
|
-
while (value > 1) {
|
49
|
-
res *= value
|
50
|
-
value--
|
51
|
-
}
|
52
|
-
|
53
|
-
if (res === 0) {
|
54
|
-
res = 1 // 0! is per definition 1
|
55
|
-
}
|
56
|
-
|
57
|
-
return res
|
48
|
+
return product(1, n - 1)
|
58
49
|
}
|
59
50
|
|
60
51
|
if (n < 0.5) {
|
@@ -153,6 +144,7 @@ function factory (type, config, load, typed) {
|
|
153
144
|
* @param {BigNumber} n
|
154
145
|
* @returns {BigNumber} Returns the factorial of n
|
155
146
|
*/
|
147
|
+
|
156
148
|
function bigFactorial (n) {
|
157
149
|
if (n.isZero()) {
|
158
150
|
return new type.BigNumber(1) // 0! is per definition 1
|
@@ -4,7 +4,7 @@ const isInteger = require('../../utils/number').isInteger
|
|
4
4
|
|
5
5
|
function factory (type, config, load, typed) {
|
6
6
|
const factorial = load(require('./factorial'))
|
7
|
-
|
7
|
+
const product = require('./product')
|
8
8
|
/**
|
9
9
|
* Compute the number of ways of obtaining an ordered subset of `k` elements
|
10
10
|
* from a set of `n` elements.
|
@@ -32,10 +32,7 @@ function factory (type, config, load, typed) {
|
|
32
32
|
*/
|
33
33
|
const permutations = typed('permutations', {
|
34
34
|
'number | BigNumber': factorial,
|
35
|
-
|
36
35
|
'number, number': function (n, k) {
|
37
|
-
let result, i
|
38
|
-
|
39
36
|
if (!isInteger(n) || n < 0) {
|
40
37
|
throw new TypeError('Positive integer value expected in function permutations')
|
41
38
|
}
|
@@ -45,14 +42,8 @@ function factory (type, config, load, typed) {
|
|
45
42
|
if (k > n) {
|
46
43
|
throw new TypeError('second argument k must be less than or equal to first argument n')
|
47
44
|
}
|
48
|
-
|
49
45
|
// Permute n objects, k at a time
|
50
|
-
|
51
|
-
for (i = n - k + 1; i <= n; i++) {
|
52
|
-
result = result * i
|
53
|
-
}
|
54
|
-
|
55
|
-
return result
|
46
|
+
return product((n - k) + 1, n)
|
56
47
|
},
|
57
48
|
|
58
49
|
'BigNumber, BigNumber': function (n, k) {
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/** @param {integer} i
|
2
|
+
* @param {integer} n
|
3
|
+
* @returns : product of i to n
|
4
|
+
*/
|
5
|
+
function product (i, n) {
|
6
|
+
let half
|
7
|
+
if (n < i) {
|
8
|
+
return 1
|
9
|
+
}
|
10
|
+
if (n === i) {
|
11
|
+
return n
|
12
|
+
}
|
13
|
+
half = (n + i) >> 1 // divide (n + i) by 2 and truncate to integer
|
14
|
+
return product(i, half) * product(half + 1, n)
|
15
|
+
}
|
16
|
+
|
17
|
+
module.exports = product
|
@@ -29,19 +29,19 @@ function factory (type, config, load, typed) {
|
|
29
29
|
* math.type.Range | `'Range'` | `math.typeof(math.range(0, 10))`
|
30
30
|
* math.type.ResultSet | `'ResultSet'` | `math.typeof(math.eval('a=2\nb=3'))`
|
31
31
|
* math.type.Unit | `'Unit'` | `math.typeof(math.unit('45 deg'))`
|
32
|
-
* math.expression.node
|
33
|
-
* math.expression.node
|
34
|
-
* math.expression.node
|
35
|
-
* math.expression.node
|
36
|
-
* math.expression.node
|
37
|
-
* math.expression.node
|
38
|
-
* math.expression.node
|
39
|
-
* math.expression.node
|
40
|
-
* math.expression.node
|
41
|
-
* math.expression.node
|
42
|
-
* math.expression.node
|
43
|
-
* math.expression.node
|
44
|
-
* math.expression.node
|
32
|
+
* math.expression.node​.AccessorNode | `'AccessorNode'` | `math.typeof(math.parse('A[2]'))`
|
33
|
+
* math.expression.node​.ArrayNode | `'ArrayNode'` | `math.typeof(math.parse('[1,2,3]'))`
|
34
|
+
* math.expression.node​.AssignmentNode | `'AssignmentNode'` | `math.typeof(math.parse('x=2'))`
|
35
|
+
* math.expression.node​.BlockNode | `'BlockNode'` | `math.typeof(math.parse('a=2; b=3'))`
|
36
|
+
* math.expression.node​.ConditionalNode | `'ConditionalNode'` | `math.typeof(math.parse('x<0 ? -x : x'))`
|
37
|
+
* math.expression.node​.ConstantNode | `'ConstantNode'` | `math.typeof(math.parse('2.3'))`
|
38
|
+
* math.expression.node​.FunctionAssignmentNode | `'FunctionAssignmentNode'` | `math.typeof(math.parse('f(x)=x^2'))`
|
39
|
+
* math.expression.node​.FunctionNode | `'FunctionNode'` | `math.typeof(math.parse('sqrt(4)'))`
|
40
|
+
* math.expression.node​.IndexNode | `'IndexNode'` | `math.typeof(math.parse('A[2]').index)`
|
41
|
+
* math.expression.node​.ObjectNode | `'ObjectNode'` | `math.typeof(math.parse('{a:2}'))`
|
42
|
+
* math.expression.node​.ParenthesisNode | `'ParenthesisNode'` | `math.typeof(math.parse('(2+3)'))`
|
43
|
+
* math.expression.node​.RangeNode | `'RangeNode'` | `math.typeof(math.parse('1:10'))`
|
44
|
+
* math.expression.node​.SymbolNode | `'SymbolNode'` | `math.typeof(math.parse('x'))`
|
45
45
|
*
|
46
46
|
* Syntax:
|
47
47
|
*
|
package/src/utils/array.js
CHANGED
@@ -218,6 +218,19 @@ export function reshape (array, sizes) {
|
|
218
218
|
throw new DimensionError(0, product(exports.size(array)), '!=')
|
219
219
|
}
|
220
220
|
|
221
|
+
var totalSize = 1
|
222
|
+
for (var sizeIndex = 0; sizeIndex < sizes.length; sizeIndex++) {
|
223
|
+
totalSize *= sizes[sizeIndex]
|
224
|
+
}
|
225
|
+
|
226
|
+
if (flatArray.length !== totalSize) {
|
227
|
+
throw new DimensionError(
|
228
|
+
product(sizes),
|
229
|
+
product(exports.size(array)),
|
230
|
+
'!='
|
231
|
+
)
|
232
|
+
}
|
233
|
+
|
221
234
|
try {
|
222
235
|
newArray = _reshape(flatArray, sizes)
|
223
236
|
} catch (e) {
|
@@ -231,41 +244,36 @@ export function reshape (array, sizes) {
|
|
231
244
|
throw e
|
232
245
|
}
|
233
246
|
|
234
|
-
if (flatArray.length > 0) {
|
235
|
-
throw new DimensionError(
|
236
|
-
product(sizes),
|
237
|
-
product(exports.size(array)),
|
238
|
-
'!='
|
239
|
-
)
|
240
|
-
}
|
241
|
-
|
242
247
|
return newArray
|
243
248
|
}
|
244
249
|
|
245
250
|
/**
|
246
|
-
*
|
251
|
+
* Iteratively re-shape a multi dimensional array to fit the specified dimensions
|
247
252
|
* @param {Array} array Array to be reshaped
|
248
253
|
* @param {Array.<number>} sizes List of sizes for each dimension
|
249
254
|
* @returns {Array} Array whose data has been formatted to fit the
|
250
255
|
* specified dimensions
|
251
|
-
*
|
252
|
-
* @throws {DimensionError} If the product of the new dimension sizes does
|
253
|
-
* not equal that of the old ones
|
254
256
|
*/
|
255
|
-
function _reshape (array, sizes) {
|
256
|
-
let accumulator = []
|
257
|
-
let i
|
258
257
|
|
259
|
-
|
260
|
-
|
261
|
-
|
258
|
+
function _reshape (array, sizes) {
|
259
|
+
// testing if there are enough elements for the requested shape
|
260
|
+
var tmpArray = array
|
261
|
+
var tmpArray2
|
262
|
+
// for each dimensions starting by the last one and ignoring the first one
|
263
|
+
for (var sizeIndex = sizes.length - 1; sizeIndex > 0; sizeIndex--) {
|
264
|
+
var size = sizes[sizeIndex]
|
265
|
+
tmpArray2 = []
|
266
|
+
|
267
|
+
// aggregate the elements of the current tmpArray in elements of the requested size
|
268
|
+
var length = tmpArray.length / size
|
269
|
+
for (var i = 0; i < length; i++) {
|
270
|
+
tmpArray2.push(tmpArray.slice(i * size, (i + 1) * size))
|
262
271
|
}
|
263
|
-
return
|
264
|
-
|
265
|
-
for (i = 0; i < sizes[0]; i += 1) {
|
266
|
-
accumulator.push(_reshape(array, sizes.slice(1)))
|
272
|
+
// set it as the new tmpArray for the next loop turn or for return
|
273
|
+
tmpArray = tmpArray2
|
267
274
|
}
|
268
|
-
|
275
|
+
|
276
|
+
return tmpArray
|
269
277
|
}
|
270
278
|
|
271
279
|
/**
|
package/src/version.js
CHANGED