UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createPow = void 0;
7
8var _factory = require("../../utils/factory");
9
10var _number = require("../../utils/number");
11
12var _array = require("../../utils/array");
13
14var _number2 = require("../../plain/number");
15
16var name = 'pow';
17var dependencies = ['typed', 'config', 'identity', 'multiply', 'matrix', 'fraction', 'number', 'Complex'];
18var createPow = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
19 var typed = _ref.typed,
20 config = _ref.config,
21 identity = _ref.identity,
22 multiply = _ref.multiply,
23 matrix = _ref.matrix,
24 number = _ref.number,
25 fraction = _ref.fraction,
26 Complex = _ref.Complex;
27
28 /**
29 * Calculates the power of x to y, `x ^ y`.
30 * Matrix exponentiation is supported for square matrices `x`, and positive
31 * integer exponents `y`.
32 *
33 * For cubic roots of negative numbers, the function returns the principal
34 * root by default. In order to let the function return the real root,
35 * math.js can be configured with `math.config({predictable: true})`.
36 * To retrieve all cubic roots of a value, use `math.cbrt(x, true)`.
37 *
38 * Syntax:
39 *
40 * math.pow(x, y)
41 *
42 * Examples:
43 *
44 * math.pow(2, 3) // returns number 8
45 *
46 * const a = math.complex(2, 3)
47 * math.pow(a, 2) // returns Complex -5 + 12i
48 *
49 * const b = [[1, 2], [4, 3]]
50 * math.pow(b, 2) // returns Array [[9, 8], [16, 17]]
51 *
52 * See also:
53 *
54 * multiply, sqrt, cbrt, nthRoot
55 *
56 * @param {number | BigNumber | Complex | Unit | Array | Matrix} x The base
57 * @param {number | BigNumber | Complex} y The exponent
58 * @return {number | BigNumber | Complex | Array | Matrix} The value of `x` to the power `y`
59 */
60 return typed(name, {
61 'number, number': _pow,
62 'Complex, Complex': function ComplexComplex(x, y) {
63 return x.pow(y);
64 },
65 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
66 if (y.isInteger() || x >= 0 || config.predictable) {
67 return x.pow(y);
68 } else {
69 return new Complex(x.toNumber(), 0).pow(y.toNumber(), 0);
70 }
71 },
72 'Fraction, Fraction': function FractionFraction(x, y) {
73 if (y.d !== 1) {
74 if (config.predictable) {
75 throw new Error('Function pow does not support non-integer exponents for fractions.');
76 } else {
77 return _pow(x.valueOf(), y.valueOf());
78 }
79 } else {
80 return x.pow(y);
81 }
82 },
83 'Array, number': _powArray,
84 'Array, BigNumber': function ArrayBigNumber(x, y) {
85 return _powArray(x, y.toNumber());
86 },
87 'Matrix, number': _powMatrix,
88 'Matrix, BigNumber': function MatrixBigNumber(x, y) {
89 return _powMatrix(x, y.toNumber());
90 },
91 'Unit, number | BigNumber': function UnitNumberBigNumber(x, y) {
92 return x.pow(y);
93 }
94 });
95 /**
96 * Calculates the power of x to y, x^y, for two numbers.
97 * @param {number} x
98 * @param {number} y
99 * @return {number | Complex} res
100 * @private
101 */
102
103 function _pow(x, y) {
104 // Alternatively could define a 'realmode' config option or something, but
105 // 'predictable' will work for now
106 if (config.predictable && !(0, _number.isInteger)(y) && x < 0) {
107 // Check to see if y can be represented as a fraction
108 try {
109 var yFrac = fraction(y);
110 var yNum = number(yFrac);
111
112 if (y === yNum || Math.abs((y - yNum) / y) < 1e-14) {
113 if (yFrac.d % 2 === 1) {
114 return (yFrac.n % 2 === 0 ? 1 : -1) * Math.pow(-x, y);
115 }
116 }
117 } catch (ex) {} // fraction() throws an error if y is Infinity, etc.
118 // Unable to express y as a fraction, so continue on
119
120 } // **for predictable mode** x^Infinity === NaN if x < -1
121 // N.B. this behavour is different from `Math.pow` which gives
122 // (-2)^Infinity === Infinity
123
124
125 if (config.predictable && (x < -1 && y === Infinity || x > -1 && x < 0 && y === -Infinity)) {
126 return NaN;
127 }
128
129 if ((0, _number.isInteger)(y) || x >= 0 || config.predictable) {
130 return (0, _number2.powNumber)(x, y);
131 } else {
132 // TODO: the following infinity checks are duplicated from powNumber. Deduplicate this somehow
133 // x^Infinity === 0 if -1 < x < 1
134 // A real number 0 is returned instead of complex(0)
135 if (x * x < 1 && y === Infinity || x * x > 1 && y === -Infinity) {
136 return 0;
137 }
138
139 return new Complex(x, 0).pow(y, 0);
140 }
141 }
142 /**
143 * Calculate the power of a 2d array
144 * @param {Array} x must be a 2 dimensional, square matrix
145 * @param {number} y a positive, integer value
146 * @returns {Array}
147 * @private
148 */
149
150
151 function _powArray(x, y) {
152 if (!(0, _number.isInteger)(y) || y < 0) {
153 throw new TypeError('For A^b, b must be a positive integer (value is ' + y + ')');
154 } // verify that A is a 2 dimensional square matrix
155
156
157 var s = (0, _array.arraySize)(x);
158
159 if (s.length !== 2) {
160 throw new Error('For A^b, A must be 2 dimensional (A has ' + s.length + ' dimensions)');
161 }
162
163 if (s[0] !== s[1]) {
164 throw new Error('For A^b, A must be square (size is ' + s[0] + 'x' + s[1] + ')');
165 }
166
167 var res = identity(s[0]).valueOf();
168 var px = x;
169
170 while (y >= 1) {
171 if ((y & 1) === 1) {
172 res = multiply(px, res);
173 }
174
175 y >>= 1;
176 px = multiply(px, px);
177 }
178
179 return res;
180 }
181 /**
182 * Calculate the power of a 2d matrix
183 * @param {Matrix} x must be a 2 dimensional, square matrix
184 * @param {number} y a positive, integer value
185 * @returns {Matrix}
186 * @private
187 */
188
189
190 function _powMatrix(x, y) {
191 return matrix(_powArray(x.valueOf(), y));
192 }
193});
194exports.createPow = createPow;
\No newline at end of file