numpy / numpy/numpy

Inconsistent type promotion in numpy.polynomial.chebyshev functions depending on scalar/array input

Open
#29,357 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

component: numpy.polynomial
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the scalar and array-input behavior of chebgrid2d, chebgrid3d, chebval, chebval2d, and chebval3d in numpy.polynomial.chebyshev. Determine whether the differing dtype promotion is expected, then check the existing tests and documentation for the intended behavior. Done means the expected promotion is documented and, if needed, covered consistently by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.