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: gpkit.repr_conventions.ReprMixin, numpy.ndarray

A Numpy array with elementwise inequalities and substitutions.

input_array : array-like

>>> px = gpkit.NomialArray([1, x, x**2])
latex(excluded=())

Returns latex representation without certain fields.

outer(other)

Returns the array and argument’s outer product.

prod(*args, **kwargs)

Returns a product. O(N) if no arguments and only contains monomials.

str_without(excluded=())

Returns string without certain fields (such as ‘lineage’).

sub(subs, require_positive=True)

Substitutes into the array

sum(*args, **kwargs)

Returns a sum. O(N) if no arguments are given.

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=())

Latex representation, parsing excluded just as .str_without does

prod()

Return self for compatibility with NomialArray

str_without(excluded=())

String representation, excluding fields (‘units’, varkey attributes)

sub = None
sum()

Return self for compatibility with NomialArray

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.core.nomial_latex_helper(c, pos_vars, neg_vars)

Combines (varlatex, exponent) tuples, separated by positive vs negative exponent, into a single latex string.

gpkit.nomials.data module

Machinery for exps, cs, varlocs data – common to nomials and programs

class gpkit.nomials.data.NomialData(hmap)

Bases: gpkit.repr_conventions.ReprMixin

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

exps

Create exps or return cached exps

to(units)

Create new Signomial converted to new units

varkeys

The NomialData’s varkeys, created when necessary for a substitution.

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.

copy()

Return a copy of this

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
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(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. Ugly optimized code.

gpkit.nomials.map.subinplace(cp, exp, o_exp, vk, cval, squished)

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)

Bases: gpkit.nomials.math.Posynomial

A Posynomial with only one term

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, right)

Bases: gpkit.nomials.math.PosynomialInequality

A Constraint of the form Monomial == Monomial.

as_hmapslt1(substitutions)

Tags posynomials for dual feasibility checking

oper = '='
sens_from_dual(la, nu, _)

Returns the variable/constraint sensitivities from lambda/nu

class gpkit.nomials.math.Posynomial(hmap=None, cs=1, require_positive=True)

Bases: gpkit.nomials.math.Signomial

A Signomial with strictly positive cs

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)

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_hmapslt1(substitutions)

Returns the posys <= 1 representation of this constraint.

feastol = 0.001
sens_from_dual(la, nu, _)

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.

bounded = {}
generated_by = None
meq_bounded = {}
parent = None
relaxed(relaxvar)

Returns the relaxation of the constraint in a list.

v_ss = None
class gpkit.nomials.math.Signomial(hmap=None, cs=1, require_positive=True)

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)

chop()

Returns a list of monomials in the signomial.

diff(var)

Derivative of this with respect to a Variable

var : Variable key
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,
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 at .unsubbed[0] as a single Signomial (0 >= self)

as_gpconstr(x0)

Returns GP-compatible approximation at x0

as_hmapslt1(substitutions)

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_gpconstr(x0)

Returns GP-compatible approximation at x0

as_hmapslt1(substitutions)

SignomialEquality is never considered GP-compatible

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 the 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