# Substituting numerical values without partial evaluation.

Given an expression of the form:

   var('x y')
e = x*y + 1


I would like to be able to substitute numerical values into an expression without it being simplified. I'm not too concerned about seeing it in the notebook, but I would like the latex display to be affected. In other words, I would like the equivalent of:

   latex(e(x=5,y=6))   =>   "5 \times 6 + 1"


Any hope of doing this? I could substitute into the text of the latex, but then I would get "5 6 + 1".

edit retag close merge delete

Sort by » oldest newest most voted

Actually, I found sympy to do almost exactly what I want, with:

   sympy.latex( e, mul_symbol='times', symbol_names=d )


where d is a dictionary of symbols -> numerical strings mappings. sympy might turn out to be just enough for my purposes, though I still would like to know if this is possible with a reasonable (i.e. small ;-)) amount of work in sage, just in case.

Basically I have a small number (3-6) of mostly linear eqns to solve where the variables to solve for are not known ahead of time. I'm generating worked out examples of equilibrium eqns for students.

more

Cool! Maybe we should have a mul_symbol option as well. Glad sympy was sufficient in this case!

( 2012-11-26 09:28:08 -0600 )edit
sage: e=x.mul(y,hold=True)+1
sage: e
x*y + 1
sage: latex(e)
x y + 1


In general, hold=True is what you want, though this is a little weird for plus and times.

I think we'd have to override the multiplication latex method to get the \times, though this should be possible.

more