Example #1
0
        /// <summary>
        /// Expand a multiplication expression.
        /// </summary>
        /// <param name="f"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        private static Expression ExpandMultiply(Expression f, Expression x)
        {
            // If the denominator is multiplication, expand partial fractions.
            if (!ReferenceEquals(x, null))
            {
                Expression d = Product.Denominator(f).Factor(x);
                if (d is Product)
                {
                    return(ExpandPartialFractions(Product.Numerator(f), (Product)d, x));
                }
            }

            // If f contains an add expression, distribute it.
            if (Product.TermsOf(f).Any(i => i is Sum))
            {
                Expression e = 1;
                foreach (Expression i in Product.TermsOf(f))
                {
                    e = Distribute(i.Expand(x), e);
                }
                return(e);
            }

            return(f);
        }
Example #2
0
        protected override PrettyString VisitProduct(Product M)
        {
            Expression N = Product.Numerator(M);
            Expression D = Product.Denominator(M);

            bool negative = false;

            if (N is Product && IsNegative(N))
            {
                negative = !negative;
                N        = -N;
            }
            if (D is Product && IsNegative(D))
            {
                negative = !negative;
                D        = -D;
            }

            if (!D.Equals(1))
            {
                return(PrettyString.ConcatColumns(negative ? "- " : "", VisitDivide(N, D)));
            }
            else if (N is Product)
            {
                return(PrettyString.ConcatColumns(negative ? "-" : "", UnSplit(Product.TermsOf(N), "*")));
            }
            else
            {
                return(PrettyString.ConcatColumns(negative ? "-" : "", Visit(N)));
            }
        }
Example #3
0
        protected override string VisitProduct(Product M)
        {
            int pr = Parser.Precedence(Operator.Multiply);

            Expression N     = Product.Numerator(M);
            string     minus = "";

            if (IsNegative(N))
            {
                minus = "-";
                N     = -N;
            }

            string n = String.Join(" ", Product.TermsOf(N).Select(i => Visit(i, pr)));
            string d = String.Join(" ", Product.TermsOf(Product.Denominator(M)).Select(i => Visit(i, pr)));

            if (d != "1")
            {
                return(minus + Frac(n, d));
            }
            else
            {
                return(minus + n);
            }
        }