Backend for expression type implementations.


exception picos.expressions.expression.PredictedFailure[source]

Bases: TypeError

Denotes that comparing two expressions will not form a constraint.

__init__(*args, **kwargs)


class picos.expressions.expression.Expression(typeStr, symbStr)[source]

Bases: picos.valuable.Valuable

Abstract base class for mathematical expressions, including mutables.

For mutables, this is the secondary base class, with Mutable or a subclass thereof being the primary one.

__init__(typeStr, symbStr)[source]

Perform basic initialization for Expression instances.

  • typeStr (str) – Short string denoting the expression type.

  • symbStr (str) – Algebraic string description of the expression.


The expression with valued mutables frozen to their current value.

If all mutables of the expression are valued (and in the subset unless subset=None), this is the same as the inversion operation ~.

If the mutables to be frozen do not appear in the expression, then the expression is not copied but returned as is.


subset – An iterable of valued mutables or names thereof that should be frozen. If None, then all valued mutables are frozen to their current value. May include mutables that are not present in the expression, but may not include mutables without a value.

Returns Expression

The frozen expression, refined to a more suitable type if possible.


>>> from picos import RealVariable
>>> x, y = RealVariable("x"), RealVariable("y")
>>> f = x + y; f
<1×1 Real Linear Expression: x + y>
>>> sorted(f.mutables, key=lambda mtb: mtb.name)
[<1×1 Real Variable: x>, <1×1 Real Variable: y>]
>>> x.value = 5
>>> g = f.frozen(); g  # g is f with x frozen at its current value of 5.
<1×1 Real Affine Expression: [x] + y>
>>> sorted(g.mutables, key=lambda mtb: mtb.name)
[<1×1 Real Variable: y>]
>>> x.value, y.value = 10, 10
>>> f.value  # x takes its new value in f.
>>> g.value  # x remains frozen at [x] = 5 in g.
>>> # If an expression is frozen to a constant, this is reversable:
>>> f.frozen().equals(~f) and ~f.frozen() is f

Whether the expression is valued.

Deprecated since version 2.0: Use valued instead.

classmethod make_type(*args, **kwargs)[source]

Create a detailed expression type from subtype parameters.


Return a copy of the expression concerning different mutables.

New mutables must have the same shape and vectorization format as the mutables that they replace. This means in particular that RealVariable, IntegerVariable and BinaryVariable of same shape are interchangeable.

If the mutables to be replaced do not appear in the expression, then the expression is not copied but returned as is.


replacement (tuple or list or dict) – Either a map from mutables or mutable names to new mutables or an iterable of new mutables to replace existing mutables of same name with. See the section on advanced usage for additional options.

Returns Expression

The new expression, refined to a more suitable type if possible.

Advanced replacement

It is also possible to replace mutables with real affine expressions concerning pairwise disjoint sets of fresh mutables. This works only on real-valued mutables that have a trivial internal vectorization format (i.e. FullVectorization). The shape of the replacing expression must match the variable’s. Additional limitations depending on the type of expression that the replacement is invoked on are possible. The replacement argument must be a dictionary.


>>> import picos
>>> x = picos.RealVariable("x"); x.value = 1
>>> y = picos.RealVariable("y"); y.value = 10
>>> z = picos.RealVariable("z"); z.value = 100
>>> c = picos.Constant("c", 1000)
>>> a = x + 2*y; a
<1×1 Real Linear Expression: x + 2·y>
>>> a.value
>>> b = a.replace_mutables({y: z}); b  # Replace y with z.
<1×1 Real Linear Expression: x + 2·z>
>>> b.value
>>> d = a.replace_mutables({x: 2*x + z, y: c}); d  # Advanced use.
<1×1 Real Affine Expression: 2·x + z + 2·c>
>>> d.value

Set the value of an expression.

Deprecated since version 2.0: Use value instead.

property certain

Always True for certain expression types.

This can be False for Expression types that inherit from UncertainExpression (with priority).

property concave

Whether the expression is concave.

property constant

Whether the expression involves no mutables.

property convex

Whether the expression is convex.

property mutables

Return the set of mutables that are involved in the expression.

property parameters[source]

The set of parameters that are involved in the expression.

property refined

A refined version of the expression.

The refined expression can be an instance of a different Expression subclass than the original expression, if that type is better suited for the mathematical object in question.

The refined expression is automatically used instead of the original one whenever a constraint is created, and in some other places.

The idea behind refined expressions is that operations that produce new expressions can be executed quickly without checking for exceptionnel cases. For instance, the sum of two ComplexAffineExpression instances could have the complex part eliminated so that storing the result as an AffineExpression would be prefered, but checking for this case on every addition would be too slow. Refinement is used sparingly to detect such cases at times where it makes the most sense.

Refinement may be disallowed within a context with the no_refinement context manager. In this case, this property returns the expression as is.

property scalar

Whether the expression is scalar.

property shape

Return the algebraic shape of the expression.

property size

The same as shape.

property square

Whether the expression is a square matrix.

property string

Symbolic string representation of the expression.

Use this over Python’s str if you want to output the symbolic representation even when the expression is valued.

property subtype

The subtype part of the expression’s detailed type.

Returns a hashable object that, together with the Python class part of the expression’s type, is sufficient to predict the constraint outcome (constraint class and subtype) of any comparison operation with any other expression.

By convention the object returned is a namedtuple instance.

property type

The expression’s detailed type for constraint prediction.

The returned value is suffcient to predict the detailed type of any constraint that can be created by comparing with another expression.

Since constraints are created from refined expressions only, the Python class part of the detailed type may differ from the type of the expression whose type is queried.

property uncertain

Always False for certain expression types.

This can be True for Expression types that inherit from UncertainExpression (with priority).

property variables[source]

The set of decision variables that are involved in the expression.

class picos.expressions.expression.ExpressionType(theClass, subtype)[source]

Bases: picos.containers.DetailedType

The detailed type of an expression for predicting constraint outcomes.

This is suffcient to predict the detailed type of any constraint that can be created by comparing with another expression.

predict(relation, other)[source]

Predict the constraint outcome of comparing expressions.

  • relation – An object from the operator namespace representing the operation being predicted.

  • other (ExpressionType) – Another expression type representing the right hand side operand.


>>> import operator, picos
>>> a = picos.RealVariable("x") + 1
>>> b = picos.RealVariable("y") + 2
>>> (a <= b).type == a.type.predict(operator.__le__, b.type)



Context manager that disables the effect of Expression.refined.

This can be necessary to ensure that the outcome of a constraint coversion is as predicted, in particular when PICOS uses overridden comparison operators for constraint creation internally.


Cast refined on both operands.

If the left hand side operand (i.e. self) is refined to an instance of a different type, then, instead of the decorated method, the method with the same name on the refined type is invoked with the (refined) right hand side operand as its argument.

This decorator is supposed to be used on all constraint creating binary operator methods so that degenerated instances (e.g. a complex affine expression with an imaginary part of zero) can occur but are not used in constraints. This speeds up many computations involving expressions as these degenerate cases do not need to be detected. Note that Expression.type also refers to the refined version of an expression.


stop_at_affine (bool) – Do not refine any affine expressions, in particular do not refine complex affine expressions to real ones.


Validate that the constraint outcome matches the predicted outcome.