# 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.

edit retag close merge delete

Can you tell us exactly how you define M? I tried a few experiments with this and couldn't reproduce your error.

( 2016-10-24 12:18:20 -0500 )edit

This 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]

( 2016-10-27 09:04:27 -0500 )edit

Thanks for reporting, thanks @kcrisman for the ticket, it is now trac ticket 21777

( 2016-10-27 14:53:47 -0500 )edit

Oops, sorry for not reporting the ticket here - I must have forgotten.

( 2016-10-28 15:47:44 -0500 )edit

Sort by » oldest newest most voted

Here 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)

more