gpkit.nomials package¶
Submodules¶
gpkit.nomials.array module¶
Module for creating NomialArray instances.
Example¶
>>> x = gpkit.Monomial('x')
>>> px = gpkit.NomialArray([1, x, x**2])
-
class
gpkit.nomials.array.
NomialArray
¶ Bases:
numpy.ndarray
A Numpy array with elementwise inequalities and substitutions.
input_array : array-like
>>> px = gpkit.NomialArray([1, x, x**2])
-
latex
(matwrap=True)¶ Returns 1D latex list of contents.
-
left
¶ Returns (0, self[0], self[1] … self[N-1])
-
outer
(other)¶ Returns the array and argument’s outer product.
-
padleft
(padding)¶ Returns ({padding}, self[0], self[1] … self[N])
-
padright
(padding)¶ Returns (self[0], self[1] … self[N], {padding})
-
prod
(*args, **kwargs)¶ Returns a product. O(N) if no arguments and only contains monomials.
-
right
¶ Returns (self[1], self[2] … self[N], 0)
-
str_without
(excluded=None)¶ Returns string without certain fields (such as ‘models’).
-
sub
(subs, require_positive=True)¶ Substitutes into the array
-
sum
(*args, **kwargs)¶ Returns a sum. O(N) if no arguments are given.
-
units
¶ units must have same dimensions across the entire nomial array
-
vectorize
(function, *args, **kwargs)¶ Apply a function to each terminal constraint, returning the array
-
-
gpkit.nomials.array.
array_constraint
(symbol, func)¶ Return function which creates constraints of the given operator.
gpkit.nomials.core module¶
The shared non-mathematical backbone of all Nomials
-
class
gpkit.nomials.core.
Nomial
(hmap)¶ Bases:
gpkit.nomials.data.NomialData
Shared non-mathematical properties of all nomials
-
latex
(excluded=None)¶ Latex representation, parsing excluded just as .str_without does
-
prod
()¶ Return self for compatibility with NomialArray
-
str_without
(excluded=None)¶ String representation, excluding fields (‘units’, varkey attributes)
-
sub
= None¶
-
sum
()¶ Return self for compatibility with NomialArray
-
to
(units)¶ Create new Signomial converted to new units
-
unitstr
(into='%s', options=None, dimless='')¶ Returns the string corresponding to an object’s units.
-
value
¶ Self, with values substituted for variables that have values
float, if no symbolic variables remain after substitution (Monomial, Posynomial, or Nomial), otherwise.
-
gpkit.nomials.data module¶
Machinery for exps, cs, varlocs data – common to nomials and programs
-
class
gpkit.nomials.data.
NomialData
(hmap)¶ Bases:
object
Object for holding cs, exps, and other basic ‘nomial’ properties.
cs: array (coefficient of each monomial term) exps: tuple of {VarKey: float} (exponents of each monomial term) varlocs: {VarKey: list} (terms each variable appears in) units: pint.UnitsContainer
-
cs
¶ Create cs or return cached cs
-
diff
(var)¶ Derivative of this with respect to a Variable
- var (Variable):
- Variable to take derivative with respect to
NomialData
-
exps
¶ Create exps or return cached exps
-
values
¶ The NomialData’s values, created when necessary.
-
varkeys
¶ The NomialData’s varkeys, created when necessary for a substitution.
-
varlocs
¶ Create varlocs or return cached varlocs
-
gpkit.nomials.map module¶
Implements the NomialMap class
-
class
gpkit.nomials.map.
NomialMap
¶ Bases:
gpkit.small_classes.HashVector
Class for efficent algebraic represention of a nomial
A NomialMap is a mapping between hashvectors representing exponents and their coefficients in a posynomial.
For example, {{x : 1}: 2.0, {y: 1}: 3.0} represents 2*x + 3*y, where x and y are VarKey objects.
-
csmap
= None¶
-
diff
(varkey)¶ Differentiates a NomialMap with respect to a varkey
-
expmap
= None¶
-
mmap
(orig)¶ Maps substituted monomials back to the original nomial
- self.expmap is the map from pre- to post-substitution exponents, and
- takes the form {original_exp: new_exp}
self.csmap is the map from pre-substitution exponents to coefficients.
m_from_ms is of the form {new_exp: [old_exps, ]}
- pmap is of the form [{orig_idx1: fraction1, orig_idx2: fraction2, }, ]
- where at the index corresponding to each new_exp is a dictionary mapping the indices corresponding to the old exps to their fraction of the post-substitution coefficient
-
remove_zeros
()¶ Removes zeroed exponents and monomials.
If only_check_cs is True, checks only whether any values are zero. If False also checks whether any exponents in the keys are zero.
-
sub
(substitutions, varkeys, parsedsubs=False)¶ Applies substitutions to a NomialMap
- substitutions : (dict-like)
- list of substitutions to perform
- varkeys : (set-like)
- varkeys that are present in self (required argument so as to require efficient code)
- parsedsubs : bool
- flag if the substitutions have already been parsed to contain only keys in varkeys
-
to
(units)¶ Returns a new NomialMap of the given units
-
units
= None¶
-
units_of_product
(thing, thing2=None)¶ Sets units to those of thing*thing2
-
-
gpkit.nomials.map.
subinplace
(cp, exp, o_exp, vk, cval, expval, exps_covered)¶ Modifies cp by substituing cval/expval for vk in exp
gpkit.nomials.math module¶
Signomial, Posynomial, Monomial, Constraint, & MonoEQCOnstraint classes
-
class
gpkit.nomials.math.
Monomial
(hmap=None, cs=1, require_positive=True, **descr)¶ Bases:
gpkit.nomials.math.Posynomial
A Posynomial with only one term
Same as Signomial. Note: Monomial historically supported several different init formats
These will be deprecated in the future, replaced with a single __init__ syntax, same as Signomial.-
c
¶ Creates c or returns a cached c
-
exp
¶ Creates exp or returns a cached exp
-
mono_approximation
(x0)¶ Monomial approximation about a point x0
- x0 (dict):
- point to monomialize about
Monomial (unless self(x0) < 0, in which case a Signomial is returned)
-
-
class
gpkit.nomials.math.
MonomialEquality
(left, oper, right)¶ Bases:
gpkit.nomials.math.PosynomialInequality
A Constraint of the form Monomial == Monomial.
-
as_posyslt1
(substitutions=None)¶ Tags posynomials for dual feasibility checking
-
sens_from_dual
(la, nu, result)¶ Returns the variable/constraint sensitivities from lambda/nu
-
-
class
gpkit.nomials.math.
Posynomial
(hmap=None, cs=1, require_positive=True, **descr)¶ Bases:
gpkit.nomials.math.Signomial
A Signomial with strictly positive cs
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.-
mono_lower_bound
(x0)¶ Monomial lower bound at a point x0
- x0 (dict):
- point to make lower bound exact
Monomial
-
-
class
gpkit.nomials.math.
PosynomialInequality
(left, oper, right, feastol=None)¶ Bases:
gpkit.nomials.math.ScalarSingleEquationConstraint
A constraint of the general form monomial >= posynomial Stored in the posylt1_rep attribute as a single Posynomial (self <= 1) Usually initialized via operator overloading, e.g. cc = (y**2 >= 1 + x)
-
as_gpconstr
(x0)¶ The GP version of a Posynomial constraint is itself
-
as_posyslt1
(substitutions=None)¶ Returns the posys <= 1 representation of this constraint.
-
feastol
= 0.001¶
-
sens_from_dual
(la, nu, result)¶ Returns the variable/constraint sensitivities from lambda/nu
-
-
class
gpkit.nomials.math.
ScalarSingleEquationConstraint
(left, oper, right)¶ Bases:
gpkit.constraints.single_equation.SingleEquationConstraint
A SingleEquationConstraint with scalar left and right sides.
-
nomials
= []¶
-
relaxed
(relaxvar)¶ Returns the relaxation of the constraint in a list.
-
-
class
gpkit.nomials.math.
Signomial
(hmap=None, cs=1, require_positive=True, **descr)¶ Bases:
gpkit.nomials.core.Nomial
A representation of a Signomial.
- exps: tuple of dicts
- Exponent dicts for each monomial term
- cs: tuple
- Coefficient values for each monomial term
- require_positive: bool
- If True and Signomials not enabled, c <= 0 will raise ValueError
Signomial Posynomial (if the input has only positive cs) Monomial (if the input has one term and only positive cs)
-
diff
(var)¶ Derivative of this with respect to a Variable
var (Variable): Variable to take derivative with respect to
Signomial (or Posynomial or Monomial)
-
mono_approximation
(x0)¶ Monomial approximation about a point x0
- x0 (dict):
- point to monomialize about
Monomial (unless self(x0) < 0, in which case a Signomial is returned)
-
posy_negy
()¶ Get the positive and negative parts, both as Posynomials
- Posynomial, Posynomial:
- p_pos and p_neg in (self = p_pos - p_neg) decomposition,
-
relaxed
(relaxvar)¶ Returns the relaxation of the constraint in a list.
-
sub
(substitutions, require_positive=True)¶ Returns a nomial with substitued values.
3 == (x**2 + y).sub({‘x’: 1, y: 2}) 3 == (x).gp.sub(x, 3)
- 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 substituted nomial.
-
class
gpkit.nomials.math.
SignomialInequality
(left, oper, right)¶ Bases:
gpkit.nomials.math.ScalarSingleEquationConstraint
A constraint of the general form posynomial >= posynomial Stored internally (exps, cs) as a single Signomial (0 >= self) Usually initialized via operator overloading, e.g. cc = (y**2 >= 1 + x - y) Additionally retains input format (lhs vs rhs) in self.left and self.right Form is self.left >= self.right.
-
as_approxsgt
(x0)¶ Returns monomial-greater-than sides, to be called after as_approxlt1
-
as_approxslt
()¶ Returns posynomial-less-than sides of a signomial constraint
-
as_gpconstr
(x0)¶ Returns GP approximation of an SP constraint at x0
-
as_posyslt1
(substitutions=None)¶ Returns the posys <= 1 representation of this constraint.
-
sens_from_dual
(la, nu, result)¶ - We want to do the following chain:
- dlog(Obj)/dlog(monomial[i]) = nu[i] * dlog(monomial)/d(monomial) = 1/(monomial value) * d(monomial)/d(var) = see below * d(var)/dlog(var) = var = dlog(Obj)/dlog(var)
- each final monomial is really
- (coeff signomial)/(negy signomial)
- and by the chain rule d(monomial)/d(var) =
- d(coeff)/d(var)*1/negy + d(1/negy)/d(var)*coeff = d(coeff)/d(var)*1/negy - d(negy)/d(var)*coeff*1/negy**2
-
-
class
gpkit.nomials.math.
SingleSignomialEquality
(left, right)¶ Bases:
gpkit.nomials.math.SignomialInequality
A constraint of the general form posynomial == posynomial
-
as_approxsgt
(x0)¶ Returns monomial-greater-than sides, to be called after as_approxlt1
-
as_approxslt
()¶ Returns posynomial-less-than sides of a signomial constraint
-
as_gpconstr
(x0)¶ Returns GP approximation of an SP constraint at x0
-
as_posyslt1
(substitutions=None)¶ Returns the posys <= 1 representation of this constraint.
-
gpkit.nomials.substitution module¶
Scripts to parse and collate substitutions
-
gpkit.nomials.substitution.
append_sub
(sub, keys, constants, sweep, linkedsweep)¶ Appends sub to constants, sweep, or linkedsweep.
-
gpkit.nomials.substitution.
parse_subs
(varkeys, substitutions, clean=False)¶ Seperates subs into constants, sweeps linkedsweeps actually present.
gpkit.nomials.variables module¶
Implement Variable and ArrayVariable classes
-
class
gpkit.nomials.variables.
ArrayVariable
¶ Bases:
gpkit.nomials.array.NomialArray
A described vector of singlet Monomials.
- shape : int or tuple
- length or shape of resulting array
- *args :
- may contain “name” (Strings)
- “value” (Iterable) “units” (Strings)
and/or “label” (Strings)
- **descr :
- VarKey description
NomialArray of Monomials, each containing a VarKey with name ‘$name_{i}’, where $name is the vector’s name and i is the VarKey’s index.
-
class
gpkit.nomials.variables.
Variable
(*args, **descr)¶ Bases:
gpkit.nomials.math.Monomial
A described singlet Monomial.
- *args : list
- may contain “name” (Strings)
- “value” (Numbers + Quantity) or (Iterable) for a sweep “units” (Strings)
and/or “label” (Strings)
- **descr : dict
- VarKey description
Monomials containing a VarKey with the name ‘$name’, where $name is the vector’s name and i is the VarKey’s index.
-
sub
(*args, **kwargs)¶ Same as nomial substitution, but also allows single-argument calls
x = Variable(‘x’) assert x.sub(3) == Variable(‘x’, value=3)
-
to
(units)¶ Create new Signomial converted to new units
-
class
gpkit.nomials.variables.
VectorizableVariable
(*args, **descr)¶ Bases:
gpkit.nomials.variables.Variable
,gpkit.nomials.variables.ArrayVariable
A Variable outside a vectorized environment, an ArrayVariable within.
-
gpkit.nomials.variables.
addmodelstodescr
(descr, addtonamedvars=None)¶ Add models to descr, optionally adding the second argument to NAMEDVARS
-
gpkit.nomials.variables.
veclinkedfn
(linkedfn, i)¶ Generate an indexed linking function.
Module contents¶
Contains nomials, inequalities, and arrays