Exemple #1
0
        public TermExpression(IValue coefficient, VariablePowExpression[] variablePows, VariableExpression[] variables, params IExpression[] others)
        {
            Coefficient = coefficient ?? new NumberDecimal(1, 0);
            if (Coefficient.Equals(NumberDecimal.Zero))
            {
                return;
            }
            var variablesR = TermExpression.Multiply(variablePows, variables?.Select(a => (VariablePowExpression)a))?.ToList();
            var othersR    = new List <IExpression>();

            if (others != null)
            {
                var result = new TermExpression(others);
                Coefficient = Coefficient.Multiply(result.Coefficient);
                variablesR.AddRange(result.Variables);
                othersR.AddRange(result.Others);
            }

            Variables = Unify(variablesR);
            Others    = othersR.ToArray();
        }
Exemple #2
0
 public static TermExpression AddIfCombinedable(TermExpression a, TermExpression b)
 {
     if (a.Variables.Count() != b.Variables.Count())
     {
         return(null);
     }
     if (a.Others.Count() != 0 || b.Others.Count() != 0)
     {
         return(null);
     }
     for (int i = 0; i < a.Variables.Count(); i++)
     {
         if (a.Variables[i].Variable.Name != b.Variables[i].Variable.Name)
         {
             return(null);
         }
         if (!a.Variables[i].Exponent.Content.Equals(b.Variables[i].Exponent.Content))
         {
             return(null);
         }
     }
     return(new TermExpression(a.Coefficient.Add(b.Coefficient), a.Variables));
 }
Exemple #3
0
        public FormulaExpression Add(params TermExpression[] terms)
        {
            var termList = this.Terms.ToList();

            foreach (var term in terms)
            {
                if (term.Coefficient.Equals(NumberDecimal.Zero))
                {
                    continue;
                }
                for (int i = 0; i < termList.Count(); i++)
                {
                    var temp = TermExpression.AddIfCombinedable(termList[i], term);
                    if (temp != null)
                    {
                        termList[i] = temp;
                        goto BREAKER;
                    }
                }
                termList.Add(term);
                BREAKER :;
            }
            return(new FormulaExpression(Value, termList.ToArray(), Others));
        }
Exemple #4
0
        public TermExpression(params IExpression[] exs)
        {
            IValue coefficientR = NumberDecimal.One;
            var    variablesR   = new List <VariablePowExpression>();
            var    othersR      = new List <IExpression>();

            foreach (var ex in exs)
            {
                switch (ex)
                {
                case NumberExpression expression: coefficientR = coefficientR.Multiply(expression.Content); break;

                case VariablePowExpression expression: variablesR.Add(expression); break;

                case VariableExpression expression: variablesR.Add(expression); break;

                case TermExpression expression:
                {
                    coefficientR = coefficientR.Multiply(expression.Coefficient);
                    if (expression.Variables != null)
                    {
                        variablesR.AddRange(expression.Variables);
                    }
                    if (expression.Others != null)
                    {
                        othersR.AddRange(expression.Others);
                    }
                    break;
                }

                case OpMulExpression expression:
                {
                    var result = new TermExpression(expression.Left, expression.Right);
                    coefficientR = coefficientR.Multiply(result.Coefficient);
                    variablesR.AddRange(result.Variables);
                    othersR.AddRange(result.Others);
                    break;
                }

                case OpDivExpression expression:
                {
                    var result = new TermExpression(expression.Left, expression.Right.Power(NumberExpression.MinusOne));
                    coefficientR = coefficientR.Multiply(result.Coefficient);
                    variablesR.AddRange(result.Variables);
                    othersR.AddRange(result.Others);
                    break;
                }

                case FormulaExpression expression:
                {
                    var result = expression.GetSingleOrDefault();
                    if (result == null)
                    {
                        othersR.Add(ex); break;
                    }
                    var term = new TermExpression(result);
                    coefficientR = coefficientR.Multiply(term.Coefficient);
                    if (term.Variables != null)
                    {
                        variablesR.AddRange(term.Variables);
                    }
                    if (term.Others != null)
                    {
                        othersR.AddRange(term.Others);
                    }
                    break;
                }

                default: othersR.Add(ex); break;
                }
            }

            this.Coefficient = coefficientR;
            this.Variables   = Unify(variablesR.ToArray());
            this.Others      = othersR.ToArray();
        }
Exemple #5
0
 public TermExpression Multiply(TermExpression value)
 {
     return(new TermExpression(this, value));
 }