First time here? Check out the FAQ!

Ask Your Question
0

Substituting numerical values without partial evaluation.

asked 12 years ago

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".

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
0

answered 12 years ago

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.

Preview: (hide)
link
0

answered 12 years ago

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.

Preview: (hide)
link

Comments

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

kcrisman gravatar imagekcrisman ( 12 years ago )

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

Seen: 1,326 times

Last updated: Nov 26 '12