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.Sun, 12 Feb 2017 04:22:28 -0600contour_plot/plot and function argumentshttp://ask.sagemath.org/question/36523/contour_plotplot-and-function-arguments/ Hello,
I am considering switching from Mathematica to Sage but I am confused on some fundamental matters pertaining Sage. I really appreciate if you could shed some light on the problem .
Let's look at the code in Sage:
reset()
f(k1,k2) = k1^2 + k2
contour_plot(f,(0,2),(3,5))
this code beautifully draws the contour. Now, what I tried was switching the order of the arguments of f from f(k1,k2) to f(k2,k1):
f(k2,k1)=k1^2+k2
What I intuitively expected was the switch of the graph. I expected (0,2) to match k2 instead of k1 since it is the first argument of the new function f and (3,5) to k1 instead of k2 (for the same reasoning). However, the graph was identical to that of f(k1,k2). How does Sage choose range arguments to match with f arguments?
I have a similar problem with plot as well:
t=var('t')
f(z)=z^2
plot(f(z),(t,0,5))
This code draws the graph, but the function is in z and the range variable is in t. Shouldn't it give an error?! This code is wrong in Mathematica
f[z_]=z^2
Plot[f[z],{t,0,5}]
it should be Plot[f[t],{t,0,5}]. Why doesn't Sage give an error?
Thu, 09 Feb 2017 16:06:46 -0600http://ask.sagemath.org/question/36523/contour_plotplot-and-function-arguments/Answer by paulmasson for <p>Hello, </p>
<p>I am considering switching from Mathematica to Sage but I am confused on some fundamental matters pertaining Sage. I really appreciate if you could shed some light on the problem .</p>
<p>Let's look at the code in Sage:</p>
<pre><code>reset()
f(k1,k2) = k1^2 + k2
contour_plot(f,(0,2),(3,5))
</code></pre>
<p>this code beautifully draws the contour. Now, what I tried was switching the order of the arguments of f from f(k1,k2) to f(k2,k1):</p>
<pre><code>f(k2,k1)=k1^2+k2
</code></pre>
<p>What I intuitively expected was the switch of the graph. I expected (0,2) to match k2 instead of k1 since it is the first argument of the new function f and (3,5) to k1 instead of k2 (for the same reasoning). However, the graph was identical to that of f(k1,k2). How does Sage choose range arguments to match with f arguments?</p>
<p>I have a similar problem with plot as well:</p>
<pre><code>t=var('t')
f(z)=z^2
plot(f(z),(t,0,5))
</code></pre>
<p>This code draws the graph, but the function is in z and the range variable is in t. Shouldn't it give an error?! This code is wrong in Mathematica</p>
<pre><code>f[z_]=z^2
Plot[f[z],{t,0,5}]
</code></pre>
<p>it should be Plot[f[t],{t,0,5}]. Why doesn't Sage give an error?</p>
http://ask.sagemath.org/question/36523/contour_plotplot-and-function-arguments/?answer=36561#post-id-36561When ranges are given without explicit variables, the plotting routine creates a list of function arguments in alphabetical order of variable names, and the ranges are then assigned in the same order. You can (and probably should) override this automatic behavior by always specifying names: `contour_plot(f,(k1,0,2),(k2,3,5))` will then differ from `contour_plot(f,(k2,0,2),(k1,3,5))` as expected.
The missing error with `plot` occurs because the routine sets a flag `expect_one_var=True`. This is meant to be a convenience that makes plotting a bit easier by suppressing a different error message. Unfortunately it creates the confusion you've noticed.Fri, 10 Feb 2017 22:23:48 -0600http://ask.sagemath.org/question/36523/contour_plotplot-and-function-arguments/?answer=36561#post-id-36561Comment by curios_mind for <p>When ranges are given without explicit variables, the plotting routine creates a list of function arguments in alphabetical order of variable names, and the ranges are then assigned in the same order. You can (and probably should) override this automatic behavior by always specifying names: <code>contour_plot(f,(k1,0,2),(k2,3,5))</code> will then differ from <code>contour_plot(f,(k2,0,2),(k1,3,5))</code> as expected.</p>
<p>The missing error with <code>plot</code> occurs because the routine sets a flag <code>expect_one_var=True</code>. This is meant to be a convenience that makes plotting a bit easier by suppressing a different error message. Unfortunately it creates the confusion you've noticed.</p>
http://ask.sagemath.org/question/36523/contour_plotplot-and-function-arguments/?comment=36583#post-id-36583Thank you very much. I greatly appreciate your answer.Sun, 12 Feb 2017 04:22:28 -0600http://ask.sagemath.org/question/36523/contour_plotplot-and-function-arguments/?comment=36583#post-id-36583