Ask Your Question
0

Valid Python identifier

asked 2020-05-15 14:24:03 +0100

Cyrille gravatar image

updated 2020-05-16 09:38:06 +0100

For some days, I struggle with unicode. I haved followed the answer of my question "An error using unicode?" wich is to define

\pi(+TAB) = var('pi')
 x = var('x')

or (0I suppose)

\pi(+TAB), x = var('pi x')
 x = var('x')

but later, in my code I use \pi + Tab and I receive an error code The name "ϕ" is not a valid Python identifier (here it is "ϕ", because I have replaced pi by phi under the reasoning that pi was certainly a keyword in some packages). Is it one more time a problem of understanding ?

I have also tried

\pi(+TAB) = var('pii')
 x = var('x')

to try to by pass the difficulty.

Here is my code. It comes in some cells so I kept them.

%%html
<style>
body {
    font-family: "Palatino Linotype", cursive, sans-serif;
     background-color: rgb(252,251,251);
}
.prompt {
    font-family: "Palatino Linotype", cursive, sans-serif;
    font-size: 15px;
    min-width: 6em;
}
.input-prompt {
    color: rgb(108, 119, 188);
}
.output-prompt {
    color: rgb(20, 129, 106);
}
code, kbd, pre, samp, .CodeMirror {
    font-family:  "Palatino Linotype", cursive, sans-serif;
    font-size: 15px;
}

</style>

p, D, w_0, x, I = var('p, D, w_0, x, I')
β = var('beta')
ϕ = var('phi') 
w00=8
D1=6

% display latex

pl=line([(0,0), (10,10)], color="cyan",ticks=[[], []])
pl+=text("$W_2$",(-.5,10),color="black", fontsize='small')
pl+=text("$W_1$",(10,-.5),color="black", fontsize='small')
pl+=text("$Ligne$ $de$\n  $certitude$",(9,9),color="red", fontsize='small', rotation=45)
pl1=circle((w00,w00-D1), .075, fill=True, edgecolor='red', facecolor='red')
pl1+=line([(w00,0), (w00,w00-D1), (0,w00-D1)], linestyle="--")
pl1+=text("$W_0-D$",(-.75, w00-D1),color="black", fontsize='small')
pl1+=text("$W_0$",(w00,-.5),color="black", fontsize='small')
pl1+=text("$W_0$",(-.5, w00),color="black", fontsize='small')
pl1+=line([(w00,w00-D1), (w00,w00),(0,w00)], linestyle="--")
pl1+=circle((w00,w00), .075, fill=True, edgecolor='red', facecolor='red')
pl2=polygon([(0,0), (0,10), (10,10), (0,0)], fill=True, rgbcolor=(0.95,0.95,0.95),ticks=[[], []]) 
pl2+=text("$Loteries$ $inaccesibles$\n $par$ $inversion$ $d'états$",(4.5,9.1),color="red", fontsize='small', 
         background_color="white")
pl2+=polygon([(w00,0), (w00,w00), (10,10), (10,0)], fill=True, rgbcolor=(0.70,0.95,0.95),ticks=[[], []]) 
pl2+=text("$Loteries$ $inaccesibles$ $par$\n  $insuffisance$ $de$ $ressources$",(9.,5),color="red", 
         fontsize='small',background_color="white", rotation=90)
pl2+=polygon([(0,0), (w00-D1,w00-D1), (w00,w00-D1), (w00,0)], fill=True, rgbcolor=(0.95,0.75,0.95),ticks=[[], []]) 
pl2+=text("$Loteries$ $indésirable$ $par$\n $dégradation$ $des$ $deux$ $états$",(4.8,1),color="red", 
         fontsize='small', background_color="white", rotation=0)
pl12=pl+pl1+pl2
show(pl12)

And now comes the error

A = matrix(SR, 2, 2, [[w_0, 1],[w_0-ϕ,1]])
y = vector([w_0-D, w_0-D-ϕ+I])
sol=A.solve_right(y)
sola=sol[0].full_simplify().function(D, ϕ, w_0)
solb=sol[1].full_simplify().function(D, ϕ, I, w_0)
f(x, D, ϕ, I, w_0)=sola*x + solb

Here it is $\phi$ but I have the same problem with other characters.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-05-15 17:18:52 +0100

tmonteil gravatar image

updated 2020-05-16 17:40:03 +0100

ϕ is a valid Python identifier, see:

sage: ϕ = 2
sage: ϕ
2

So, you should provide the whole code, what you want to achieve, and what is the problem, so that we can help debugging.

EDIT

TL;DR in the last line, this should be:

sage: f(x, D, phi, I, w_0)=sola*x + solb

Your strategy is as follows: while some libraries do not accept unicode symbols, Python accepts unicode names. So your trick is to let the Python name ϕ point to the symbol SR.var('phi'), and benefit from the fact that %display latex prints the symbol phi as ϕ.

Now, when you write

sage: f(x, D, ϕ, I, w_0)=sola*x + solb

this not Pythonic (you can not define f(x) = in Python), but we want such statement to be possible for mathematicians, so Sage adds a preparsing layer to Python. See:

sage: preparse('f(x, D, ϕ, I, w_0)=sola*x + solb')
⎯⎯𝚝𝚖𝚙⎯⎯=𝚟𝚊𝚛("𝚡,𝙳,ϕ,𝙸,𝚠⎯𝟶");𝚏=𝚜𝚢𝚖𝚋𝚘𝚕𝚒𝚌⎯𝚎𝚡𝚙𝚛𝚎𝚜𝚜𝚒𝚘𝚗(𝚜𝚘𝚕𝚊*𝚡+𝚜𝚘𝚕𝚋).𝚏𝚞𝚗𝚌𝚝𝚒𝚘𝚗(𝚡,𝙳,ϕ,𝙸,𝚠⎯𝟶)

As you can see, the symbol ϕ is involved (it appears inside the var function), which is not acceptable for some underlying libraries.

The fist thing is to understand the difference between a symbol, and a Python name, and again the var('x') function is a bit confusing with that respect since it lets the Python name x point to the symbol SR.var('x').

edit flag offensive delete link more

Comments

The code has been provided And thanks for your help. I have tried your code of course it works. So I do not understand what happen.

Cyrille gravatar imageCyrille ( 2020-05-16 09:29:55 +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

1 follower

Stats

Asked: 2020-05-15 14:24:03 +0100

Seen: 711 times

Last updated: May 16 '20