ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 24 Oct 2016 10:30:39 -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.JackMMon, 24 Oct 2016 10:30:39 -0500http://ask.sagemath.org/question/35246/density_plot + def'ed python function = TypeError?http://ask.sagemath.org/question/25617/density_plot-defed-python-function-typeerror/I have the following functions:
var('a, b')
# This will work with both contour_plot and density_plot
f1(a, b) = 1 - b / a
# So will this
f2(a, b) = 1 - a / b
# This only works with contour_plot?!
def f12(a, b):
if a - b < 0:
return f1(a, b)
else:
return f2(a, b)
Obviously, `f1` and `f2` are symbolic functions, where `f12` is a `def`'ed python function. Calling `contour_plot` works on all three. Calling `density_plot` works on `f1` and `f2`, but raises a `TypeError` when called with `f12`, the `def`'ed python function.
I've [created a project](https://cloud.sagemath.com/projects/443a2a33-d145-4b63-893f-175543cc50c6/files/Test%20Case.sagews) that has an interactive session demonstrating the problem.
Here's the trace:
Error in lines 1-1
Traceback (most recent call last):
...
File "/projects/443a2a33-d145-4b63-893f-175543cc50c6/.sagemathcloud/sage_salvus.py", line 2306, in show
show_2d_plot_using_matplotlib(obj, svg=svg, **kwds)
File "/projects/443a2a33-d145-4b63-893f-175543cc50c6/.sagemathcloud/sage_salvus.py", line 2229, in show_2d_plot_using_matplotlib
obj.save(t, **kwds)
File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 471, in wrapper
return func(*args, **kwds)
File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/sage/plot/graphics.py", line 2972, in save
figure = self.matplotlib(**options)
File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/sage/plot/graphics.py", line 2418, in matplotlib
g._render_on_subplot(subplot)
File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/sage/plot/density_plot.py", line 146, in _render_on_subplot
subplot.imshow(self.xy_data_array, origin='lower', cmap=cmap, extent=(x0,x1,y0,y1), interpolation=options['interpolation'])
File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/axes.py", line 7300, in imshow
im.set_data(X)
File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-linux-x86_64.egg/matplotlib/image.py", line 425, in set_data
raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float
Am I asking `density_plot` to do something crazy here? If not, what am I doing wrong?positaTue, 27 Jan 2015 13:32:24 -0600http://ask.sagemath.org/question/25617/