]>
gitweb.michael.orlitzky.com - dunshire.git/blob - src/dunshire/matrices.py
2 Utility functions for working with CVXOPT matrices (instances of the
3 class:`cvxopt.base.matrix` class).
7 from cvxopt
import matrix
8 from cvxopt
.lapack
import syev
12 def append_col(left
, right
):
14 Append the matrix ``right`` to the right side of the matrix ``left``.
19 The two matrices to append to one another.
24 >>> A = matrix([1,2,3,4], (2,2))
25 >>> B = matrix([5,6,7,8,9,10], (2,3))
34 >>> print(append_col(A,B))
40 return matrix([left
.trans(), right
.trans()]).trans()
42 def append_row(top
, bottom
):
44 Append the matrix ``bottom`` to the bottom of the matrix ``top``.
49 The two matrices to append to one another.
54 >>> A = matrix([1,2,3,4], (2,2))
55 >>> B = matrix([5,6,7,8,9,10], (3,2))
65 >>> print(append_row(A,B))
74 return matrix([top
, bottom
])
77 def eigenvalues(symmat
):
79 Return the eigenvalues of the given symmetric real matrix.
85 The real symmetric matrix whose eigenvalues you want.
91 If the input matrix is not symmetric.
96 >>> A = matrix([[2,1],[1,2]], tc='d')
100 If the input matrix is not symmetric, it may not have real
101 eigenvalues, and we don't know what to do::
103 >>> A = matrix([[1,2],[3,4]])
105 Traceback (most recent call last):
107 TypeError: input must be a symmetric real matrix
110 if not norm(symmat
.trans() - symmat
) < options
.ABS_TOL
:
111 # Ensure that ``symmat`` is symmetric (and thus square).
112 raise TypeError('input must be a symmetric real matrix')
114 domain_dim
= symmat
.size
[0]
115 eigs
= matrix(0, (domain_dim
, 1), tc
='d')
120 def identity(domain_dim
):
122 Create an identity matrix of the given dimensions.
128 The dimension of the vector space on which the identity will act.
133 A ``domain_dim``-by-``domain_dim`` dense integer identity matrix.
138 >>> print(identity(3))
146 raise ValueError('domain dimension must be positive')
148 entries
= [int(i
== j
)
149 for i
in range(domain_dim
)
150 for j
in range(domain_dim
)]
151 return matrix(entries
, (domain_dim
, domain_dim
))
154 def inner_product(vec1
, vec2
):
156 Compute the Euclidean inner product of two vectors.
162 The two vectors whose inner product you want.
167 The inner product of ``vec1`` and ``vec2``.
174 >>> inner_product(x,y)
177 >>> x = matrix([1,1,1])
178 >>> y = matrix([2,3,4], (1,3))
179 >>> inner_product(x,y)
184 >>> inner_product(x,y)
185 Traceback (most recent call last):
187 TypeError: the lengths of vec1 and vec2 must match
190 if not len(vec1
) == len(vec2
):
191 raise TypeError('the lengths of vec1 and vec2 must match')
193 return sum([x
*y
for (x
, y
) in zip(vec1
, vec2
)])
196 def norm(matrix_or_vector
):
198 Return the Frobenius norm of a matrix or vector.
200 When the input is a vector, its matrix-Frobenius norm is the same
201 thing as its vector-Euclidean norm.
206 matrix_or_vector : matrix
207 The matrix or vector whose norm you want.
213 The norm of ``matrix_or_vector``.
218 >>> v = matrix([1,1])
219 >>> print('{:.5f}'.format(norm(v)))
222 >>> A = matrix([1,1,1,1], (2,2))
227 return sqrt(inner_product(matrix_or_vector
, matrix_or_vector
))
232 Create a long vector in column-major order from ``mat``.
238 Any sort of real matrix that you want written as a long vector.
244 An ``len(mat)``-by-``1`` long column vector containign the
245 entries of ``mat`` in column major order.
250 >>> A = matrix([[1,2],[3,4]])
263 Note that if ``mat`` is a vector, this function is a no-op:
265 >>> v = matrix([1,2,3,4], (4,1))
280 return matrix(mat
, (len(mat
), 1))