symbolicEqual.js 2.4 KB

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