BUG: geomspace returns wrong sign for the endpoints when start and stop are negative

Open Beginner friendly
#2,561 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@mayuriphad is already working on this.

Since Oct 26, 2026.

  • #2570 by @mayuriphad — open

Assessment

Difficulty
2/5
Estimated time
Under an hour
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Find the geomspace implementation in pytensor/tensor/ (search for set_subtensor on result[0] and result[-1]); the endpoints are set before multiplying by sign(start), which flips them again for negative start. Move or reorder so endpoints are correct, e.g. multiply by sign(start) before setting them. Reproduce with pt.geomspace(np.float64(-2.0), np.float64(-30.0), num=4).eval() and compare against np.geomspace; done when endpoints match NumPy for scalar and array inputs. Note: the filer says they have a patch and may open a PR, so check before starting.

Written by the indexing model from the issue text.

Description

Describe the issue:

pt.geomspace returns the wrong sign for the first and last samples when start and stop are negative. The interior samples are correct.

In geomspace, the endpoints are written into the result with set_subtensor(result[0, ...], start) and set_subtensor(result[-1, ...], stop), and only afterwards the whole result is multiplied by sign(start). For a negative start this multiplies the already-negative endpoints by -1 again.

np.geomspace(-2, -30, 4) gives [-2, -4.93, -12.16, -30]; pt.geomspace gives [2, -4.93, -12.16, 30].

Reproducible code example:
import numpy as np
import pytensor.tensor as pt

start, stop = np.float64(-2.0), np.float64(-30.0)
print(pt.geomspace(start, stop, num=4).eval())
print(np.geomspace(start, stop, num=4))
Error message:
[  2.          -4.93242404 -12.16440357  30.        ]
[ -2.          -4.93242415 -12.16440399 -30.        ]

The same happens per element for array inputs, e.g. start=[-1, 2], stop=[-8, 16] gives [1, -2, -4, 8] for the first sequence.

PyTensor version information:

PyTensor main (2e1005b), NumPy 2.5.4, Python 3.12, macOS arm64.

Context for the issue:

A possible fix is to multiply by sign(start) before setting the exact endpoints (a 2-line change). I have a patch with a regression test and can open a PR if that is welcome.

Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.

Dominant language
Python
Stars
655
Forks
266
Avg merge
3d 23h
Merged PRs (30d)
43

Getting set up

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.

More from pymc-devs/pytensor

All issues in pymc-devs/pytensor

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.