/// <summary> /// Creates a MacLaurien series based off the function /// </summary> /// <param name="mcLaurienRoot">Where the McLaurien Series will be outputted</param> /// <param name="order">Nth order of a series</param> public void CreateMcLaurienSeries(out BaseNode mcLaurienRoot, int order = 5) { if (derivativeRoot == null) { CreateDerivativeTree(); derivativeRoot.Simplify(); } // we made sure that there is a derivative BaseNode myDerivative = Plotter.CloneTree(derivativeRoot); myDerivative.derivativeRoot = myDerivative; double[] values = new double[order + 1]; // values for functions (f(0), derivative of f(0), second derivative of f(0), etc..) values[0] = root.Calculate(0); // set up a value for f(0) if (values.Length >= 2) { values[1] = derivativeRoot.Calculate(0); // set up a value of the first derivative of f(0) } if (values.Length >= 3) { for (int i = 2; i < values.Length; i++) { myDerivative.CreateDerivativeTree(null); values[i] = myDerivative.derivativeRoot.Calculate(0); myDerivative = myDerivative.derivativeRoot; } } List <BaseNode> mcLaurienItems = new List <BaseNode> (); SumNode result = new SumNode(null, null, null); result.left = new NumberNode(null, values[0]); for (int i = 1; i < values.Length; i++) { DivisionNode item = new DivisionNode(null, null, null); FactorialNode denominator = new FactorialNode(new NumberNode(null, i), null); // not sure about this line MultiplicationNode numerator = new MultiplicationNode( new NumberNode(null, values[i]), new PowerNode(new BasicFunctionXNode("", null), new NumberNode(null, i), null), null ); item.left = numerator; item.right = denominator; mcLaurienItems.Add(item); } foreach (var item in mcLaurienItems) { result.PutToRightNode(item); } mcLaurienRoot = result; }
/// <summary> /// Sets the derivateRoot to the root of a last inputted function and gets its derivative /// </summary> public void CreateDerivativeTree() { derivativeRoot = root; derivativeRoot.CreateDerivativeTree(null); derivativeRoot = derivativeRoot.derivativeRoot; /* * What happens here is I calculate the derivative of the derivativeRoot (which is initially equal to the root of the function) * Inside of the class BaseNode, which every other node derives from, there is a public field derivativeRoot which holds the value * for the derivative. This field is altered after the call of CreateDerivativeTree() so that's why we make our plotter.derivativeRoot * equal to the one inside of a node. * * VERY BAD DESIGN * */ }