private Node GenerateMaclaurinSeriesPolynomial(double x, int order) { if (order == 0) { return(this); } if (order == 1) { return(new PlusSign(new MultiplicationSign(ReturnDerivative(x), new VariableNode()), this)); } VariableNode variableNode = new VariableNode(); NaturalNumberNode orderNode = new NaturalNumberNode(order); CaretSign caretSign = new CaretSign(variableNode, orderNode); Node f0 = ReturnDerivative(x); for (int i = 1; i < order; i++) { f0 = f0.ReturnDerivative(x); } MultiplicationSign multiplicationSign = new MultiplicationSign(caretSign, new RationalNumberNode(f0.Calculate(0))); FactorialFunction factorialFunction = new FactorialFunction(orderNode); DivisionSign divisionSign = new DivisionSign(multiplicationSign, factorialFunction); return(new PlusSign(divisionSign, ReturnMaclaurinSeriesPolynomial(x, order - 1))); }
public double ReturnMaclaurinSeries(double x, int order) { if (order == 0) { return(Calculate(0)); } if (order == 1) { return(ReturnDerivative(x).Calculate(0) * x + Calculate(0)); } VariableNode variableNode = new VariableNode(); NaturalNumberNode orderNode = new NaturalNumberNode(order); CaretSign caretSign = new CaretSign(variableNode, orderNode); Node f0 = ReturnDerivative(x); for (int i = 1; i < order; i++) { f0 = f0.ReturnDerivative(x); } MultiplicationSign multiplicationSign = new MultiplicationSign(caretSign, new RationalNumberNode(f0.Calculate(0))); FactorialFunction factorialFunction = new FactorialFunction(orderNode); DivisionSign divisionSign = new DivisionSign(multiplicationSign, factorialFunction); return(divisionSign.Calculate(x) + ReturnMaclaurinSeries(x, order - 1)); }
public override Node ReturnDerivative(double x) { NaturalNumberNode zero = new NaturalNumberNode(0); SineFunction sineNode = new SineFunction(child.Copy()); MinusSign minusNode = new MinusSign(zero, sineNode); Node childDerivative = child.ReturnDerivative(x); MultiplicationSign multiplicationSign = new MultiplicationSign(minusNode, childDerivative); return(multiplicationSign); }
public override Node ReturnDerivative(double x) { MultiplicationSign multiplicationSignA = new MultiplicationSign(leftNode.ReturnDerivative(x), rightNode.Copy()); MultiplicationSign multiplicationSignB = new MultiplicationSign(leftNode.Copy(), rightNode.ReturnDerivative(x)); MinusSign minusSign = new MinusSign(multiplicationSignA, multiplicationSignB); NaturalNumberNode naturalNumberNode = new NaturalNumberNode(2); CaretSign caretSign = new CaretSign(rightNode.Copy(), naturalNumberNode); DivisionSign division = new DivisionSign(minusSign, caretSign); return(division); }
private PlusSign[] ReturnNewtonIndeterminates(int order) { PlusSign[] funcs = new PlusSign[order + 1]; HNode hNode = new HNode(); NaturalNumberNode orderNode; MultiplicationSign multiplicationSign; VariableNode variableNode = new VariableNode(); for (int i = 0; i <= order; i++) { orderNode = new NaturalNumberNode(order - i); multiplicationSign = new MultiplicationSign(orderNode, hNode); funcs[i] = new PlusSign(variableNode, multiplicationSign); } return(funcs); }
public double ReturnNewtonMaclaurinSeries(double x, int order) { if (order == 0) { return(Calculate(0)); } if (order == 1) { return(ReturnNewtonDerivative(0) * x + Calculate(0)); } VariableNode variableNode = new VariableNode(); NaturalNumberNode orderNode = new NaturalNumberNode(order); CaretSign caretSign = new CaretSign(variableNode, orderNode); RationalNumberNode f0 = new RationalNumberNode(CalculatePolynomialDerivative(0, order)); MultiplicationSign multiplicationSign = new MultiplicationSign(caretSign, f0); FactorialFunction factorialFunction = new FactorialFunction(orderNode); DivisionSign divisionSign = new DivisionSign(multiplicationSign, factorialFunction); return(divisionSign.Calculate(x) + ReturnNewtonMaclaurinSeries(x, order - 1)); }
private Node ReturnNewtonPolynomial(double x, int order) { Operator sign = null; int[] coefficients = ReturnIndeterminateCoefficients(order); PlusSign[] indeterminates = ReturnNewtonIndeterminates(order); if (order == 0) { return(new RationalNumberNode(Calculate(indeterminates[0].Calculate(x)))); } if (order == 1) { return(new MinusSign(new RationalNumberNode(Calculate(indeterminates[0].Calculate(x))), new RationalNumberNode(Calculate(indeterminates[1].Calculate(x))))); } NaturalNumberNode naturalNumberNode = new NaturalNumberNode(coefficients[0]); MultiplicationSign multiplicationSign = new MultiplicationSign(naturalNumberNode, new RationalNumberNode(Calculate(indeterminates[0].Calculate(x)))); sign = multiplicationSign; for (int i = 1; i <= order; i++) { naturalNumberNode = new NaturalNumberNode(Math.Abs(coefficients[i])); multiplicationSign = new MultiplicationSign(naturalNumberNode, new RationalNumberNode(Calculate(indeterminates[i].Calculate(x)))); if (i % 2 == 0) { sign = new PlusSign(sign, multiplicationSign); } else { sign = new MinusSign(sign, multiplicationSign); } } return(sign); }
private Node Parse(ref string s) { Node a; Node b; char op = ' '; if (s[0] == '+' || s[0] == '-' || s[0] == '*' || s[0] == '/' || s[0] == '^') { op = s[0]; s = s.Substring(1); SkipCharacter(ref s, '('); a = Parse(ref s); SkipCharacter(ref s, ','); b = Parse(ref s); SkipCharacter(ref s, ')'); if (op == '+') { return(new PlusSign(a, b)); } else if (op == '-') { return(new MinusSign(a, b)); } else if (op == '*') { return(new MultiplicationSign(a, b)); } else if (op == '/') { return(new DivisionSign(a, b)); } else { return(new CaretSign(a, b)); } } else if (s[0] == 's' || s[0] == 'c' || s[0] == 'e' || s[0] == 'l' || s[0] == '!') { op = s[0]; s = s.Substring(1); SkipCharacter(ref s, '('); a = Parse(ref s); SkipCharacter(ref s, ')'); if (op == 's') { return(new SineFunction(a)); } else if (op == 'c') { return(new CosineFunction(a)); } else if (op == 'e') { return(new ExponentialFunction(a)); } else if (op == 'l') { return(new LogarithmFunction(a)); } else { return(new FactorialFunction(a)); } } else if (s[0] == 'n' || s[0] == 'r') { op = s[0]; s = s.Substring(1); SkipCharacter(ref s, '('); string tempS = s.Split(')')[0]; tempS = tempS.Replace(',', '.'); if (op == 'n') { if (tempS.Contains('.') || tempS.Contains('-')) { throw new IsNotValidNaturalNumberException(); } } s = s.Substring(s.IndexOf(')') + 1); if (op == 'n') { return(new NaturalNumberNode(int.Parse(tempS))); } else { return(new RationalNumberNode(float.Parse(tempS))); } } else if (s[0] == 'p') { s = s.Substring(1); return(new PiNode()); } else if (s[0] == 'x') { s = s.Substring(1); return(new VariableNode()); } else if (char.IsDigit(s[0])) { string number = s[0].ToString(); s = s.Substring(1); while (s.Length > 0) { if (!char.IsDigit(s[0])) { break; } number += s[0]; s = s.Substring(1); } a = new NaturalNumberNode(int.Parse(number)); return(a); } throw new InvalidExpressionException(); }