ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 26 Nov 2012 16:28:08 +0100Substituting numerical values without partial evaluation.https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/Given an expression of the form:
<pre>
var('x y')
e = x*y + 1
</pre>
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:
<pre>
latex(e(x=5,y=6)) => "5 \times 6 + 1"
</pre>
Any hope of doing this? I could substitute into the text of the latex, but then I
would get "5 6 + 1".Mon, 26 Nov 2012 14:31:56 +0100https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/Answer by kcrisman for <p>Given an expression of the form:</p>
<p></p><pre> var('x y')
e = x*y + 1
</pre><p></p>
<p>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:</p>
<p></p><pre> latex(e(x=5,y=6)) => "5 \times 6 + 1"
</pre><p></p>
<p>Any hope of doing this? I could substitute into the text of the latex, but then I
would get "5 6 + 1".</p>
https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/?answer=14298#post-id-14298 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.Mon, 26 Nov 2012 15:08:10 +0100https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/?answer=14298#post-id-14298Answer by nholtz for <p>Given an expression of the form:</p>
<p></p><pre> var('x y')
e = x*y + 1
</pre><p></p>
<p>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:</p>
<p></p><pre> latex(e(x=5,y=6)) => "5 \times 6 + 1"
</pre><p></p>
<p>Any hope of doing this? I could substitute into the text of the latex, but then I
would get "5 6 + 1".</p>
https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/?answer=14299#post-id-14299Actually, I found sympy to do almost exactly what I want, with:
<pre>
sympy.latex( e, mul_symbol='times', symbol_names=d )
</pre>
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.Mon, 26 Nov 2012 15:52:21 +0100https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/?answer=14299#post-id-14299Comment by kcrisman for <p>Actually, I found sympy to do almost exactly what I want, with:</p>
<p></p><pre> sympy.latex( e, mul_symbol='times', symbol_names=d )
</pre><p></p>
<p>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.</p>
<p>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.</p>
https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/?comment=18648#post-id-18648Cool! Maybe we should have a `mul_symbol` option as well. Glad sympy was sufficient in this case!Mon, 26 Nov 2012 16:28:08 +0100https://ask.sagemath.org/question/9565/substituting-numerical-values-without-partial-evaluation/?comment=18648#post-id-18648