Quantcast
Channel: MaplePrimes - Questions and Posts tagged with solve
Viewing all articles
Browse latest Browse all 1080

on obtaining solutions of equations as list

$
0
0

This is a programming question.

The goal is to solve an equation such as eq:=x^2+2*x-1=0; and obtain the solution as list with x= on each solution, like this

                       sol := [x = sqrt(2) - 1, x = -1 - sqrt(2)]

The command  to start with is sol:=solve(eq,x) which gives 

                       sol := sqrt(2) - 1, -1 - sqrt(2)

But to have x= show up, the command is modifed to be sol:=solve(eq,{x}) by adding {} around the variable to solve for, and now Maple gives 

                       sol := {x = sqrt(2) - 1}, {x = -1 - sqrt(2)}

Which is not yet the goal.. Changing the command to sol:=[solve(eq,{x})]  gives 

                       sol := [{x = sqrt(2) - 1}, {x = -1 - sqrt(2)}]

Which is still not the goal. Changing the command to sol:=solve(eq,[x])  gets closer to the goal.  it gives

                      sol := [[x = sqrt(2) - 1], [x = -1 - sqrt(2)]]

To remove the extra pair [ ] the list is Flattened like this

eq:=x^2+2*x-1=0;
sol:=solve(eq,[x]);
sol:=ListTools:-Flatten(sol)

Which gives me what I want, which is one list (not list of lists), and with x= in there

                             sol := [x = sqrt(2) - 1, x = -1 - sqrt(2)]

Is there a better way to obtain the above form of result than what I have above?

 


Viewing all articles
Browse latest Browse all 1080

Trending Articles