예제 #1
0
        public SymbolicExpression Update(SymbolicExpression operand)
        {
            if (!ReferenceEquals(Operand, operand))
            {
                switch (Operator)
                {
                case UnaryOperator.Plus: return(operand);

                case UnaryOperator.Minus: return(Negate(operand));

                case UnaryOperator.Abs: return(SymbolicMath.Abs(operand));

                case UnaryOperator.Sin: return(SymbolicMath.Sin(operand));

                case UnaryOperator.Cos: return(SymbolicMath.Cos(operand));

                case UnaryOperator.Tan: return(SymbolicMath.Tan(operand));

                case UnaryOperator.Ln: return(SymbolicMath.Ln(operand));

                default: throw new InvalidOperationException();
                }
            }

            return(this);
        }
        public SymbolicExpression Update(SymbolicExpression firstOperand, SymbolicExpression secondOperand)
        {
            if (!ReferenceEquals(FirstOperand, firstOperand) || !ReferenceEquals(SecondOperand, secondOperand))
            {
                switch (Operator)
                {
                case BinaryOperator.Addition: return(Add(firstOperand, secondOperand));

                case BinaryOperator.Subtraction: return(Subtract(firstOperand, secondOperand));

                case BinaryOperator.Multiplication: return(Multiply(firstOperand, secondOperand));

                case BinaryOperator.Division: return(Divide(firstOperand, secondOperand));

                case BinaryOperator.Power: return(SymbolicMath.Pow(firstOperand, secondOperand));

                case BinaryOperator.Root: return(SymbolicMath.Root(firstOperand, secondOperand));

                default: throw new InvalidOperationException();
                }
            }

            return(this);
        }
예제 #3
0
        public static SymbolicExpression Constant(double value) => Constant((decimal)value);         // Hoping that things will round nicely to decimal…

        public static SymbolicExpression Constant(decimal value)
        {
            if (value.IsInteger())
            {
                return(Constant(checked ((long)value)));
            }
            else
            {
                (long numerator, byte power) = value.Decompose();

                // TODO: Use BigInteger or some kind of larger integer that can at least hold the whole value of a decimal.

                long denominator = MathUtil.Pow(10, Math.Min(power, (byte)18));                 // Compute 10^n, for n up to 18

                var result = Divide(numerator, denominator);

                if (power > 18)
                {
                    result = Multiply(result, SymbolicMath.Pow(10, 18 - power));
                }

                return(result);
            }
        }