Ask Your Question
1

I want my plotting function to treat numbers as numbers, not variables

asked 2016-10-24 17:30:39 +0100

JackM gravatar image

updated 2023-01-09 23:59:40 +0100

tmonteil gravatar image

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 flag offensive close merge delete

Comments

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

kcrisman gravatar imagekcrisman ( 2016-10-24 19:18:20 +0100 )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]
slelievre gravatar imageslelievre ( 2016-10-27 16:04:27 +0100 )edit

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

tmonteil gravatar imagetmonteil ( 2016-10-27 21:53:47 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2016-10-28 22:47:44 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-10-27 16:11:57 +0100

slelievre gravatar image

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)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-10-24 17:30:39 +0100

Seen: 390 times

Last updated: Oct 27 '16