Ask Your Question

Plotting 2d vector fields – how to delay function evaluation

asked 2016-12-30 18:54:07 +0200

anonymous user


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

tmonteil gravatar image

I want to plot, for example, the following 2d vector field: $$\vec{F}(x,y)= \begin{cases} (0.1,0.1), & \text{for }r\le d_{min}\newline \frac{\vec{r}}{r^3}, & \text{otherwise}. \end{cases}$$ In one SageMath cell I have defined corresponding Python function:

dmin = 0.03
def my_vector_field_2D(x,y):
    vector_field = vector([0.1,0.1])
    r = vector([x,y])
    if r.norm()>dmin:
        vector_field = 1/ r.norm()^3 * r
    return vector_field

In another cell, I use function plot_vector_field to plot the field:


However, function my_vector_field_2D(x,y) is evaluated before it is given to the function plot_vector_field and because in this evaluation my_vector_field_2D receives instead of numbers symbolic variables x and y, expression r.norm()>dmin is always False. As a result, my_vector_field_2D(x,y) is evalueted to: (0.100000000000000, 0.100000000000000). So the plotted filed is actually $\vec{F}(x,y)=(0.1,0.1)$ everywhere and that is not what I want.

How can I prevent this function evaluation?

I tried something that works with the plot function (provide the plot function with just the name of a function that you want to plot (without parameters) in order to prevent function evaluation):


But here this method does not work: TypeError: 'function' object is not iterable.

My first why question: Why this does not work? It works for plot function and also for contour_plot function and probably for some other plot functions.

Then I found in the SageMath documentation that “when plotting non-symbolic functions, they should be wrapped in lambda”. So I tried:

plot_vector_field(lambda x,y:my_vector_field_2D(x,y),(-0.3,0.3),(-0.3,0.3))

And the same error popped up: TypeError: 'function' object is not iterable.

But this works:

plot_vector_field((lambda x,y:my_vector_field_2D(x,y)[0],lambda x,y:my_vector_field_2D(x,y)[1]),(-0.3,0.3),(-0.3,0.3))

My second why question: Why do I need two lambda functions or why do I need to manually decompose vector returned by my_vector_field_2D function into its two components?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2016-12-31 22:49:51 +0200

tmonteil gravatar image

To understand the problem, it suffices to read the source code of the plot_vector_field function:

sage: plot_vector_field??

As you can see, the first line of the code is

(f,g) = f_g

where f_g is the first parameter of the function, which means that it assumes that f_g is either a couple of functions (as in (lambda x,y:x+y, lambda x,y:cos(x))), or a couple of symbolic expressions (as in (x+y,cos(x)), not a a single Python function with 2-dimensional output.

This explains your problem, and i consider this as a bug, or at least a missing feature.

Thanks for reporting, it is now trac ticket 22115.

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

1 follower


Asked: 2016-12-30 18:54:07 +0200

Seen: 617 times

Last updated: Dec 31 '16