[Numpy-discussion] deprecating float(x) for ndim > 0

george trojan george.trojan at gmail.com
Thu Sep 16 01:31:59 EDT 2021


To Andrew Nelson:

> In the return section for fsolve the documentation states that the return
> value, `x`, is an `ndarray`.

True, my bad. There is another issue with `fsolve`: it implicitly changes
the argument  passed to `func`. Consider

def func(x):
    # x = float(x)
    if not np.isscalar(x) and x.shape != ():
        raise ValueError('The argument must be a number or a 0-d array')
    return x - 1

fsolve(func, np.array(2.0))

---------------------------------------------------------------------------ValueError
                               Traceback (most recent call
last)/tmp/ipykernel_1017360/2091764495.py in <module>      5
return x - 1      6 ----> 7 fsolve(func, np.array(2.0))
. . .
 ValueError: The argument must be a number or a 0-d array

I had the commented out line in my code (in real life I am calling a
library function that accepts only
numeric types, not arrays). Changing this to line to x = x.item() makes sense.
The documentation states that `func` takes at least one (possibly
vector) argument. It must take a vector.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/numpy-discussion/attachments/20210916/6fdfda96/attachment.html>


More information about the NumPy-Discussion mailing list