1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createNthRootNumber = exports.createNthRoot = void 0;
|
7 |
|
8 | var _factory = require("../../utils/factory");
|
9 |
|
10 | var _algorithm = require("../../type/matrix/utils/algorithm01");
|
11 |
|
12 | var _algorithm2 = require("../../type/matrix/utils/algorithm02");
|
13 |
|
14 | var _algorithm3 = require("../../type/matrix/utils/algorithm06");
|
15 |
|
16 | var _algorithm4 = require("../../type/matrix/utils/algorithm11");
|
17 |
|
18 | var _algorithm5 = require("../../type/matrix/utils/algorithm13");
|
19 |
|
20 | var _algorithm6 = require("../../type/matrix/utils/algorithm14");
|
21 |
|
22 | var _number = require("../../plain/number");
|
23 |
|
24 | var name = 'nthRoot';
|
25 | var dependencies = ['typed', 'matrix', 'equalScalar', 'BigNumber'];
|
26 | var createNthRoot = (0, _factory.factory)(name, dependencies, function (_ref) {
|
27 | var typed = _ref.typed,
|
28 | matrix = _ref.matrix,
|
29 | equalScalar = _ref.equalScalar,
|
30 | _BigNumber = _ref.BigNumber;
|
31 | var algorithm01 = (0, _algorithm.createAlgorithm01)({
|
32 | typed: typed
|
33 | });
|
34 | var algorithm02 = (0, _algorithm2.createAlgorithm02)({
|
35 | typed: typed,
|
36 | equalScalar: equalScalar
|
37 | });
|
38 | var algorithm06 = (0, _algorithm3.createAlgorithm06)({
|
39 | typed: typed,
|
40 | equalScalar: equalScalar
|
41 | });
|
42 | var algorithm11 = (0, _algorithm4.createAlgorithm11)({
|
43 | typed: typed,
|
44 | equalScalar: equalScalar
|
45 | });
|
46 | var algorithm13 = (0, _algorithm5.createAlgorithm13)({
|
47 | typed: typed
|
48 | });
|
49 | var algorithm14 = (0, _algorithm6.createAlgorithm14)({
|
50 | typed: typed
|
51 | });
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | var complexErr = '' + 'Complex number not supported in function nthRoot. ' + 'Use nthRoots instead.';
|
83 | var nthRoot = typed(name, {
|
84 | number: function number(x) {
|
85 | return (0, _number.nthRootNumber)(x, 2);
|
86 | },
|
87 | 'number, number': _number.nthRootNumber,
|
88 | BigNumber: function BigNumber(x) {
|
89 | return _bigNthRoot(x, new _BigNumber(2));
|
90 | },
|
91 | Complex: function Complex(x) {
|
92 | throw new Error(complexErr);
|
93 | },
|
94 | 'Complex, number': function ComplexNumber(x, y) {
|
95 | throw new Error(complexErr);
|
96 | },
|
97 | 'BigNumber, BigNumber': _bigNthRoot,
|
98 | 'Array | Matrix': function ArrayMatrix(x) {
|
99 | return nthRoot(x, 2);
|
100 | },
|
101 | 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
|
102 |
|
103 | if (y.density() === 1) {
|
104 |
|
105 | return algorithm06(x, y, nthRoot);
|
106 | } else {
|
107 |
|
108 | throw new Error('Root must be non-zero');
|
109 | }
|
110 | },
|
111 | 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
|
112 | return algorithm02(y, x, nthRoot, true);
|
113 | },
|
114 | 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
|
115 |
|
116 | if (y.density() === 1) {
|
117 |
|
118 | return algorithm01(x, y, nthRoot, false);
|
119 | } else {
|
120 |
|
121 | throw new Error('Root must be non-zero');
|
122 | }
|
123 | },
|
124 | 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
|
125 | return algorithm13(x, y, nthRoot);
|
126 | },
|
127 | 'Array, Array': function ArrayArray(x, y) {
|
128 |
|
129 | return nthRoot(matrix(x), matrix(y)).valueOf();
|
130 | },
|
131 | 'Array, Matrix': function ArrayMatrix(x, y) {
|
132 |
|
133 | return nthRoot(matrix(x), y);
|
134 | },
|
135 | 'Matrix, Array': function MatrixArray(x, y) {
|
136 |
|
137 | return nthRoot(x, matrix(y));
|
138 | },
|
139 | 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) {
|
140 | return algorithm11(x, y, nthRoot, false);
|
141 | },
|
142 | 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) {
|
143 | return algorithm14(x, y, nthRoot, false);
|
144 | },
|
145 | 'number | BigNumber, SparseMatrix': function numberBigNumberSparseMatrix(x, y) {
|
146 |
|
147 | if (y.density() === 1) {
|
148 |
|
149 | return algorithm11(y, x, nthRoot, true);
|
150 | } else {
|
151 |
|
152 | throw new Error('Root must be non-zero');
|
153 | }
|
154 | },
|
155 | 'number | BigNumber, DenseMatrix': function numberBigNumberDenseMatrix(x, y) {
|
156 | return algorithm14(y, x, nthRoot, true);
|
157 | },
|
158 | 'Array, number | BigNumber': function ArrayNumberBigNumber(x, y) {
|
159 |
|
160 | return nthRoot(matrix(x), y).valueOf();
|
161 | },
|
162 | 'number | BigNumber, Array': function numberBigNumberArray(x, y) {
|
163 |
|
164 | return nthRoot(x, matrix(y)).valueOf();
|
165 | }
|
166 | });
|
167 | return nthRoot;
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | function _bigNthRoot(a, root) {
|
177 | var precision = _BigNumber.precision;
|
178 |
|
179 | var Big = _BigNumber.clone({
|
180 | precision: precision + 2
|
181 | });
|
182 |
|
183 | var zero = new _BigNumber(0);
|
184 | var one = new Big(1);
|
185 | var inv = root.isNegative();
|
186 |
|
187 | if (inv) {
|
188 | root = root.neg();
|
189 | }
|
190 |
|
191 | if (root.isZero()) {
|
192 | throw new Error('Root must be non-zero');
|
193 | }
|
194 |
|
195 | if (a.isNegative() && !root.abs().mod(2).equals(1)) {
|
196 | throw new Error('Root must be odd when a is negative.');
|
197 | }
|
198 |
|
199 |
|
200 | if (a.isZero()) {
|
201 | return inv ? new Big(Infinity) : 0;
|
202 | }
|
203 |
|
204 | if (!a.isFinite()) {
|
205 | return inv ? zero : a;
|
206 | }
|
207 |
|
208 | var x = a.abs().pow(one.div(root));
|
209 |
|
210 |
|
211 | x = a.isNeg() ? x.neg() : x;
|
212 | return new _BigNumber((inv ? one.div(x) : x).toPrecision(precision));
|
213 | }
|
214 | });
|
215 | exports.createNthRoot = createNthRoot;
|
216 | var createNthRootNumber = (0, _factory.factory)(name, ['typed'], function (_ref2) {
|
217 | var typed = _ref2.typed;
|
218 | return typed(name, {
|
219 | number: _number.nthRootNumber,
|
220 | 'number, number': _number.nthRootNumber
|
221 | });
|
222 | });
|
223 | exports.createNthRootNumber = createNthRootNumber; |
\ | No newline at end of file |