simplifyConstant.js 19 KB

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