Ask Your Question
0

solve, indexes of solution variables are incrementing

asked 5 years ago

ortollj gravatar image

updated 5 years ago

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

Preview: (hide)

Comments

Finally, I ended up thinking that it was embarrassing !

ortollj gravatar imageortollj ( 5 years ago )

2 Answers

Sort by » oldest newest most voted
1

answered 5 years ago

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.

Preview: (hide)
link

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 ( 5 years ago )

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 ( 5 years ago )

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 ( 5 years ago )

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 ( 5 years ago )

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 ( 5 years ago )
0

answered 5 years ago

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)
Preview: (hide)
link

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: 5 years ago

Seen: 366 times

Last updated: May 18 '19