-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Description
I noticed inconsistent type promotion behavior across different functions in numpy.polynomial.chebyshev
, particularly when passing scalar values (e.g., 0
) versus array-like inputs (e.g., np.array(0)
), even though the underlying value is numerically identical.
I started by looking at chebgrid2d
and assumed array(0)
could be causing the issue, but it looked acceptable to me.
So I ended up trying out a few of the other polynomial functions mentioned in the docs. Turns out chebgrid2d
and chebgrid3d
behave the same, but chebval
, chebval2d
, and chebval3d
act a bit differently.
# Example
import numpy as np
# chebgrid2d
x1 = np.polynomial.chebyshev.chebgrid2d(0, np.array(0), np.float32(93.5))
x2 = np.polynomial.chebyshev.chebgrid2d(0, 0, np.float32(93.5))
print(getattr(x1, 'dtype', type(x1))) # float64
print(getattr(x2, 'dtype', type(x2))) # float32
# chebgrid3d
x1 = np.polynomial.chebyshev.chebgrid3d(0, 0, np.array(0), np.float32(93.5))
x2 = np.polynomial.chebyshev.chebgrid3d(0, 0, 0, np.float32(93.5))
print(getattr(x1, 'dtype', type(x1))) # float64
print(getattr(x2, 'dtype', type(x2))) # float32
# chebval2d
x1 = np.polynomial.chebyshev.chebval2d(0, np.array(0), np.float32(93.5))
x2 = np.polynomial.chebyshev.chebval2d(0, 0, np.float32(93.5))
print(getattr(x1, 'dtype', type(x1))) # float64
print(getattr(x2, 'dtype', type(x2))) # float64
# chebval3d
x1 = np.polynomial.chebyshev.chebval3d(0, 0, np.array(0), np.float32(93.5))
x2 = np.polynomial.chebyshev.chebval3d(0, 0, 0, np.float32(93.5))
print(getattr(x1, 'dtype', type(x1))) # float64
print(getattr(x2, 'dtype', type(x2))) # float64
# chebval
x1 = np.polynomial.chebyshev.chebval(np.array(0), np.float32(93.5))
x2 = np.polynomial.chebyshev.chebval(0, np.float32(93.5))
print(getattr(x1, 'dtype', type(x1))) # float64
print(getattr(x2, 'dtype', type(x2))) # float32
I kind of stumbled on this by accident — it's possible everything is working as intended, and I just need to take a closer look at the source code to be sure. Either way, it might be worth checking whether this behavior is expected, or if it's an area where things could be made more consistent and predictable.