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); }
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); }
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); }
public override bool Calculate() { bool leftResult = LeftSuccessor.Calculate(); bool rightResult = RightSuccessor.Calculate(); return(!(leftResult && rightResult)); }
public override Operand Copy() { Power copy = new Power(); copy.LeftSuccessor = LeftSuccessor.Copy(); copy.RightSuccessor = RightSuccessor.Copy(); return(copy); }
public override Operand Copy() { Addition copy = new Addition(); copy.LeftSuccessor = LeftSuccessor.Copy(); copy.RightSuccessor = RightSuccessor.Copy(); return(copy); }
public override Operand Copy() { Multiplication copy = new Multiplication(); copy.LeftSuccessor = LeftSuccessor.Copy(); copy.RightSuccessor = RightSuccessor.Copy(); return(copy); }
public override Proposition Copy() { BiImplication copy = new BiImplication(); copy.LeftSuccessor = LeftSuccessor.Copy(); copy.RightSuccessor = RightSuccessor.Copy(); return(copy); }
/// <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); }
public override Proposition Copy() { Conjunction copy = new Conjunction(); copy.LeftSuccessor = LeftSuccessor.Copy(); copy.RightSuccessor = RightSuccessor.Copy(); return(copy); }
public override bool Calculate() { if (LeftSuccessor.Calculate() == true && (RightSuccessor.Calculate() == false)) { return(false); } return(true); }
public override Proposition Copy() { Nand copy = new Nand(); copy.LeftSuccessor = LeftSuccessor.Copy(); copy.RightSuccessor = RightSuccessor.Copy(); return(copy); }
/// <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); }
public override Proposition Nandify() { if (LeftSuccessor.GetType() != typeof(Proposition)) { LeftSuccessor = LeftSuccessor.Nandify(); } if (RightSuccessor.GetType() != typeof(Proposition)) { RightSuccessor = RightSuccessor.Nandify(); } return(this); }
public override Operand Simplify() { Multiplication simplifiedExpression = (Multiplication)Copy(); simplifiedExpression.LeftSuccessor = LeftSuccessor.Simplify(); simplifiedExpression.RightSuccessor = RightSuccessor.Simplify(); // If either of the operands equals 0 or 1 then we want to simplify specifically. if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Right is a number but not PI. { // If Left is also an integer we can create a new real number operand for the result and return it up. if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // As long as the left is not PI or crash. { double result = Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) * Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data); return(new RealNumber(result)); } // If both are numbers we could multiply and return the result. // But this might be iffy with double values and rounding errors? switch (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data)) // Safe as Right is not PI in this case. { case 0.0: // Multiply by zero means return 0. return(new Integer(0.0)); case 1.0: // Multiply by one means return the other part of the expression. return(simplifiedExpression.LeftSuccessor.Simplify()); } } else if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // This means the simplified right expression was not a number. { // Since the Right is not a number or it was PI. switch (Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data)) // The left is not PI so we can convert the data to a number { case 0.0: return(new Integer(0.0)); case 1.0: return(simplifiedExpression.RightSuccessor.Simplify()); } } return(simplifiedExpression); }
/// <summary> /// Applies the product rule to the sub trees. /// (f * g)' = f * g' + f' * g /// /// The method differentiates the appropriate sub trees and /// eventually returns a single Operand object (in this case /// an addition operator object) holding both an original /// expression sub tree as well as a differentiated sub tree. /// </summary> /// <returns> /// An operator object, representing the applied product rule. /// </returns> public override Operand Differentiate() { // Create and assign fg' Multiplication newLeftExpression = new Multiplication(); newLeftExpression.LeftSuccessor = LeftSuccessor.Copy(); newLeftExpression.RightSuccessor = RightSuccessor.Differentiate(); // Create and assign f'g Multiplication newRightExpression = new Multiplication(); newRightExpression.LeftSuccessor = LeftSuccessor.Differentiate(); newRightExpression.RightSuccessor = RightSuccessor.Copy(); // Create and assign fg' + f'g Addition derivative = new Addition(); derivative.LeftSuccessor = newLeftExpression; derivative.RightSuccessor = newRightExpression; return(derivative); }
public override double Calculate(double x) { return(LeftSuccessor.Calculate(x) * RightSuccessor.Calculate(x)); }
public override bool Calculate() { // Will be true if both left and right successor are true or false return(LeftSuccessor.Calculate() == RightSuccessor.Calculate()); }
public override double Calculate(double x) { return(Math.Pow(LeftSuccessor.Calculate(x), RightSuccessor.Calculate(x))); }
public override bool Calculate() { return(LeftSuccessor.Calculate() && RightSuccessor.Calculate()); }