simplifyConstant.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.createSimplifyConstant = void 0;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _is = require("../../utils/is.js");
  9. var _factory = require("../../utils/factory.js");
  10. var _util = require("./simplify/util.js");
  11. var _noop = require("../../utils/noop.js");
  12. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  13. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  14. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  15. var name = 'simplifyConstant';
  16. var dependencies = ['typed', 'parse', 'config', 'mathWithTransform', 'matrix', '?fraction', '?bignumber', 'AccessorNode', 'ArrayNode', 'ConstantNode', 'FunctionNode', 'IndexNode', 'ObjectNode', 'OperatorNode', 'SymbolNode'];
  17. var createSimplifyConstant = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  18. var typed = _ref.typed,
  19. parse = _ref.parse,
  20. config = _ref.config,
  21. mathWithTransform = _ref.mathWithTransform,
  22. matrix = _ref.matrix,
  23. fraction = _ref.fraction,
  24. bignumber = _ref.bignumber,
  25. AccessorNode = _ref.AccessorNode,
  26. ArrayNode = _ref.ArrayNode,
  27. ConstantNode = _ref.ConstantNode,
  28. FunctionNode = _ref.FunctionNode,
  29. IndexNode = _ref.IndexNode,
  30. ObjectNode = _ref.ObjectNode,
  31. OperatorNode = _ref.OperatorNode,
  32. SymbolNode = _ref.SymbolNode;
  33. var _createUtil = (0, _util.createUtil)({
  34. FunctionNode: FunctionNode,
  35. OperatorNode: OperatorNode,
  36. SymbolNode: SymbolNode
  37. }),
  38. isCommutative = _createUtil.isCommutative,
  39. isAssociative = _createUtil.isAssociative,
  40. allChildren = _createUtil.allChildren,
  41. createMakeNodeFunction = _createUtil.createMakeNodeFunction;
  42. /**
  43. * simplifyConstant() takes a mathjs expression (either a Node representing
  44. * a parse tree or a string which it parses to produce a node), and replaces
  45. * any subexpression of it consisting entirely of constants with the computed
  46. * value of that subexpression.
  47. *
  48. * Syntax:
  49. *
  50. * simplifyConstant(expr)
  51. * simplifyConstant(expr, options)
  52. *
  53. * Examples:
  54. *
  55. * math.simplifyConstant('x + 4*3/6') // Node "x + 2"
  56. * math.simplifyConstant('z cos(0)') // Node "z 1"
  57. * math.simplifyConstant('(5.2 + 1.08)t', {exactFractions: false}) // Node "6.28 t"
  58. *
  59. * See also:
  60. *
  61. * simplify, simplifyCore, resolve, derivative
  62. *
  63. * @param {Node | string} node
  64. * The expression to be simplified
  65. * @param {Object} options
  66. * Simplification options, as per simplify()
  67. * @return {Node} Returns expression with constant subexpressions evaluated
  68. */
  69. var simplifyConstant = typed('simplifyConstant', {
  70. Node: function Node(node) {
  71. return _ensureNode(foldFraction(node, {}));
  72. },
  73. 'Node, Object': function NodeObject(expr, options) {
  74. return _ensureNode(foldFraction(expr, options));
  75. }
  76. });
  77. function _removeFractions(thing) {
  78. if ((0, _is.isFraction)(thing)) {
  79. return thing.valueOf();
  80. }
  81. if (thing instanceof Array) {
  82. return thing.map(_removeFractions);
  83. }
  84. if ((0, _is.isMatrix)(thing)) {
  85. return matrix(_removeFractions(thing.valueOf()));
  86. }
  87. return thing;
  88. }
  89. function _eval(fnname, args, options) {
  90. try {
  91. return mathWithTransform[fnname].apply(null, args);
  92. } catch (ignore) {
  93. // sometimes the implicit type conversion causes the evaluation to fail, so we'll try again after removing Fractions
  94. args = args.map(_removeFractions);
  95. return _toNumber(mathWithTransform[fnname].apply(null, args), options);
  96. }
  97. }
  98. var _toNode = typed({
  99. Fraction: _fractionToNode,
  100. number: function number(n) {
  101. if (n < 0) {
  102. return unaryMinusNode(new ConstantNode(-n));
  103. }
  104. return new ConstantNode(n);
  105. },
  106. BigNumber: function BigNumber(n) {
  107. if (n < 0) {
  108. return unaryMinusNode(new ConstantNode(-n));
  109. }
  110. return new ConstantNode(n); // old parameters: (n.toString(), 'number')
  111. },
  112. Complex: function Complex(s) {
  113. throw new Error('Cannot convert Complex number to Node');
  114. },
  115. string: function string(s) {
  116. return new ConstantNode(s);
  117. },
  118. Matrix: function Matrix(m) {
  119. return new ArrayNode(m.valueOf().map(function (e) {
  120. return _toNode(e);
  121. }));
  122. }
  123. });
  124. function _ensureNode(thing) {
  125. if ((0, _is.isNode)(thing)) {
  126. return thing;
  127. }
  128. return _toNode(thing);
  129. }
  130. // convert a number to a fraction only if it can be expressed exactly,
  131. // and when both numerator and denominator are small enough
  132. function _exactFraction(n, options) {
  133. var exactFractions = options && options.exactFractions !== false;
  134. if (exactFractions && isFinite(n) && fraction) {
  135. var f = fraction(n);
  136. var fractionsLimit = options && typeof options.fractionsLimit === 'number' ? options.fractionsLimit : Infinity; // no limit by default
  137. if (f.valueOf() === n && f.n < fractionsLimit && f.d < fractionsLimit) {
  138. return f;
  139. }
  140. }
  141. return n;
  142. }
  143. // Convert numbers to a preferred number type in preference order: Fraction, number, Complex
  144. // BigNumbers are left alone
  145. var _toNumber = typed({
  146. 'string, Object': function stringObject(s, options) {
  147. if (config.number === 'BigNumber') {
  148. if (bignumber === undefined) {
  149. (0, _noop.noBignumber)();
  150. }
  151. return bignumber(s);
  152. } else if (config.number === 'Fraction') {
  153. if (fraction === undefined) {
  154. (0, _noop.noFraction)();
  155. }
  156. return fraction(s);
  157. } else {
  158. var n = parseFloat(s);
  159. return _exactFraction(n, options);
  160. }
  161. },
  162. 'Fraction, Object': function FractionObject(s, options) {
  163. return s;
  164. },
  165. // we don't need options here
  166. 'BigNumber, Object': function BigNumberObject(s, options) {
  167. return s;
  168. },
  169. // we don't need options here
  170. 'number, Object': function numberObject(s, options) {
  171. return _exactFraction(s, options);
  172. },
  173. 'Complex, Object': function ComplexObject(s, options) {
  174. if (s.im !== 0) {
  175. return s;
  176. }
  177. return _exactFraction(s.re, options);
  178. },
  179. 'Matrix, Object': function MatrixObject(s, options) {
  180. return matrix(_exactFraction(s.valueOf()));
  181. },
  182. 'Array, Object': function ArrayObject(s, options) {
  183. return s.map(_exactFraction);
  184. }
  185. });
  186. function unaryMinusNode(n) {
  187. return new OperatorNode('-', 'unaryMinus', [n]);
  188. }
  189. function _fractionToNode(f) {
  190. var n;
  191. var vn = f.s * f.n;
  192. if (vn < 0) {
  193. n = new OperatorNode('-', 'unaryMinus', [new ConstantNode(-vn)]);
  194. } else {
  195. n = new ConstantNode(vn);
  196. }
  197. if (f.d === 1) {
  198. return n;
  199. }
  200. return new OperatorNode('/', 'divide', [n, new ConstantNode(f.d)]);
  201. }
  202. /* Handles constant indexing of ArrayNodes, matrices, and ObjectNodes */
  203. function _foldAccessor(obj, index, options) {
  204. if (!(0, _is.isIndexNode)(index)) {
  205. // don't know what to do with that...
  206. return new AccessorNode(_ensureNode(obj), _ensureNode(index));
  207. }
  208. if ((0, _is.isArrayNode)(obj) || (0, _is.isMatrix)(obj)) {
  209. var remainingDims = Array.from(index.dimensions);
  210. /* We will resolve constant indices one at a time, looking
  211. * just in the first or second dimensions because (a) arrays
  212. * of more than two dimensions are likely rare, and (b) pulling
  213. * out the third or higher dimension would be pretty intricate.
  214. * The price is that we miss simplifying [..3d array][x,y,1]
  215. */
  216. while (remainingDims.length > 0) {
  217. if ((0, _is.isConstantNode)(remainingDims[0]) && typeof remainingDims[0].value !== 'string') {
  218. var first = _toNumber(remainingDims.shift().value, options);
  219. if ((0, _is.isArrayNode)(obj)) {
  220. obj = obj.items[first - 1];
  221. } else {
  222. // matrix
  223. obj = obj.valueOf()[first - 1];
  224. if (obj instanceof Array) {
  225. obj = matrix(obj);
  226. }
  227. }
  228. } else if (remainingDims.length > 1 && (0, _is.isConstantNode)(remainingDims[1]) && typeof remainingDims[1].value !== 'string') {
  229. var second = _toNumber(remainingDims[1].value, options);
  230. var tryItems = [];
  231. var fromItems = (0, _is.isArrayNode)(obj) ? obj.items : obj.valueOf();
  232. var _iterator = _createForOfIteratorHelper(fromItems),
  233. _step;
  234. try {
  235. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  236. var item = _step.value;
  237. if ((0, _is.isArrayNode)(item)) {
  238. tryItems.push(item.items[second - 1]);
  239. } else if ((0, _is.isMatrix)(obj)) {
  240. tryItems.push(item[second - 1]);
  241. } else {
  242. break;
  243. }
  244. }
  245. } catch (err) {
  246. _iterator.e(err);
  247. } finally {
  248. _iterator.f();
  249. }
  250. if (tryItems.length === fromItems.length) {
  251. if ((0, _is.isArrayNode)(obj)) {
  252. obj = new ArrayNode(tryItems);
  253. } else {
  254. // matrix
  255. obj = matrix(tryItems);
  256. }
  257. remainingDims.splice(1, 1);
  258. } else {
  259. // extracting slice along 2nd dimension failed, give up
  260. break;
  261. }
  262. } else {
  263. // neither 1st or 2nd dimension is constant, give up
  264. break;
  265. }
  266. }
  267. if (remainingDims.length === index.dimensions.length) {
  268. /* No successful constant indexing */
  269. return new AccessorNode(_ensureNode(obj), index);
  270. }
  271. if (remainingDims.length > 0) {
  272. /* Indexed some but not all dimensions */
  273. index = new IndexNode(remainingDims);
  274. return new AccessorNode(_ensureNode(obj), index);
  275. }
  276. /* All dimensions were constant, access completely resolved */
  277. return obj;
  278. }
  279. if ((0, _is.isObjectNode)(obj) && index.dimensions.length === 1 && (0, _is.isConstantNode)(index.dimensions[0])) {
  280. var key = index.dimensions[0].value;
  281. if (key in obj.properties) {
  282. return obj.properties[key];
  283. }
  284. return new ConstantNode(); // undefined
  285. }
  286. /* Don't know how to index this sort of obj, at least not with this index */
  287. return new AccessorNode(_ensureNode(obj), index);
  288. }
  289. /*
  290. * Create a binary tree from a list of Fractions and Nodes.
  291. * Tries to fold Fractions by evaluating them until the first Node in the list is hit, so
  292. * `args` should be sorted to have the Fractions at the start (if the operator is commutative).
  293. * @param args - list of Fractions and Nodes
  294. * @param fn - evaluator for the binary operation evaluator that accepts two Fractions
  295. * @param makeNode - creates a binary OperatorNode/FunctionNode from a list of child Nodes
  296. * if args.length is 1, returns args[0]
  297. * @return - Either a Node representing a binary expression or Fraction
  298. */
  299. function foldOp(fn, args, makeNode, options) {
  300. var first = args.shift();
  301. // In the following reduction, sofar always has one of the three following
  302. // forms: [NODE], [CONSTANT], or [NODE, CONSTANT]
  303. var reduction = args.reduce(function (sofar, next) {
  304. if (!(0, _is.isNode)(next)) {
  305. var last = sofar.pop();
  306. if ((0, _is.isNode)(last)) {
  307. return [last, next];
  308. }
  309. // Two constants in a row, try to fold them into one
  310. try {
  311. sofar.push(_eval(fn, [last, next], options));
  312. return sofar;
  313. } catch (ignoreandcontinue) {
  314. sofar.push(last);
  315. // fall through to Node case
  316. }
  317. }
  318. // Encountered a Node, or failed folding --
  319. // collapse everything so far into a single tree:
  320. sofar.push(_ensureNode(sofar.pop()));
  321. var newtree = sofar.length === 1 ? sofar[0] : makeNode(sofar);
  322. return [makeNode([newtree, _ensureNode(next)])];
  323. }, [first]);
  324. if (reduction.length === 1) {
  325. return reduction[0];
  326. }
  327. // Might end up with a tree and a constant at the end:
  328. return makeNode([reduction[0], _toNode(reduction[1])]);
  329. }
  330. // destroys the original node and returns a folded one
  331. function foldFraction(node, options) {
  332. switch (node.type) {
  333. case 'SymbolNode':
  334. return node;
  335. case 'ConstantNode':
  336. switch ((0, _typeof2["default"])(node.value)) {
  337. case 'number':
  338. return _toNumber(node.value, options);
  339. case 'string':
  340. return node.value;
  341. default:
  342. if (!isNaN(node.value)) return _toNumber(node.value, options);
  343. }
  344. return node;
  345. case 'FunctionNode':
  346. if (mathWithTransform[node.name] && mathWithTransform[node.name].rawArgs) {
  347. return node;
  348. }
  349. {
  350. // Process operators as OperatorNode
  351. var operatorFunctions = ['add', 'multiply'];
  352. if (operatorFunctions.indexOf(node.name) === -1) {
  353. var args = node.args.map(function (arg) {
  354. return foldFraction(arg, options);
  355. });
  356. // If all args are numbers
  357. if (!args.some(_is.isNode)) {
  358. try {
  359. return _eval(node.name, args, options);
  360. } catch (ignoreandcontinue) {}
  361. }
  362. // Size of a matrix does not depend on entries
  363. if (node.name === 'size' && args.length === 1 && (0, _is.isArrayNode)(args[0])) {
  364. var sz = [];
  365. var section = args[0];
  366. while ((0, _is.isArrayNode)(section)) {
  367. sz.push(section.items.length);
  368. section = section.items[0];
  369. }
  370. return matrix(sz);
  371. }
  372. // Convert all args to nodes and construct a symbolic function call
  373. return new FunctionNode(node.name, args.map(_ensureNode));
  374. } else {
  375. // treat as operator
  376. }
  377. }
  378. /* falls through */
  379. case 'OperatorNode':
  380. {
  381. var fn = node.fn.toString();
  382. var _args;
  383. var res;
  384. var makeNode = createMakeNodeFunction(node);
  385. if ((0, _is.isOperatorNode)(node) && node.isUnary()) {
  386. _args = [foldFraction(node.args[0], options)];
  387. if (!(0, _is.isNode)(_args[0])) {
  388. res = _eval(fn, _args, options);
  389. } else {
  390. res = makeNode(_args);
  391. }
  392. } else if (isAssociative(node, options.context)) {
  393. _args = allChildren(node, options.context);
  394. _args = _args.map(function (arg) {
  395. return foldFraction(arg, options);
  396. });
  397. if (isCommutative(fn, options.context)) {
  398. // commutative binary operator
  399. var consts = [];
  400. var vars = [];
  401. for (var i = 0; i < _args.length; i++) {
  402. if (!(0, _is.isNode)(_args[i])) {
  403. consts.push(_args[i]);
  404. } else {
  405. vars.push(_args[i]);
  406. }
  407. }
  408. if (consts.length > 1) {
  409. res = foldOp(fn, consts, makeNode, options);
  410. vars.unshift(res);
  411. res = foldOp(fn, vars, makeNode, options);
  412. } else {
  413. // we won't change the children order since it's not neccessary
  414. res = foldOp(fn, _args, makeNode, options);
  415. }
  416. } else {
  417. // non-commutative binary operator
  418. res = foldOp(fn, _args, makeNode, options);
  419. }
  420. } else {
  421. // non-associative binary operator
  422. _args = node.args.map(function (arg) {
  423. return foldFraction(arg, options);
  424. });
  425. res = foldOp(fn, _args, makeNode, options);
  426. }
  427. return res;
  428. }
  429. case 'ParenthesisNode':
  430. // remove the uneccessary parenthesis
  431. return foldFraction(node.content, options);
  432. case 'AccessorNode':
  433. return _foldAccessor(foldFraction(node.object, options), foldFraction(node.index, options), options);
  434. case 'ArrayNode':
  435. {
  436. var foldItems = node.items.map(function (item) {
  437. return foldFraction(item, options);
  438. });
  439. if (foldItems.some(_is.isNode)) {
  440. return new ArrayNode(foldItems.map(_ensureNode));
  441. }
  442. /* All literals -- return a Matrix so we can operate on it */
  443. return matrix(foldItems);
  444. }
  445. case 'IndexNode':
  446. {
  447. return new IndexNode(node.dimensions.map(function (n) {
  448. return simplifyConstant(n, options);
  449. }));
  450. }
  451. case 'ObjectNode':
  452. {
  453. var foldProps = {};
  454. for (var prop in node.properties) {
  455. foldProps[prop] = simplifyConstant(node.properties[prop], options);
  456. }
  457. return new ObjectNode(foldProps);
  458. }
  459. case 'AssignmentNode':
  460. /* falls through */
  461. case 'BlockNode':
  462. /* falls through */
  463. case 'FunctionAssignmentNode':
  464. /* falls through */
  465. case 'RangeNode':
  466. /* falls through */
  467. case 'ConditionalNode':
  468. /* falls through */
  469. default:
  470. throw new Error("Unimplemented node type in simplifyConstant: ".concat(node.type));
  471. }
  472. }
  473. return simplifyConstant;
  474. });
  475. exports.createSimplifyConstant = createSimplifyConstant;