2
$\begingroup$

I want to flatten the output of solve into a an un-nested list. For instance,

Solve[y[x] == x^2 + c, c]

yields

{{c -> -x^2 + y[x]}}

which I may reduce using Flatten[] to

eq=Flatten[%]
{c -> -x^2 + y[x]}

However, this is still not a list.

If I call List@@ on it, I get

List @@ Flatten[eq]
{c -> -x^2 + y[x]}

So the solution is not fully flattened.

Currently, I have to do

List @@ Flatten[eq][[1]]
{c, -x^2 + y[x]}

Ultimately I would like to be able to directly take answers from Solve and use functions like List @@ and Equals @@ on them to reassign the -> operator, rather than the programmatic way I currently do it which is using list references.

Is there a more elegant way to swap the [rule] operator for the Equal == or other operators?

edit:

From the comments -

Apply[#,1] or @@@ works, but is somewhat kludgy, as it does not fully flatten the list. For instance:

List @@@ Flatten[eq]
{{c, -x^2 + y[x]}}

My expected result either a list of depth 1 - in the case of flattening into one list, eg

{c, -x^2 + y[x]}

or in the case of using Equal or another assignment

c == -x + y[x]
$\endgroup$
  • 1
    $\begingroup$ Like Equal @@@ Flatten[{{c -> -x^2 + y[x]}}]? $\endgroup$ – Kuba Sep 6 '16 at 13:36
  • 1
    $\begingroup$ It is Apply[#, {1}] so almost the second :) $\endgroup$ – Kuba Sep 6 '16 at 13:39
  • 1
    $\begingroup$ Isn't my first comment giving you the final solution? $\endgroup$ – Kuba Sep 6 '16 at 13:41
  • 2
    $\begingroup$ Use First but what if there are two solutions? and please include an exact expected result in the question., $\endgroup$ – Kuba Sep 6 '16 at 13:50
  • 1
    $\begingroup$ General approach: Or @@ And @@@ Solve[y[x] == x^2 + c^2 && b^2 == c, {b, c}] /. Rule -> Equal. Your List answer relies on the equation having exactly one solution; a general approach substituting Rule -> List can't work on it, but a specialized one could, such as Flatten[eq /. Rule -> List]. $\endgroup$ – Michael E2 Sep 6 '16 at 14:02

Your Answer

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

Browse other questions tagged or ask your own question.