# solve, indexes of solution variables are incrementing

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 close merge delete

Finally, I ended up thinking that it was embarrassing !

Sort by » oldest newest most voted

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.

more

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))


I tried this (litle heavy !) without success:

# solution variables list (solution internal variables ri)
solVarList=[]
for eq in S :
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)
show(varsol)

show(Unum.substitute(varsol=0,varsol=1))
#show(Unum.substitute(vsol=1,vsol=0))


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

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,(varsol)=1)


That I'd write

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


RTFM...

I disagree Emmanuel the error is still present with show(Unum.substitute([varsol)==0, varsol==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

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 :
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)
show(varsol)

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

show(Unum1)

more