Exemplo n.º 1
0
        /// <summary>
        /// Method's sole purpose is to create the i-th MacLaurin polynomial term.
        /// </summary>
        /// <param name="expression">
        /// Requires an Operand, representing expression or a derrivative
        /// </param>
        /// <param name="i">An integer the i-th degree</param>
        /// <returns></returns>
        private Operand CreateMaclaurinTerm(Operand expression, int i)
        {
            // The zero-th order MacLaurin Polynomial is (P0(x)) =
            // f(0) * (x^0)/0! which simplifies to a constant?
            if (i == 0)
            {
                // We can return calculate and return the function value
                // evaluated in x = 0.
                double functionValue = expression.Calculate(0);
                return(new RealNumber(functionValue));
            }
            else
            {
                // The expression is differentiated if i != 0.
                // Calculate the coefficient in a = 0.
                double slope = expression.Calculate(0);
                // If the slope is NaN we pretty much evaluated all possible
                // derivatives (there is no more derivative possible)
                // Thus return 0 such that we can simplify later on.
                if (double.IsNaN(slope))
                {
                    slope = 0;
                }
                Operand resultSlope = new RealNumber(slope);

                Multiplication MacLaurinTerm = new Multiplication();
                MacLaurinTerm.LeftSuccessor = resultSlope;
                Division div = new Division();
                MacLaurinTerm.RightSuccessor = div;
                // Now create the right part of the multiplication (f'i(0) * (x - 0)^i / i!)
                // Create the numerator (x - 0)^i
                Power numerator = new Power();
                numerator.LeftSuccessor  = new DependentVariableX();
                numerator.RightSuccessor = new Integer(i);
                // Create the denominator (i)!.
                Factorial denominator = new Factorial();
                denominator.LeftSuccessor = new Integer(i);
                // Add the numerator and denominator into the division Operator.
                div.LeftSuccessor  = numerator;
                div.RightSuccessor = denominator;

                return(MacLaurinTerm);
            }
        }
Exemplo n.º 2
0
 private double CalculateMacLaurinPolynomial(double a, double x, int i)
 {
     // If some counter = 0, return the constant.
     if (i == 0)
     {
         // return f(0) * x^0 / 0!
         Factorial iFactorial = new Factorial();
         iFactorial.LeftSuccessor = new Integer(i);
         //Console.WriteLine(expressionRoot);
         return(expressionRoot.Calculate(0) * Math.Pow(x, i) / iFactorial.Calculate(x));
     }
     else
     {
         // calc the i-th term of the maclaurin polynomial + any lower polynomial.
         Factorial iFactorial = new Factorial();
         iFactorial.LeftSuccessor = new Integer(i);
         return((HigherOrderDerivativeByDifferenceQuotient(a, i) * Math.Pow(x, i) / iFactorial.Calculate(x)) + CalculateMacLaurinPolynomial(a, x, i - 1)); // ith term + (i - 1) term if the order/degree is > 0.
     }
 }
Exemplo n.º 3
0
        // Method that will create an appropriate operator class
        // from the obtained character.
        private void CreateNode(char operat0r)
        {
            Operand result = null;

            switch (operat0r)
            {
            case '+':
                result = new Addition();
                break;

            case '-':
                result = new Subtraction();
                break;

            case '*':
                result = new Multiplication();
                break;

            case '/':
                result = new Division();
                break;

            case '^':
                result = new Power();
                break;

            // Now for operators that take one argument.
            case '!':
                result = new Factorial();
                break;

            case 'l':
                result = new NaturalLog();
                break;

            case 'e':
                result = new Exp();
                break;

            case 'c':
                result = new Cosine();
                break;

            case 's':
                result = new Sine();
                break;
            }
            if (result is BinaryOperator)
            {
                // Add the operands to the binary operator.
                ((BinaryOperator)result).LeftSuccessor  = operands[operands.Count - 2];
                ((BinaryOperator)result).RightSuccessor = operands[operands.Count - 1];

                // Remove the operands from the stack.
                operands.RemoveAt(operands.Count - 1);
                operands.RemoveAt(operands.Count - 1);
            }
            else
            {
                // Same story, add operand to unary operator.
                ((UnaryOperator)result).LeftSuccessor = operands[operands.Count - 1];

                // Remove operand from stack.
                operands.RemoveAt(operands.Count - 1);
            }

            // Add the sub tree on top of the operands stack.
            operands.Add(result);
        }