2
$\begingroup$

Say I have a list of expressions involving many different variables. I would like to see how many of the expressions I can set equal to zero thereby solving for some of the variables in terms of the rest.

For example, say I have the following array,

A={a^2-b, b-a^2+1}

and I would like Mathematica to try to set as many of the components of A to zero as possible. Solve[A==0, {a,b}] will give an empty set since obviously both components of A cannot be zero at the same time. I would like Mathematica to solve the first component b->a^2 and ignore the second.

How can I tell it to ignore those expressions that lead to a contradiction and keep going down the list of expressions, setting them equal to zero if possible?

$\endgroup$
2
$\begingroup$

One can Fold Solve into the equations and thread the solutions generated at each step. When there is no solution, Solve returns {} and the post-processing replacement rule {} -> {#} simply replaces the no-solution {} with the current solution. (Solve automatically choose which variable to solve for. If that is not acceptable, then the question needs to specify how the variables are to be chosen.)

annihilateIfPossible[exprs_] := 
 Fold[Function[{sols, expr}, 
   Sequence @@ (Solve[expr == 0 /. #] /. {s__Rule} :> 
          Join[#, {s}] /. {} -> {#}) & /@ sols],
  {{}},
  exprs];

annihilateIfPossible[{a^2 - b, b - a^2 + 1}]
(*  {{b -> a^2}}  *)

annihilateIfPossible[{a^2 - b, b - a^2 + 1, c - b^2 + a}]
(*  {{b -> a^2, c -> a (-1 + a^3)}}  *)

annihilateIfPossible[{a^2 - b^2, b - a^2 + 1, c - b^2 + a}]
(*
  {{b -> -a, a -> 1/2 (-1 - Sqrt[5]), c -> 1/2 ( 1 + Sqrt[5] + 2 a^2)},
   {b -> -a, a -> 1/2 (-1 + Sqrt[5]), c -> 1/2 ( 1 - Sqrt[5] + 2 a^2)},
   {b ->  a, a -> 1/2 ( 1 - Sqrt[5]), c -> 1/2 (-1 + Sqrt[5] + 2 a^2)},
   {b ->  a, a -> 1/2 ( 1 + Sqrt[5]), c -> 1/2 (-1 - Sqrt[5] + 2 a^2)}}
*)
|improve this answer|||||
$\endgroup$
  • $\begingroup$ You, sir, are a gentleman and a scholar. $\endgroup$ – Orn Arnaldsson Mar 16 '16 at 3:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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