mathjs 7.3.0 → 7.6.0

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.
Files changed (104) hide show
  1. package/HISTORY.md +29 -0
  2. package/dist/math.js +2716 -2222
  3. package/dist/math.min.js +5 -5
  4. package/dist/math.min.map +1 -1
  5. package/docs/datatypes/units.md +21 -0
  6. package/docs/reference/functions/ceil.md +10 -1
  7. package/docs/reference/functions/fix.md +10 -2
  8. package/docs/reference/functions/floor.md +12 -3
  9. package/docs/reference/functions/rotate.md +47 -0
  10. package/docs/reference/functions/rotationMatrix.md +51 -0
  11. package/docs/reference/functions/round.md +6 -2
  12. package/docs/reference/functions.md +2 -0
  13. package/es/entry/dependenciesAny/dependenciesCeil.generated.js +4 -0
  14. package/es/entry/dependenciesAny/dependenciesFix.generated.js +2 -0
  15. package/es/entry/dependenciesAny/dependenciesFloor.generated.js +4 -0
  16. package/es/entry/dependenciesAny/dependenciesRotate.generated.js +14 -0
  17. package/es/entry/dependenciesAny/dependenciesRotationMatrix.generated.js +30 -0
  18. package/es/entry/dependenciesAny.generated.js +2 -0
  19. package/es/entry/impureFunctionsAny.generated.js +55 -53
  20. package/es/entry/pureFunctionsAny.generated.js +230 -206
  21. package/es/expression/embeddedDocs/embeddedDocs.js +4 -0
  22. package/es/expression/embeddedDocs/function/matrix/rotate.js +8 -0
  23. package/es/expression/embeddedDocs/function/matrix/rotationMatrix.js +8 -0
  24. package/es/expression/node/FunctionNode.js +1 -1
  25. package/es/expression/node/Node.js +2 -2
  26. package/es/factoriesAny.js +2 -0
  27. package/es/function/arithmetic/ceil.js +88 -4
  28. package/es/function/arithmetic/fix.js +43 -6
  29. package/es/function/arithmetic/floor.js +90 -6
  30. package/es/function/arithmetic/round.js +6 -2
  31. package/es/function/matrix/rotate.js +78 -0
  32. package/es/function/matrix/rotationMatrix.js +176 -0
  33. package/es/function/matrix/sqrtm.js +4 -0
  34. package/es/function/probability/pickRandom.js +2 -6
  35. package/es/function/special/erf.js +1 -0
  36. package/es/plain/number/constants.js +1 -1
  37. package/es/plain/number/probability.js +1 -0
  38. package/es/type/unit/Unit.js +17 -18
  39. package/es/utils/object.js +3 -1
  40. package/es/version.js +1 -1
  41. package/examples/bignumbers.js +2 -0
  42. package/lib/entry/dependenciesAny/dependenciesCeil.generated.js +6 -0
  43. package/lib/entry/dependenciesAny/dependenciesFix.generated.js +3 -0
  44. package/lib/entry/dependenciesAny/dependenciesFloor.generated.js +6 -0
  45. package/lib/entry/dependenciesAny/dependenciesRotate.generated.js +26 -0
  46. package/lib/entry/dependenciesAny/dependenciesRotationMatrix.generated.js +50 -0
  47. package/lib/entry/dependenciesAny.generated.js +16 -0
  48. package/lib/entry/impureFunctionsAny.generated.js +58 -56
  49. package/lib/entry/mainAny.js +5 -0
  50. package/lib/entry/mainNumber.js +5 -0
  51. package/lib/entry/pureFunctionsAny.generated.js +270 -244
  52. package/lib/expression/embeddedDocs/embeddedDocs.js +6 -0
  53. package/lib/expression/embeddedDocs/function/matrix/rotate.js +15 -0
  54. package/lib/expression/embeddedDocs/function/matrix/rotationMatrix.js +15 -0
  55. package/lib/expression/node/FunctionNode.js +1 -1
  56. package/lib/expression/node/Node.js +2 -2
  57. package/lib/factoriesAny.js +16 -0
  58. package/lib/function/arithmetic/ceil.js +91 -4
  59. package/lib/function/arithmetic/fix.js +44 -6
  60. package/lib/function/arithmetic/floor.js +93 -6
  61. package/lib/function/arithmetic/round.js +6 -2
  62. package/lib/function/matrix/rotate.js +88 -0
  63. package/lib/function/matrix/rotationMatrix.js +186 -0
  64. package/lib/function/matrix/sqrtm.js +4 -0
  65. package/lib/function/probability/pickRandom.js +3 -7
  66. package/lib/function/special/erf.js +1 -0
  67. package/lib/header.js +2 -2
  68. package/lib/plain/bignumber/index.js +1 -0
  69. package/lib/plain/number/constants.js +2 -1
  70. package/lib/plain/number/index.js +9 -0
  71. package/lib/plain/number/probability.js +1 -0
  72. package/lib/type/unit/Unit.js +17 -18
  73. package/lib/utils/object.js +3 -1
  74. package/lib/version.js +1 -1
  75. package/package.json +11 -11
  76. package/src/core/function/import.js +2 -2
  77. package/src/entry/dependenciesAny/dependenciesCeil.generated.js +4 -0
  78. package/src/entry/dependenciesAny/dependenciesFix.generated.js +2 -0
  79. package/src/entry/dependenciesAny/dependenciesFloor.generated.js +4 -0
  80. package/src/entry/dependenciesAny/dependenciesRotate.generated.js +15 -0
  81. package/src/entry/dependenciesAny/dependenciesRotationMatrix.generated.js +31 -0
  82. package/src/entry/dependenciesAny.generated.js +2 -0
  83. package/src/entry/impureFunctionsAny.generated.js +84 -80
  84. package/src/entry/pureFunctionsAny.generated.js +75 -71
  85. package/src/expression/embeddedDocs/embeddedDocs.js +4 -0
  86. package/src/expression/embeddedDocs/function/matrix/rotate.js +19 -0
  87. package/src/expression/embeddedDocs/function/matrix/rotationMatrix.js +19 -0
  88. package/src/expression/node/FunctionNode.js +1 -1
  89. package/src/expression/node/Node.js +2 -1
  90. package/src/factoriesAny.js +2 -0
  91. package/src/function/arithmetic/ceil.js +63 -3
  92. package/src/function/arithmetic/fix.js +45 -6
  93. package/src/function/arithmetic/floor.js +65 -5
  94. package/src/function/arithmetic/round.js +6 -2
  95. package/src/function/matrix/rotate.js +79 -0
  96. package/src/function/matrix/rotationMatrix.js +186 -0
  97. package/src/function/matrix/sqrtm.js +4 -0
  98. package/src/function/probability/pickRandom.js +2 -6
  99. package/src/function/special/erf.js +2 -0
  100. package/src/plain/number/constants.js +1 -1
  101. package/src/plain/number/probability.js +2 -0
  102. package/src/type/unit/Unit.js +18 -21
  103. package/src/utils/object.js +3 -1
  104. package/src/version.js +1 -1
@@ -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
- * const c = math.complex(3.2, -2.7)
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
- * const c = math.complex(3.2, -2.7)
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]) // returns Array [3, 3, -4]
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 {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
33
- * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
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
- * const c = math.complex(3.2, -2.7)
26
- * math.floor(c) // returns Complex 3 - 3i
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]) // returns Array [3, 3, -5]
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
  })
@@ -34,10 +34,14 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed,
34
34
  *
35
35
  * Examples:
36
36
  *
37
- * math.round(3.2) // returns number 3
38
- * math.round(3.8) // returns number 4
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
  *
@@ -0,0 +1,79 @@
1
+ import { factory } from '../../utils/factory'
2
+ import { arraySize } from '../../utils/array'
3
+
4
+ const name = 'rotate'
5
+ const dependencies = [
6
+ 'typed',
7
+ 'multiply',
8
+ 'rotationMatrix'
9
+ ]
10
+
11
+ export const createRotate = /* #__PURE__ */ factory(name, dependencies, (
12
+ {
13
+ typed, multiply, rotationMatrix
14
+ }) => {
15
+ /**
16
+ * Rotate a vector of size 1x2 counter-clockwise by a given angle
17
+ * Rotate a vector of size 1x3 counter-clockwise by a given angle around the given axis
18
+ *
19
+ * Syntax:
20
+ *
21
+ * math.rotate(w, theta)
22
+ * math.rotate(w, theta, v)
23
+ *
24
+ * Examples:
25
+ *
26
+ * math.rotate([11, 12], math.pi / 2) // returns matrix([-12, 11])
27
+ * math.rotate(matrix([11, 12]), math.pi / 2) // returns matrix([-12, 11])
28
+ *
29
+ * math.rotate([1, 0, 0], unit('90deg'), [0, 0, 1]) // returns matrix([0, 1, 0])
30
+ * math.rotate(matrix([1, 0, 0]), unit('90deg'), [0, 0, 1]) // returns matrix([0, 1, 0])
31
+ *
32
+ * math.rotate([1, 0], math.complex(1 + i)) // returns matrix([cos(1 + i) - sin(1 + i), sin(1 + i) + cos(1 + i)])
33
+ *
34
+ * See also:
35
+ *
36
+ * matrix, rotationMatrix
37
+ *
38
+ * @param {Array | Matrix} w Vector to rotate
39
+ * @param {number | BigNumber | Complex | Unit} theta Rotation angle
40
+ * @param {Array | Matrix} [v] Rotation axis
41
+ * @return {Array | Matrix} Multiplication of the rotation matrix and w
42
+ */
43
+ return typed(name, {
44
+ 'Array , number | BigNumber | Complex | Unit': function (w, theta) {
45
+ _validateSize(w, 2)
46
+ const matrixRes = multiply(rotationMatrix(theta), w)
47
+ return matrixRes.toArray()
48
+ },
49
+
50
+ 'Matrix , number | BigNumber | Complex | Unit': function (w, theta) {
51
+ _validateSize(w, 2)
52
+ return multiply(rotationMatrix(theta), w)
53
+ },
54
+
55
+ 'Array, number | BigNumber | Complex | Unit, Array | Matrix': function (w, theta, v) {
56
+ _validateSize(w, 3)
57
+ const matrixRes = multiply(rotationMatrix(theta, v), w)
58
+ return matrixRes
59
+ },
60
+
61
+ 'Matrix, number | BigNumber | Complex | Unit, Array | Matrix': function (w, theta, v) {
62
+ _validateSize(w, 3)
63
+ return multiply(rotationMatrix(theta, v), w)
64
+ }
65
+ })
66
+
67
+ function _validateSize (v, expectedSize) {
68
+ const actualSize = Array.isArray(v) ? arraySize(v) : v.size()
69
+ if (actualSize.length > 2) {
70
+ throw new RangeError(`Vector must be of dimensions 1x${expectedSize}`)
71
+ }
72
+ if (actualSize.length === 2 && actualSize[1] !== 1) {
73
+ throw new RangeError(`Vector must be of dimensions 1x${expectedSize}`)
74
+ }
75
+ if (actualSize[0] !== expectedSize) {
76
+ throw new RangeError(`Vector must be of dimensions 1x${expectedSize}`)
77
+ }
78
+ }
79
+ })
@@ -0,0 +1,186 @@
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(1)) // returns [[bignumber(cos(1)), bignumber(-sin(1))], [bignumber(sin(1)), bignumber(cos(1))]]
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, undefined)
80
+ },
81
+
82
+ 'number | BigNumber | Complex | Unit, Matrix': function (theta, v) {
83
+ _validateVector(v)
84
+ const storageType = v.storage() || (config.matrix === 'Matrix' ? 'dense' : undefined)
85
+ return _rotationMatrix3x3(theta, v, storageType)
86
+ },
87
+
88
+ 'number | BigNumber | Complex | Unit, Array, string': function (theta, v, format) {
89
+ const matrixV = matrix(v)
90
+ _validateVector(matrixV)
91
+ return _rotationMatrix3x3(theta, matrixV, format)
92
+ },
93
+
94
+ 'number | BigNumber | Complex | Unit, Matrix, string': function (theta, v, format) {
95
+ _validateVector(v)
96
+ return _rotationMatrix3x3(theta, v, format)
97
+ }
98
+
99
+ })
100
+
101
+ /**
102
+ * Returns 2x2 matrix of 2D rotation of angle theta
103
+ *
104
+ * @param {number | BigNumber | Complex | Unit} theta The rotation angle
105
+ * @param {string} format The result Matrix storage format
106
+ * @returns {Matrix}
107
+ * @private
108
+ */
109
+ function _rotationMatrix2x2 (theta, format) {
110
+ const Big = isBigNumber(theta)
111
+
112
+ const minusOne = Big ? new BigNumber(-1) : -1
113
+ const cosTheta = cos(theta)
114
+ const sinTheta = sin(theta)
115
+ const data = [[cosTheta, multiplyScalar(minusOne, sinTheta)], [sinTheta, cosTheta]]
116
+
117
+ return _convertToFormat(data, format)
118
+ }
119
+
120
+ function _validateVector (v) {
121
+ const size = v.size()
122
+ if (size.length < 1 || size[0] !== 3) {
123
+ throw new RangeError('Vector must be of dimensions 1x3')
124
+ }
125
+ }
126
+
127
+ function _mul (array) {
128
+ return array.reduce((p, curr) => multiplyScalar(p, curr))
129
+ }
130
+
131
+ function _convertToFormat (data, format) {
132
+ if (format) {
133
+ if (format === 'sparse') {
134
+ return new SparseMatrix(data)
135
+ }
136
+ if (format === 'dense') {
137
+ return new DenseMatrix(data)
138
+ }
139
+ throw new TypeError(`Unknown matrix type "${format}"`)
140
+ }
141
+ return data
142
+ }
143
+
144
+ /**
145
+ * Returns a 3x3 matrix of rotation of angle theta around vector v
146
+ *
147
+ * @param {number | BigNumber | Complex | Unit} theta The rotation angle
148
+ * @param {Matrix} v The rotation axis vector
149
+ * @param {string} format The storage format of the resulting matrix
150
+ * @returns {Matrix}
151
+ * @private
152
+ */
153
+ function _rotationMatrix3x3 (theta, v, format) {
154
+ const normV = norm(v)
155
+ if (normV === 0) {
156
+ throw new RangeError('Rotation around zero vector')
157
+ }
158
+
159
+ const Big = isBigNumber(theta) ? BigNumber : null
160
+
161
+ const one = Big ? new Big(1) : 1
162
+ const minusOne = Big ? new Big(-1) : -1
163
+ const vx = Big ? new Big(v.get([0]) / normV) : v.get([0]) / normV
164
+ const vy = Big ? new Big(v.get([1]) / normV) : v.get([1]) / normV
165
+ const vz = Big ? new Big(v.get([2]) / normV) : v.get([2]) / normV
166
+ const c = cos(theta)
167
+ const oneMinusC = addScalar(one, unaryMinus(c))
168
+ const s = sin(theta)
169
+
170
+ const r11 = addScalar(c, _mul([vx, vx, oneMinusC]))
171
+ const r12 = addScalar(_mul([vx, vy, oneMinusC]), _mul([minusOne, vz, s]))
172
+ const r13 = addScalar(_mul([vx, vz, oneMinusC]), _mul([vy, s]))
173
+
174
+ const r21 = addScalar(_mul([vx, vy, oneMinusC]), _mul([vz, s]))
175
+ const r22 = addScalar(c, _mul([vy, vy, oneMinusC]))
176
+ const r23 = addScalar(_mul([vy, vz, oneMinusC]), _mul([minusOne, vx, s]))
177
+
178
+ const r31 = addScalar(_mul([vx, vz, oneMinusC]), _mul([minusOne, vy, s]))
179
+ const r32 = addScalar(_mul([vy, vz, oneMinusC]), _mul([vx, s]))
180
+ const r33 = addScalar(c, _mul([vz, vz, oneMinusC]))
181
+
182
+ const data = [[r11, r12, r13], [r21, r22, r23], [r31, r32, r33]]
183
+
184
+ return _convertToFormat(data, format)
185
+ }
186
+ })
@@ -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') {
@@ -1,3 +1,5 @@
1
+ /* eslint-disable no-loss-of-precision */
2
+
1
3
  import { deepMap } from '../../utils/collection'
2
4
  import { sign } from '../../utils/number'
3
5
  import { factory } from '../../utils/factory'
@@ -2,4 +2,4 @@
2
2
  export const pi = Math.PI
3
3
  export const tau = 2 * Math.PI
4
4
  export const e = Math.E
5
- export const phi = 1.61803398874989484820458683436563811772030917980576286213545
5
+ export const phi = 1.6180339887498948 // eslint-disable-line no-loss-of-precision