]>
gitweb.michael.orlitzky.com - dunshire.git/blob - matrices.py
5acc7a876e417beafd259d416f59fc283c1c5f67
2 Utility functions for working with CVXOPT matrices (instances of the
3 ``cvxopt.base.matrix`` class).
7 from cvxopt
import matrix
8 from cvxopt
.lapack
import syev
10 def append_col(left
, right
):
12 Append the matrix ``right`` to the right side of the matrix ``left``.
16 >>> A = matrix([1,2,3,4], (2,2))
17 >>> B = matrix([5,6,7,8,9,10], (2,3))
18 >>> print(append_col(A,B))
24 return matrix([left
.trans(), right
.trans()]).trans()
26 def append_row(top
, bottom
):
28 Append the matrix ``bottom`` to the bottom of the matrix ``top``.
32 >>> A = matrix([1,2,3,4], (2,2))
33 >>> B = matrix([5,6,7,8,9,10], (3,2))
34 >>> print(append_row(A,B))
43 return matrix([top
, bottom
])
46 def eigenvalues(real_matrix
):
48 Return the eigenvalues of the given ``real_matrix``.
52 >>> A = matrix([[2,1],[1,2]], tc='d')
57 domain_dim
= real_matrix
.size
[0] # Assume ``real_matrix`` is square.
58 W
= matrix(0, (domain_dim
, 1), tc
='d')
63 def identity(domain_dim
):
65 Return a ``domain_dim``-by-``domain_dim`` dense integer identity
70 >>> print(identity(3))
78 raise ValueError('domain dimension must be positive')
80 entries
= [int(i
== j
)
81 for i
in range(domain_dim
)
82 for j
in range(domain_dim
)]
83 return matrix(entries
, (domain_dim
, domain_dim
))
86 def norm(matrix_or_vector
):
88 Return the Frobenius norm of ``matrix_or_vector``, which is the same
89 thing as its Euclidean norm when it's a vector (when one of its
95 >>> print('{:.5f}'.format(norm(v)))
98 >>> A = matrix([1,1,1,1], (2,2))
103 return sqrt(sum([x
**2 for x
in matrix_or_vector
]))