I am using MMA do do some algebra and I need to do some simplification. For example, I have the following expression $$ x^2 \left( \frac{6 \left(2 c_1 x^6+c_2\right)}{x^4} \right)-x \left( 4 c_1 x^3-\frac{2 c_2}{x^3}\right)-8\left(c_1 x^4+\frac{c_2}{x^2}\right ) $$ Can I get intermediate steps to reach the final answer? For example, I want to factor out $c_1$ and $c_2$ and write the expression as $c_1 (x\dots) + c_2 (x\dots)$ but the terms $x\dots $ need not be evaluated. I can to the calculation as it is simple, but if I could do this from MMA, it would greatly save my time.

I always have some terms that could be factored out. It could be constant $c_k$ or polynomial $x^n$ or trig functions and there could be multiple terms but they are always unique and do not conflict with each other.

  • $\begingroup$ If you write this expression in Mma notation, and then apply Simplify to it you get 0. Next time try that yourself. $\endgroup$ – Alexei Boulbitch Feb 5 '16 at 8:58
  • $\begingroup$ @AlexeiBoulbitch that gives the final answer, I don't want the final answer. I want the intermediate step, collect the variables by factoring $c_1$ and $c_2$ so that I can copy steps from mathematica instead of writing the entire step. $\endgroup$ – Santosh Linkha Feb 5 '16 at 9:36
  • 3
    $\begingroup$ Requests for Mathematica to show steps come up often, and typically the answer is negative; see e.g. Get a "step-by-step" evaluation in Mathematica. $\endgroup$ – MarcoB Feb 5 '16 at 11:55
  • $\begingroup$ Try this stuff on wolframalpha.com $\endgroup$ – user9660 Feb 5 '16 at 18:52
  • $\begingroup$ If I understand OP correctly, I don't think you're asking (necessarily) to trace out the intermediate steps that Mathematica actually goes through during the calculation, but rather to find a way to break the calculation into steps. As previous commenters said, the former seems impossible, but the latter is definitely doable, if you're willing to put in a lot of work. (If you agree with what I said, I would suggest editing the question to reflect this.) After all, you don't need to use any of Mathematica's built-in algebra functions if you'd prefer to write your own. $\endgroup$ – jjc385 Feb 5 '16 at 20:32

You have your expression

expression = 
 x^2 (6 (2 c1 x^6 + c2)/x^4) - x (4 c1 x^3 - 2 c2/x^3) - 
  8 (c1 x^4 + c2/x^2)
(* -x (-((2 c2)/x^3) + 4 c1 x^3) - 8 (c2/x^2 + c1 x^4) + (
 6 (c2 + 2 c1 x^6))/x^2 *)

for now it hasn't been evaluated to zero yet, but if you try most anything it will do so:

Series[expression, {c1, 0, 2}]
Coefficient[expression, c2]
(* 0 *)
(* O[c1]^3 *)
(* 0 *)

One way to get what you are looking for is to change the Head of your expression from Plus to List,

List @@ expression
(* {-x (-((2 c2)/x^3) + 4 c1 x^3), -8 (c2/x^2 + c1 x^4), (
 6 (c2 + 2 c1 x^6))/x^2} *)

Now you can get the coefficients for c1 and c2 for each of the terms in the sum,

Coefficient[#, c1] & /@ (List @@ expression)
(* {-4 x^4, -8 x^4, 12 x^4} *)

Coefficient[#, c2] & /@ (List @@ expression)
(* {2/x^2, -(8/x^2), 6/x^2} *)

I don't know of a general way to do what you want, but this may work with some input from you.

| improve this answer | |
  • $\begingroup$ works for me with little bit of tweaks!! $\endgroup$ – Santosh Linkha Feb 5 '16 at 16:12

I generalize Jason B's answer to group by like terms in any number of coefficients, as well as in cases where you have polynomials multiplying one another:

It sounds like you mostly want Plus to keep from evaluating. You can do this by replacing Plus with plus, where plus is a Flat function (since we might as well keep associativity of addition)

SetAttributes[plus, Flat];

expr = (a + b c) (a - b c);
expr /. Plus->plus

plus[a, -b c] plus[a, b c]

Now make plus distrbutive (including over itself -- i.e., when it appears in an integer power) :

sep = % //. {  a_ b_plus :> (a*# &) /@ b,  Power[a_plus, n_ /; n > 0 && IntegerQ@n] :> (Power[a, n - 1]*# &) /@ a  }

plus[a^2, -a b c, a b c, -b^2 c^2]

Now say you want to sort the output based on powers of a, b, and c. To this end, I define a getPower function :

(* operator form *)
getPower[var_][expr_] := getPower[expr, var]

(* Thread over a list of variables *)
getPower[expr_, varList_List] := getPower[expr, #] & /@ varList

getPower[expr_, var_] /; FreeQ[expr, var] := 0
getPower[expr_Times, var_] := Plus @@ getPower[var] /@ List @@ expr
getPower[(expr_)^n_, var_] := n getPower[expr, var]
getPower[var_, var_] := 1

(Note that getPower requires expr to be a monomial in the supplied variables; otherwise the output will contain getPower. Furthermore, note that getPower[ 1/(a+x), x] returns 0, but getPower[ 1/x, x] returns -1. )

Then you can use GatherBy to sort the output by coefficient:

GatherBy[List @@ sep, getPower@{a, b, c}]

{{a^2}, {-a b c, a b c}, {-b^2 c^2}}

It might be desirable to convert the sublists back to plus :


{plus[a^2], plus[-a b c, a b c], plus[-b^2 c^2]}

You can imagine generalizing the above to pull the coefficients out front.

Note that it may or may not be useful to have given plus attribute OneIdentity from the start, depending on how you'd like to generalize.

| improve this answer | |
  • $\begingroup$ If I have some more time in the next few days, I might find a way to pull out functions rather than just coefficients. $\endgroup$ – jjc385 Feb 5 '16 at 16:14
  • $\begingroup$ thanks, I'll wait for it. Please notify me if you do. $\endgroup$ – Santosh Linkha Feb 5 '16 at 16:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.