/// <summary> /// Expand a power expression. /// </summary> /// <param name="f"></param> /// <param name="x"></param> /// <returns></returns> private static Expression ExpandPower(Power f, Expression x) { // Get integral exponent of f. int n = Power.IntegralExponentOf(f); // If this is an an integral constant negative exponent, attempt to use partial fractions. if (n < 0 && !ReferenceEquals(x, null)) { Expression b = f.Left.Factor(x); if (n != -1) { b = Power.New(b, Math.Abs(n)); } return(ExpandPartialFractions(1, b, x)); } // If f is an add expression, expand it as if it were multiplication. if (n > 1 && f.Left is Sum) { Expression e = f.Left; for (int i = 1; i < n; ++i) { e = Distribute(f.Left, e); } return(e); } return(f); }
// Expand N(x)/D(x) using partial fractions. private static Expression ExpandPartialFractions(Expression N, Expression D, Expression x) { List <Expression> terms = new List <Expression>(); List <Variable> unknowns = new List <Variable>(); List <Expression> basis = new List <Expression>(); foreach (Expression i in Product.TermsOf(D)) { // Get the multiplicity of this basis term. Expression e = i; int n = Power.IntegralExponentOf(e); if (n != 1) { e = ((Power)i).Left; } // Convert to a polynomial. Polynomial Pi = Polynomial.New(e, x); // Add new terms for each multiplicity n. for (int j = 1; j <= n; ++j) { // Expression for the unknown numerator of this term. Expression unknown = 0; for (int k = 0; k < Pi.Degree; ++k) { Variable Ai = Variable.New("_A" + unknowns.Count.ToString()); unknown += Ai * (x ^ k); unknowns.Add(Ai); } terms.Add(Product.New(unknown, Power.New(e, -j))); } basis.Add(i); } // Equate the original expression with the decomposed expressions. D = Sum.New(terms.Select(j => (Expression)(D * j))).Expand(); Polynomial l = Polynomial.New(N, x); Polynomial r = Polynomial.New(D, x); // Equate terms of equal degree and solve for the unknowns. int degree = Math.Max(l.Degree, r.Degree); List <Equal> eqs = new List <Equal>(degree + 1); for (int i = 0; i <= degree; ++i) { eqs.Add(Equal.New(l[i], r[i])); } List <Arrow> A = eqs.Solve(unknowns); // Substitute the now knowns. return(Sum.New(terms.Select(i => i.Evaluate(A)))); }