Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.

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.