]>
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 syevr
13 def append_col(left
, right
):
15 Append two matrices side-by-side.
21 The two matrices to append to one another.
27 A new matrix consisting of ``right`` appended to the right
33 >>> A = matrix([1,2,3,4], (2,2))
34 >>> B = matrix([5,6,7,8,9,10], (2,3))
43 >>> print(append_col(A,B))
49 return matrix([left
.trans(), right
.trans()]).trans()
52 def append_row(top
, bottom
):
54 Append two matrices top-to-bottom.
60 The two matrices to append to one another.
66 A new matrix consisting of ``bottom`` appended below ``top``.
71 >>> A = matrix([1,2,3,4], (2,2))
72 >>> B = matrix([5,6,7,8,9,10], (3,2))
82 >>> print(append_row(A,B))
91 return matrix([top
, bottom
])
94 def eigenvalues(symmat
):
96 Return the eigenvalues of the given symmetric real matrix.
102 The real symmetric matrix whose eigenvalues you want.
108 A list of the eigenvalues (in no particular order) of ``symmat``.
114 If the input matrix is not symmetric.
119 >>> A = matrix([[2,1],[1,2]], tc='d')
123 If the input matrix is not symmetric, it may not have real
124 eigenvalues, and we don't know what to do::
126 >>> A = matrix([[1,2],[3,4]])
128 Traceback (most recent call last):
130 TypeError: input must be a symmetric real matrix
133 if not norm(symmat
.trans() - symmat
) < options
.ABS_TOL
:
134 # Ensure that ``symmat`` is symmetric (and thus square).
135 raise TypeError('input must be a symmetric real matrix')
137 domain_dim
= symmat
.size
[0]
138 eigs
= matrix(0, (domain_dim
, 1), tc
='d')
143 def identity(domain_dim
):
145 Create an identity matrix of the given dimensions.
151 The dimension of the vector space on which the identity will act.
157 A ``domain_dim``-by-``domain_dim`` dense integer identity matrix.
163 If you ask for the identity on zero or fewer dimensions.
168 >>> print(identity(3))
176 raise ValueError('domain dimension must be positive')
178 entries
= [int(i
== j
)
179 for i
in range(domain_dim
)
180 for j
in range(domain_dim
)]
181 return matrix(entries
, (domain_dim
, domain_dim
))
184 def inner_product(vec1
, vec2
):
186 Compute the Euclidean inner product of two vectors.
192 The two vectors whose inner product you want.
198 The inner product of ``vec1`` and ``vec2``.
204 If the lengths of ``vec1`` and ``vec2`` differ.
211 >>> inner_product(x,y)
214 >>> x = matrix([1,1,1])
215 >>> y = matrix([2,3,4], (1,3))
216 >>> inner_product(x,y)
221 >>> inner_product(x,y)
222 Traceback (most recent call last):
224 TypeError: the lengths of vec1 and vec2 must match
227 if not len(vec1
) == len(vec2
):
228 raise TypeError('the lengths of vec1 and vec2 must match')
230 return sum([x
*y
for (x
, y
) in zip(vec1
, vec2
)])
233 def norm(matrix_or_vector
):
235 Return the Frobenius norm of a matrix or vector.
237 When the input is a vector, its matrix-Frobenius norm is the same
238 thing as its vector-Euclidean norm.
243 matrix_or_vector : matrix
244 The matrix or vector whose norm you want.
250 The norm of ``matrix_or_vector``.
255 >>> v = matrix([1,1])
256 >>> print('{:.5f}'.format(norm(v)))
259 >>> A = matrix([1,1,1,1], (2,2))
264 return sqrt(inner_product(matrix_or_vector
, matrix_or_vector
))
269 Create a long vector in column-major order from ``mat``.
275 Any sort of real matrix that you want written as a long vector.
281 An ``len(mat)``-by-``1`` long column vector containign the
282 entries of ``mat`` in column major order.
287 >>> A = matrix([[1,2],[3,4]])
300 Note that if ``mat`` is a vector, this function is a no-op:
302 >>> v = matrix([1,2,3,4], (4,1))
317 return matrix(mat
, (len(mat
), 1))