X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fdunshire%2Fmatrices.py;h=1e0f2a5b7970fd19b78a2d136dc0a98dee2547af;hb=002b5370da24f083d2088c3482cf076615a13563;hp=a33259e6f3e71f7a4b5f2d47234464d80a9a3199;hpb=56ea961887d507114174af5f92b8c3c77b0b7a50;p=dunshire.git diff --git a/src/dunshire/matrices.py b/src/dunshire/matrices.py index a33259e..1e0f2a5 100644 --- a/src/dunshire/matrices.py +++ b/src/dunshire/matrices.py @@ -5,6 +5,7 @@ Utility functions for working with CVXOPT matrices (instances of the from math import sqrt from cvxopt import matrix +from cvxopt.lapack import syev def append_col(left, right): """ @@ -41,6 +42,24 @@ def append_row(top, bottom): """ return matrix([top, bottom]) + +def eigenvalues(real_matrix): + """ + Return the eigenvalues of the given ``real_matrix``. + + EXAMPLES: + + >>> A = matrix([[2,1],[1,2]], tc='d') + >>> eigenvalues(A) + [1.0, 3.0] + + """ + domain_dim = real_matrix.size[0] # Assume ``real_matrix`` is square. + eigs = matrix(0, (domain_dim, 1), tc='d') + syev(real_matrix, eigs) + return list(eigs) + + def identity(domain_dim): """ Return a ``domain_dim``-by-``domain_dim`` dense integer identity @@ -82,3 +101,26 @@ def norm(matrix_or_vector): """ return sqrt(sum([x**2 for x in matrix_or_vector])) + + +def vec(real_matrix): + """ + Create a long vector in column-major order from ``real_matrix``. + + EXAMPLES: + + >>> A = matrix([[1,2],[3,4]]) + >>> print(A) + [ 1 3] + [ 2 4] + + + >>> print(vec(A)) + [ 1] + [ 2] + [ 3] + [ 4] + + + """ + return matrix(real_matrix, (len(real_matrix), 1))