Commit 18f2710f authored by Tim Keil's avatar Tim Keil

remove assertion and fall back to generic case instead

parent de06f078
Pipeline #64790 passed with stages
in 50 minutes and 15 seconds
......@@ -62,21 +62,18 @@ class Function(ParametricObject):
return self
other = ConstantFunction(other, dim_domain=self.dim_domain)
if self.name != 'LincombFunction':
if other.name == 'LincombFunction':
assert isinstance(other, LincombFunction)
if self.name != 'LincombFunction' or (self.name == 'LincombFunction'
and not isinstance(self, LincombFunction)):
if other.name == 'LincombFunction' and isinstance(other, LincombFunction):
functions = (self,) + other.functions
coefficients = (1.,) + (other.coefficients if sign == 1. else tuple(-c for c in other.coefficients))
else:
functions, coefficients = (self, other), (1., sign)
elif other.name == 'LincombFunction':
assert isinstance(self, LincombFunction)
assert isinstance(other, LincombFunction)
elif other.name == 'LincombFunction' and isinstance(other, LincombFunction):
functions = self.functions + other.functions
coefficients = self.coefficients + (other.coefficients if sign == 1.
else tuple(-c for c in other.coefficients))
else:
assert isinstance(self, LincombFunction)
functions, coefficients = self.functions + (other,), self.coefficients + (sign,)
return LincombFunction(functions, coefficients)
......@@ -92,10 +89,10 @@ class Function(ParametricObject):
other = ConstantFunction(other, dim_domain=self.dim_domain)
# note that 'other' can never be a LincombFunction
if self.name != 'LincombFunction':
if self.name != 'LincombFunction' or (self.name == 'LincombFunction'
and not isinstance(self, LincombFunction)):
functions, coefficients = (other, self), (1., sign)
else:
assert isinstance(self, LincombFunction)
functions = (other,) + self.functions
coefficients = (1.,) + (self.coefficients if sign == 1. else tuple(-c for c in self.coefficients))
......
......@@ -524,21 +524,18 @@ class Operator(ParametricObject):
if not isinstance(other, Operator):
return NotImplemented
from pymor.operators.constructions import LincombOperator
if self.name != 'LincombOperator':
if other.name == 'LincombOperator':
assert isinstance(other, LincombOperator)
if self.name != 'LincombOperator' or (self.name == 'LincombOperator'
and not isinstance(self, LincombOperator)):
if other.name == 'LincombOperator' and isinstance(other, LincombOperator):
operators = (self,) + other.operators
coefficients = (1.,) + (other.coefficients if sign == 1. else tuple(-c for c in other.coefficients))
else:
operators, coefficients = (self, other), (1., sign)
elif other.name == 'LincombOperator':
assert isinstance(self, LincombOperator)
assert isinstance(other, LincombOperator)
elif other.name == 'LincombOperator' and isinstance(other, LincombOperator):
operators = self.operators + other.operators
coefficients = self.coefficients + (other.coefficients if sign == 1.
else tuple(-c for c in other.coefficients))
else:
assert isinstance(self, LincombOperator)
operators, coefficients = self.operators + (other,), self.coefficients + (sign,)
return LincombOperator(operators, coefficients, solver_options=self.solver_options)
......@@ -548,10 +545,10 @@ class Operator(ParametricObject):
return NotImplemented
from pymor.operators.constructions import LincombOperator
# note that 'other' can never be a LincombOperator
if self.name != 'LincombOperator':
if self.name != 'LincombOperator' or (self.name == 'LincombOperator'
and not isinstance(self, LincombOperator)):
operators, coefficients = (other, self), (1., sign)
else:
assert isinstance(self, LincombOperator)
operators = (other,) + self.operators
coefficients = (1.,) + (self.coefficients if sign == 1. else tuple(-c for c in self.coefficients))
......@@ -572,10 +569,10 @@ class Operator(ParametricObject):
def __mul__(self, other):
assert isinstance(other, (Number, ParameterFunctional))
from pymor.operators.constructions import LincombOperator
if self.name != 'LincombOperator':
if self.name != 'LincombOperator' or (self.name == 'LincombOperator'
and not isinstance(self, LincombOperator)):
return LincombOperator((self,), (other,))
else:
assert isinstance(self, LincombOperator)
return self.with_(coefficients=tuple(c * other for c in self.coefficients))
def __rmul__(self, other):
......
......@@ -54,21 +54,18 @@ class ParameterFunctional(ParametricObject):
return self
other = ConstantParameterFunctional(other)
if self.name != 'LincombParameterFunctional':
if other.name == 'LincombParameterFunctional':
assert isinstance(other, LincombParameterFunctional)
if self.name != 'LincombParameterFunctional' or (self.name == 'LincombParameterFunctional'
and not isinstance(self, LincombParameterFunctional)):
if other.name == 'LincombParameterFunctional' and isinstance(other, LincombParameterFunctional):
functionals = (self,) + other.functionals
coefficients = (1.,) + (other.coefficients if sign == 1. else tuple(-c for c in other.coefficients))
else:
functionals, coefficients = (self, other), (1., sign)
elif other.name == 'LincombParameterFunctional':
assert isinstance(self, LincombParameterFunctional)
assert isinstance(other, LincombParameterFunctional)
elif other.name == 'LincombParameterFunctional' and isinstance(other, LincombParameterFunctional):
functionals = self.functionals + other.functionals
coefficients = self.coefficients + (other.coefficients if sign == 1.
else tuple(-c for c in other.coefficients))
else:
assert isinstance(self, LincombParameterFunctional)
functionals, coefficients = self.functionals + (other,), self.coefficients + (sign,)
return LincombParameterFunctional(functionals, coefficients)
......@@ -83,10 +80,10 @@ class ParameterFunctional(ParametricObject):
other = ConstantParameterFunctional(other)
# note that 'other' can never be a LincombParameterFunctional
if self.name != 'LincombParameterFunctional':
if self.name != 'LincombParameterFunctional' or (self.name == 'LincombParameterFunctional'
and not isinstance(self, LincombParameterFunctional)):
functionals, coefficients = (other, self), (1., sign)
else:
assert isinstance(self, LincombParameterFunctional)
functionals = (other,) + self.functionals
coefficients = (1.,) + (self.coefficients if sign == 1. else tuple(-c for c in self.coefficients))
......@@ -107,17 +104,16 @@ class ParameterFunctional(ParametricObject):
def __mul__(self, other):
if not isinstance(other, (Number, ParameterFunctional)):
return NotImplemented
if self.name != 'ProductParameterFunctional':
if self.name != 'ProductParameterFunctional' or (self.name == 'LincombParameterFunctional'
and not isinstance(self, LincombParameterFunctional)):
if isinstance(other, ProductParameterFunctional) and other.name == 'ProductParameterFunctional':
return other.with_(factors=other.factors + [self])
else:
return ProductParameterFunctional([self, other])
elif isinstance(other, ProductParameterFunctional) and other.name == 'ProductParameterFunctional':
assert isinstance(self, ProductParameterFunctional)
factors = self.factors + other.factors
return ProductParameterFunctional(factors)
else:
assert isinstance(self, ProductParameterFunctional)
return self.with_(factors=self.factors + [other])
__rmul__ = __mul__
......
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