Exemplo n.º 1
0
 public ProductExpressionList Multiply(AlgebraExpression expression)
 {
     return(expression is ProductExpressionList
         ? ExpressionFactory.Product(this.Terms.Concat((expression as ProductExpressionList).Terms).ToImmutableList())
         : ExpressionFactory.Multiply(this, expression)
            );
 }
Exemplo n.º 2
0
 public override AlgebraExpression Substitute(SymbolExpression variable, AlgebraExpression replacement)
 {
     return(this
            .WithExpression(this.Expression.Substitute(variable, replacement))
            .WithTo(this.To.Substitute(variable, replacement))
            );
 }
 public SumExpressionList Add(AlgebraExpression expression)
 {
     return(expression is SumExpressionList
         ? ExpressionFactory.Sum(this.Terms.Concat((expression as SumExpressionList).Terms).ToImmutableList())
         : ExpressionFactory.Add(this, expression)
            );
 }
 public SumExpressionList Subtract(AlgebraExpression expression)
 {
     return(expression is SumExpressionList
         ? ExpressionFactory.Sum(this.Terms.Concat((expression as SumExpressionList).Terms.Select(ExpressionFactory.Negate)).ToImmutableList())
         : ExpressionFactory.Subtract(this, expression)
            );
 }
Exemplo n.º 5
0
        public IntegralExpression(AlgebraExpression expression, SymbolExpression respectTo, AlgebraExpression from, AlgebraExpression to)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (respectTo == null)
            {
                throw new ArgumentNullException(nameof(respectTo));
            }

            if (from != null && to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }

            if (from == null && to != null)
            {
                throw new ArgumentNullException(nameof(from));
            }

            this.Expression = expression;
            this.RespectTo  = respectTo;
            this.From       = from;
            this.To         = to;
        }
Exemplo n.º 6
0
 public ProductExpressionList Divide(AlgebraExpression expression)
 {
     return(expression is ProductExpressionList
         ? ExpressionFactory.Product(this, ExpressionFactory.Reciprocal(expression))
         : ExpressionFactory.Divide(this, expression)
            );
 }
Exemplo n.º 7
0
        public AbsoluteValueExpression(AlgebraExpression expression)
        {
            if (expression == null)
                throw new ArgumentNullException(nameof(expression));

            this.Expression = expression;
        }
Exemplo n.º 8
0
 public override AlgebraExpression Substitute(SymbolExpression variable, AlgebraExpression replacement)
 {
     return(this
            .WithBase(this.Base.Substitute(variable, replacement))
            .WithExponent(this.Exponent.Substitute(variable, replacement))
            );
 }
 public override AlgebraExpression Substitute(SymbolExpression variable, AlgebraExpression replacement)
 {
     return(this.WithTerms(
                this.Terms
                .Select(t => t == variable ? replacement : variable)
                .Select(t => t.Substitute(variable, replacement))
                .ToImmutableList()
                ));
 }
Exemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool IsEquivalentTo(AlgebraExpression other)
        {
            if (this.Equals(other))
            {
                return(true);
            }

            return(this.Simplify().Equals(other.Simplify()));
        }
Exemplo n.º 11
0
        public static IEnumerable <AlgebraExpression> DescendantsAndSelf(this AlgebraExpression expression)
        {
            yield return(expression);

            foreach (var descendant in expression.Descendants())
            {
                yield return(descendant);
            }
        }
Exemplo n.º 12
0
        public static PowerExpression AsPower(this AlgebraExpression expression)
        {
            if (expression is PowerExpression power)
            {
                return(power);
            }

            return(ExpressionFactory.Exponentiate(expression, ExpressionFactory.One));
        }
Exemplo n.º 13
0
        internal static ProductExpressionList AsProduct(this AlgebraExpression expression)
        {
            if (expression is ProductExpressionList product)
            {
                return(product);
            }

            return(Product(expression));
        }
Exemplo n.º 14
0
 public static IEnumerable <AlgebraExpression> Descendants(this AlgebraExpression expression)
 {
     foreach (var child in expression.Children())
     {
         foreach (var descendant in child.DescendantsAndSelf())
         {
             yield return(descendant);
         }
     }
 }
        public static AlgebraExpression TotalDerivative(this AlgebraExpression function, IReadOnlyCollection <SymbolExpression> parameters, SymbolExpression respectTo)
        {
            if (!parameters.Contains(respectTo))
            {
                throw new ArgumentException($"Parameter '{nameof(respectTo)}' must be one of the variables provided in parameter '{nameof(parameters)}'.", nameof(respectTo));
            }

            return(Sum(
                       parameters
                       .Select(p => function.PartialDerivative(p) * p.Differentiate(respectTo))
                       .ToImmutableList()
                       ));
        }
        public DifferentiationExpression(AlgebraExpression expression, SymbolExpression respectTo)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (respectTo == null)
            {
                throw new ArgumentNullException(nameof(respectTo));
            }

            this.Expression = expression;
            this.RespectTo  = respectTo;
        }
Exemplo n.º 17
0
        /// <summary>
        /// Simplifies an <see cref="AlgebraExpression"/> as much as it can.
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static AlgebraExpression Simplify(this AlgebraExpression expression, CancellationToken cancellationToken = default(CancellationToken))
        {
            AlgebraExpression simplified = expression;
            AlgebraExpression lastResult;

            do
            {
                lastResult = simplified;

                // simplify by kind
                simplified = SimplifySpecificKind(simplified, cancellationToken);
            } while (!lastResult.Equals(simplified));

            return(simplified);
        }
Exemplo n.º 18
0
        public PowerExpression(AlgebraExpression @base, AlgebraExpression exponent)
        {
            if (@base == null)
            {
                throw new ArgumentNullException(nameof(@base));
            }

            if (exponent == null)
            {
                throw new ArgumentNullException(nameof(exponent));
            }

            this.Base     = @base;
            this.Exponent = exponent;
        }
Exemplo n.º 19
0
        public LimitExpression(AlgebraExpression expression, SymbolExpression respectTo, AlgebraExpression to)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (respectTo == null)
            {
                throw new ArgumentNullException(nameof(respectTo));
            }

            if (to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }

            this.Expression = expression;
            this.RespectTo  = respectTo;
            this.To         = to;
        }
Exemplo n.º 20
0
        private static AlgebraExpression SimplifySpecificKind(AlgebraExpression expression, CancellationToken cancellationToken)
        {
            AlgebraExpression simplified = expression;
            AlgebraExpression lastResult;

            // find applicable simplifiers
            IReadOnlyCollection <object> simplifiers = GetSimplifiers(expression);

            do
            {
                lastResult = simplified;

                // query each simplifier
                foreach (var simplifier in simplifiers)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var original = simplified;

                    MethodInfo method = simplifier.GetType().GetRuntimeMethod(nameof(ISimplifier <AlgebraExpression> .Simplify), new[] { expression.GetType(), typeof(CancellationToken) });
                    simplified = method.Invoke(simplifier, new object[] { simplified, cancellationToken }) as AlgebraExpression;

                    // trace
                    if (!original.Equals(simplified))
                    {
                        Debug.WriteLine($"Simplified '{original}' to '{simplified}' using '{simplifier}'");
                    }

                    // stop if not the same kind of expression
                    if (lastResult.GetType() != simplified.GetType())
                    {
                        return(simplified);
                    }
                }
            } while (!lastResult.Equals(simplified));

            return(simplified);
        }
Exemplo n.º 21
0
 public SineFunctionExpression(AlgebraExpression argument)
     : base(PrimaryName, ImmutableList <AlgebraExpression> .Empty.Add(argument))
 {
 }
Exemplo n.º 22
0
 public static PowerExpression SquareRoot(AlgebraExpression expression)
 {
     return(Exponentiate(expression, Divide(NumberExpression.One, Constant(2))));
 }
Exemplo n.º 23
0
 public static PowerExpression Square(AlgebraExpression expression)
 {
     return(Exponentiate(expression, Constant(2)));
 }
Exemplo n.º 24
0
 public static PowerExpression Root(AlgebraExpression @base, AlgebraExpression exponent)
 {
     return(Exponentiate(@base, Divide(NumberExpression.One, exponent)));
 }
Exemplo n.º 25
0
 public static PowerExpression Exponentiate(AlgebraExpression @base, AlgebraExpression exponent)
 {
     return(new PowerExpression(@base, exponent));
 }
Exemplo n.º 26
0
 public PowerExpression WithBase(AlgebraExpression newBase)
 {
     return(ExpressionFactory.Exponentiate(newBase, this.Exponent));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Creates a sine function invocation expression.
 /// </summary>
 public static SineFunctionExpression Sine(AlgebraExpression argument)
 {
     return(Invoke(WellKnownFunctionNames.Sine, argument) as SineFunctionExpression);
 }
Exemplo n.º 28
0
 /// <summary>
 /// Replaces every occurrences of <paramref name="subject"/> to <paramref name="replacement"/>.
 /// </summary>
 /// <param name="variable"></param>
 /// <param name="replacement"></param>
 /// <returns></returns>
 public abstract AlgebraExpression Substitute(SymbolExpression variable, AlgebraExpression replacement);
Exemplo n.º 29
0
 public PowerExpression WithExponent(AlgebraExpression newExponent)
 {
     return(ExpressionFactory.Exponentiate(this.Base, newExponent));
 }
Exemplo n.º 30
0
 public static bool DependsUpon(this AlgebraExpression expression, SymbolExpression respectTo)
 {
     return(expression.DescendantsAndSelf().Contains(respectTo));
 }