len(f)
and with a value that depends on the values of its variables.
Functions have two public attributes.
) |
) |
None
, then f.value() returns None
.
Otherwise, it returns a dense 'd'
matrix of size
(len(f)
,1) with the function value computed from the
value attributes of the variables of f.
Three types of functions are supported: affine, convex piecewise-linear and concave piecewise-linear.
Affine functions represent vector valued functions of the form
Affine functions result from the following operations.
+x
results in
an affine function with x as variable, coefficient 1.0, and
constant term 0.0. The unary operation -x
returns an
affine function with x as variable, coefficient -1.0, and
constant term 0.0. For an affine function f, +f
is
a copy of f, and -f
is a copy of f with the
signs of its coefficients and constant term reversed.
'd'
matrices with one column.
The rules for addition and subtraction follow the conventions for
matrix addition and subtraction in sections 2.3
and 6.3, with variables and affine functions
interpreted as dense 'd'
matrices with one column.
In particular, a scalar term (integer, float, 1 by 1 dense 'd'
matrix,
variable of length 1, or affine function of length 1) can be added
to an affine function or variable of length greater than 1.
'd'
matrix. The products
a*v
and v*a
are
valid affine functions whenever the product is allowed under the rules
for matrix and scalar multiplication of sections 2.3
and 6.3, with v interpreted as a 'd'
matrix
with one column.
In particular, the product a*v
is defined if
a is a scalar (integer, float or 1 by 1 dense 'd'
matrix),
or a matrix (dense or sparse) with
a.size[1] = len(v)
.
The operation v*a
is defined if a is scalar,
or if len(v)
= 1 and a is a matrix with one
column.
v) |
u,v) |
'd'
matrix of size (len(v)
,1), then
dot(u,v)
and dot(v,u)
are
equivalent to u.trans()*v
.
If u and v are dense matrices, then
dot(u,v)
is equivalent to the function blas.dot(u,v)
defined in section 3.2, i.e., it returns the inner product of
the two matrices.
In the following example, the variable x has length 1 and y has length 2. The functions f and g are given by
>>> from cvxopt.modeling import variable >>> x = variable(1,'x') >>> y = variable(2,'y') >>> f = 2*x + y + 3 >>> A = matrix([[1., 2.], [3.,4.]]) >>> b = matrix([1.,-1.]) >>> g = A*f + sum(y) + b >>> print g affine function of length 2 constant term: 1.3000e+01 1.7000e+01 linear term: linear function of length 2 coefficient of variable(2,'y'): 2.0000e+00 4.0000e+00 3.0000e+00 5.0000e+00 coefficient of variable(1,'x'): 8.0000e+00 1.2000e+01
f += u
and f -= u
, with u a constant, a variable or
an affine function, are allowed if they do not change the length of
f, i.e., if u has length len(f)
or length 1.
In-place multiplication f *= u
and division
f /= u
are allowed if u is an integer, float, or
1 by 1 matrix.
>>> x = variable(4,'x') >>> f = x[::2] >>> print f >>> linear function of length 2 linear term: linear function of length 2 coefficient of variable(4,'x'): TYPE: general SIZE: (2,4) (0, 0) 1.0000e+00 (1, 2) 1.0000e+00 >>> y = variable(3,'x') >>> g = matrix(range(12),(3,4),'d')*x - 3*y + 1 >>> print g[0] + g[2] affine function of length 1 constant term: 2.0000e+00 linear term: linear function of length 1 coefficient of variable(4,'x'): 2.0000e+00 8.0000e+00 1.4000e+01 2.0000e+01 coefficient of variable(3,'y'): TYPE: general SIZE: (1,3) (0, 0) -3.0000e+00 (0, 2) -3.0000e+00
The general expression of a convex piecewise-linear function is
Piecewise-linear functions can be created using the following operations.
f = max(y1,y2, ...)
do not include any variables or functions, then the Python built-in
max() is evaluated.
If one or more of the arguments are variables or functions,
max() returns a piecewise-linear function defined as the
elementwise maximum of its arguments.
In other words, f[k] =
max(y1[k],y2[k], ...)
for k=0, ..., len(f)
-1.
The length of f is equal to the maximum of the lengths of the
arguments. Each argument must have length equal to
len(f)
or length one.
Arguments with length one are interpreted as vectors of
length len(f)
with identical entries.
The arguments can be scalars of type integer or float,
dense 'd'
matrices with one column, variables, affine functions or
convex piecewise-linear functions.
With one argument, f = max(u)
is interpreted as
f = max(u[0],u[1],...,
u[len(u)-1])
.
'd'
matrices with one column,
variables, affine functions or concave piecewise-linear functions.
f = abs(u)
returns the convex piecewise-linear
function max(u,-u)
.
+f
creates a copy of f.
-f
is a concave piecewise-linear function if f is
convex and a convex piecewise-linear function if f is concave.
sum(f)
is equivalent
to f[0] + f[1] + ...+ f[len(f)-1]
.
a*f
of a
piecewise-linear function f is defined if a
is an integer, float, 1 by 1 'd'
matrix.
Matrix-matrix multiplications a*f
or
f*a
are only defined if a is a dense or
sparse 1 by 1 matrix.
In the following example, f is the 1-norm of a vector
variable x of length 10, g is its infinity-norm and
h is the function
>>> f = sum(abs(x)) >>> g = max(abs(x)) >>> h = sum(max(0, abs(x)-1, 2*abs(x)-3))
f += u
, f -= u
,
f *= u
, f /= u
are defined if the
corresponding expanded operations f = f+u
,
f = f-u
, f = f*u
and f = f/u
are defined and if they do not
change the length of f.