Skip to content

Commit

Permalink
[Feat] Provide derivatives for pow (#246)
Browse files Browse the repository at this point in the history
* [Feat] Provide manual derivatives for __pow__

* [Feat] Also applied changes to rpow

* [Test] Another pow test added.
  • Loading branch information
fjosw authored Nov 26, 2024
1 parent 0ce765a commit 30bfb55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 3 additions & 6 deletions pyerrors/obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,15 +856,12 @@ def __rtruediv__(self, y):

def __pow__(self, y):
if isinstance(y, Obs):
return derived_observable(lambda x: x[0] ** x[1], [self, y])
return derived_observable(lambda x, **kwargs: x[0] ** x[1], [self, y], man_grad=[y.value * self.value ** (y.value - 1), self.value ** y.value * np.log(self.value)])
else:
return derived_observable(lambda x: x[0] ** y, [self])
return derived_observable(lambda x, **kwargs: x[0] ** y, [self], man_grad=[y * self.value ** (y - 1)])

def __rpow__(self, y):
if isinstance(y, Obs):
return derived_observable(lambda x: x[0] ** x[1], [y, self])
else:
return derived_observable(lambda x: y ** x[0], [self])
return derived_observable(lambda x, **kwargs: y ** x[0], [self], man_grad=[y ** self.value * np.log(y)])

def __abs__(self):
return derived_observable(lambda x: anp.abs(x[0]), [self])
Expand Down
12 changes: 12 additions & 0 deletions tests/obs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,18 @@ def test_cobs_overloading():
obs / cobs


def test_pow():
data = [1, 2.341, pe.pseudo_Obs(4.8, 0.48, "test_obs"), pe.cov_Obs(1.1, 0.3 ** 2, "test_cov_obs")]

for d in data:
assert d * d == d ** 2
assert d * d * d == d ** 3

for d2 in data:
assert np.log(d ** d2) == d2 * np.log(d)
assert (d ** d2) ** (1 / d2) == d


def test_reweighting():
my_obs = pe.Obs([np.random.rand(1000)], ['t'])
assert not my_obs.reweighted
Expand Down

0 comments on commit 30bfb55

Please sign in to comment.