Commit 0bb08987 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

cosmetics

parent fa1348a7
Pipeline #20795 failed with stage
in 4 minutes and 39 seconds
......@@ -5,26 +5,33 @@ from .random import Random
class data_object(object):
def __init__(self, npdata):
self._data = np.asarray(npdata)
def __getitem__(self, key):
res = self._data[key]
return res if np.isscalar(res) else data_object(res)
def __setitem__(self, key, value):
if isinstance(value, data_object):
self._data[key] = value._data
else:
self._data[key] = value
@property
def dtype(self):
return self._data.dtype
@property
def shape(self):
return self._data.shape
@property
def size(self):
return self._data.size
@property
def real(self):
return data_object(self._data.real)
@property
def imag(self):
return data_object(self._data.imag)
......@@ -46,51 +53,68 @@ class data_object(object):
return self._contraction_helper("sum", axis)
def _binary_helper(self, other, op):
a=self._data
a = self._data
if isinstance(other, data_object):
b=other._data
#if a.shape != b.shape:
# print("shapes are incompatible.")
b = other._data
# if a.shape != b.shape:
# print("shapes are incompatible.")
else:
b=other
b = other
tval = getattr(a, op)(b)
return self if tval is a else data_object(tval)
def __add__(self, other):
return self._binary_helper(other, op='__add__')
def __radd__(self, other):
return self._binary_helper(other, op='__radd__')
def __iadd__(self, other):
return self._binary_helper(other, op='__iadd__')
def __sub__(self, other):
return self._binary_helper(other, op='__sub__')
def __rsub__(self, other):
return self._binary_helper(other, op='__rsub__')
def __isub__(self, other):
return self._binary_helper(other, op='__isub__')
def __mul__(self, other):
return self._binary_helper(other, op='__mul__')
def __rmul__(self, other):
return self._binary_helper(other, op='__rmul__')
def __imul__(self, other):
return self._binary_helper(other, op='__imul__')
def __div__(self, other):
return self._binary_helper(other, op='__div__')
def __rdiv__(self, other):
return self._binary_helper(other, op='__rdiv__')
def __truediv__(self, other):
return self._binary_helper(other, op='__truediv__')
def __rtruediv__(self, other):
return self._binary_helper(other, op='__rtruediv__')
def __pow__(self, other):
return self._binary_helper(other, op='__pow__')
def __rpow__(self, other):
return self._binary_helper(other, op='__rpow__')
def __ipow__(self, other):
return self._binary_helper(other, op='__ipow__')
def __eq__(self, other):
return self._binary_helper(other, op='__eq__')
def __ne__(self, other):
return self._binary_helper(other, op='__ne__')
......@@ -108,9 +132,11 @@ class data_object(object):
def all(self):
return self._data.all()
def any(self):
return self._data.any()
def full(shape, fill_value, dtype=None):
return data_object(np.full(shape, fill_value, dtype))
......@@ -131,7 +157,7 @@ def empty_like(a, dtype=None):
return data_object(np.empty_like(a._data, dtype))
def vdot(a,b):
def vdot(a, b):
return np.vdot(a._data, b._data)
......@@ -178,8 +204,10 @@ def from_random(random_type, shape, dtype=np.float64, **kwargs):
generator_function = getattr(Random, random_type)
return data_object(generator_function(dtype=dtype, shape=shape, **kwargs))
def to_ndarray(arr):
return arr._data
def from_ndarray(arr):
return data_object(arr)
......@@ -14,8 +14,10 @@ def from_random(random_type, shape, dtype=np.float64, **kwargs):
generator_function = getattr(Random, random_type)
return generator_function(dtype=dtype, shape=shape, **kwargs)
def to_ndarray(arr):
return arr
def from_ndarray(arr):
return np.asarray(arr)
......@@ -17,13 +17,12 @@
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import division
from builtins import range
import numpy as np
from ..field import Field
from ..domain_tuple import DomainTuple
from .endomorphic_operator import EndomorphicOperator
from ..nifty_utilities import cast_iseq_to_tuple
from .. import dobj
class DiagonalOperator(EndomorphicOperator):
""" NIFTY class for diagonal operators.
......@@ -87,8 +86,9 @@ class DiagonalOperator(EndomorphicOperator):
raise ValueError("too many spaces")
if nspc > len(set(self._spaces)):
raise ValueError("non-unique space indices")
# if nspc==len(self.diagonal.domain.domains, we could do some optimization
for i, j in enumerate(self._spaces):
# if nspc==len(self.diagonal.domain.domains,
# we could do some optimization
for i, j in enumerate(self._spaces):
if diagonal.domain[i] != self._domain[j]:
raise ValueError("domain mismatch")
......
......@@ -22,6 +22,7 @@ from .. import nifty_utilities as utilities
from ..low_level_library import hartley
from ..dobj import to_ndarray as to_np, from_ndarray as from_np
class Transformation(object):
def __init__(self, domain, codomain):
self.domain = domain
......@@ -69,8 +70,8 @@ class RGRGTransformation(Transformation):
# Perform the transformation
if issubclass(val.dtype.type, np.complexfloating):
Tval = from_np(hartley(to_np(val.real), axes) \
+ 1j*hartley(to_np(val.imag), axes))
Tval = from_np(hartley(to_np(val.real), axes) +
1j*hartley(to_np(val.imag), axes))
else:
Tval = from_np(hartley(to_np(val), axes))
......
......@@ -21,7 +21,6 @@ from ..field import Field
from ..spaces.power_space import PowerSpace
from .endomorphic_operator import EndomorphicOperator
from .. import DomainTuple
from .. import nifty_utilities as utilities
from ..dobj import to_ndarray as to_np, from_ndarray as from_np
......@@ -50,7 +49,7 @@ class LaplaceOperator(EndomorphicOperator):
raise ValueError("need a Field with exactly one domain")
space = 0
space = int(space)
if (space<0) or space>=len(self._domain.domains):
if space < 0 or space >= len(self._domain.domains):
raise ValueError("space index out of range")
self._space = space
......@@ -107,7 +106,8 @@ class LaplaceOperator(EndomorphicOperator):
ret /= np.sqrt(dposc)
ret[prefix + (slice(None, 2),)] = 0.
ret[prefix + (-1,)] = 0.
return Field(self.domain, val=from_np(ret)).weight(-0.5, spaces=self._space)
return Field(self.domain, val=from_np(ret)).weight(-0.5,
spaces=self._space)
def _adjoint_times(self, x):
axes = x.domain.axes[self._space]
......@@ -127,4 +127,5 @@ class LaplaceOperator(EndomorphicOperator):
ret[sl_l] = deriv
ret[prefix + (-1,)] = 0.
ret[sl_r] -= deriv
return Field(self.domain, val=from_np(ret)).weight(-1, spaces=self._space)
return Field(self.domain, val=from_np(ret)).weight(-1,
spaces=self._space)
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