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.Thu, 20 Oct 2016 21:12:16 -0500Why region_plot dont work with absolute value of complex numbers?http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/ Im trying to plot something like
var('y')
region_plot([x*abs(x+y*I)-cos(y) <= 5, y^2+e^x<=3],(x,-5,5), (y,-5,5),incol='lightblue',bordercol='blue')
but I get an errorType because the imaginary unit. I dont know how to fix it, any help would be appreciated.Wed, 19 Oct 2016 14:07:09 -0500http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/Answer by paulmasson for <p>Im trying to plot something like</p>
<pre><code>var('y')
region_plot([x*abs(x+y*I)-cos(y) <= 5, y^2+e^x<=3],(x,-5,5), (y,-5,5),incol='lightblue',bordercol='blue')
</code></pre>
<p>but I get an errorType because the imaginary unit. I dont know how to fix it, any help would be appreciated.</p>
http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?answer=35197#post-id-35197One way to fix this is to replace `abs(x+I*y)` with `sqrt(x^2+y^2)`.
Sage plotting routines use native Python type `float` under the hood for domain variables, while input functions are of the symbolic Sage type `Expression`. The two don't always mix well for plotting, especially when complex variables are involved.
Another way around the problem is to put `lambda x,y:` in front of the function containing the complex unit to force evaluation as a Python callable.Wed, 19 Oct 2016 16:05:39 -0500http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?answer=35197#post-id-35197Comment by paulmasson for <p>One way to fix this is to replace <code>abs(x+I*y)</code> with <code>sqrt(x^2+y^2)</code>.</p>
<p>Sage plotting routines use native Python type <code>float</code> under the hood for domain variables, while input functions are of the symbolic Sage type <code>Expression</code>. The two don't always mix well for plotting, especially when complex variables are involved.</p>
<p>Another way around the problem is to put <code>lambda x,y:</code> in front of the function containing the complex unit to force evaluation as a Python callable.</p>
http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35208#post-id-35208Karl-Dieter, I haven't seen any tickets yet addressing this issue. I tried a simple fix today where I though it most effective but it was only partially successful. I suspect that fixing this problem completely will require some changes to `fast_float` as well as the fast-callable coercion process.Thu, 20 Oct 2016 21:12:16 -0500http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35208#post-id-35208Comment by paulmasson for <p>One way to fix this is to replace <code>abs(x+I*y)</code> with <code>sqrt(x^2+y^2)</code>.</p>
<p>Sage plotting routines use native Python type <code>float</code> under the hood for domain variables, while input functions are of the symbolic Sage type <code>Expression</code>. The two don't always mix well for plotting, especially when complex variables are involved.</p>
<p>Another way around the problem is to put <code>lambda x,y:</code> in front of the function containing the complex unit to force evaluation as a Python callable.</p>
http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35207#post-id-35207Masacroso, I don't know of a single good reference for general types available in Sage. I only know about the `float` issue in plots because of bugs I've been working on over the summer.
You can always test the type of any expression by evaluating `type( expression )`. That will show you the entire class parentage of the expression, with its type at the end.
Here's a couple links that might help:
http://doc.sagemath.org/html/en/tutorial/tour_coercion.html
http://doc.sagemath.org/html/en/tutorial/tour_functions.html
The second is particularly worth reading to save frustration when plots don't work as expected. It led me to post my own question here about native Python functions:
https://ask.sagemath.org/question/33259/functions-returning-unexpected-results-for-symbolic-expressionsThu, 20 Oct 2016 21:09:01 -0500http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35207#post-id-35207Comment by Masacroso for <p>One way to fix this is to replace <code>abs(x+I*y)</code> with <code>sqrt(x^2+y^2)</code>.</p>
<p>Sage plotting routines use native Python type <code>float</code> under the hood for domain variables, while input functions are of the symbolic Sage type <code>Expression</code>. The two don't always mix well for plotting, especially when complex variables are involved.</p>
<p>Another way around the problem is to put <code>lambda x,y:</code> in front of the function containing the complex unit to force evaluation as a Python callable.</p>
http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35199#post-id-35199Thank you very much Paul for this great teaching. But after I read you I need to add an important question, where I can learn how work the structure of SAGE? I mean, you said that some functions use the arguments as an element float, and others are symbolic, if I understood correctly. Then, how can I know all the different type of variables that use SAGE to know what I can do and what I need to do to make that something works?Wed, 19 Oct 2016 23:46:49 -0500http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35199#post-id-35199Comment by kcrisman for <p>One way to fix this is to replace <code>abs(x+I*y)</code> with <code>sqrt(x^2+y^2)</code>.</p>
<p>Sage plotting routines use native Python type <code>float</code> under the hood for domain variables, while input functions are of the symbolic Sage type <code>Expression</code>. The two don't always mix well for plotting, especially when complex variables are involved.</p>
<p>Another way around the problem is to put <code>lambda x,y:</code> in front of the function containing the complex unit to force evaluation as a Python callable.</p>
http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35198#post-id-35198Excellent answer - and maybe we need to (finally) fix that. Are there some open tickets about this?Wed, 19 Oct 2016 20:10:51 -0500http://ask.sagemath.org/question/35193/why-region_plot-dont-work-with-absolute-value-of-complex-numbers/?comment=35198#post-id-35198