Ask Your Question

Revision history [back]

This is indeed a bug of the preparse function. Your syntax is perfectly valid in Python

>>> A = 1 + (3*
... 4
... ) * 2
25

However Sage preparser function is not smart in matching the start and end of an instruction.

sage: s = """phi(epsilon, Q, r) = 1/(4*pi*
....:                         epsilon
....: )*Q/r"""
sage: print(s)
phi(epsilon, Q, r) = 1/(4*pi*
                        epsilon
)*Q/r
sage: print(preparse(s))
__tmp__=var("epsilon,Q,r"); phi = symbolic_expression(Integer(1)/(Integer(4)*pi*).function(epsilon,Q,r)
                        epsilon
)*Q/r

Somehow, only the first line went transformed into a function. If you call directly preparse_calculus (which is the part that perform the preparsing of the instruction defining the function) it works

sage: from sage.repl.preparse import preparse_calculus
sage: print(preparse_calculus(";" + s + ";"))
;__tmp__=var("epsilon,Q,r"); phi = symbolic_expression(1/(4*pi*
                        epsilon
)*Q/r).function(epsilon,Q,r);

(Don't ask me why, but the function preparse_calculus needs a ";" at the begining and the end of the instruction). This can then be executed manually

sage: exec(preparse_calculus(";" + s + ";")[1:-1])
sage: phi
(epsilon, Q, r) |--> 1/4*Q/(pi*epsilon*r)

Note that this problem has been reported 8 years ago in trac ticket #11621.