Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #8450: work around unplottable points in plot_vector_field().
Browse files Browse the repository at this point in the history
Now that many of our plotting functions are using fast_callable()
WITHOUT domain=float to support complex numbers that appear in
intermediate computations, we need to work around some plot points
that used to be NaN (as a result of domain=float) but now throw errors
as we try to compute them.

One such instance is in plot_vector_field(), where the lines

  xvec_array.append(f(x, y))

and

  yvec_array.append(g(x, y))

can fail to evaluate either f or g. This commit adds two try/except
blocks to catch ValueError (calling math.sqrt on a negative number)
and ZeroDivisionError (function undefined at some point), and turn
them into NaN. The hope is that this leaves the behavior unchanged,
since domain=float would have forced these evaluations to return NaN
in the past.
  • Loading branch information
orlitzky committed Jul 23, 2021
1 parent 877aeee commit a773fd3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/sage/plot/plot_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,16 @@ def plot_vector_field(f_g, xrange, yrange, **options):
for y in xsrange(*ranges[1], include_endpoint=True):
xpos_array.append(x)
ypos_array.append(y)
xvec_array.append(f(x, y))
yvec_array.append(g(x, y))
try:
fxy = f(x, y)
except (ValueError, ZeroDivisionError):
fxy = float("nan")
xvec_array.append(fxy)
try:
gxy = g(x, y)
except (ValueError, ZeroDivisionError):
gxy = float("nan")
yvec_array.append(gxy)

import numpy
xvec_array = numpy.ma.masked_invalid(numpy.array(xvec_array, dtype=float))
Expand Down

0 comments on commit a773fd3

Please sign in to comment.