Ask Your Question

# How to get access of a single solution from a bunch of solutions

I do not use the thing for longer time, so I had a question.

If I say

a,b,c,d,e,x = var('a b c d e x')
f(x)=a*x^4+b*x^3+c*x^2+d*x+e
f1(x)=diff(f(x),x)
f2(x)=diff(f(x),x,2)
sols = solve([f(0)==-2, f(2)==0,  f1(4)==0, f(4)==8,  f2(2)==0], a,b,c,d,e)
print(sols)


I get

 [
[a == (3/16), b == (-41/16), c == (87/8), d == -12, e == -2]
]


How can I have these single value separately?
That means: I want to get an output f(x) = 3/16*x^4 -41/16*x^3 +87/8*x^2 -12*x -2
(which I can copy and paste).

edit retag close merge delete

## 1 Answer

Sort by » oldest newest most voted

The first trick is to use the solution_dict parameter to get the solutions as Python dictionaries, instead of lists of expressions:

sage: relations = [f(0) == -2, f(2) == 0,  f1(4) == 0, f(4) == 8,  f2(2) == 0]
sage: sols = solve(relations, a, b, c, d, e, solution_dict=True)
sage: sols
[{a: 3/16, b: -41/16, c: 87/8, d: -12, e: -2}]


As you can see, sols is a list, with a single element, that is a dictionary:

sage: sols
{a: 3/16, b: -41/16, c: 87/8, d: -12, e: -2}
sage: sols[a]
3/16
sage: sols[b]
-41/16


Now, you can use this dictionary to substitute the symbols a, b, c, d, e with their values:

sage: f.substitute(sols)
x |--> 3/16*x^4 - 41/16*x^3 + 87/8*x^2 - 12*x - 2

more

## Your Answer

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

Add Answer

## Stats

Asked: 2020-10-21 14:12:04 +0200

Seen: 75 times

Last updated: Oct 21 '20