Commit cc3a84e0 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

Merge branch 'op_pow' into 'NIFTy_5'

Op pow

See merge request ift/nifty-dev!123
parents 3e343cd4 f3cb4e73
from __future__ import absolute_import, division, print_function
import numpy as np
......@@ -101,6 +102,12 @@ class Linearization(object):
def __rtruediv__(self, other):
return self.inverse().__mul__(other)
def __pow__(self, power):
if not np.isscalar(power):
return NotImplemented
return self.new(self._val**power,
makeOp(self._val**(power-1)).scale(power)(self._jac))
def inverse(self):
return self.new(1./self._val, makeOp(-1./(self._val**2))(self._jac))
......
from __future__ import absolute_import, division, print_function
import numpy as np
from ..compat import *
from ..utilities import NiftyMetaBase
......@@ -67,6 +68,16 @@ class Operator(NiftyMetaBase()):
return NotImplemented
return _OpSum(self, x)
def __sub__(self, x):
if not isinstance(x, Operator):
return NotImplemented
return _OpSum(self, -x)
def __pow__(self, power):
if not np.isscalar(power):
return NotImplemented
return _OpChain.make((_PowerOp(self.target, power), self))
def apply(self, x):
raise NotImplementedError
......@@ -108,7 +119,17 @@ class _FunctionApplier(Operator):
return getattr(x, self._funcname)()
# FIXME Is this class used except in _OpChain? Can it be merged?
class _PowerOp(Operator):
def __init__(self, domain, power):
from ..sugar import makeDomain
self._domain = self._target = makeDomain(domain)
self._power = power
def apply(self, x):
self._check_input(x)
return x**self._power
class _CombinedOperator(Operator):
def __init__(self, ops, _callingfrommake=False):
if not _callingfrommake:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment