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])
c

The coefficient vector in the GP input data sense

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.data module

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

class gpkit.nomials.data.NomialData(exps=None, cs=None, simplify=True)

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

diff(var)

Derivative of this with respect to a Variable

var (Variable):
Variable to take derivative with respect to

NomialData

init_from_nomials(nomials)

Way to initialize from nomials. Calls __init__. Used by subclass __init__ methods.

values

The NomialData’s values, created when necessary.

varkeys

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

gpkit.nomials.data.simplify_exps_and_cs(exps, cs, return_map=False)

Reduces the number of monomials, and casts them to a sorted form.

exps : list of Hashvectors
The exponents of each monomial
cs : array of floats or Quantities
The coefficients of each monomial
return_map : bool (optional)
Whether to return the map of which monomials combined to form a simpler monomial, and their fractions of that monomial’s final c.
exps : list of Hashvectors
Exponents of simplified monomials.
cs : array of floats or Quantities
Coefficients of simplified monomials.
mmap : list of HashVectors
List for each new monomial of {originating indexes: fractions}

gpkit.nomials.nomial_core module

The shared non-mathematical backbone of all Nomials

class gpkit.nomials.nomial_core.Nomial(exps=None, cs=None, simplify=True)

Bases: gpkit.nomials.data.NomialData

Shared non-mathematical properties of all nomials

c = None
convert_to(arg)

Convert this signomial to new units

latex(excluded=None)

For pretty printing with Sympy

str_without(excluded=None)

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

sub = None
to(arg)

Create new Signomial converted to new 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.nomial_math module

Signomial, Posynomial, Monomial, Constraint, & MonoEQCOnstraint classes

class gpkit.nomials.nomial_math.Monomial(exps=None, cs=1, require_positive=True, simplify=True, **descr)

Bases: gpkit.nomials.nomial_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.
mono_approximation(x0)
class gpkit.nomials.nomial_math.MonomialEquality(left, oper, right)

Bases: gpkit.nomials.nomial_math.PosynomialInequality

A Constraint of the form Monomial == Monomial.

sens_from_dual(la, nu)

Returns the variable/constraint sensitivities from lambda/nu

class gpkit.nomials.nomial_math.Posynomial(exps=None, cs=1, require_positive=True, simplify=True, **descr)

Bases: gpkit.nomials.nomial_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.nomial_math.PosynomialInequality(left, oper, right)

Bases: gpkit.nomials.nomial_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)

GP version of a Posynomial constraint is itself

as_posyslt1(substitutions=None)

Returns the posys <= 1 representation of this constraint.

sens_from_dual(la, nu)

Returns the variable/constraint sensitivities from lambda/nu

class gpkit.nomials.nomial_math.ScalarSingleEquationConstraint(left, oper, right)

Bases: gpkit.constraints.single_equation.SingleEquationConstraint

A SingleEquationConstraint with scalar left and right sides.

nomials = []
subinplace(substitutions)

Modifies the constraint in place with substitutions.

class gpkit.nomials.nomial_math.Signomial(exps=None, cs=1, require_positive=True, simplify=True, **descr)

Bases: gpkit.nomials.nomial_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(wrt)

Derivative of this with respect to a Variable

wrt (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,
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.

subinplace(substitutions)

Substitutes in place.

class gpkit.nomials.nomial_math.SignomialInequality(left, oper, right)

Bases: gpkit.nomials.nomial_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_gpconstr(x0)

Returns GP approximation of an SP constraint at x0

as_posyslt1(substitutions=None)

Returns the posys <= 1 representation of this constraint.

class gpkit.nomials.nomial_math.SingleSignomialEquality(left, right)

Bases: gpkit.nomials.nomial_math.SignomialInequality

A constraint of the general form posynomial == posynomial

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

Module containing the substitution function

gpkit.nomials.substitution.append_sub(sub, keys, constants, sweep, linkedsweep)

Appends sub to constants, sweep, or linkedsweep.

gpkit.nomials.substitution.parse_subs(varkeys, substitutions)

Seperates subs into constants, sweeps linkedsweeps actually present.

gpkit.nomials.substitution.substitution(nomial, substitutions)

Efficient substituton into a list of monomials.

varlocs : dict
Dictionary mapping variables to lists of monomial indices.
exps : Iterable of dicts
Dictionary mapping variables to exponents, for each monomial.
cs : list
Coefficient for each monomial.
substitutions : dict
Substitutions to apply to the above.
val : number (optional)
Used to substitute singlet variables.
varlocs_ : dict
Dictionary of monomial indexes for each variable.
exps_ : dict
Dictionary of variable exponents for each monomial.
cs_ : list
Coefficients each monomial.
subs_ : dict
Substitutions to apply to the above.

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 + Quantity)

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.nomial_math.Monomial

A described singlet Monomial.

*args : list
may contain “name” (Strings)
“value” (Numbers + Quantity) or (Iterable) for a sweep “units” (Strings + Quantity)

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.

descr

a Variable’s descr is derived from its VarKey.

key

Get the VarKey associated with this Variable

sub(*args, **kwargs)

Same as nomial substitution, but also allows single-argument calls

x = Variable(‘x’) assert x.sub(3) == Variable(‘x’, value=3)

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.

Module contents

Contains nomials, inequalities, and arrays