]> gitweb.michael.orlitzky.com - dunshire.git/blobdiff - src/dunshire/matrices.py
Remove unused import.
[dunshire.git] / src / dunshire / matrices.py
index 52ba2a34c0352ac9d16744c64abc5ac195a50d7e..ef3c4c0ba6b8926c67332b242a19c070032319ee 100644 (file)
@@ -1,20 +1,45 @@
 """
 Utility functions for working with CVXOPT matrices (instances of the
-``cvxopt.base.matrix`` class).
+class:`cvxopt.base.matrix` class).
 """
 
 from math import sqrt
 from cvxopt import matrix
-from cvxopt.lapack import syev
+from cvxopt.lapack import gees, syevr
+
+import options
+
 
 def append_col(left, right):
     """
-    Append the matrix ``right`` to the right side of the matrix ``left``.
+    Append two matrices side-by-side.
+
+    Parameters
+    ----------
+
+    left, right : matrix
+        The two matrices to append to one another.
+
+    Returns
+    -------
+
+    matrix
+        A new matrix consisting of ``right`` appended to the right
+        of ``left``.
 
-    EXAMPLES:
+    Examples
+    --------
 
         >>> A = matrix([1,2,3,4], (2,2))
         >>> B = matrix([5,6,7,8,9,10], (2,3))
+        >>> print(A)
+        [ 1  3]
+        [ 2  4]
+        <BLANKLINE>
+        >>> print(B)
+        [  5   7   9]
+        [  6   8  10]
+        <BLANKLINE>
         >>> print(append_col(A,B))
         [  1   3   5   7   9]
         [  2   4   6   8  10]
@@ -23,14 +48,37 @@ def append_col(left, right):
     """
     return matrix([left.trans(), right.trans()]).trans()
 
+
 def append_row(top, bottom):
     """
-    Append the matrix ``bottom`` to the bottom of the matrix ``top``.
+    Append two matrices top-to-bottom.
+
+    Parameters
+    ----------
+
+    top, bottom : matrix
+        The two matrices to append to one another.
+
+    Returns
+    -------
+
+    matrix
+        A new matrix consisting of ``bottom`` appended below ``top``.
 
-    EXAMPLES:
+    Examples
+    --------
 
         >>> A = matrix([1,2,3,4], (2,2))
         >>> B = matrix([5,6,7,8,9,10], (3,2))
+        >>> print(A)
+        [ 1  3]
+        [ 2  4]
+        <BLANKLINE>
+        >>> print(B)
+        [  5   8]
+        [  6   9]
+        [  7  10]
+        <BLANKLINE>
         >>> print(append_row(A,B))
         [  1   3]
         [  2   4]
@@ -43,29 +91,148 @@ def append_row(top, bottom):
     return matrix([top, bottom])
 
 
-def eigenvalues(real_matrix):
+def eigenvalues(symmat):
     """
-    Return the eigenvalues of the given ``real_matrix``.
+    Return the eigenvalues of the given symmetric real matrix.
+
+    On the surface, this appears redundant to the :func:`eigenvalues_re`
+    function. However, if we know in advance that our input is
+    symmetric, a better algorithm can be used.
+
+    Parameters
+    ----------
+
+    symmat : matrix
+        The real symmetric matrix whose eigenvalues you want.
 
-    EXAMPLES:
+    Returns
+    -------
+
+    list of float
+       A list of the eigenvalues (in no particular order) of ``symmat``.
+
+    Raises
+    ------
+
+    TypeError
+        If the input matrix is not symmetric.
+
+    Examples
+    --------
 
         >>> A = matrix([[2,1],[1,2]], tc='d')
         >>> eigenvalues(A)
         [1.0, 3.0]
 
+    If the input matrix is not symmetric, it may not have real
+    eigenvalues, and we don't know what to do::
+
+        >>> A = matrix([[1,2],[3,4]])
+        >>> eigenvalues(A)
+        Traceback (most recent call last):
+        ...
+        TypeError: input must be a symmetric real matrix
+
     """
-    domain_dim = real_matrix.size[0] # Assume ``real_matrix`` is square.
+    if not norm(symmat.trans() - symmat) < options.ABS_TOL:
+        # Ensure that ``symmat`` is symmetric (and thus square).
+        raise TypeError('input must be a symmetric real matrix')
+
+    domain_dim = symmat.size[0]
     eigs = matrix(0, (domain_dim, 1), tc='d')
-    syev(real_matrix, eigs)
+    syevr(symmat, eigs)
     return list(eigs)
 
 
+def eigenvalues_re(anymat):
+    """
+    Return the real parts of the eigenvalues of the given square matrix.
+
+    Parameters
+    ----------
+
+    anymat : matrix
+        The square matrix whose eigenvalues you want.
+
+    Returns
+    -------
+
+    list of float
+       A list of the real parts (in no particular order) of the
+       eigenvalues of ``anymat``.
+
+    Raises
+    ------
+
+    TypeError
+        If the input matrix is not square.
+
+    Examples
+    --------
+
+    This is symmetric and has two real eigenvalues:
+
+        >>> A = matrix([[2,1],[1,2]], tc='d')
+        >>> sorted(eigenvalues_re(A))
+        [1.0, 3.0]
+
+    But this rotation matrix has eigenvalues `i` and `-i`, both of whose
+    real parts are zero:
+
+        >>> A = matrix([[0,-1],[1,0]])
+        >>> eigenvalues_re(A)
+        [0.0, 0.0]
+
+    If the input matrix is not square, it doesn't have eigenvalues::
+
+        >>> A = matrix([[1,2],[3,4],[5,6]])
+        >>> eigenvalues_re(A)
+        Traceback (most recent call last):
+        ...
+        TypeError: input matrix must be square
+
+    """
+    if not anymat.size[0] == anymat.size[1]:
+        raise TypeError('input matrix must be square')
+
+    domain_dim = anymat.size[0]
+    eigs = matrix(0, (domain_dim, 1), tc='z')
+
+    # Create a copy of ``anymat`` here for two reasons:
+    #
+    #   1. ``gees`` clobbers its input.
+    #   2. We need to ensure that the type code of ``dummy`` is 'd' or 'z'.
+    #
+    dummy = matrix(anymat, anymat.size, tc='d')
+
+    gees(dummy, eigs)
+    return [eig.real for eig in eigs]
+
+
 def identity(domain_dim):
     """
-    Return a ``domain_dim``-by-``domain_dim`` dense integer identity
-    matrix.
+    Create an identity matrix of the given dimensions.
 
-    EXAMPLES:
+    Parameters
+    ----------
+
+    domain_dim : int
+        The dimension of the vector space on which the identity will act.
+
+    Returns
+    -------
+
+    matrix
+        A ``domain_dim``-by-``domain_dim`` dense integer identity matrix.
+
+    Raises
+    ------
+
+    ValueError
+        If you ask for the identity on zero or fewer dimensions.
+
+    Examples
+    --------
 
        >>> print(identity(3))
        [ 1  0  0]
@@ -85,10 +252,28 @@ def identity(domain_dim):
 
 def inner_product(vec1, vec2):
     """
-    Compute the (Euclidean) inner product of the two vectors ``vec1``
-    and ``vec2``.
+    Compute the Euclidean inner product of two vectors.
+
+    Parameters
+    ----------
+
+    vec1, vec2 : matrix
+        The two vectors whose inner product you want.
+
+    Returns
+    -------
+
+    float
+        The inner product of ``vec1`` and ``vec2``.
+
+    Raises
+    ------
 
-    EXAMPLES:
+    TypeError
+        If the lengths of ``vec1`` and ``vec2`` differ.
+
+    Examples
+    --------
 
         >>> x = [1,2,3]
         >>> y = [3,4,1]
@@ -111,16 +296,30 @@ def inner_product(vec1, vec2):
     if not len(vec1) == len(vec2):
         raise TypeError('the lengths of vec1 and vec2 must match')
 
-    return sum([x*y for (x,y) in zip(vec1,vec2)])
+    return sum([x*y for (x, y) in zip(vec1, vec2)])
 
 
 def norm(matrix_or_vector):
     """
-    Return the Frobenius norm of ``matrix_or_vector``, which is the same
-    thing as its Euclidean norm when it's a vector (when one of its
-    dimensions is unity).
+    Return the Frobenius norm of a matrix or vector.
+
+    When the input is a vector, its matrix-Frobenius norm is the same
+    thing as its vector-Euclidean norm.
+
+    Parameters
+    ----------
+
+    matrix_or_vector : matrix
+        The matrix or vector whose norm you want.
+
+    Returns
+    -------
 
-    EXAMPLES:
+    float
+        The norm of ``matrix_or_vector``.
+
+    Examples
+    --------
 
         >>> v = matrix([1,1])
         >>> print('{:.5f}'.format(norm(v)))
@@ -131,14 +330,28 @@ def norm(matrix_or_vector):
         2.0
 
     """
-    return sqrt(inner_product(matrix_or_vector,matrix_or_vector))
+    return sqrt(inner_product(matrix_or_vector, matrix_or_vector))
 
 
-def vec(real_matrix):
+def vec(mat):
     """
-    Create a long vector in column-major order from ``real_matrix``.
+    Create a long vector in column-major order from ``mat``.
+
+    Parameters
+    ----------
+
+    mat : matrix
+        Any sort of real matrix that you want written as a long vector.
+
+    Returns
+    -------
+
+    matrix
+        An ``len(mat)``-by-``1`` long column vector containign the
+        entries of ``mat`` in column major order.
 
-    EXAMPLES:
+    Examples
+    --------
 
         >>> A = matrix([[1,2],[3,4]])
         >>> print(A)
@@ -153,7 +366,7 @@ def vec(real_matrix):
         [ 4]
         <BLANKLINE>
 
-    Note that if ``real_matrix`` is a vector, this function is a no-op:
+    Note that if ``mat`` is a vector, this function is a no-op:
 
         >>> v = matrix([1,2,3,4], (4,1))
         >>> print(v)
@@ -170,4 +383,4 @@ def vec(real_matrix):
         <BLANKLINE>
 
     """
-    return matrix(real_matrix, (len(real_matrix), 1))
+    return matrix(mat, (len(mat), 1))