Exemplo n.º 1
0
        public override Operand Copy()
        {
            NaturalLog copy = new NaturalLog();

            copy.LeftSuccessor = LeftSuccessor.Copy();
            return(copy);
        }
Exemplo n.º 2
0
        public override Operand Simplify()
        {
            Subtraction simplifiedExpression = new Subtraction();

            simplifiedExpression.LeftSuccessor  = LeftSuccessor.Simplify();
            simplifiedExpression.RightSuccessor = RightSuccessor.Simplify();
            if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // Left is not PI and a number.
            {
                // In this scenario the right sub tree can be an expression still.
                if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Right not PI and a number.
                {
                    // We have 2 numbers so we can safely calculate.
                    double difference = Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) - Convert.ToDouble(simplifiedExpression.RightSuccessor.Data);
                    return(new RealNumber(difference));
                }
                // Left can be 0 and that means the right is negative and we leave the expression tree as 0 - right expression
                // to not have to worry about unary or binary operator of subtraction.
            }
            else if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Left was either an expression or it is PI.
            {                                                                                                        // Thus we have to ensure that the Right is not PI or crash.
                // This case means that left is not a number. And we thus should be
                // able to return the negative of the right expression.
                if (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) == 0.0)
                {
                    return(simplifiedExpression.LeftSuccessor.Simplify());
                }
            }
            return(simplifiedExpression);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The quotient rule
        ///
        /// (f / g)' = (gf' - g'f) / g^2
        ///
        /// </summary>
        /// <returns>A division operator object holding respective expressions.</returns>
        public override Operand Differentiate()
        {
            // Create gf'
            Multiplication leftNumerator = new Multiplication();

            leftNumerator.LeftSuccessor  = RightSuccessor.Copy();
            leftNumerator.RightSuccessor = LeftSuccessor.Differentiate();

            // Create g'f
            Multiplication rightNumerator = new Multiplication();

            rightNumerator.LeftSuccessor  = RightSuccessor.Differentiate();
            rightNumerator.RightSuccessor = LeftSuccessor.Copy();

            // Create the numrator of previous terms gf' - g'f
            Subtraction derivativeNumerator = new Subtraction();

            derivativeNumerator.LeftSuccessor  = leftNumerator;
            derivativeNumerator.RightSuccessor = rightNumerator;

            // Create g^2 of a new copy of g.
            Power derivativeDenominator = new Power();

            derivativeDenominator.LeftSuccessor  = RightSuccessor.Copy();
            derivativeDenominator.RightSuccessor = new Integer(2);

            // Create the final result and assign. (f / g)' = (gf' - g'f) / g^2
            Division derivative = new Division();

            derivative.LeftSuccessor  = derivativeNumerator;
            derivative.RightSuccessor = derivativeDenominator;

            return(derivative);
        }
Exemplo n.º 4
0
        public override Operand Differentiate()
        {
            // (f(x))^b where b is a natural number even.
            // The general power rule but combined with the
            // chain rule. b * (f(x))^(b - 1) * f'(x)

            // b
            Operand power = RightSuccessor.Copy();

            // b - 1
            Integer newPower = new Integer(Convert.ToDouble(power.Data) - 1);

            // f(x)^(b - 1)
            Power onePowerLess = new Power();

            onePowerLess.LeftSuccessor  = LeftSuccessor.Copy();
            onePowerLess.RightSuccessor = newPower;

            // f'
            Operand derivativeOfF = LeftSuccessor.Differentiate();

            // b * f(x)^(b - 1)
            Multiplication intermediaryResult = new Multiplication();

            intermediaryResult.LeftSuccessor  = power;
            intermediaryResult.RightSuccessor = onePowerLess;

            // (b * f(x)^(b - 1)) * f'
            Multiplication derivative = new Multiplication();

            derivative.LeftSuccessor  = intermediaryResult;
            derivative.RightSuccessor = derivativeOfF;

            return(derivative);
        }
Exemplo n.º 5
0
        public override Operand Simplify()
        {
            NaturalLog simplifiedExpression = new NaturalLog();

            simplifiedExpression.LeftSuccessor = LeftSuccessor.Simplify();
            return(simplifiedExpression);
        }
Exemplo n.º 6
0
        public override Operand Simplify()
        {
            // Try to simplify as much as possible.
            Division simplifiedExpression = new Division();

            simplifiedExpression.LeftSuccessor  = LeftSuccessor.Simplify();
            simplifiedExpression.RightSuccessor = RightSuccessor.Simplify();
            // When we divide by 1, we can just return
            // the numerator of the division.
            if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // If a number but not PI.
            {
                // The denominator = 1, thus we can return the numerator.
                if (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) == 1) // If the number is 1, we simplify it to just the simplification of the left branch.
                {
                    return(simplifiedExpression.LeftSuccessor.Simplify());
                }
            }
            else if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // The right was either not a number or PI.
            {                                                                                                      // We ensured that the left is not PI
                // Check if the numerator is 0
                // if so we can return a number 0.
                if (Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) == 0.0)
                {
                    return(new RealNumber(0.0));
                }
            }
            return(simplifiedExpression);
        }
Exemplo n.º 7
0
        public override Operand Copy()
        {
            Cosine copy = new Cosine();

            copy.LeftSuccessor = LeftSuccessor.Copy();
            return(copy);
        }
Exemplo n.º 8
0
        public override Operand Copy()
        {
            Exp copy = new Exp();

            copy.LeftSuccessor = LeftSuccessor.Copy();
            return(copy);
        }
Exemplo n.º 9
0
        public override Operand Simplify()
        {
            Cosine simplifiedExpression = new Cosine();

            simplifiedExpression.LeftSuccessor = LeftSuccessor.Simplify();
            return(simplifiedExpression);
        }
Exemplo n.º 10
0
        public override Operand Simplify()
        {
            Addition simplifiedExpression = new Addition();

            simplifiedExpression.LeftSuccessor  = LeftSuccessor.Simplify();
            simplifiedExpression.RightSuccessor = RightSuccessor.Simplify();
            if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // Must not be PI or crash.
            {
                // If the right is also a number, we can simplify the two by calculating the result.
                // and return a new operand object holding the result as data up to the previous call.
                if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Must not be PI or crash, or loss of info if we simplify perhaps.
                {
                    // These will always be non PI numbers either int or real.
                    double addition = Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) + Convert.ToDouble(simplifiedExpression.RightSuccessor.Data);
                    return(new RealNumber(addition));
                }
                else if (Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) == 0.0) // This left is never PI, if it is 0 we can simplify right safely.
                {
                    return(simplifiedExpression.RightSuccessor.Simplify());
                }
            }
            else if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Right must not be PI or it'll crash.
            {
                if (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) == 0)
                {
                    return(simplifiedExpression.LeftSuccessor.Simplify());
                }
            }
            return(simplifiedExpression);
        }
Exemplo n.º 11
0
        public override bool Calculate()
        {
            bool leftResult  = LeftSuccessor.Calculate();
            bool rightResult = RightSuccessor.Calculate();

            return(!(leftResult && rightResult));
        }
Exemplo n.º 12
0
        public override Operand Copy()
        {
            Power copy = new Power();

            copy.LeftSuccessor  = LeftSuccessor.Copy();
            copy.RightSuccessor = RightSuccessor.Copy();
            return(copy);
        }
Exemplo n.º 13
0
        public override Operand Copy()
        {
            Division copy = new Division();

            copy.LeftSuccessor  = LeftSuccessor.Copy();
            copy.RightSuccessor = RightSuccessor.Copy();
            return(copy);
        }
Exemplo n.º 14
0
        public override Operand Copy()
        {
            Multiplication copy = new Multiplication();

            copy.LeftSuccessor  = LeftSuccessor.Copy();
            copy.RightSuccessor = RightSuccessor.Copy();
            return(copy);
        }
Exemplo n.º 15
0
        public override Proposition Copy()
        {
            Negation copy = new Negation();

            copy.LeftSuccessor = LeftSuccessor.Copy();

            return(copy);
        }
Exemplo n.º 16
0
        public override Operand Copy()
        {
            Subtraction copy = new Subtraction();

            copy.LeftSuccessor  = LeftSuccessor.Copy();
            copy.RightSuccessor = RightSuccessor.Copy();
            return(copy);
        }
Exemplo n.º 17
0
        public override Proposition Copy()
        {
            Conjunction copy = new Conjunction();

            copy.LeftSuccessor  = LeftSuccessor.Copy();
            copy.RightSuccessor = RightSuccessor.Copy();

            return(copy);
        }
Exemplo n.º 18
0
        /// <summary>
        /// (f + g)' = f' + g'
        /// Meaning we differentiate both the left and right sub tree first
        /// and return an addition operator with the differentiated sub trees
        /// as it's successors
        /// </summary>
        /// <returns>
        /// An addition operator object with both left
        /// and right successor differentiated.
        /// </returns>
        public override Operand Differentiate()
        {
            Addition derivative = new Addition();

            derivative.LeftSuccessor  = LeftSuccessor.Differentiate();
            derivative.RightSuccessor = RightSuccessor.Differentiate();

            return(derivative);
        }
Exemplo n.º 19
0
        /// <summary>
        /// This returns the result set of: (A\B) U (B\A) U (A Intersection B)
        ///
        /// Example:
        ///
        /// A = { A, B }
        /// B = { A, C }
        ///
        /// A\B = { B } || The elements that are in A but not in B
        /// B\A = { C } || The elements that are in B but not in A
        /// A Intersection B = { A } || The elements that are in both A and B
        ///
        /// result = { B, C, A }! All unique variables from two different sets.
        /// </summary>
        /// <returns>A list representing the set of all unique variables in the proposition formula.</returns>
        public override List <Proposition> GetVariables()
        {
            List <Proposition> leftChildVariables  = LeftSuccessor.GetVariables();
            List <Proposition> rightChildVariables = RightSuccessor.GetVariables();

            return(leftChildVariables.Except(rightChildVariables).                     // Set difference gets applied { A } \ { B }
                   Union(rightChildVariables.Except(leftChildVariables)).              // { B } \ { A }
                   Union(leftChildVariables.Intersect(rightChildVariables)).ToList()); // { A } Intersection { B }, resulting in a set containing all unique elements (unordered though)
        }
Exemplo n.º 20
0
        public override Proposition Copy()
        {
            Nand copy = new Nand();

            copy.LeftSuccessor  = LeftSuccessor.Copy();
            copy.RightSuccessor = RightSuccessor.Copy();

            return(copy);
        }
Exemplo n.º 21
0
        public override bool Calculate()
        {
            if (LeftSuccessor.Calculate() == true && (RightSuccessor.Calculate() == false))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 22
0
        public override Proposition Copy()
        {
            BiImplication copy = new BiImplication();

            copy.LeftSuccessor  = LeftSuccessor.Copy();
            copy.RightSuccessor = RightSuccessor.Copy();

            return(copy);
        }
Exemplo n.º 23
0
        /// <summary>
        /// (f - g)' = f'- g'
        /// Meaning we differentiate both the left and right sub tree first
        /// and return a subtraction operator with the differentiated sub trees
        /// as it's successors
        /// </summary>
        /// <returns>
        /// A subtraction operator object with both left
        /// and right successor differentiated.
        /// </returns>
        public override Operand Differentiate()
        {
            Operand leftDerivative  = LeftSuccessor.Differentiate();
            Operand rightDerivative = RightSuccessor.Differentiate();

            Subtraction derivative = new Subtraction();

            derivative.LeftSuccessor  = leftDerivative;
            derivative.RightSuccessor = rightDerivative;

            return(derivative);
        }
Exemplo n.º 24
0
        public override Proposition Nandify()
        {
            if (LeftSuccessor.GetType() != typeof(Proposition))
            {
                LeftSuccessor = LeftSuccessor.Nandify();
            }

            if (RightSuccessor.GetType() != typeof(Proposition))
            {
                RightSuccessor = RightSuccessor.Nandify();
            }

            return(this);
        }
Exemplo n.º 25
0
        public override string ToPrefixString()
        {
            if (LeftSuccessor == null || RightSuccessor == null)
            {
                throw new NullReferenceException("Both left and right successor must not be null!");
            }

            string result = $"{Data}(";

            result += LeftSuccessor.ToPrefixString();
            result += ", ";
            result += RightSuccessor.ToPrefixString();
            result += ")";
            return(result);
        }
Exemplo n.º 26
0
        public override Operand Differentiate()
        {
            // (e^u)' = e^u * u'
            // e^u
            Exp outerFunction = (Exp)Copy();
            // u'
            Operand innerDerivative = LeftSuccessor.Differentiate();
            // e^u * u'
            Multiplication derivative = new Multiplication();

            derivative.LeftSuccessor  = outerFunction;
            derivative.RightSuccessor = innerDerivative;

            return(derivative);
        }
Exemplo n.º 27
0
        public override string ToPrefixString()
        {
            if (LeftSuccessor == null)
            {
                throw new NullReferenceException("A predicate is required to be set");
            }

            string result = $"{Data}{GetBoundVariable()}.";

            result += "(";
            result += LeftSuccessor.ToPrefixString();
            result += ")";

            return(result);
        }
Exemplo n.º 28
0
 public override Operand Simplify()
 {
     if (!(RightSuccessor is PI))                          // Ensure the power is not PI or it would crash.
     {
         if (Convert.ToDouble(RightSuccessor.Data) == 1.0) // If it's 1 we return the simplified version of left.
         {
             return(LeftSuccessor.Simplify());
         }
         return(Copy()); // Otherwise just the copy.
     }
     else // All other cases, just return the copy.
     {
         return(Copy());
     }
 }
Exemplo n.º 29
0
        public override Proposition Nandify()
        {
            // ~(A) == ~(A & A) == A % A
            Nand        nand          = new Nand();
            Proposition nandifiedLeft = LeftSuccessor;

            if (LeftSuccessor.GetType() != typeof(Proposition))
            {
                nandifiedLeft = LeftSuccessor.Nandify();
            }

            nand.LeftSuccessor  = nandifiedLeft;
            nand.RightSuccessor = nandifiedLeft;

            return(nand);
        }
Exemplo n.º 30
0
        public override Operand Differentiate()
        {
            // Derivative of sin(u) = cos(u) * u'
            // c(u)
            Cosine outerDerivative = new Cosine();

            outerDerivative.LeftSuccessor = LeftSuccessor.Copy();

            // Now for u' we call it's differentiate method.
            Operand innerDerivative = LeftSuccessor.Differentiate();

            // c(u) * u'
            Multiplication derivative = new Multiplication();

            derivative.LeftSuccessor  = outerDerivative;
            derivative.RightSuccessor = innerDerivative;

            return(derivative);
        }