Ask Your Question

Revision history [back]

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

sage: sols = solve([f(0)==-2, f(2)==0,  f1(4)==0, f(4)==8,  f2(2)==0], 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[0]    
{a: 3/16, b: -41/16, c: 87/8, d: -12, e: -2}
sage: sols[0][a]                                                                                                                                                                                             
3/16
sage: sols[0][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[0])                                                                                                                                                                                  
x |--> 3/16*x^4 - 41/16*x^3 + 87/8*x^2 - 12*x - 2

The first trick is to use the solution_dict parameter to get the solutions 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([f(0)==-2, f(2)==0,  f1(4)==0, f(4)==8,  f2(2)==0], a,b,c,d,e, solution_dict=True)                                                                                                        
solve(relations, a, b, c, d, e, solution_dict=True)
sage: sols                                                                                                                                                                                                   
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[0]    
sols[0]
{a: 3/16, b: -41/16, c: 87/8, d: -12, e: -2}
sage: sols[0][a]                                                                                                                                                                                             
sols[0][a]
3/16
sage: sols[0][b]                                                                                                                                                                                             
sols[0][b]
-41/16

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

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