The cvxopt.blas module provides an interface to the double-precision real and complex Basic Linear Algebra Subprograms (BLAS). The names and calling sequences of the Python functions in the interface closely match the corresponding Fortran BLAS routines (described in the references below) and their functionality is exactly the same. Many of the operations performed by the BLAS routines can be implemented in a more straightforward way by using the matrix arithmetic of the section Arithmetic Operations, combined with the slicing and indexing of the section Indexing and Slicing. As an example, C = A * B gives the same result as the BLAS call gemm(A, B, C). The BLAS interface offers two advantages. First, some of the functions it includes are not easily implemented using the basic matrix arithmetic. For example, BLAS includes functions that efficiently exploit symmetry or triangular matrix structure. Second, there is a performance difference that can be significant for large matrices. Although our implementation of the basic matrix arithmetic makes internal calls to BLAS, it also often requires creating temporary matrices to store intermediate results. The BLAS functions on the other hand always operate directly on their matrix arguments and never require any copying to temporary matrices. Thus they can be viewed as generalizations of the in-place matrix addition and scalar multiplication of the section Arithmetic Operations to more complicated operations.
See also
The BLAS exploit several types of matrix structure: symmetric, Hermitian, triangular, and banded. We represent all these matrix classes by dense real or complex matrix objects, with additional arguments that specify the structure.
A real or complex symmetric matrix of order is represented
by a real or complex matrix of size (
,
),
and a character argument uplo with two possible values:
'L' and 'U'. If uplo is 'L', the lower
triangular part of the symmetric matrix is stored; if uplo is
'U', the upper triangular part is stored. A square
matrix X of size (
,
) can therefore be
used to represent the symmetric matrices
A complex Hermitian matrix of order is represented by a
matrix of type 'z' and size (
,
),
and a character argument uplo with the same meaning as for
symmetric matrices. A complex matrix X of size
(
,
) can represent the Hermitian matrices
A real or complex triangular matrix of order is represented
by a real or complex matrix of size (
,
),
and two character arguments: an argument uplo with possible values
'L' and 'U' to distinguish between lower and upper
triangular matrices, and an argument diag with possible values
'U' and 'N' to distinguish between unit and non-unit
triangular matrices. A square matrix X of size
(
,
) can represent the triangular matrices
A general real or complex by
band matrix with
subdiagonals and
superdiagonals is represented
by a real or complex matrix X of size
(
,
), and the two integers
and
. The diagonals of the band matrix are stored in the rows
of X, starting at the top diagonal, and shifted horizontally so that
the entries of column
of the band matrix are stored in column
of X. A matrix X of size
(
,
) therefore represents the
by
band matrix
A real or complex symmetric band matrix of order with
subdiagonals, is represented by a real or complex matrix X
of size (
,
), and an argument uplo to indicate
whether the subdiagonals (uplo is 'L') or superdiagonals
(uplo is 'U') are stored. The
diagonals are
stored as rows of X, starting at the top diagonal (i.e., the main
diagonal if uplo is 'L', or the
-th superdiagonal
if uplo is 'U') and shifted horizontally so that the
entries of the
-th column of the band matrix are stored in
column
of X. A matrix X of size
(
,
) can therefore represent the band matrices
A complex Hermitian band matrix of order with
subdiagonals is represented by a complex matrix of size
(
,
) and an argument uplo, with the same meaning
as for symmetric band matrices. A matrix X of size
(
,
) can represent the band matrices
A triangular band matrix of order with
subdiagonals
or superdiagonals is represented by a real complex matrix of size
(
,
) and two character arguments uplo and
diag, with similar conventions as for symmetric band matrices.
A matrix X of size (
,
) can represent
the band matrices
When discussing BLAS functions in the following sections we will omit several less important optional arguments that can be used to select submatrices for in-place operations. The complete specification is documented in the docstrings of the source code, and can be viewed with the pydoc help program.
The level 1 functions implement vector operations.
Scales a vector by a constant:
If x is a real matrix, the scalar argument alpha must be a Python integer or float. If x is complex, alpha can be an integer, float, or complex.
Euclidean norm of a vector: returns
1-Norm of a vector: returns
Returns
If more than one coefficient achieves the maximum, the index of the
first is returned.
Interchanges two vectors:
x and y are matrices of the same type ('d' or 'z').
Copies a vector to another vector:
x and y are matrices of the same type ('d' or 'z').
Constant times a vector plus a vector:
x and y are matrices of the same type ('d' or 'z'). If x is real, the scalar argument alpha must be a Python integer or float. If x is complex, alpha can be an integer, float, or complex.
Returns
x and y are matrices of the same type ('d' or 'z').
Returns
x and y are matrices of the same type ('d' or 'z').
The level 2 functions implement matrix-vector products and rank-1 and rank-2 matrix updates. Different types of matrix structure can be exploited using the conventions of the section Matrix Classes.
Matrix-vector product with a general matrix:
The arguments A, x, and y must have the same type ('d' or 'z'). Complex values of alpha and beta are only allowed if A is complex.
Matrix-vector product with a real symmetric matrix:
where is a real symmetric matrix. The arguments A,
x, and y must have type 'd', and alpha and
beta must be real.
Matrix-vector product with a real symmetric or complex Hermitian matrix:
where is real symmetric or complex Hermitian. The arguments
A, x, y must have the same type ('d' or
'z'). Complex values of alpha and beta are only
allowed if A is complex.
Matrix-vector product with a triangular matrix:
where is square and triangular. The arguments A and
x must have the same type ('d' or 'z').
Solution of a nonsingular triangular set of linear equations:
where is square and triangular with nonzero diagonal elements.
The arguments A and x must have the same type ('d' or
'z').
Matrix-vector product with a general band matrix:
where is a rectangular band matrix with
rows and
subdiagonals. The arguments A, x, y must have
the same type ('d' or 'z'). Complex values of
alpha and beta are only allowed if A is complex.
Matrix-vector product with a real symmetric band matrix:
where is a real symmetric band matrix. The arguments
A, x, y must have type 'd', and alpha and
beta must be real.
Matrix-vector product with a real symmetric or complex Hermitian band matrix:
where is a real symmetric or complex Hermitian band matrix.
The arguments A, x, y must have the same type
('d' or 'z'). Complex values of alpha and
beta are only allowed if A is complex.
Matrix-vector product with a triangular band matrix:
The arguments A and x must have the same type ('d' or 'z').
Solution of a triangular banded set of linear equations:
where is a triangular band matrix of with nonzero diagonal
elements. The arguments A and x must have the same type
('d' or 'z').
General rank-1 update:
where is a general matrix. The arguments A, x, and
y must have the same type ('d' or 'z'). Complex
values of alpha are only allowed if A is complex.
General rank-1 update:
where is a general matrix. The arguments A, x, and
y must have the same type ('d' or 'z'). Complex
values of alpha are only allowed if A is complex.
Symmetric rank-1 update:
where is a real symmetric matrix. The arguments A and
x must have type 'd'. alpha must be a real number.
Hermitian rank-1 update:
where is a real symmetric or complex Hermitian matrix. The
arguments A and x must have the same type ('d' or
'z'). alpha must be a real number.
Symmetric rank-2 update:
where is a real symmetric matrix. The arguments A, x,
and y must have type 'd'. alpha must be real.
Symmetric rank-2 update:
where is a a real symmetric or complex Hermitian matrix.
The arguments A, x, and y must have the same type
('d' or 'z'). Complex values of alpha are only
allowed if A is complex.
As an example, the following code multiplies the tridiagonal matrix
with the vector .
>>> from cvxopt import matrix
>>> from cvxopt.blas import gbmv
>>> A = matrix([[0., 1., 2.], [6., -4., -3.], [3., -1., 0.], [1., 0., 0.]])
>>> x = matrix([1., -1., 2., -2.])
>>> y = matrix(0., (3,1))
>>> gbmv(A, 3, 1, x, y)
>>> print y
[-5.00e+00]
[ 1.20e+01]
[-1.00e+00]
The following example illustrates the use of tbsv.
>>> from cvxopt import matrix
>>> from cvxopt.blas import tbsv
>>> A = matrix([-6., 5., -1., 2.], (1,4))
>>> x = matrix(1.0, (4,1))
>>> tbsv(A, x) # x := diag(A)^{-1}*x
>>> print x
[-1.67e-01]
[ 2.00e-01]
[-1.00e+00]
[ 5.00e-01]
The level 3 BLAS include functions for matrix-matrix multiplication.
Matrix-matrix product of two general matrices:
where
The arguments A, B, and C must have the same type ('d' or 'z'). Complex values of alpha and beta are only allowed if A is complex.
Product of a real or complex symmetric matrix and a general
matrix
:
The arguments A, B, and C must have the same type ('d' or 'z'). Complex values of alpha and beta are only allowed if A is complex.
Product of a real symmetric or complex Hermitian matrix and a
general matrix
:
The arguments A, B, and C must have the same type ('d' or 'z'). Complex values of alpha and beta are only allowed if A is complex.
Product of a triangular matrix and a general matrix
:
where
The arguments A and B must have the same type ('d' or 'z'). Complex values of alpha are only allowed if A is complex.
Solution of a nonsingular triangular system of equations:
where
is triangular and
is a general matrix. The
arguments A and B must have the same type ('d' or
'z'). Complex values of alpha are only allowed if A
is complex.
Rank- update of a real or complex symmetric matrix
:
where is a general matrix. The arguments A and C
must have the same type ('d' or 'z'). Complex values
of alpha and beta are only allowed if A is complex.
Rank- update of a real symmetric or complex Hermitian matrix
:
where is a general matrix. The arguments A and C
must have the same type ('d' or 'z'). alpha and
beta must be real.
Rank- update of a real or complex symmetric matrix
:
and
are general real or complex matrices. The
arguments A, B, and C must have the same type. Complex
values of alpha and beta are only allowed if A is complex.
Rank- update of a real symmetric or complex Hermitian matrix
:
where and
are general matrices. The arguments
A, B, and C must have the same type ('d' or
'z'). Complex values of alpha are only allowed if A
is complex. beta must be real.