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

未关闭 适合新手
#2,561 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@mayuriphad 已经在做这个了。

开始于 2026年10月26日。

  • #2570 来自 @mayuriphad —— 未关闭

评估

难度
2/5
预计耗时
1 小时以内
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

在 pytensor/tensor/ 中找到 geomspace 的实现(搜索对 result[0] 和 result[-1] 的 set_subtensor);端点在乘以 sign(start) 之前就被设置,这会在 start 为负时再次翻转它们。移动或调整顺序使端点正确,例如在设置端点之前先乘以 sign(start)。用 pt.geomspace(np.float64(-2.0), np.float64(-30.0), num=4).eval() 复现,并与 np.geomspace 比较;当标量和数组输入下端点都与 NumPy 一致时即完成。注意:提交者说他们已有补丁并可能开 PR,所以开始前请先确认。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
654
派生
258
平均合并
3 天 41 分钟
30 天内合并 PR
42

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pymc-devs/pytensor 的其他 Issue

查看 pymc-devs/pytensor 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。