9

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. :-)

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Romain
  • 91
  • 2

1 Answers1

3
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

eldo
  • 67,911
  • 5
  • 60
  • 168
  • 3
    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]. – Romain Sep 17 '14 at 13:58
  • 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. – eldo Sep 17 '14 at 14:04