--- /dev/null
+"""
+Unit tests for the functions in the ``matrices`` module.
+"""
+
+from unittest import TestCase
+
+from cvxopt import matrix
+
+from dunshire.matrices import (append_col, append_row, condition_number,
+ eigenvalues, eigenvalues_re, identity,
+ inner_product, norm)
+from dunshire.options import ABS_TOL
+from .randomgen import random_matrix, random_natural, random_scalar
+
+class AppendColTest(TestCase):
+
+ def test_size_increases(self):
+ """
+ If we append a column to a matrix, the result should be bigger
+ than the original matrix.
+ """
+ dims = random_natural()
+ mat1 = random_matrix(dims)
+ mat2 = random_matrix(dims)
+ bigmat = append_col(mat1, mat2)
+ self.assertTrue(bigmat.size[0] >= mat1.size[0])
+ self.assertTrue(bigmat.size[1] >= mat1.size[1])
+
+
+class AppendRowTest(TestCase):
+
+ def test_size_increases(self):
+ """
+ If we append a row to a matrix, the result should be bigger
+ than the original matrix.
+ """
+ dims = random_natural()
+ mat1 = random_matrix(dims)
+ mat2 = random_matrix(dims)
+ bigmat = append_row(mat1, mat2)
+ self.assertTrue(bigmat.size[0] >= mat1.size[0])
+ self.assertTrue(bigmat.size[1] >= mat1.size[1])
+
+
+class EigenvaluesTest(TestCase):
+
+ def test_eigenvalues_input_not_clobbered(self):
+ mat = random_matrix(random_natural())
+ symmat = mat + mat.trans()
+ symmat_copy = matrix(symmat, symmat.size)
+ eigs = eigenvalues(symmat)
+ self.assertTrue(norm(symmat - symmat_copy) < ABS_TOL)
+
+ def test_eigenvalues_re_input_not_clobbered(self):
+ mat = random_matrix(random_natural())
+ mat_copy = matrix(mat, mat.size)
+ eigs = eigenvalues_re(mat)
+ self.assertTrue(norm(mat - mat_copy) < ABS_TOL)
+
+ def test_eigenvalues_of_symmetric_are_real(self):
+ mat = random_matrix(random_natural())
+ symmat = mat + mat.trans()
+ eigs1 = sorted(eigenvalues(symmat))
+ eigs2 = sorted(eigenvalues_re(symmat))
+ diffs = [abs(e1-e2) for (e1,e2) in zip(eigs1,eigs2)]
+ self.assertTrue(all([diff < ABS_TOL for diff in diffs]))
+
+
+ def test_eigenvalues_of_identity(self):
+ mat = identity(random_natural(), typecode='d')
+ eigs1 = eigenvalues(mat)
+ eigs2 = eigenvalues_re(mat)
+ self.assertTrue(all([abs(e1 - 1) < ABS_TOL for e1 in eigs1]))
+ self.assertTrue(all([abs(e2 - 1) < ABS_TOL for e2 in eigs2]))
+
+
+class InnerProductTest(TestCase):
+
+ def test_norm_is_nonnegative(self):
+ vec = matrix([random_scalar() for _ in range(random_natural())])
+ self.assertTrue(inner_product(vec,vec) >= 0)
+
+
+def ConditionNumberTest(TestCase):
+
+ def test_condition_number_ge_one(self):
+ mat = random_matrix(random_natural())
+ self.assertTrue(condition_number(mat) >= 1)