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