Ask Your Question
0

Substituting numerical values without partial evaluation.

asked 2012-11-26 14:31:56 +0100

nholtz gravatar image

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

2 Answers

Sort by ยป oldest newest most voted
0

answered 2012-11-26 15:08:10 +0100

kcrisman gravatar image
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.

edit flag offensive delete link more
0

answered 2012-11-26 15:52:21 +0100

nholtz gravatar image

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.

edit flag offensive delete link more

Comments

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

kcrisman gravatar imagekcrisman ( 2012-11-26 16:28:08 +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

Stats

Asked: 2012-11-26 14:31:56 +0100

Seen: 1,109 times

Last updated: Nov 26 '12