Page MenuHomec4science

parameter.py
No OneTemporary

File Metadata

Created
Sat, Apr 27, 18:04

parameter.py

# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from copy import deepcopy as copy
from rrompy.utilities.base.types import TupleAny
from rrompy.utilities.exception_manager import RROMPyException, RROMPyAssert
__all__ = ['parameter']
class parameter:
"""HERE"""
def __init__(self, data:TupleAny, lengthCheck : int = None):
if (isinstance(data, (list,))
or (hasattr(data, "shape") and isinstance(data.shape, (tuple,))
and len(data.shape) > 1)):
raise RROMPyException(("Parameter data cannot be a list. Tuple "
"required."))
if not hasattr(data, "__len__"): data = [data]
if isinstance(data, (self.__class__,)): data = data.data
self.data = tuple(data)
if lengthCheck is not None:
RROMPyAssert(len(self), lengthCheck, "Number of parameters")
self._dtype = type(sum(self.data))
def __len__(self):
return len(self.data)
def __str__(self):
return str(self.data)
def __repr__(self):
return repr(self.data)
@property
def shape(self):
return (len(self))
@property
def re(self):
return parameter(tuple([np.real(x) for x in self.data]))
@property
def im(self):
return parameter(tuple([np.imag(x) for x in self.data]))
@property
def abs(self):
return parameter(tuple([np.abs(x) for x in self.data]))
@property
def angle(self):
return parameter(tuple([np.angle(x) for x in self.data]))
@property
def conj(self):
return parameter(tuple([np.conj(x) for x in self.data]))
@property
def dtype(self):
return self._dtype
@dtype.setter
def dtype(self, dtype):
self._dtype = dtype
def __call__(self, dim):
return self.data[dim]
def __eq__(self, other):
if not hasattr(other, "shape") or self.shape != other.shape:
return False
if isinstance(other, self.__class__):
fac = other.data
else:
fac = other
return isinstance(fac, (list, tuple,)) and np.allclose(self.data, fac)
def __ne__(self, other):
return not self == other
def __copy__(self):
return parameter(self.data)
def __deepcopy__(self, memo):
return parameter(copy(self.data, memo))
def __add__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
data = [self(j) + fac[j] for j in range(len(self))]
return parameter(tuple(data))
def __iadd__(self, other):
self.data = (self + other).data
return self
def __sub__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
data = [self(j) - fac[j] for j in range(len(self))]
return parameter(tuple(data))
def __isub__(self, other):
self.data = (self - other).data
return self
def __mul__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
data = [self(j) * fac[j] for j in range(len(self))]
return parameter(tuple(data))
def __imul__(self, other):
self.data = (self * other).data
return self
def __truediv__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
data = [self(j) / fac[j] for j in range(len(self))]
return parameter(tuple(data))
def __idiv__(self, other):
self.data = (self / other).data
return self
def __pow__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
data = [self(j) ** fac[j] for j in range(len(self))]
return parameter(tuple(data))
def __ipow__(self, other):
self.data = (self ** other).data
return self
def __neg__(self):
return parameter(tuple([-x for x in self.data]))
def __pos__(self):
return parameter(self.data)
def __lt__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
return self.data < fac
def __le__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
return self.data <= fac
def __ge__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
return self.data >= fac
def __gt__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(len(self), len(other), "Number of parameters")
fac = other.data
elif hasattr(other, "__len__"):
fac = other
else:
fac = [other] * len(self)
return self.data > fac
def flatten(self, idx = 0):
return self(idx)

Event Timeline