예제 #1
0
    public override bool Equals(object obj, bool includeChildren)
    {
        if (!base.Equals(obj, includeChildren))
        {
            return(false);
        }

        BinaryToken other = (BinaryToken)obj;

        if (Lhs != null)
        {
            if (!Lhs.Equals(other.Lhs))
            {
                return(false);
            }
        }
        else if (other.Lhs != null)
        {
            return(false);
        }

        if (Rhs != null)
        {
            if (!Rhs.Equals(other.Rhs))
            {
                return(false);
            }
        }
        else if (other.Rhs != null)
        {
            return(false);
        }

        return(true);
    }
예제 #2
0
        public override bool Equals(object obj)
        {
            var eq = obj as Equation;

            if (eq != null)
            {
                return(Lhs.Equals(eq.Lhs) && Rhs.Equals(eq.Rhs));
            }
            return(false);
        }
예제 #3
0
        public override bool Equals(object obj)
        {
            var eqGoal = obj as EqGoal;

            if (eqGoal != null)
            {
                if (Rhs == null)
                {
                    return(Lhs.Equals(eqGoal.Lhs));
                }

                bool isNum1 = LogicSharp.IsNumeric(Rhs);
                bool isNum2 = LogicSharp.IsNumeric(eqGoal.Rhs);
                bool result;
                if (isNum1 && isNum2)
                {
                    result = LogicSharp.NumericEqual(Rhs, eqGoal.Rhs);
                }
                else
                {
                    result = Rhs.Equals(eqGoal.Rhs);
                }
                return(Lhs.Equals(eqGoal.Lhs) && result);
            }

            var eq = obj as Equation;

            if (eq != null)
            {
                if (Rhs == null)
                {
                    return(Lhs.Equals(eq.Lhs));
                }
                bool isNum1 = LogicSharp.IsNumeric(Rhs);
                bool isNum2 = LogicSharp.IsNumeric(eq.Rhs);
                bool result;
                if (isNum1 && isNum2)
                {
                    result = LogicSharp.NumericEqual(Rhs, eq.Rhs);
                }
                else
                {
                    result = Rhs.Equals(eq.Rhs);
                }

                if (eq.Lhs == null)
                {
                    return(false);
                }
                return(Lhs.ToString().Equals(eq.Lhs.ToString()) && result);
            }

            return(false);
        }
예제 #4
0
        public bool Reify(Dictionary <object, object> substitutions)
        {
            Lhs = LogicSharp.Reify(Lhs, substitutions);
            Rhs = LogicSharp.Reify(Rhs, substitutions);

            if (Var.ContainsVar(Lhs) || Var.ContainsVar(Rhs))
            {
                return(true);
            }
            else
            {
                return(Lhs.Equals(Rhs));
            }
        }
예제 #5
0
 public bool Equals(BinaryOperation operation) => HashCode == operation.HashCode &&
 Value == operation.Value &&
 Operator == operation.Operator &&
 Lhs.Equals(operation.Lhs) &&
 Rhs.Equals(operation.Rhs);
예제 #6
0
        public Assignment?SolveFor(Symbol symbol)
        {
            // Do base cases (already rearranged for one of the symbol instances)
            if (Lhs.Equals(symbol))
            {
                return(this);
            }
            else if (Rhs.Equals(symbol))
            {
                return(new Assignment(this.Rhs, this.Lhs));
            }
            // Do recursive case
            // Re-write based on possible algebraic inverses for this given tree
            // https://www.cs.utexas.edu/users/novak/algebra.pdf
            Assignment?solved = null;

            // LHS Operation & Inverse(s)
            switch (this.Lhs)
            {
            case Addition bop: {
                /*
                 *  bop.lhs + bop.rhs = this.rhs
                 *  bop.lhs = this.rhs - bop.rhs
                 *  bop.rhs = this.rhs - bop.lhs
                 */
                var try1 = new Assignment(bop.Lhs, new Subtraction(this.Rhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Subtraction(this.Rhs, bop.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Subtraction bop: {
                /*
                 *  bop.lhs - bop.rhs = this.rhs
                 *  bop.lhs = this.rhs + bop.rhs
                 *  bop.rhs = bop.lhs - this.rhs
                 */
                var try1 = new Assignment(bop.Lhs, new Addition(this.Rhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Subtraction(bop.Lhs, this.Rhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Multiplication bop: {
                /*
                 *  bop.lhs * bop.rhs = this.rhs
                 *  bop.lhs = this.rhs / bop.rhs
                 *  bop.rhs = this.rhs / bop.lhs
                 */
                var try1 = new Assignment(bop.Lhs, new Division(this.Rhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Division(this.Rhs, bop.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Division bop: {
                /*
                 *  bop.lhs / bop.rhs = this.rhs
                 *  bop.lhs = this.rhs * bop.rhs
                 *  bop.rhs = bop.lhs / this.rhs
                 */
                var try1 = new Assignment(bop.Lhs, new Multiplication(this.Rhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Division(bop.Lhs, this.Rhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Exponentiation bop: {
                /*
                 *  bop.lhs ^ bop.rhs = this.rhs
                 *  bop.lhs = bop.rhs √ this.rhs  =>  this.rhs ^ (1/bop.rhs)
                 *  bop.rhs = log_{bop.lhs}(this.rhs)
                 */
                var try1 = new Assignment(bop.Root, new Exponentiation(this.Rhs, new Division(Real.One, bop.Power)));
                var try2 = new Assignment(bop.Power, new Logarithm(bop.Root, this.Rhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Logarithm log: {
                /*
                 *  log_{bop.base}(bop.arg) = this.rhs
                 *  bop.base = this.rhs √ bop.arg  => bop.arg ^ (1/this.rhs)
                 *  bop.arg = bop.base ^ this.rhs
                 */
                var try1 = new Assignment(log.Base, new Exponentiation(log.Argument, new Division(Real.One, this.Rhs)));
                var try2 = new Assignment(log.Argument, new Exponentiation(log.Base, this.Rhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Function fn: {
                /*
                 * fn(arg) = this.rhs => arg = fn^-1(this.rhs)
                 */
                var inverse = GetInverse(fn, this.Rhs);

                var try1 = new Assignment(fn.Argument, inverse);
                solved = solved ?? try1.SolveFor(symbol);
                break;
            }

            default: {
                break; // DO nothing... no recursive case
            }
            }

            // RHS Operation & Inverse(s)
            switch (this.Rhs)
            {
            case Addition bop: {
                /*
                 *  this.lhs = bop.lhs + bop.rhs
                 *  bop.lhs = this.lhs - bop.rhs
                 *  bop.rhs = this.lhs - bop.lhs
                 */
                var try1 = new Assignment(bop.Lhs, new Subtraction(this.Lhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Subtraction(this.Lhs, bop.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Subtraction bop: {
                /*
                 *  this.lhs = bop.lhs - bop.rhs
                 *  bop.lhs = this.lhs + bop.rhs
                 *  bop.rhs = bop.lhs - this.lhs
                 */
                var try1 = new Assignment(bop.Lhs, new Addition(this.Lhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Subtraction(bop.Lhs, this.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Multiplication bop: {
                /*
                 *  this.lhs = bop.lhs * bop.rhs
                 *  bop.lhs = this.lhs / bop.rhs
                 *  bop.rhs = this.lhs / bop.lhs
                 */
                var try1 = new Assignment(bop.Lhs, new Division(this.Lhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Division(this.Lhs, bop.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Division bop: {
                /*
                 *  this.lhs = bop.lhs / bop.rhs
                 *  bop.lhs = this.lhs * bop.rhs
                 *  bop.rhs = bop.lhs / this.lhs
                 */
                var try1 = new Assignment(bop.Lhs, new Multiplication(this.Lhs, bop.Rhs));
                var try2 = new Assignment(bop.Rhs, new Division(bop.Lhs, this.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Exponentiation bop: {
                /*
                 *  this.lhs = bop.lhs ^ bop.rhs
                 *  bop.lhs = bop.rhs √ this.lhs  =>  this.lhs ^ (1/bop.rhs)
                 *  bop.rhs = log_{bop.lhs}(this.lhs)
                 */
                var try1 = new Assignment(bop.Root, new Exponentiation(this.Lhs, new Division(Real.One, bop.Power)));
                var try2 = new Assignment(bop.Power, new Logarithm(bop.Root, this.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Logarithm log: {
                /*
                 *  this.lhs = log_{bop.base}(bop.arg)
                 *  bop.base = this.lhs √ bop.arg  => bop.arg ^ (1/this.lhs)
                 *  bop.arg = bop.base ^ this.lhs
                 */
                var try1 = new Assignment(log.Base, new Exponentiation(log.Argument, new Division(Real.One, this.Lhs)));
                var try2 = new Assignment(log.Argument, new Exponentiation(log.Base, this.Lhs));
                solved = solved ?? try1.SolveFor(symbol) ?? try2.SolveFor(symbol);
                break;
            }

            case Function fn: {
                /*
                 * this.lhs = fn(arg) => fn^-1(this.lhs) = arg
                 */
                var inverse = GetInverse(fn, this.Lhs);

                var try1 = new Assignment(inverse, fn.Argument);
                solved = solved ?? try1.SolveFor(symbol);
                break;
            }

            default: {
                break; // DO nothing... no recursive case
            }
            }

            // Return solved expression or null
            return(solved);
        }
예제 #7
0
 public static bool operator !=(TypeName Lhs, TypeName Rhs) => !Lhs.Equals(Rhs);