Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Arguments
---------
substitutions : dict or key
Either a dictionary whose keys are strings, Variables, or VarKeys,
and whose values are numbers, or a string, Variable or Varkey.
val : number (optional)
If the substitutions entry is a single key, val holds the value
require_positive : boolean (optional, default is True)
Controls whether the returned value can be a Signomial.
Returns
-------
Returns substituted nomial.
"""
_, exps, cs, _ = substitution(self, substitutions)
return Signomial(exps, cs, require_positive=require_positive)
if isinstance(self.cs, Quantity) or isinstance(other.cs, Quantity):
if not isinstance(self.cs, Quantity):
sunits = ureg.dimensionless
else:
sunits = Quantity(1, self.cs[0].units)
if not isinstance(other.cs, Quantity):
ounits = ureg.dimensionless
else:
ounits = Quantity(1, other.cs[0].units)
# HACK: fix for pint not working with np.outer
C = C * sunits * ounits
Exps = np.empty((len(self.exps), len(other.exps)), dtype="object")
for i, exp_s in enumerate(self.exps):
for j, exp_o in enumerate(other.exps):
Exps[i, j] = exp_s + exp_o
return Signomial(Exps.flatten(), C.flatten())
elif isinstance(other, NomialArray):
return np.array(self)*other
else:
return NotImplemented
def __init__(self, left, oper, right):
super(ScalarSingleEquationConstraint,
self).__init__(Signomial(left), oper, Signomial(right))
self.varkeys = KeySet(self.left.varlocs)
self.varkeys.update(self.right.varlocs)
def diff(self, wrt):
"""Derivative of this with respect to a Variable
Arguments
---------
wrt (Variable):
Variable to take derivative with respect to
Returns
-------
Signomial (or Posynomial or Monomial)
"""
deriv = super(Signomial, self).diff(wrt)
# pylint: disable=unexpected-keyword-arg
return Signomial(deriv.exps, deriv.cs, require_positive=False)
def subinplace(self, substitutions):
"Substitutes in place."
_, exps, cs, _ = substitution(self, substitutions)
super(Signomial, self).__init__(exps, cs)
def diff(self, wrt):
"""Derivative of this with respect to a Variable
Arguments
---------
wrt (Variable):
Variable to take derivative with respect to
Returns
-------
Signomial (or Posynomial or Monomial)
"""
deriv = super(Signomial, self).diff(wrt)
# pylint: disable=unexpected-keyword-arg
return Signomial(deriv.exps, deriv.cs, require_positive=False)
def __sub__(self, other):
from .. import SIGNOMIALS_ENABLED
if SIGNOMIALS_ENABLED:
return self + -other
else:
return NotImplemented
def __rsub__(self, other):
from .. import SIGNOMIALS_ENABLED
if SIGNOMIALS_ENABLED:
return other + -self
else:
return NotImplemented
class Posynomial(Signomial):
"""A Signomial with strictly positive cs
Arguments
---------
Same as Signomial.
Note: Posynomial historically supported several different init formats
These will be deprecated in the future, replaced with a single
__init__ syntax, same as Signomial.
"""
def __le__(self, other):
if isinstance(other, Numbers + (Monomial,)):
return PosynomialInequality(self, "<=", other)
else:
# fall back on other's __ge__
return NotImplemented
return NotImplemented
def __neg__(self):
from .. import SIGNOMIALS_ENABLED
return -1*self if SIGNOMIALS_ENABLED else NotImplemented
def __sub__(self, other):
from .. import SIGNOMIALS_ENABLED
return self + -other if SIGNOMIALS_ENABLED else NotImplemented
def __rsub__(self, other):
from .. import SIGNOMIALS_ENABLED
return other + -self if SIGNOMIALS_ENABLED else NotImplemented
class Posynomial(Signomial):
"""A Signomial with strictly positive cs
Arguments
---------
Same as Signomial.
Note: Posynomial historically supported several different init formats
These will be deprecated in the future, replaced with a single
__init__ syntax, same as Signomial.
"""
def __le__(self, other):
if isinstance(other, Numbers + (Monomial,)):
return PosynomialInequality(self, "<=", other)
# fall back on other's __ge__
return NotImplemented
# Posynomial.__ge__ falls back on Signomial.__ge__
Arguments
---------
substitutions : dict or key
Either a dictionary whose keys are strings, Variables, or VarKeys,
and whose values are numbers, or a string, Variable or Varkey.
val : number (optional)
If the substitutions entry is a single key, val holds the value
require_positive : boolean (optional, default is True)
Controls whether the returned value can be a Signomial.
Returns
-------
Returns substituted nomial.
"""
_, exps, cs, _ = substitution(self, substitutions)
return Signomial(exps, cs, require_positive=require_positive)