simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

symbolicEqual.js (3336B)


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