Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7799bd08
提交
7799bd08
authored
6月 26, 2025
作者:
Abhinav-Khot
提交者:
Ricardo Vieira
7月 08, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add negative value support for the digamma function
上级
7584614e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
47 行增加
和
3 行删除
+47
-3
math.py
pytensor/scalar/math.py
+20
-3
test_math.py
tests/scalar/test_math.py
+27
-0
没有找到文件。
pytensor/scalar/math.py
浏览文件 @
7799bd08
...
@@ -389,6 +389,10 @@ class Psi(UnaryScalarOp):
...
@@ -389,6 +389,10 @@ class Psi(UnaryScalarOp):
#define ga_double double
#define ga_double double
#endif
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef _PSIFUNCDEFINED
#ifndef _PSIFUNCDEFINED
#define _PSIFUNCDEFINED
#define _PSIFUNCDEFINED
DEVICE double _psi(ga_double x) {
DEVICE double _psi(ga_double x) {
...
@@ -396,7 +400,8 @@ class Psi(UnaryScalarOp):
...
@@ -396,7 +400,8 @@ class Psi(UnaryScalarOp):
/*taken from
/*taken from
Bernardo, J. M. (1976). Algorithm AS 103:
Bernardo, J. M. (1976). Algorithm AS 103:
Psi (Digamma) Function. Applied Statistics. 25 (3), 315-317.
Psi (Digamma) Function. Applied Statistics. 25 (3), 315-317.
http://www.uv.es/~bernardo/1976AppStatist.pdf */
http://www.uv.es/~bernardo/1976AppStatist.pdf
*/
ga_double y, R, psi_ = 0;
ga_double y, R, psi_ = 0;
ga_double S = 1.0e-5;
ga_double S = 1.0e-5;
...
@@ -406,10 +411,22 @@ class Psi(UnaryScalarOp):
...
@@ -406,10 +411,22 @@ class Psi(UnaryScalarOp):
ga_double S5 = 3.968253968e-3;
ga_double S5 = 3.968253968e-3;
ga_double D1 = -0.5772156649;
ga_double D1 = -0.5772156649;
if (x <= 0) {
// the digamma function approaches infinity from one side and -infinity from the other, around negative integers and zero
if (x == floor(x)) {
return INFINITY; // note that scipy returns -INF for 0 and NaN for negative integers
}
// Use reflection formula
ga_double pi_x = M_PI * x;
ga_double cot_pi_x = cos(pi_x) / sin(pi_x);
return _psi(1.0 - x) + M_PI * cot_pi_x;
}
y = x;
y = x;
if (y <= 0
.0
)
if (y <= 0)
return psi_;
return psi_;
if (y <= S)
if (y <= S)
return D1 - 1.0/y;
return D1 - 1.0/y;
...
...
tests/scalar/test_math.py
浏览文件 @
7799bd08
...
@@ -2,6 +2,7 @@ import itertools
...
@@ -2,6 +2,7 @@ import itertools
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
import
scipy
import
scipy.special
as
sp
import
scipy.special
as
sp
import
pytensor.tensor
as
pt
import
pytensor.tensor
as
pt
...
@@ -19,6 +20,7 @@ from pytensor.scalar.math import (
...
@@ -19,6 +20,7 @@ from pytensor.scalar.math import (
gammal
,
gammal
,
gammau
,
gammau
,
hyp2f1
,
hyp2f1
,
psi
,
)
)
from
tests.link.test_link
import
make_function
from
tests.link.test_link
import
make_function
...
@@ -149,3 +151,28 @@ def test_scalarloop_grad_mixed_dtypes(op, scalar_loop_grads):
...
@@ -149,3 +151,28 @@ def test_scalarloop_grad_mixed_dtypes(op, scalar_loop_grads):
(
var
.
owner
and
isinstance
(
var
.
owner
.
op
,
ScalarLoop
))
(
var
.
owner
and
isinstance
(
var
.
owner
.
op
,
ScalarLoop
))
for
var
in
ancestors
(
grad
)
for
var
in
ancestors
(
grad
)
)
)
@pytest.mark.parametrize
(
"linker"
,
[
"py"
,
"c"
],
)
def
test_psi
(
linker
):
x
=
float64
(
"x"
)
out
=
psi
(
x
)
fn
=
function
([
x
],
out
,
mode
=
Mode
(
linker
=
linker
,
optimizer
=
"fast_run"
))
fn
.
dprint
()
x_test
=
np
.
float64
(
0.5
)
np
.
testing
.
assert_allclose
(
fn
(
x_test
),
scipy
.
special
.
psi
(
x_test
),
strict
=
True
,
)
np
.
testing
.
assert_allclose
(
fn
(
-
x_test
),
scipy
.
special
.
psi
(
-
x_test
),
strict
=
True
,
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论