Core Data Structures#
Vector#
- class pepflow.Vector(is_basis: bool, eval_expression: VectorRepresentation | VectorByBasisRepresentation | ZeroVector | None = None, tags: list[str] = NOTHING, math_expr: MathExpr = NOTHING)#
A
Vectorobject represents an element of a vector space.Examples include a point or a gradient.
Vectorobjects can be constructed as linear combinations of otherVectorobjects. Let a and b be some numeric data type. Let x and y beVectorobjects. Then, we can form a newVectorobject: a*x+b*y.The inner product of two
Vectorobjects can also be taken. Let x and y beVectorobjects. Then, their inner product is x*y and returns aScalarobject.- Variables:
is_basis (bool) – True if this vector is not formed through a linear combination of other vectors. False otherwise.
tags (list[str]) – A list that contains tags that can be used to identify the
Vectorobject. Tags should be unique.math_expr (
MathExpr) – AMathExprobject with a member variable that contains a mathematical expression represented as a string.
Example
>>> import pepflow as pf >>> ctx = pf.PEPContext("ctx").set_as_current() >>> x_0 = pf.Vector(is_basis=True).add_tag("x_0")
Note
Basis
Vectorobjects should be defined using the constructor as shown in the example but compositeVectorobjects should be created using operations onVectorobjects.- add_tag(tag: str) Vector#
Add a new tag for this
Vectorobject.- Parameters:
tag (str) – The new tag to be added to the tags list.
- Returns:
The instance itself.
- eval(ctx: PEPContext | None = None, *, resolve_parameters: dict[str, Number | Number] | None = None, sympy_mode: bool = False) ndarray#
Return the concrete representation of this
Vector.Concrete representations of
Vectorobjects areEvaluatedVectorobjects.- Parameters:
ctx (
PEPContext| None) – ThePEPContextobject we consider. None if we consider the current globalPEPContextobject.resolve_parameters (dict[str,
NUMERICAL_TYPE] | None) – A dictionary that maps the name of parameters to the numerical values.sympy_mode (bool) – If true, then the input should be defined completely in terms of SymPy objects and should not mix Python numeric objects. Will raise an error if sympy_mode is True and the input contains a Python numeric object. By default False.
- Returns:
EvaluatedVector– The concrete representation of thisVector.
- repr_by_basis(ctx: PEPContext | None = None, *, resolve_parameters: dict[str, Number | Number] | None = None, sympy_mode: bool = False) str#
Express this
Vectorobject as the linear combination of the basisVectorobjects of the givenPEPContext.This linear combination is expressed as a str where, to refer to the basis
Vectorobjects, we use their tags.- Parameters:
ctx (
PEPContext) – ThePEPContextobject whose basisVectorobjects we consider. None if we consider the current globalPEPContextobject.resolve_parameters (dict[str,
NUMERICAL_TYPE] | None) – A dictionary that maps the name of parameters to the numerical values.sympy_mode (bool) – If true, then the input should be defined completely in terms of SymPy objects and should not mix Python numeric objects. Will raise an error if sympy_mode is True and the input contains a Python numeric object. By default False.
- Returns:
str – The representation of this
Vectorobject in terms of the basisVectorobjects of the givenPEPContext.
- simplify(tag: str | None = None) Vector#
Simplify the mathematical expression of this
Vectorobject.
- class pepflow.EvaluatedVector(coords: ndarray)#
The concrete representation of the abstract
Vector.Each abstract basis
Vectorobject has a unique concrete representation as a unit vector. The concrete representations of linear combinations of abstract basisVectorobjects are linear combinations of the unit vectors. This information is stored in the coords attribute.EvaluatedVectorobjects can be constructed as linear combinations of otherEvaluatedVectorobjects. Let a and b be some numeric data type. Let x and y beEvaluatedVectorobjects. Then, we can form a newEvaluatedVectorobject: a*x+b*y.- Variables:
coords (np.ndarray) – The concrete representation of an abstract
Vector.
Scalar#
- class pepflow.Scalar(is_basis: bool, eval_expression: ScalarRepresentation | ScalarByBasisRepresentation | ZeroScalar | None = None, tags: list[str] = NOTHING, math_expr: MathExpr = NOTHING)#
A
Scalarobject represents linear combination of functions values, inner products ofPointobjects, and constant scalar values.Scalarobjects can be constructed as linear combinations of otherScalarobjects. Let a and b be some numeric data type. Let x and y beScalarobjects. Then, we can form a newScalarobject: a*x+b*y.- Variables:
is_basis (bool) – True if this scalar is not formed through a linear combination of other scalars. False otherwise.
tags (list[str]) – A list that contains tags that can be used to identify the
Vectorobject. Tags should be unique.math_expr (
MathExpr) – AMathExprobject with a member variable that contains a mathematical expression represented as a string.
Example
>>> import pepflow as pf >>> ctx = pf.PEPContext("cts").set_as_current() >>> s1 = pf.Scalar(is_basis=True, tags=["s1"])
Note
Basis
Scalarobjects should be defined using the constructor as shown in the example but compositeScalarobjects should be created using operations onScalarobjects.- add_tag(tag: str) Scalar#
Add a new tag for this
Scalarobject.- Parameters:
tag (str) – The new tag to be added to the tags list.
- Returns:
The instance itself.
- eq(other: Scalar | float | int, name: str) ScalarConstraint#
Generate a
ScalarConstraintobject that represents the inequality self = other.- Parameters:
other (
Scalar| float | int) – The other side of the relation.name (str) – The name of the generated
ScalarConstraintobject.
- Returns:
ScalarConstraint– An object that represents the inequality self = other.
- eval(ctx: PEPContext | None = None, *, resolve_parameters: dict[str, Number | Number] | None = None, sympy_mode: bool = False) EvaluatedScalar#
Return the concrete representation of this
Scalar.Concrete representations of
Scalarobjects areEvaluatedScalarobjects.- Parameters:
ctx (
PEPContext| None) – ThePEPContextobject we consider. None if we consider the current globalPEPContextobject.resolve_parameters (dict[str,
NUMERICAL_TYPE] | None) – A dictionary that maps the name of parameters to the numerical values.sympy_mode (bool) – If true, then the input should be defined completely in terms of SymPy objects and should not mix Python numeric objects. Will raise an error if sympy_mode is True and the input contains a Python numeric object. By default False.
- Returns:
EvaluatedScalar– The concrete representation of thisScalar.
- ge(other: Scalar | float | int, name: str) ScalarConstraint#
Generate a
ScalarConstraintobject that represents the inequality self >= other.- Parameters:
other (
Scalar| float | int) – The other side of the relation.name (str) – The name of the generated
ScalarConstraintobject.
- Returns:
ScalarConstraint– An object that represents the inequality self >= other.
- gt(other: Scalar | float | int, name: str) ScalarConstraint#
Generate a
ScalarConstraintobject that represents the inequality self > other.- Parameters:
other (
Scalar| float | int) – The other side of the relation.name (str) – The name of the generated
ScalarConstraintobject.
- Returns:
ScalarConstraint– An object that represents the inequality self > other.
- le(other: Scalar | float | int, name: str) ScalarConstraint#
Generate a
ScalarConstraintobject that represents the inequality self <= other.- Parameters:
other (
Scalar| float | int) – The other side of the relation.name (str) – The name of the generated
ScalarConstraintobject.
- Returns:
ScalarConstraint– An object that represents the inequality self <= other.
- lt(other: Scalar | float | int, name: str) ScalarConstraint#
Generate a
ScalarConstraintobject that represents the inequality self < other.- Parameters:
other (
Scalar| float | int) – The other side of the relation.name (str) – The name of the generated
ScalarConstraintobject.
- Returns:
ScalarConstraint– An object that represents the inequality self < other.
- repr_by_basis(ctx: PEPContext | None = None, *, greedy_square: bool = False, resolve_parameters: dict[str, Number | Number] | None = None, sympy_mode: bool = False) str#
Express this
Scalarobject in terms of the basisVectorandScalarobjects of the givenPEPContext.A
Scalarcan be formed by linear combinations of basisScalarobjects. AScalarcan also be formed through the inner product of two basisVectorobjects. This function returns the representation of thisScalarobject in terms of the basisVectorandScalarobjects as a str.- Parameters:
ctx (
PEPContext) – ThePEPContextobject whose basisVectorandScalarobjects we consider. None if we consider the current globalPEPContextobject.greedy_square (bool) – If greedy_square is true, the function will try to return \(\|a-b\|^2\) whenever possible. If not, the function will return \(\|a\|^2 - 2 * \langle a, b \rangle + \|b\|^2\) instead. True by default.
resolve_parameters (dict[str,
NUMERICAL_TYPE] | None) – A dictionary that maps the name of parameters to the numerical values.sympy_mode (bool) – If true, then the input should be defined completely in terms of SymPy objects and should not mix Python numeric objects. Will raise an error if sympy_mode is True and the input contains a Python numeric object. By default False.
- Returns:
str – The representation of this
Scalarobject in terms of the basisVectorandScalarobjects of the givenPEPContext.
Example
>>> import pepflow as pf >>> import sympy as sp >>> ctx = pf.PEPContext("ctx").set_as_current() >>> xi = pf.Vector(is_basis=True, tags=["x_i"]) >>> xj = pf.Vector(is_basis=True, tags=["x_j"]) >>> em = pf.ExpressionManager(ctx) >>> term = 2 / sp.S(3) * (xi + xj) ** 2 + 1 / sp.S(3) * (xi - xj) ** 2 >>> term_str = term.repr_by_basis(ctx, sympy_mode=True) >>> pf.pprint_str(term_str)
- simplify(tag: str | None = None) Scalar#
Simplify the mathematical expression of this
Scalarobject.
- class pepflow.EvaluatedScalar(func_coords: ndarray, inner_prod_coords: ndarray, offset: float)#
The concrete representation of the abstract
Scalar.Each abstract basis
Scalarobject has a unique concrete representation as a unit vector. The concrete representations of linear combinations of abstract basisScalarobjects are linear combinations of the unit vectors. This information is stored in the func_coords attribute.Abstract
Scalarobjects can be formed through taking the inner product of two abstractVectorobjects. The concrete representation of an abstractScalarobject formed this way is the outer product of the concrete representations of the two abstractVectorobjects, i.e., a matrix. This information is stored in the inner_prod_coords attribute.Abstract
Scalarobjects can be added or subtracted with numeric data types. This information is stored in the offset attribute.EvaluatedScalarobjects can be constructed as linear combinations of otherEvaluatedScalarobjects. Let a and b be some numeric data type. Let u and v beEvaluatedScalarobjects. Then, we can form a newEvaluatedScalarobject: a*u+b*v.- Variables:
func_coords (np.ndarray) – The vector component of the concrete representation of the abstract
Scalar.inner_prod_coords (np.ndarray) – The matrix component of the concrete representation of the abstract
Scalar. An alias is matrix.offset (float) – The constant component of the concrete representation of the abstract
Scalar.
- property matrix: ndarray#
A short alias for inner_prod_coords.
Parameter#
- class pepflow.Parameter(name: str | None, eval_expression: ParameterRepresentation | None = None)#
A
Parameterobject that represents some numerial value that can be resolved later.- Variables:
name (str | None) – The name of the
Parameterobject. If name is None, it means it is a composite parameter and eval_expression needs to be provided.eval_expression (
ParameterRepresentation| None) – A datastructure that is used to evaluate the value of parameter.
Example
>>> import pepflow as pf >>> ctx = pf.PEPContext("example").set_as_current() >>> v = pf.Vector(is_basis=True, tag=["p1"]) >>> pm = pf.Parameter(name="param") >>> v2 = pm * v >>> v2.eval(resolve_parameters={"param": 2})
Scalar Constraint#
- class pepflow.ScalarConstraint(lhs: Scalar | float, rhs: Scalar | float, cmp: Comparator, name: str, associated_dual_var_constraints: list[tuple[Comparator, float]] = NOTHING)#
A
ScalarConstraintobject that represents inequalities and equalities ofScalarobjects.Denote an arbitrary
Scalarobjects as x and y.ScalarConstraintobjects represent: x <= y, x >= y, and x = y.- Variables:
lhs (
Scalar) – TheScalarobject on the left hand side of the relation.rhs (
Scalar) – TheScalarobject on the right hand side of the relation.cmp (
Comparator) –Comparatoris an enumeration that can be either GE, LE, or EQ. They represent >=, <=, or = respectively.name (str) – The unique name of the
Comparatorobject.associated_dual_var_constraints (list[tuple[
Comparator, float]]) – A list of all the constraints imposed on the associated dual variable of thisScalarConstraintobject.
Example
>>> s1 = scalar.Scalar(is_basis=True, tags=["s1"]) >>> s2 = scalar.Scalar(is_basis=True, tags=["s2"]) >>> (s1).le(s2, name="constraint_1")
- dual_eq(val: float) None#
Generates a = constraint on the dual variable associated with this constraint.
Denote the associated dual variable of this constraint as lambd. This generates a relation of the form lambd = val.
- Parameters:
val (float) – The other object in the relation.
- dual_ge(val: float) None#
Generates a >= constraint on the dual variable associated with this constraint.
Denote the associated dual variable of this constraint as lambd. This generates a relation of the form lambd >= val.
- Parameters:
val (float) – The other object in the relation.
- dual_le(val: float) None#
Generates a <= constraint on the dual variable associated with this constraint.
Denote the associated dual variable of this constraint as lambd. This generates a relation of the form lambd <= val.
- Parameters:
val (float) – The other object in the relation.
- classmethod make(lhs: Scalar | float, op: str, rhs: Scalar | float, name: str) ScalarConstraint#
A static method to construct a
ScalarConstraintobject.- Parameters:
lhs (
Scalar| float) – TheScalarobject on the left hand side of the relation.op (str) – A str which represents the type of relation. Possible options are le, ge, lt, gt, eq, <=, >=, <, >, or ==.
rhs (
Scalar| float) – TheScalarobject on the right hand side of the relation.name (str) – The unique name of the constructed
Comparatorobject.
PSD Constraint#
- class pepflow.PSDConstraint(lhs: ndarray | float, rhs: ndarray | float, cmp: Comparator, name: str, associated_dual_var_constraints: list[tuple[Comparator, ndarray | float]] = NOTHING)#
A
PSDConstraintobject that represents positive semidefinite constraints.Denote matrices as X and Y.
PSDConstraintobjects represent: X << Y, X >> Y, and X = Y.- Variables:
lhs (np.ndarray | float) – The matrix on the left hand side of the relation. The np.ndarray is composed of
Scalarobjects.rhs (np.ndarray | float) – The matrix on the right hand side of the relation. The np.ndarray is composed of
Scalarobjects.cmp (
Comparator) –Comparatoris an enumeration that can be either SEQ, PEQ, or EQ. They represent >>, <<, or = respectively.name (str) – The unique name of the
Comparatorobject.associated_dual_var_constraints (list[tuple[
Comparator, float]]) – A list of all the constraints imposed on the associated dual variable of thisPSDConstraintobject.
- dual_eq(val: ndarray | float) None#
Generates a = constraint on the dual variable associated with this constraint.
Denote the associated dual variable of this constraint as lambd. This generates a relation of the form lambd = val.
- Parameters:
val (np.ndarray | float) – The other object in the relation.
- dual_peq(val: ndarray | float) None#
Generates a << constraint on the dual variable associated with this constraint.
Denote the associated dual variable of this constraint as lambd. This generates a relation of the form lambd << val.
- Parameters:
val (np.ndarray | float) – The other object in the relation.
- dual_seq(val: ndarray | float) None#
Generates a >> constraint on the dual variable associated with this constraint.
Denote the associated dual variable of this constraint as lambd. This generates a relation of the form lambd >> val.
- Parameters:
val (np.ndarray | float) – The other object in the relation.
- is_compatiable_shape(val: ndarray | float) None#
Check that if val is a np.ndarray whether it is of the same shape as the LHS and RHS.
- classmethod make(lhs: ndarray | float, op: str, rhs: ndarray | float, name: str) PSDConstraint#
A static method to construct a
PSDConstraintobject.- Parameters:
lhs (np.ndarray | float) – The np.ndarray object on the left hand side of the relation.
op (str) – A str which represents the type of relation. Possible options are peq, seq, eq, <<, >>, or ==.
rhs (np.ndarray | float) – The np.ndarray object on the right hand side of the relation.
name (str) – The unique name of the constructed
Comparatorobject.
ConstraintData#
- class pepflow.ConstraintData(func_or_oper: Function | Operator, sc_dict: dict[str, list[ScalarConstraint]] = NOTHING, psd_dict: dict[str, PSDConstraint] = NOTHING)#
A data class that will store a
Function’s orOperator’s interpolation conditions.Functions and operators can have multiple groups of interpolations conditions. There are typically two types of groups. One group is composed of associated
ScalarConstraintobjects. The other is an individualPSDConstraintobject.- Variables:
func_or_oper (
Function|Operator) – The associatedFunctionorOperator.sc_dict (dict[str, list[
ScalarConstraint]]) – A dictionary in which the keys are the name of the groups ofScalarConstraintobjects and the values are a list of associatedScalarConstraintobjects.psd_dict (dict[str,
PSDConstraint]) – A dictionary in which the keys are the names of the types of thePSDConstraintobjects and the values are thePSDConstraintobjects.
- add_psd_constraint(constraint_type: str, psd_constraint: PSDConstraint) None#
Add a new
PSDConstraintobject.- Parameters:
constraint_type (str) – The name to refer to the group of scalar constraints repesented by scal_constraint.
psd_constraint (
PSDConstraint) – The new list ofScalarConstraintobjects to add.
- add_sc_constraint(constraint_type: str, scal_constraint: list[ScalarConstraint]) None#
Add a new list of
ScalarConstraintobjects.- Parameters:
constraint_type (str) – The name to refer to the group of scalar constraints repesented by scal_constraint.
scal_constraint (list[
ScalarConstraint]) – The new list ofScalarConstraintobjects to add.