symbolicEqual.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createSymbolicEqual = void 0;
  6. var _is = require("../../utils/is.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'symbolicEqual';
  9. var dependencies = ['parse', 'simplify', 'typed', 'OperatorNode'];
  10. var createSymbolicEqual = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  11. var parse = _ref.parse,
  12. simplify = _ref.simplify,
  13. typed = _ref.typed,
  14. OperatorNode = _ref.OperatorNode;
  15. /**
  16. * Attempts to determine if two expressions are symbolically equal, i.e.
  17. * one is the result of valid algebraic manipulations on the other.
  18. * Currently, this simply checks if the difference of the two expressions
  19. * simplifies down to 0. So there are two important caveats:
  20. * 1. whether two expressions are symbolically equal depends on the
  21. * manipulations allowed. Therefore, this function takes an optional
  22. * third argument, which are the options that control the behavior
  23. * as documented for the `simplify()` function.
  24. * 2. it is in general intractable to find the minimal simplification of
  25. * an arbitrarily complicated expression. So while a `true` value
  26. * of `symbolicEqual` ensures that the two expressions can be manipulated
  27. * to match each other, a `false` value does not absolutely rule this out.
  28. *
  29. * Syntax:
  30. *
  31. * symbolicEqual(expr1, expr2)
  32. * symbolicEqual(expr1, expr2, options)
  33. *
  34. * Examples:
  35. *
  36. * symbolicEqual('x*y', 'y*x') // Returns true
  37. * symbolicEqual('x*y', 'y*x', {context: {multiply: {commutative: false}}}) // Returns false
  38. * symbolicEqual('x/y', '(y*x^(-1))^(-1)') // Returns true
  39. * symbolicEqual('abs(x)','x') // Returns false
  40. * symbolicEqual('abs(x)','x', simplify.positiveContext) // Returns true
  41. *
  42. * See also:
  43. *
  44. * simplify, evaluate
  45. *
  46. * @param {Node|string} expr1 The first expression to compare
  47. * @param {Node|string} expr2 The second expression to compare
  48. * @param {Object} [options] Optional option object, passed to simplify
  49. * @returns {boolean}
  50. * Returns true if a valid manipulation making the expressions equal
  51. * is found.
  52. */
  53. function _symbolicEqual(e1, e2) {
  54. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  55. var diff = new OperatorNode('-', 'subtract', [e1, e2]);
  56. var simplified = simplify(diff, {}, options);
  57. return (0, _is.isConstantNode)(simplified) && !simplified.value;
  58. }
  59. return typed(name, {
  60. 'Node, Node': _symbolicEqual,
  61. 'Node, Node, Object': _symbolicEqual
  62. });
  63. });
  64. exports.createSymbolicEqual = createSymbolicEqual;