Skip to content

Commit 7f8c2ce

Browse files
authored
feat: added support for addition and multiplication of complex numbers (#209)
to Corr objects.
1 parent 01ef97f commit 7f8c2ce

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

pyerrors/correlators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ def __add__(self, y):
10751075
newcontent.append(self.content[t] + y.content[t])
10761076
return Corr(newcontent)
10771077

1078-
elif isinstance(y, (Obs, int, float, CObs)):
1078+
elif isinstance(y, (Obs, int, float, CObs, complex)):
10791079
newcontent = []
10801080
for t in range(self.T):
10811081
if _check_for_none(self, self.content[t]):
@@ -1103,7 +1103,7 @@ def __mul__(self, y):
11031103
newcontent.append(self.content[t] * y.content[t])
11041104
return Corr(newcontent)
11051105

1106-
elif isinstance(y, (Obs, int, float, CObs)):
1106+
elif isinstance(y, (Obs, int, float, CObs, complex)):
11071107
newcontent = []
11081108
for t in range(self.T):
11091109
if _check_for_none(self, self.content[t]):

pyerrors/obs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,8 @@ def __add__(self, y):
784784
else:
785785
if isinstance(y, np.ndarray):
786786
return np.array([self + o for o in y])
787+
elif isinstance(y, complex):
788+
return CObs(self, 0) + y
787789
elif y.__class__.__name__ in ['Corr', 'CObs']:
788790
return NotImplemented
789791
else:

tests/correlators_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,13 @@ def test_corr_item():
749749
corr_mat = pe.Corr(np.array([[corr_aa, corr_ab], [corr_ab, corr_aa]]))
750750
corr_mat.item(0, 0)
751751
assert corr_mat[0].item(0, 1) == corr_mat.item(0, 1)[0]
752+
753+
754+
def test_complex_add_and_mul():
755+
o = pe.pseudo_Obs(1.0, 0.3, "my_r345sfg16£$%&$%^%$^$", samples=47)
756+
co = pe.CObs(o, 0.341 * o)
757+
for obs in [o, co]:
758+
cc = pe.Corr([obs for _ in range(4)])
759+
cc += 2j
760+
cc = cc * 4j
761+
cc.real + cc.imag

tests/obs_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,3 +1333,10 @@ def test_vec_gm():
13331333
cc = pe.Corr(obs)
13341334
pe.gm(cc, S=4.12)
13351335
assert np.all(np.vectorize(lambda x: x.S["qq"])(cc.content) == 4.12)
1336+
1337+
def test_complex_addition():
1338+
o = pe.pseudo_Obs(34.12, 1e-4, "testens")
1339+
r = o + 2j
1340+
assert r.real == o
1341+
r = r * 1j
1342+
assert r.imag == o

0 commit comments

Comments
 (0)