1 |
|
2 | import { isFraction, isNode, isOperatorNode } from '../../../utils/is'
|
3 | import { factory } from '../../../utils/factory'
|
4 | import { createUtil } from './util'
|
5 | import { noBignumber, noFraction } from '../../../utils/noop'
|
6 |
|
7 | const name = 'simplifyConstant'
|
8 | const dependencies = [
|
9 | 'typed',
|
10 | 'config',
|
11 | 'mathWithTransform',
|
12 | '?fraction',
|
13 | '?bignumber',
|
14 | 'ConstantNode',
|
15 | 'OperatorNode',
|
16 | 'FunctionNode',
|
17 | 'SymbolNode'
|
18 | ]
|
19 |
|
20 | export const createSimplifyConstant = factory(name, dependencies, ({
|
21 | typed,
|
22 | config,
|
23 | mathWithTransform,
|
24 | fraction,
|
25 | bignumber,
|
26 | ConstantNode,
|
27 | OperatorNode,
|
28 | FunctionNode,
|
29 | SymbolNode
|
30 | }) => {
|
31 | const { isCommutative, isAssociative, allChildren, createMakeNodeFunction } =
|
32 | createUtil({ FunctionNode, OperatorNode, SymbolNode })
|
33 |
|
34 | function simplifyConstant (expr, options) {
|
35 | const res = foldFraction(expr, options)
|
36 | return isNode(res) ? res : _toNode(res)
|
37 | }
|
38 |
|
39 | function _eval (fnname, args, options) {
|
40 | try {
|
41 | return _toNumber(mathWithTransform[fnname].apply(null, args), options)
|
42 | } catch (ignore) {
|
43 |
|
44 | args = args.map(function (x) {
|
45 | if (isFraction(x)) {
|
46 | return x.valueOf()
|
47 | }
|
48 | return x
|
49 | })
|
50 | return _toNumber(mathWithTransform[fnname].apply(null, args), options)
|
51 | }
|
52 | }
|
53 |
|
54 | const _toNode = typed({
|
55 | 'Fraction': _fractionToNode,
|
56 | 'number': function (n) {
|
57 | if (n < 0) {
|
58 | return unaryMinusNode(new ConstantNode(-n))
|
59 | }
|
60 | return new ConstantNode(n)
|
61 | },
|
62 | 'BigNumber': function (n) {
|
63 | if (n < 0) {
|
64 | return unaryMinusNode(new ConstantNode(-n))
|
65 | }
|
66 | return new ConstantNode(n)
|
67 | },
|
68 | 'Complex': function (s) {
|
69 | throw new Error('Cannot convert Complex number to Node')
|
70 | }
|
71 | })
|
72 |
|
73 |
|
74 | function _exactFraction (n, options) {
|
75 | const exactFractions = (options && options.exactFractions !== false)
|
76 | if (exactFractions && isFinite(n) && fraction) {
|
77 | const f = fraction(n)
|
78 | if (f.valueOf() === n) {
|
79 | return f
|
80 | }
|
81 | }
|
82 | return n
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 | const _toNumber = typed({
|
88 | 'string, Object': function (s, options) {
|
89 | if (config.number === 'BigNumber') {
|
90 | if (bignumber === undefined) {
|
91 | noBignumber()
|
92 | }
|
93 | return bignumber(s)
|
94 | } else if (config.number === 'Fraction') {
|
95 | if (fraction === undefined) {
|
96 | noFraction()
|
97 | }
|
98 | return fraction(s)
|
99 | } else {
|
100 | const n = parseFloat(s)
|
101 | return _exactFraction(n, options)
|
102 | }
|
103 | },
|
104 |
|
105 | 'Fraction, Object': function (s, options) { return s },
|
106 |
|
107 | 'BigNumber, Object': function (s, options) { return s },
|
108 |
|
109 | 'number, Object': function (s, options) {
|
110 | return _exactFraction(s, options)
|
111 | },
|
112 |
|
113 | 'Complex, Object': function (s, options) {
|
114 | if (s.im !== 0) {
|
115 | return s
|
116 | }
|
117 | return _exactFraction(s.re, options)
|
118 | }
|
119 | })
|
120 |
|
121 | function unaryMinusNode (n) {
|
122 | return new OperatorNode('-', 'unaryMinus', [n])
|
123 | }
|
124 |
|
125 | function _fractionToNode (f) {
|
126 | let n
|
127 | const vn = f.s * f.n
|
128 | if (vn < 0) {
|
129 | n = new OperatorNode('-', 'unaryMinus', [new ConstantNode(-vn)])
|
130 | } else {
|
131 | n = new ConstantNode(vn)
|
132 | }
|
133 |
|
134 | if (f.d === 1) {
|
135 | return n
|
136 | }
|
137 | return new OperatorNode('/', 'divide', [n, new ConstantNode(f.d)])
|
138 | }
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function foldOp (fn, args, makeNode, options) {
|
151 | return args.reduce(function (a, b) {
|
152 | if (!isNode(a) && !isNode(b)) {
|
153 | try {
|
154 | return _eval(fn, [a, b], options)
|
155 | } catch (ignoreandcontinue) {}
|
156 | a = _toNode(a)
|
157 | b = _toNode(b)
|
158 | } else if (!isNode(a)) {
|
159 | a = _toNode(a)
|
160 | } else if (!isNode(b)) {
|
161 | b = _toNode(b)
|
162 | }
|
163 |
|
164 | return makeNode([a, b])
|
165 | })
|
166 | }
|
167 |
|
168 |
|
169 | function foldFraction (node, options) {
|
170 | switch (node.type) {
|
171 | case 'SymbolNode':
|
172 | return node
|
173 | case 'ConstantNode':
|
174 | if (typeof node.value === 'number' || !isNaN(node.value)) {
|
175 | return _toNumber(node.value, options)
|
176 | }
|
177 | return node
|
178 | case 'FunctionNode':
|
179 | if (mathWithTransform[node.name] && mathWithTransform[node.name].rawArgs) {
|
180 | return node
|
181 | }
|
182 |
|
183 |
|
184 | const operatorFunctions = [ 'add', 'multiply' ]
|
185 | if (operatorFunctions.indexOf(node.name) === -1) {
|
186 | let args = node.args.map(arg => foldFraction(arg, options))
|
187 |
|
188 |
|
189 | if (!args.some(isNode)) {
|
190 | try {
|
191 | return _eval(node.name, args, options)
|
192 | } catch (ignoreandcontine) {}
|
193 | }
|
194 |
|
195 |
|
196 | args = args.map(function (arg) {
|
197 | return isNode(arg) ? arg : _toNode(arg)
|
198 | })
|
199 | return new FunctionNode(node.name, args)
|
200 | } else {
|
201 |
|
202 | }
|
203 |
|
204 | case 'OperatorNode':
|
205 | const fn = node.fn.toString()
|
206 | let args
|
207 | let res
|
208 | const makeNode = createMakeNodeFunction(node)
|
209 | if (isOperatorNode(node) && node.isUnary()) {
|
210 | args = [foldFraction(node.args[0], options)]
|
211 | if (!isNode(args[0])) {
|
212 | res = _eval(fn, args, options)
|
213 | } else {
|
214 | res = makeNode(args)
|
215 | }
|
216 | } else if (isAssociative(node)) {
|
217 | args = allChildren(node)
|
218 | args = args.map(arg => foldFraction(arg, options))
|
219 |
|
220 | if (isCommutative(fn)) {
|
221 |
|
222 | const consts = []
|
223 | const vars = []
|
224 |
|
225 | for (let i = 0; i < args.length; i++) {
|
226 | if (!isNode(args[i])) {
|
227 | consts.push(args[i])
|
228 | } else {
|
229 | vars.push(args[i])
|
230 | }
|
231 | }
|
232 |
|
233 | if (consts.length > 1) {
|
234 | res = foldOp(fn, consts, makeNode, options)
|
235 | vars.unshift(res)
|
236 | res = foldOp(fn, vars, makeNode, options)
|
237 | } else {
|
238 |
|
239 | res = foldOp(fn, args, makeNode, options)
|
240 | }
|
241 | } else {
|
242 |
|
243 | res = foldOp(fn, args, makeNode, options)
|
244 | }
|
245 | } else {
|
246 |
|
247 | args = node.args.map(arg => foldFraction(arg, options))
|
248 | res = foldOp(fn, args, makeNode, options)
|
249 | }
|
250 | return res
|
251 | case 'ParenthesisNode':
|
252 |
|
253 | return foldFraction(node.content, options)
|
254 | case 'AccessorNode':
|
255 |
|
256 | case 'ArrayNode':
|
257 |
|
258 | case 'AssignmentNode':
|
259 |
|
260 | case 'BlockNode':
|
261 |
|
262 | case 'FunctionAssignmentNode':
|
263 |
|
264 | case 'IndexNode':
|
265 |
|
266 | case 'ObjectNode':
|
267 |
|
268 | case 'RangeNode':
|
269 |
|
270 | case 'ConditionalNode':
|
271 |
|
272 | default:
|
273 | throw new Error(`Unimplemented node type in simplifyConstant: ${node.type}`)
|
274 | }
|
275 | }
|
276 |
|
277 | return simplifyConstant
|
278 | })
|