ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 14 Jan 2011 06:29:28 -0600Evaluate with a data set?http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/Can sage evaluate an expression with a given data set like ev function in maxima?
For example something like:
A,B = var('A,B')
data = [A==1,B==2]
A + B with data
3
A + B
A + B
Thanks for help.Fri, 14 Jan 2011 04:47:17 -0600http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/Answer by kcrisman for <p>Can sage evaluate an expression with a given data set like ev function in maxima?</p>
<p>For example something like:</p>
<pre><code> A,B = var('A,B')
data = [A==1,B==2]
A + B with data
3
A + B
A + B
</code></pre>
<p>Thanks for help.</p>
http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?answer=11972#post-id-11972I think you are looking for the `subs()` method of symbolic expressions.
sage: var('A B')
(A, B)
sage: data = {A:1,B:2}
sage: expr = A+B
sage: expr
A + B
sage: expr.subs(data)
3
This will substitute as a dictionary. You can also do
sage: expr.subs(A=1,B=2)
3
if you like that syntax. Does that help?Fri, 14 Jan 2011 04:54:52 -0600http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?answer=11972#post-id-11972Comment by DSM for <p>I think you are looking for the <code>subs()</code> method of symbolic expressions. </p>
<pre><code>sage: var('A B')
(A, B)
sage: data = {A:1,B:2}
sage: expr = A+B
sage: expr
A + B
sage: expr.subs(data)
3
</code></pre>
<p>This will substitute as a dictionary. You can also do</p>
<pre><code>sage: expr.subs(A=1,B=2)
3
</code></pre>
<p>if you like that syntax. Does that help?</p>
http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?comment=22282#post-id-22282@stanislav: As kcrisman does, note that it's more Pythonic to store key, value pairs in a dict than as a list of equations. That said, (a+b).subs_expr(*data) should work for lists of equations too.Fri, 14 Jan 2011 05:03:34 -0600http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?comment=22282#post-id-22282Answer by stanislav for <p>Can sage evaluate an expression with a given data set like ev function in maxima?</p>
<p>For example something like:</p>
<pre><code> A,B = var('A,B')
data = [A==1,B==2]
A + B with data
3
A + B
A + B
</code></pre>
<p>Thanks for help.</p>
http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?answer=11973#post-id-11973Thanks, the second variant is the more comfortable for me, because then I can use a list of sets and put it into a for loop.
I think, the is no better way in sage because the way, you can use it in maxima can not be used in python. In maxima you can give the data list after an expression just by typing a comma like:
A+B,data
But I'm glad, the way with subs is working fine.Fri, 14 Jan 2011 06:02:19 -0600http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?answer=11973#post-id-11973Comment by kcrisman for <p>Thanks, the second variant is the more comfortable for me, because then I can use a list of sets and put it into a for loop.</p>
<p>I think, the is no better way in sage because the way, you can use it in maxima can not be used in python. In maxima you can give the data list after an expression just by typing a comma like:</p>
<pre><code>A+B,data
</code></pre>
<p>But I'm glad, the way with subs is working fine.</p>
http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?comment=22281#post-id-22281Great - especially feel free to use the *list unpacking syntax DSM mentions. (You can also accept the answer if you like it enough :-) )Fri, 14 Jan 2011 06:29:28 -0600http://ask.sagemath.org/question/7868/evaluate-with-a-data-set/?comment=22281#post-id-22281