Skip to content

BUG: numpy.logspace sometimes fails with a broadcasting error if base is array-like. #23274

@roytsmart

Description

@roytsmart

Describe the issue:

Two related failure modes here. If start and stop are both scalars and base is array-like then there is always a broadcasting error. If start/stop, and base are array-like and axis != 0 then there is also a broadcasting error.

I think this could be fixed pretty easily by inserting a new axis into base so that it broadcasts correctly.

Reproduce the code example:

import numpy as np
np.logspace(0, 1, base=[1, 2, 3])

Error message:

Traceback (most recent call last):
  File "C:\Users\byrdie\AppData\Local\Programs\Python\Python311\Lib\site-packages\IPython\core\interactiveshell.py", line 3442, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-17-817fad8c0c56>", line 1, in <module>
    b = np.logspace(0, 1, base=[1, 2, 3])
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 200, in logspace
  File "C:\Users\byrdie\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\function_base.py", line 284, in logspace
    return _nx.power(base, y)
           ^^^^^^^^^^^^^^^^^^
ValueError: operands could not be broadcast together with shapes (3,) (50,)

Runtime information:

import sys, numpy; print(numpy.__version__); print(sys.version)
1.24.1
3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)]
print(numpy.show_runtime())
WARNING: `threadpoolctl` not found in system! Install it by `pip install threadpoolctl`. Once installed, try `np.show_runtime` again for more detailed build information
[{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}}]
None

Context for the issue:

If we think this is a good idea I'm happy to open a PR to try and resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions