Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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
Solve[quadratic == 0, x]  /. { Discriminant[quadratic, x] -> 0 }

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?

share|improve this question

marked as duplicate by Daniel Lichtblau, R. M. Feb 21 '13 at 0:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

For the first part you can just do k^2 - 2 k x + x^2 /. {k -> 1/2}. – b.gatessucks 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. – bsdz 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]][[1]] ? – chuy Feb 20 '13 at 17:31
Possible duplicate: – R. M. 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)}} *)

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

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

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