8.6.21. PsdExpr

class PsdExpr

Represents a positive semi-definite expression. It is usually related to PsdConstr or objective function. A PsdExpr contains many semi-definite terms and a linear expression. Semi-definite expressions can be involved in arithmetic operations. For example:

# semi-definite expression of a term
psdx * mat
# semi-definite expressions for multiple terms
psdx0 * mat0 + psdx1 * mat1
# semi-definite expression containing linear part
psdx * mat + x * 2 + 1

Methods

__init__()

Construct a semi-definite expression

add()

Add all terms of another PsdExpr to the current PsdExpr

addConstant()

Add a value to the constant term of the PsdExpr

addTerms()

Add one or more terms(s)

clear()

Clear all included terms and set constant to 0

getCoeff()

Obtain the coefficient of a semi-definite term in a PsdExpr

getConstant()

Obtain the constant term of a PsdExpr

getLinExpr()

Get a linear expression contained in a PsdExpr

getValue()

After solving the problem, obtain the value of the PsdExpr

getVar()

Obtain the PsdVar of a term in a PsdExpr

remove()

Delete some terms contained in PsdExpr

size()

Obtain the number of semi-definite terms, excluding linear terms and constant terms

__init__(arg1=0, arg2=None)

Construct a semi-definite expression

Parameters
  • arg1=0 – The initial value of a PsdExpr, which can be a constant, a Var, a LinExpr, or another PsdExpr.

  • arg2=None – When it is not None, it is usually a Var or PsdVar, a list of variables.

example:

PsdExpr([mat], [psdx])
PsdExpr(mat, psdx)
PsdExpr(coeff, x)
PsdExpr(x)
PsdExpr(mat * psdx)
PsdExpr(2 * x + 1)
PsdExpr([(mat1, px1), (mat2, px2), (mat3, px3)])
PsdExpr([(1, x), (2, y), (1, z)])
PsdExpr(1)
add(expr, mult=1.0)

Add all terms of another PsdExpr to the current PsdExpr

Parameters
  • expr – Another PsdExpr

  • mult=1.0 – Multiplier. Default value: 1.0.

example:

psdExpr.add(psdExpr1, -1)
addConstant(c)

Add a value to the constant term of the PsdExpr.

Parameters

c – The value to be added. A negative number indicates that a value is subtracted.

example:

psdExpr.addConstant(-psdExpr.getConstant())
addTerms(coeffs, vars)

Add one or more terms(s). When plain variable(s) and numeric coefficient(s) provided, linear term(s) are added. When PsdVar(s) and matrix(matrices) provided, a semi-definite term is added.

Parameters
  • coeffs – The coefficient of the term(s) to be added, which may be a single number, a single matrix, or a list.

  • vars – The variable of the term(s) to be added, which can be a single Var, a PsdVar, or a list.

example:

psdExpr.addTerms([1, 2], [x, y])
psdExpr.addTerms([mat1, mat2], [px1, px2])
psdExpr.addTerms(1, x)
psdExpr.addTerms(mat, px)
clear()

Clear all included terms and set constant to 0

example:

psdExpr = mat1 * px1 + 3 * x + 1
psdExpr.clear()
print(psdExpr.size() == 0)
print(psdExpr.getLinExpr().size() == 0)
print(psdExpr.getConstant() == 0)
getCoeff(index)

Obtain the coefficient of a semi-definite term in a PsdExpr.

Parameters

index – To obtain the index of the semi-definite term of the coefficient

example:

psdExpr = mat1 * px1 + mat2 * px2
print(psdExpr.getCoeff(0) == mat1)
getConstant()

Obtain the constant term of a PsdExpr.

example:

psdExpr.addConstant(-psdExpr.getConstant())
getLinExpr()

Get a linear expression contained in a PsdExpr.

example:

psdExpr = mat * px + 1 * x + 3 * y + 1
print(psdExpr.getLinExpr().size() == 2)
print(psdExpr.getLinExpr().getConstant() == 1)
getValue()

After solving the problem, obtain the value of the PsdExpr.

example:

m.optimize()
psdExpr = mat1 * px1 + mat2 * px2 + 1 * x + 2
print(psdExpr.getValue())
getVar(index)

Obtain the PsdVar of a term in a PsdExpr.

Parameters

index – To obtain the index of the term of the PsdVar.

example:

psdExpr = mat1 * px1 + mat2 * px2
print(psdExpr.getVar(1).sameAs(px2))
remove(item)

Delete some terms contained in PsdExpr.

Parameters

item – If item is a number, the semi-definite term whose index is item is deleted. If item is a Var, all terms of the linear item that contain this Var are deleted. If item is a PsdVar, all semi-definite terms that contain this PsdVar are deleted.

example:

psdExpr = mat1 * px1 + mat2 * px2 + 3 * y + 4 * x
psdExpr.remove(0)
psdExpr.remove(px2)
print(psdExpr.size() == 0)
psdExpr.remove(x)
psdExpr.remove(y)
print(psdExpr.getLinExpr().size() == 0)
size()

Obtain the number of semi-definite terms, excluding linear terms and constant terms.

example:

psdExpr = mat1 * px1 + 3 * x + 1
print(psdExpr.size() == 1)