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