I am trying to evaluate multiple independent expressions with common parts. I would like Mathematica to somehow give me this common parts. It's probably not so clear, so let me give you an example.

Let's say I want to compute both of those expressions:

x0 = (-b + Sqrt[b²-4ac])/(2a)
x1 = (-b - Sqrt[b²-4ac])/(2a)

a, b and c are three scalars but I don't know their values yet.

What I would like is Mathematica telling me to define:

delta = Sqrt[b²-4ac]

and then I can get:

x0 = (-b + delta)/(2a)
x1 = (-b - delta)/(2a)

Of course this example is easy and can be done manually just by looking at it. But the case I am trying to solve is much more complex in size and cannot be easily done manually.

Here is a concrete example:

t = (-a02 a11 b0+a01 a12 b0+a01 a02 b1-a00 a12 b1-a01^2 b2+a00 a11 b2)/
      (a02^2 a11-2 a01 a02 a12+a01^2 a22+a00 (a12^2-a11 a22))
u = (-a12^2 b0+a11 a22 b0+a02 a12 b1-a01 a22 b1-a02 a11 b2+a01 a12 b2)/
      (a02^2 a11-2 a01 a02 a12+a01^2 a22+a00 (a12^2-a11 a22))
v = (a02 a12 b0-a01 a22 b0-a02^2 b1+a00 a22 b1+a01 a02 b2-a00 a12 b2)/
      (a02^2 a11-2 a01 a02 a12+a00 a12^2+a01^2 a22-a00 a11 a22)

There are clearly some common parts here and there (of course the denominator). I'd like Mathematica to tell me to declare a few intermediate (smaller) expressions to compute the final t,u and v.

Even better would be if it could for example detect that the denominator is the determinant of a 3x3 matrix and tell me that. But maybe this is too difficult. :-)

  • $\begingroup$ Welcome to Mathematica.SE, Please edit your question to improve it as now is hard to understand. Particularly, include a minimum example of the code you are working on that shows the problem and an example of the desired output. $\endgroup$ – rhermans Sep 17 '14 at 13:08
  • $\begingroup$ Thanks @Sektor for the edit. I'm still failing to understand what is the original function from which to get the parts. If its a polynomial, then the OP may use Coefficient or CoefficientList. $\endgroup$ – rhermans Sep 17 '14 at 13:16
  • $\begingroup$ Something like this,for starters candidates = Select[Intersection[Level[x0, Infinity], Level[x1, Infinity]], Depth@# > 3 &]. But the important thing (I suppose) is the cost of the calc. Impossible to foresee using algebra only $\endgroup$ – Dr. belisarius Sep 17 '14 at 13:20
  • 2
    $\begingroup$ Also, take a look at Experimental`OptimizeExpression[{x0, x1}] $\endgroup$ – Dr. belisarius Sep 17 '14 at 13:28
  • 1
    $\begingroup$ I took @bel's suggestion to an extreme here. $\endgroup$ – Michael E2 Sep 17 '14 at 13:58
x0 = (-b + Sqrt[b^2 - 4 ac])/(2 a);
x1 = (-b - Sqrt[b^2 - 4 ac])/(2 a);

delta = Sqrt[b^2 - 4 ac];

expr = {x0, x1} /. delta -> HoldForm[delta]

enter image description here

expr // ReleaseHold

enter image description here

|improve this answer|||||
  • 2
    $\begingroup$ Replacing is easy when I know the common parts. But I want Mathematica to tell me that I should define a variable equals to Sqrt[b^2-4ac]. $\endgroup$ – Romain Sep 17 '14 at 13:58
  • $\begingroup$ Quote from @belisarius comment:" ... Impossible to foresee using algebra only." Using his comment you can see the common parts but then, again, you have to make your choice. $\endgroup$ – eldo Sep 17 '14 at 14:04

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