示例#1
0
        public override Operand Simplify()
        {
            Sine simplifiedExpression = new Sine();

            simplifiedExpression.LeftSuccessor = LeftSuccessor.Simplify();
            return(simplifiedExpression);
        }
示例#2
0
        public override Operand Copy()
        {
            Sine copy = new Sine();

            copy.LeftSuccessor = LeftSuccessor.Copy();
            return(copy);
        }
示例#3
0
        public override Operand Differentiate()
        {
            // Apply the chain rule.
            // Derivative of cos(u) = -sin(u) * u'
            Sine rightOuterDerivativeExpression = new Sine();

            // s(u)
            rightOuterDerivativeExpression.LeftSuccessor = LeftSuccessor.Copy();
            // -1 * sin(u)
            Multiplication outerDerivative = new Multiplication();

            outerDerivative.LeftSuccessor  = new Integer(-1);
            outerDerivative.RightSuccessor = rightOuterDerivativeExpression;

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

            Multiplication derivative = new Multiplication();

            derivative.LeftSuccessor  = outerDerivative;
            derivative.RightSuccessor = innerDerivative;
            return(derivative);
        }
示例#4
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);
        }