Skip to content

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

@Smith-cc

Description

@Smith-cc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy