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, 28 Oct 2016 15:47:44 -0500I want my plotting function to treat numbers as numbers, not variableshttp://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/ I'm creating a plot, like this:
density_plot(f(x, y), (x, 0, 1), (y, 0, 1))
The function *f* looks basically like this:
def f(x, y):
u = vector([x, y, 1 - x - y])
return u.norm(2)
That works. Now I introduce a vector v like this:
def f(x, y):
u = vector([x, y, 1 - x - y])
v = vector([5, 4, 2])
return (u - v).norm(2)
That works. But if v is the solution of an equation, like this:
v = M.right_kernel().basis()[0]
Now Sage can't handle the subtraction u - v:
> TypeError: unsupported operand parent(s) for '-': 'Vector space of dimension 3 over Symbolic Ring' and 'Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision Basis matrix:[ 1.00000000000000 0.571428571428571 1.57142857142857]'
So okay, I get that basically what's happening is that u consists of symbolic polynomials in x and y, but v consists of actual numbers, or something like that. But why is it not a problem when v is defined explicitly by `vector([5, 5, 5])` or whatever? What is it about the object returned by `.right_kernel().basis()[0]` (which in all other respects behaves like a vector) that's so incompatible in this context?
How can I solve the matrix equation Mx = 0 in such a way that the vector I get can be subtracted from u? Note that M is singular in my case, with nullity 1, so I need to be able to get an arbitrary vector out of its nullspace.Mon, 24 Oct 2016 10:30:39 -0500http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/Comment by kcrisman for <p>I'm creating a plot, like this:</p>
<pre><code>density_plot(f(x, y), (x, 0, 1), (y, 0, 1))
</code></pre>
<p>The function <em>f</em> looks basically like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
return u.norm(2)
</code></pre>
<p>That works. Now I introduce a vector v like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
v = vector([5, 4, 2])
return (u - v).norm(2)
</code></pre>
<p>That works. But if v is the solution of an equation, like this:</p>
<pre><code>v = M.right_kernel().basis()[0]
</code></pre>
<p>Now Sage can't handle the subtraction u - v:</p>
<blockquote>
<p>TypeError: unsupported operand parent(s) for '-': 'Vector space of dimension 3 over Symbolic Ring' and 'Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision Basis matrix:[ 1.00000000000000 0.571428571428571 1.57142857142857]'</p>
</blockquote>
<p>So okay, I get that basically what's happening is that u consists of symbolic polynomials in x and y, but v consists of actual numbers, or something like that. But why is it not a problem when v is defined explicitly by <code>vector([5, 5, 5])</code> or whatever? What is it about the object returned by <code>.right_kernel().basis()[0]</code> (which in all other respects behaves like a vector) that's so incompatible in this context?</p>
<p>How can I solve the matrix equation Mx = 0 in such a way that the vector I get can be subtracted from u? Note that M is singular in my case, with nullity 1, so I need to be able to get an arbitrary vector out of its nullspace.</p>
http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35297#post-id-35297Oops, sorry for not reporting the ticket here - I must have forgotten.Fri, 28 Oct 2016 15:47:44 -0500http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35297#post-id-35297Comment by tmonteil for <p>I'm creating a plot, like this:</p>
<pre><code>density_plot(f(x, y), (x, 0, 1), (y, 0, 1))
</code></pre>
<p>The function <em>f</em> looks basically like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
return u.norm(2)
</code></pre>
<p>That works. Now I introduce a vector v like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
v = vector([5, 4, 2])
return (u - v).norm(2)
</code></pre>
<p>That works. But if v is the solution of an equation, like this:</p>
<pre><code>v = M.right_kernel().basis()[0]
</code></pre>
<p>Now Sage can't handle the subtraction u - v:</p>
<blockquote>
<p>TypeError: unsupported operand parent(s) for '-': 'Vector space of dimension 3 over Symbolic Ring' and 'Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision Basis matrix:[ 1.00000000000000 0.571428571428571 1.57142857142857]'</p>
</blockquote>
<p>So okay, I get that basically what's happening is that u consists of symbolic polynomials in x and y, but v consists of actual numbers, or something like that. But why is it not a problem when v is defined explicitly by <code>vector([5, 5, 5])</code> or whatever? What is it about the object returned by <code>.right_kernel().basis()[0]</code> (which in all other respects behaves like a vector) that's so incompatible in this context?</p>
<p>How can I solve the matrix equation Mx = 0 in such a way that the vector I get can be subtracted from u? Note that M is singular in my case, with nullity 1, so I need to be able to get an arbitrary vector out of its nullspace.</p>
http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35293#post-id-35293Thanks for reporting, thanks @kcrisman for the ticket, it is now [trac ticket 21777](https://trac.sagemath.org/ticket/21777)Thu, 27 Oct 2016 14:53:47 -0500http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35293#post-id-35293Comment by slelievre for <p>I'm creating a plot, like this:</p>
<pre><code>density_plot(f(x, y), (x, 0, 1), (y, 0, 1))
</code></pre>
<p>The function <em>f</em> looks basically like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
return u.norm(2)
</code></pre>
<p>That works. Now I introduce a vector v like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
v = vector([5, 4, 2])
return (u - v).norm(2)
</code></pre>
<p>That works. But if v is the solution of an equation, like this:</p>
<pre><code>v = M.right_kernel().basis()[0]
</code></pre>
<p>Now Sage can't handle the subtraction u - v:</p>
<blockquote>
<p>TypeError: unsupported operand parent(s) for '-': 'Vector space of dimension 3 over Symbolic Ring' and 'Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision Basis matrix:[ 1.00000000000000 0.571428571428571 1.57142857142857]'</p>
</blockquote>
<p>So okay, I get that basically what's happening is that u consists of symbolic polynomials in x and y, but v consists of actual numbers, or something like that. But why is it not a problem when v is defined explicitly by <code>vector([5, 5, 5])</code> or whatever? What is it about the object returned by <code>.right_kernel().basis()[0]</code> (which in all other respects behaves like a vector) that's so incompatible in this context?</p>
<p>How can I solve the matrix equation Mx = 0 in such a way that the vector I get can be subtracted from u? Note that M is singular in my case, with nullity 1, so I need to be able to get an arbitrary vector out of its nullspace.</p>
http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35280#post-id-35280This matrix seems to reproduce it:
sage: M = matrix([[1., 1., -1.], [1., 12., -5.]])
sage: M.right_kernel()
Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision
Basis matrix:
[ 1.00000000000000 0.571428571428571 1.57142857142857]Thu, 27 Oct 2016 09:04:27 -0500http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35280#post-id-35280Comment by kcrisman for <p>I'm creating a plot, like this:</p>
<pre><code>density_plot(f(x, y), (x, 0, 1), (y, 0, 1))
</code></pre>
<p>The function <em>f</em> looks basically like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
return u.norm(2)
</code></pre>
<p>That works. Now I introduce a vector v like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
v = vector([5, 4, 2])
return (u - v).norm(2)
</code></pre>
<p>That works. But if v is the solution of an equation, like this:</p>
<pre><code>v = M.right_kernel().basis()[0]
</code></pre>
<p>Now Sage can't handle the subtraction u - v:</p>
<blockquote>
<p>TypeError: unsupported operand parent(s) for '-': 'Vector space of dimension 3 over Symbolic Ring' and 'Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision Basis matrix:[ 1.00000000000000 0.571428571428571 1.57142857142857]'</p>
</blockquote>
<p>So okay, I get that basically what's happening is that u consists of symbolic polynomials in x and y, but v consists of actual numbers, or something like that. But why is it not a problem when v is defined explicitly by <code>vector([5, 5, 5])</code> or whatever? What is it about the object returned by <code>.right_kernel().basis()[0]</code> (which in all other respects behaves like a vector) that's so incompatible in this context?</p>
<p>How can I solve the matrix equation Mx = 0 in such a way that the vector I get can be subtracted from u? Note that M is singular in my case, with nullity 1, so I need to be able to get an arbitrary vector out of its nullspace.</p>
http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35248#post-id-35248Can you tell us exactly how you define `M`? I tried a few experiments with this and couldn't reproduce your error.Mon, 24 Oct 2016 12:18:20 -0500http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?comment=35248#post-id-35248Answer by slelievre for <p>I'm creating a plot, like this:</p>
<pre><code>density_plot(f(x, y), (x, 0, 1), (y, 0, 1))
</code></pre>
<p>The function <em>f</em> looks basically like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
return u.norm(2)
</code></pre>
<p>That works. Now I introduce a vector v like this:</p>
<pre><code>def f(x, y):
u = vector([x, y, 1 - x - y])
v = vector([5, 4, 2])
return (u - v).norm(2)
</code></pre>
<p>That works. But if v is the solution of an equation, like this:</p>
<pre><code>v = M.right_kernel().basis()[0]
</code></pre>
<p>Now Sage can't handle the subtraction u - v:</p>
<blockquote>
<p>TypeError: unsupported operand parent(s) for '-': 'Vector space of dimension 3 over Symbolic Ring' and 'Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision Basis matrix:[ 1.00000000000000 0.571428571428571 1.57142857142857]'</p>
</blockquote>
<p>So okay, I get that basically what's happening is that u consists of symbolic polynomials in x and y, but v consists of actual numbers, or something like that. But why is it not a problem when v is defined explicitly by <code>vector([5, 5, 5])</code> or whatever? What is it about the object returned by <code>.right_kernel().basis()[0]</code> (which in all other respects behaves like a vector) that's so incompatible in this context?</p>
<p>How can I solve the matrix equation Mx = 0 in such a way that the vector I get can be subtracted from u? Note that M is singular in my case, with nullity 1, so I need to be able to get an arbitrary vector out of its nullspace.</p>
http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?answer=35281#post-id-35281Here is a workaround to your problem.
I still hope someone can give a better explanation of what is going on.
Define `u`:
sage: x, y = SR.var('x y')
sage: u = vector((x, y, 1 - x - y)
Define `v`:
sage: M = matrix([[1., 1., -1.], [1., 12., -5.]])
sage: v = M.right_kernel().basis()[0]
Trying to subtract `v` from `u` fails, as you noticed.
sage: u - v
Traceback (most recent call last)
...
TypeError: unsupported operand parent(s) for '-': 'Vector space of dimension 3 over Symbolic Ring' and 'Vector space of degree 3 and dimension 1 over Real Field with 53 bits of precision
Basis matrix:
[ 1.00000000000000 0.571428571428571 1.57142857142857]'
But you can force it as follows:
sage: u - u.parent()(v)
(x - 1.00000000000000, y - 0.571428571428571, -x - y - 0.571428571428571)
Thu, 27 Oct 2016 09:11:57 -0500http://ask.sagemath.org/question/35246/i-want-my-plotting-function-to-treat-numbers-as-numbers-not-variables/?answer=35281#post-id-35281