]>
gitweb.michael.orlitzky.com - dunshire.git/blob - matrices.py
a33259e6f3e71f7a4b5f2d47234464d80a9a3199
2 Utility functions for working with CVXOPT matrices (instances of the
3 ``cvxopt.base.matrix`` class).
7 from cvxopt
import matrix
9 def append_col(left
, right
):
11 Append the matrix ``right`` to the right side of the matrix ``left``.
15 >>> A = matrix([1,2,3,4], (2,2))
16 >>> B = matrix([5,6,7,8,9,10], (2,3))
17 >>> print(append_col(A,B))
23 return matrix([left
.trans(), right
.trans()]).trans()
25 def append_row(top
, bottom
):
27 Append the matrix ``bottom`` to the bottom of the matrix ``top``.
31 >>> A = matrix([1,2,3,4], (2,2))
32 >>> B = matrix([5,6,7,8,9,10], (3,2))
33 >>> print(append_row(A,B))
42 return matrix([top
, bottom
])
44 def identity(domain_dim
):
46 Return a ``domain_dim``-by-``domain_dim`` dense integer identity
51 >>> print(identity(3))
59 raise ValueError('domain dimension must be positive')
61 entries
= [int(i
== j
)
62 for i
in range(domain_dim
)
63 for j
in range(domain_dim
)]
64 return matrix(entries
, (domain_dim
, domain_dim
))
67 def norm(matrix_or_vector
):
69 Return the Frobenius norm of ``matrix_or_vector``, which is the same
70 thing as its Euclidean norm when it's a vector (when one of its
76 >>> print('{:.5f}'.format(norm(v)))
79 >>> A = matrix([1,1,1,1], (2,2))
84 return sqrt(sum([x
**2 for x
in matrix_or_vector
]))