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

introduce 'out' argument in basic_arithmetics; other small fixes

parent 8d177bd0
......@@ -27,81 +27,92 @@ __all__ = ['cos', 'sin', 'cosh', 'sinh', 'tan', 'tanh', 'arccos', 'arcsin',
'conjugate', 'clipped_exp', 'limited_exp', 'limited_exp_deriv']
def _math_helper(x, function):
if isinstance(x, Field):
result_val = x.val.apply_scalar_function(function)
result = x.copy_empty(dtype=result_val.dtype)
result.val = result_val
elif isinstance(x, distributed_data_object):
result = x.apply_scalar_function(function, inplace=False)
def _math_helper(x, function, out):
if not isinstance(x, Field):
raise TypeError("This function only accepts Field objects.")
if out is not None:
if not isinstance(out, Field) or x.domain!=out.domain:
raise ValueError("Bad 'out' argument")
function(x.val, out=out.val)
return out
else:
result = function(np.asarray(x))
return Field(domain=x.domain, val=function(x.val))
return result
def cos(x, out=None):
return _math_helper(x, np.cos, out)
def sin(x, out=None):
return _math_helper(x, np.sin, out)
def cos(x):
return _math_helper(x, np.cos)
def cosh(x, out=None):
return _math_helper(x, np.cosh, out)
def sin(x):
return _math_helper(x, np.sin)
def sinh(x, out=None):
return _math_helper(x, np.sinh, out)
def cosh(x):
return _math_helper(x, np.cosh)
def tan(x, out=None):
return _math_helper(x, np.tan, out)
def sinh(x):
return _math_helper(x, np.sinh)
def tanh(x, out=None):
return _math_helper(x, np.tanh, out)
def tan(x):
return _math_helper(x, np.tan)
def arccos(x, out=None):
return _math_helper(x, np.arccos, out)
def tanh(x):
return _math_helper(x, np.tanh)
def arcsin(x, out=None):
return _math_helper(x, np.arcsin, out)
def arccos(x):
return _math_helper(x, np.arccos)
def arccosh(x, out=None):
return _math_helper(x, np.arccosh, out)
def arcsin(x):
return _math_helper(x, np.arcsin)
def arcsinh(x, out=None):
return _math_helper(x, np.arcsinh, out)
def arccosh(x):
return _math_helper(x, np.arccosh)
def arctan(x, out=None):
return _math_helper(x, np.arctan, out)
def arcsinh(x):
return _math_helper(x, np.arcsinh)
def arctanh(x, out=None):
return _math_helper(x, np.arctanh, out)
def arctan(x):
return _math_helper(x, np.arctan)
def sqrt(x, out=None):
return _math_helper(x, np.sqrt, out)
def arctanh(x):
return _math_helper(x, np.arctanh)
def exp(x, out=None):
return _math_helper(x, np.exp, out)
def sqrt(x):
return _math_helper(x, np.sqrt)
def log(x, out=None):
return _math_helper(x, np.log, out)
def exp(x):
return _math_helper(x, np.exp)
def conjugate(x, out=None):
return _math_helper(x, np.conjugate, out)
def clipped_exp(x):
return _math_helper(x, lambda z: np.exp(np.minimum(200, z)))
def conj(x, out=None):
return _math_helper(x, np.conj, out)
def limited_exp(x):
return _math_helper(x, _limited_exp_helper)
def clipped_exp(x, out=None):
return _math_helper(x, lambda z: np.exp(np.minimum(200, z)), out)
def limited_exp(x, out=None):
return _math_helper(x, _limited_exp_helper, out)
def _limited_exp_helper(x):
......@@ -114,8 +125,8 @@ def _limited_exp_helper(x):
return result
def limited_exp_deriv(x):
return _math_helper(x, _limited_exp_deriv_helper)
def limited_exp_deriv(x, out=None):
return _math_helper(x, _limited_exp_deriv_helper, out)
def _limited_exp_deriv_helper(x):
......@@ -127,19 +138,3 @@ def _limited_exp_deriv_helper(x):
result[mask] = np.exp(thr)
result[~mask] = np.exp(x[~mask])
return result
def log(x, base=None):
result = _math_helper(x, np.log)
if base is not None:
result = result/log(base)
return result
def conjugate(x):
return _math_helper(x, np.conjugate)
def conj(x):
return _math_helper(x, np.conjugate)
......@@ -39,7 +39,7 @@ class IterationController(
The concrete convergence criteria can be chosen by inheriting from this
class; the implementer has full flexibility to use whichever criteria are
appropriate for a particular problem - as ong as they can be computed from
appropriate for a particular problem - as long as they can be computed from
the information passed to the controller during the iteration process.
"""
......
......@@ -20,7 +20,6 @@ import numpy as np
from ...field import Field
from ...spaces.power_space import PowerSpace
from ..endomorphic_operator import EndomorphicOperator
from ... import sqrt
from ... import nifty_utilities as utilities
......@@ -109,7 +108,7 @@ class LaplaceOperator(EndomorphicOperator):
ret[sl_l] = deriv
ret[prefix + (-1,)] = 0.
ret[sl_r] -= deriv
ret /= sqrt(dposc)
ret /= np.sqrt(dposc)
ret[prefix + (slice(None, 2),)] = 0.
ret[prefix + (-1,)] = 0.
return Field(self.domain, val=ret).weight(power=-0.5, spaces=spaces)
......@@ -131,7 +130,7 @@ class LaplaceOperator(EndomorphicOperator):
dpos = self._dpos.reshape((1,)*axis + (nval-1,))
dposc = self._dposc.reshape((1,)*axis + (nval,))
y = x.copy().weight(power=0.5).val
y /= sqrt(dposc)
y /= np.sqrt(dposc)
y[prefix + (slice(None, 2),)] = 0.
y[prefix + (-1,)] = 0.
deriv = (y[sl_r]-y[sl_l])/dpos # defined between points
......
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