# How to transform an expression using algebraical instead of pattern rules [duplicate]

I would like to transform rules algebraically. A very simple example would be: -

k^2 - 2 k x + x^2 /. {2*k -> 1}


This transforms to: -

$$k^2-2 k x+x^2$$

However, I would like it to return something more like: -

$$\frac{1}{4}- x+x^2$$

In fact, I am more interested in solving quadratic, cubic and quartic polynomials. For example, I am able to set the discriminant of a quadratic to zero as a rule since the solution of a quadratic is already in an expanded form: -

quadratic = a*x^2 + b*x + c


This yields the correct results: -

$$\left\{\left\{x\to -\frac{b}{2 a}\right\},\left\{x\to -\frac{b}{2 a}\right\}\right\}$$

To achieve something similar for the cubic one must expand the solution while also transforming the discriminant.

cubic := a*x^3 + b*x^2 + c*x + d;
ExpandAll[Solve[cubic == 0 , x]] /. { Expand[-27*a^2*Discriminant[cubic, x]] -> 0 }


This process requires some forward thinking and doesn't easily apply to a quartic.

I am hoping Mathematica has some built in transformation rules for substituting expressions algebraically (obviously assuming certain criteria, i.e field/ring etc..) Does anyone have any suggestions?

• For the first part you can just do k^2 - 2 k x + x^2 /. {k -> 1/2}. – b.gates.you.know.what Feb 20 '13 at 17:19
• @b.gatessucks Yes, that's obvious. The point is that requires one to solve for k in the rule before using it. I am looking for an automated way of making these transformations. – Blair Azzopardi Feb 20 '13 at 17:23
• I think I am also not getting it yet either, but maybe something like: k^2-2 k x+x^2/.Evaluate[Solve[2k==1,k]][] ? – chuy Feb 20 '13 at 17:31
• Possible duplicate: mathematica.stackexchange.com/q/3463/5 – rm -rf Feb 20 '13 at 17:41
• or rather something like: Simplify[Solve[cubic == 0, x], Discriminant[cubic, x] == 0] ? (not sure if that is what you wanted) – Pinguin Dirk Feb 20 '13 at 17:49

I understand you want to transform the solution to an equation by imposing that it's solutions also satisfy another equation (discriminant = 0).

This is for the quadratic case : all output are equivalent, though the first one is the most familiar.

Solve[{quadratic == 0, Discriminant[quadratic, x] == 0}, {x, c}]
(* {{x -> -(b/(2 a)), c -> b^2/(4 a)}} *)