提交 2bf690af authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Remove use of deprecated np.complex

上级 258d5f12
...@@ -2159,7 +2159,7 @@ class Mod(BinaryScalarOp): ...@@ -2159,7 +2159,7 @@ class Mod(BinaryScalarOp):
) )
def impl(self, x, y): def impl(self, x, y):
if isinstance(x, np.complex) or isinstance(y, np.complex): if isinstance(x, builtins.complex) or isinstance(y, builtins.complex):
raise self.complex_error raise self.complex_error
return x % y return x % y
...@@ -3936,7 +3936,7 @@ class Complex(BinaryScalarOp): ...@@ -3936,7 +3936,7 @@ class Complex(BinaryScalarOp):
return [complex64] return [complex64]
def impl(self, x, y): def impl(self, x, y):
return np.complex(x, y) return builtins.complex(x, y)
def grad(self, inputs, gout): def grad(self, inputs, gout):
(x, y) = inputs (x, y) = inputs
...@@ -3979,9 +3979,9 @@ class ComplexFromPolar(BinaryScalarOp): ...@@ -3979,9 +3979,9 @@ class ComplexFromPolar(BinaryScalarOp):
x = r * np.cos(theta) x = r * np.cos(theta)
y = r * np.sin(theta) y = r * np.sin(theta)
if x.dtype == "float32": if x.dtype == "float32":
return np.complex64(np.complex(x, y)) return np.complex64(builtins.complex(x, y))
else: else:
return np.complex128(np.complex(x, y)) return np.complex128(builtins.complex(x, y))
def grad(self, inputs, gout): def grad(self, inputs, gout):
(r, theta) = inputs (r, theta) = inputs
......
...@@ -4,7 +4,8 @@ import pytest ...@@ -4,7 +4,8 @@ import pytest
import aesara import aesara
from aesara.gradient import GradientError from aesara.gradient import GradientError
from aesara.tensor.basic import cast from aesara.tensor.basic import cast
from aesara.tensor.math import complex, complex_from_polar, imag, real from aesara.tensor.math import complex as at_complex
from aesara.tensor.math import complex_from_polar, imag, real
from aesara.tensor.type import cvector, dvector, fmatrix, fvector, imatrix, zvector from aesara.tensor.type import cvector, dvector, fmatrix, fvector, imatrix, zvector
from tests import unittest_tools as utt from tests import unittest_tools as utt
...@@ -15,8 +16,7 @@ class TestRealImag: ...@@ -15,8 +16,7 @@ class TestRealImag:
rng = np.random.default_rng(23) rng = np.random.default_rng(23)
xval = np.asarray( xval = np.asarray(
list( list(
np.complex(rng.standard_normal(), rng.standard_normal()) complex(rng.standard_normal(), rng.standard_normal()) for i in range(10)
for i in range(10)
) )
) )
assert np.all(xval.real == aesara.function([x], real(x))(xval)) assert np.all(xval.real == aesara.function([x], real(x))(xval))
...@@ -42,7 +42,7 @@ class TestRealImag: ...@@ -42,7 +42,7 @@ class TestRealImag:
def test_complex(self): def test_complex(self):
rng = np.random.default_rng(2333) rng = np.random.default_rng(2333)
m = fmatrix() m = fmatrix()
c = complex(m[0], m[1]) c = at_complex(m[0], m[1])
assert c.type == cvector assert c.type == cvector
r, i = [real(c), imag(c)] r, i = [real(c), imag(c)]
assert r.type == fvector assert r.type == fvector
...@@ -57,7 +57,7 @@ class TestRealImag: ...@@ -57,7 +57,7 @@ class TestRealImag:
@pytest.mark.skip(reason="Complex grads not enabled, see #178") @pytest.mark.skip(reason="Complex grads not enabled, see #178")
def test_complex_grads(self): def test_complex_grads(self):
def f(m): def f(m):
c = complex(m[0], m[1]) c = at_complex(m[0], m[1])
return 0.5 * real(c) + 0.9 * imag(c) return 0.5 * real(c) + 0.9 * imag(c)
rng = np.random.default_rng(9333) rng = np.random.default_rng(9333)
...@@ -67,7 +67,7 @@ class TestRealImag: ...@@ -67,7 +67,7 @@ class TestRealImag:
@pytest.mark.skip(reason="Complex grads not enabled, see #178") @pytest.mark.skip(reason="Complex grads not enabled, see #178")
def test_mul_mixed0(self): def test_mul_mixed0(self):
def f(a): def f(a):
ac = complex(a[0], a[1]) ac = at_complex(a[0], a[1])
return abs((ac) ** 2).sum() return abs((ac) ** 2).sum()
rng = np.random.default_rng(9333) rng = np.random.default_rng(9333)
...@@ -82,7 +82,7 @@ class TestRealImag: ...@@ -82,7 +82,7 @@ class TestRealImag:
@pytest.mark.skip(reason="Complex grads not enabled, see #178") @pytest.mark.skip(reason="Complex grads not enabled, see #178")
def test_mul_mixed1(self): def test_mul_mixed1(self):
def f(a): def f(a):
ac = complex(a[0], a[1]) ac = at_complex(a[0], a[1])
return abs(ac).sum() return abs(ac).sum()
rng = np.random.default_rng(9333) rng = np.random.default_rng(9333)
...@@ -97,7 +97,7 @@ class TestRealImag: ...@@ -97,7 +97,7 @@ class TestRealImag:
@pytest.mark.skip(reason="Complex grads not enabled, see #178") @pytest.mark.skip(reason="Complex grads not enabled, see #178")
def test_mul_mixed(self): def test_mul_mixed(self):
def f(a, b): def f(a, b):
ac = complex(a[0], a[1]) ac = at_complex(a[0], a[1])
return abs((ac * b) ** 2).sum() return abs((ac * b) ** 2).sum()
rng = np.random.default_rng(9333) rng = np.random.default_rng(9333)
...@@ -123,7 +123,7 @@ class TestRealImag: ...@@ -123,7 +123,7 @@ class TestRealImag:
@pytest.mark.skip(reason="Complex grads not enabled, see #178") @pytest.mark.skip(reason="Complex grads not enabled, see #178")
def test_abs_grad(self): def test_abs_grad(self):
def f(m): def f(m):
c = complex(m[0], m[1]) c = at_complex(m[0], m[1])
return 0.5 * abs(c) return 0.5 * abs(c)
rng = np.random.default_rng(9333) rng = np.random.default_rng(9333)
......
...@@ -1832,12 +1832,8 @@ class TestDivimpl: ...@@ -1832,12 +1832,8 @@ class TestDivimpl:
assert np.allclose(function([i, ii], ii // i)(5, 3), (3 // 5)) assert np.allclose(function([i, ii], ii // i)(5, 3), (3 // 5))
assert np.allclose(function([i, ii], true_div(i, ii))(5, 3), (5.0 / 3.0)) assert np.allclose(function([i, ii], true_div(i, ii))(5, 3), (5.0 / 3.0))
assert np.allclose(function([i, ii], true_div(ii, i))(5, 3), (3.0 / 5.0)) assert np.allclose(function([i, ii], true_div(ii, i))(5, 3), (3.0 / 5.0))
assert np.allclose( assert np.allclose(function([i, c], i / c)(5, complex(5, 3)), (5.0 / (5 + 3j)))
function([i, c], i / c)(5, np.complex(5, 3)), (5.0 / (5 + 3j)) assert np.allclose(function([i, c], c / i)(5, complex(5, 3)), ((5 + 3j) / 5.0))
)
assert np.allclose(
function([i, c], c / i)(5, np.complex(5, 3)), ((5 + 3j) / 5.0)
)
class TestMean: class TestMean:
...@@ -1929,17 +1925,17 @@ class TestDot: ...@@ -1929,17 +1925,17 @@ class TestDot:
# This strange way of doing things is the only way that worked # This strange way of doing things is the only way that worked
# on NumPy 1.4.1. # on NumPy 1.4.1.
if r.ndim == 0: if r.ndim == 0:
return np.asarray(np.complex(1.1, 2.1), dtype=r.dtype) return np.asarray(complex(1.1, 2.1), dtype=r.dtype)
if r.ndim == 1: if r.ndim == 1:
if r.dtype == "complex64": if r.dtype == "complex64":
return np.complex64([np.complex(1.2, 2.2)]) return np.complex64([complex(1.2, 2.2)])
elif r.dtype == "complex128": elif r.dtype == "complex128":
return np.complex128([np.complex(1.2, 2.2)]) return np.complex128([complex(1.2, 2.2)])
elif r.ndim == 2: elif r.ndim == 2:
if r.dtype == "complex64": if r.dtype == "complex64":
return np.complex64([[np.complex(1.3, 2.3)]]) return np.complex64([[complex(1.3, 2.3)]])
elif r.dtype == "complex128": elif r.dtype == "complex128":
return np.complex128([[np.complex(1.3, 2.3)]]) return np.complex128([[complex(1.3, 2.3)]])
if r.ndim == 0: if r.ndim == 0:
return np.asarray(1.1, dtype=r.dtype) return np.asarray(1.1, dtype=r.dtype)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论