# cannot evaluate symbolic expression numerically

I'm probably doing something wrong on a fundamental level, so: sorry for my ignorance. Yet i'd very much appreciate any suggestions, how to make this work:

I have me a function f, defined in somewhat lenghty way:

var('a,b,c,d')

i=matrix(SR,3,3, [1,0,0, 0,1,0, 0,0,1])

e_1=matrix(SR,3,3, [1,0,-1, 0,0,0, -1,0,1])
e_2=matrix(SR,3,3, [0,1,-1, 0,0,0, 0,-1,1])
e_3=matrix(SR,3,3, [0,0,0, 1,0,-1, -1,0,1])
e_4=matrix(SR,3,3, [0,0,0, 0,1,-1, 0,-1,1])

M=matrix(SR,3,3, [a,b,1-a-b, c,d,1-c-d, 1-a-c, 1-b-d,a+b+c+d-1])

M_1=e_1*M+M*e_1
M_2=e_2*M+M*e_2
M_3=e_3*M+M*e_3
M_4=e_4*M+M*e_4
A=matrix(SR,4,4, [ M_1[0,0], M_1[0,1],M_1[1,0],M_1[1,1], M_2[0,0], M_2[0,1],M_2[1,0],M_2[1,1], M_3[0,0], M_3[0,1],M_3[1,0],M_3[1,1], M_4[0,0], M_4[0,1],M_4[1,0],M_4[1,1] ])

f(a,b,c,d)=A.determinant()


And then I want a solution to f==0, after giving some random arguments to it

var('k,l,m,n,t,y')

for i in range(5):
k=random(); n=random()
if k>n: y=k
else: y=n
l=random()*(1-y)
m=random()*(1-y)
if k+l+m+n>1:
sols=solve([f(k*t+1-t,l*t,m*t,n*t+1-t)==0], t);
sols


A wild string of numbers pops out. But how do i get to see how much is it? Changing to sols.n() gives "TypeError: cannot evaluate symbolic expression numerically".

edit retag close merge delete

Sort by » oldest newest most voted solve is returning a list (technically a Sequence). sols is a long expression which will look something like

 t == -1/153680390222338173222270*sqrt((42892173.. etc


You can't turn a relation like this into a number, which is why Sage is complaining. You could extract the rhs:

sage: sols.rhs().n()
0.901734601169342 - 1.74583945992054e-17*I


Or use solution_dict=True instead (my favourite), i.e.

sols=solve([f(k*t+1-t,l*t,m*t,n*t+1-t)==0], t, solution_dict=True);


and then use [different random numbers here]:

sage: sols[t].n()
4.70733338158850 - 5.70444926240249e-18*I

more

Thank you, just what I wanted! I've got another question; i'm not sure if I can ask it here: is there any way to tell sage, that i'm only interested in values of 't' variable in 'solve' running in [0;1] only. I want an answer to 'yes or no' question, if is there a solution for any t belonging to [0;1]. I'm asking, since I need the range if iteration to be more like (999) than (5) and thing get little slow. Thanks in advance for commenting!