Ask Your Question
1

cannot evaluate symbolic expression numerically

asked 2012-09-02 04:39:48 +0100

ozik gravatar image

updated 2012-09-04 15:27:57 +0100

kcrisman gravatar image

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[1]

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

Thanks in advance!

edit retag flag offensive close merge delete

Comments

Can you reformat your code properly?

Jennifer Dylan gravatar imageJennifer Dylan ( 2012-09-02 14:32:08 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2012-09-02 15:55:57 +0100

DSM gravatar image

updated 2012-09-02 15:56:10 +0100

solve is returning a list (technically a Sequence). sols[0] 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[0].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[0][t].n() 
4.70733338158850 - 5.70444926240249e-18*I
edit flag offensive delete link more

Comments

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!

ozik gravatar imageozik ( 2012-09-04 18:08:32 +0100 )edit

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: 2012-09-02 04:39:48 +0100

Seen: 4,940 times

Last updated: Sep 04 '12