Ask Your Question
0

solve, indexes of solution variables are incrementing

asked 2019-05-05 16:44:32 +0100

ortollj gravatar image

updated 2019-05-05 16:47:31 +0100

Hi

SageMath 8.6 notebook with W10 and Edge.

why indexes of solution variables are incrementing each time I execute the cell, despite I put a forget() at the beginning? (on the cocalc site, you must add or delete a space and then click on run to see the incrementation of the variables from the solution of the vector U),first r_1,r_2 then r_3,r_4 ... etc. if I reset the kernel then it start again at r_1,r_2 . On Cocalc : Solve matrix N*U=M

it's not embarrassing, but it puzzles me ;-)

edit retag flag offensive close merge delete

Comments

Finally, I ended up thinking that it was embarrassing !

ortollj gravatar imageortollj ( 2019-05-16 16:27:03 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-05-06 00:22:20 +0100

Emmanuel Charpentier gravatar image

Those variables are generated by Maxima, which has non problem generating new symbols for (e. g.) integration constants or integers denoting the multiplicity of solutions. Those variables denoting arbitrary quantities, there is no reason for them to share names...

One might note that other solvers (e. g. Sympy) may be less wasteful.

A workaround is to collect the names of the variables appearing in the solution(s) and declare them as vars if they have to be used outside the call context. Or substitute them with more significant names.

edit flag offensive delete link more

Comments

Sorry Emmanuel, but something I do not understand is how can I collect the names of the variables appearing in the solution(s) ? (clic on the link in the first post)

var('r1 r2 ')
show(Unum.substitute(r1=0,r2=1))
show(Unum.substitute(r1=1,r2=0))

or

var('r3 r4 ')
show(Unum.substitute(r3=0,r4=1))
show(Unum.substitute(r3=1,r4=0))
ortollj gravatar imageortollj ( 2019-05-16 15:31:59 +0100 )edit

I tried this (litle heavy !) without success:

# solution variables list (solution internal variables ri)
solVarList=[]
for eq in S[0] :
    for v in eq.rhs().variables():
        #print(v)
        solVarList.append(str(v))
solVarListIndice=[]
for v in solVarList :
    #print v.replace('r','')
    solVarListIndice.append(int(v.replace('r','')))
show("solVarListIndice : ",solVarListIndice)
show(" minimum : ",min(solVarListIndice)," maximum : ",max(solVarListIndice))
vsol=[]
for i in [min(solVarListIndice)..max(solVarListIndice)] :
        vsol.append("r%d"%(i))
varsol=var(vsol)
show(varsol[0])
show(varsol[1])

show(Unum.substitute(varsol[0]=0,varsol[1]=1))
#show(Unum.substitute(vsol[0]=1,vsol[1]=0))
ortollj gravatar imageortollj ( 2019-05-17 10:08:31 +0100 )edit

look at the third cell on Cocalc (link above in the first message), I got the message: show(Unum.substitute(varsol[Integer(0)]=Integer(0),varsolL[Integer(1)]=Integer(1))) SyntaxError: keyword can't be an expression

ortollj gravatar imageortollj ( 2019-05-17 10:10:06 +0100 )edit

No time to re-run your (heavy) program. But a cursory look makes me think that the problem might be with

show(Unum.substitute((varsol[0])=0,(varsol[1])=1)

That I'd write

show(Unum.substitute([varsol[0])==0, varsol[1]==1])

RTFM...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-05-18 08:17:21 +0100 )edit

I disagree Emmanuel the error is still present with show(Unum.substitute([varsol[0])==0, varsol[1]==1])

the solution of this problem was given by tmonteil here: keyword can't be an expression

to verify you can run the cell 2 and 3 of the cocalc link given in the first post

ortollj gravatar imageortollj ( 2019-05-18 08:38:35 +0100 )edit
0

answered 2019-05-18 08:43:23 +0100

ortollj gravatar image

a (litle heavy !, if someone has a simpler solution, I'm interested) workaround :

# solution variables list (solution internal variables ri)
solVarList=[]
for eq in S[0] :
    for v in eq.rhs().variables():
        #print(v)
        solVarList.append(str(v))
solVarListIndice=[]
for v in solVarList :
    #print v.replace('r','')
    solVarListIndice.append(int(v.replace('r','')))
show("solVarListIndice : ",solVarListIndice)
show(" minimum : ",min(solVarListIndice)," maximum : ",max(solVarListIndice))
vsol=[]
for i in [min(solVarListIndice)..max(solVarListIndice)] :
        vsol.append(SR("r%d"%(i)))
#varsol=var(vsol)
show(varsol[0])
show(varsol[1])

Unum0=(Unum.substitute({varsol[0]:1}))
Unum1=(Unum0.substitute({varsol[1]:0}))

show(Unum1)
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

1 follower

Stats

Asked: 2019-05-05 16:44:32 +0100

Seen: 329 times

Last updated: May 18 '19