Ask Your Question
0

Finding solutions as numerical values rather than equations

asked 2015-09-03 16:29:52 +0100

DaveG gravatar image

The output of solve() returns equations. What's the easiest way to "unwrap" these to get numerical values? For instance, solve(x^2-4==0,x) returns [x==-2, x==2]. I'd like to define the associated list [-2,2].

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-09-03 19:45:28 +0100

jaia gravatar image

I would use:

sol=solve(x^2-4==0,x)
[eq.rhs() for eq in sol]

The .rhs() method extracts the right-hand side of an equation.

edit flag offensive delete link more
1

answered 2015-09-03 17:15:19 +0100

kcrisman gravatar image

One of many, MANY possible solutions to this:

sage: solve(x^2-4==0,x, solution_dict=True)
[{x: -2}, {x: 2}]
sage: [ sol.values()[0] for sol in solve(x^2-4==0,x, solution_dict=True) ]
[-2, 2]

There isn't really an easy way to do this without programming, because the solution types can vary a lot. In the one-variable case this should be one reasonable way, though.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2015-09-03 16:29:52 +0100

Seen: 427 times

Last updated: Sep 03 '15