def test_funny_matrix_determinants(self):
     """
     >>> funny_matrix_determinants()
     'Funny' matrix determinant test passed.
     """
     error = False
     a = symbol('a')
     b = symbol('b')
     c = symbol('c')
     for size in range(3, 8):
         A = matrix(size, size)
         for co in range(size-1):
             # Populate one or two elements in each row
             for ec in range(0, 2):
                 numer = sparse_tree(a, b, c, randrange(1, 4), True, True, False)
                 denom = numeric(0)
                 while denom == 0:
                     denom = sparse_tree(a, b, c, randrange(2), False, True, False)
                 A[randrange(size), co] = numer/denom
             # Set the last column to a linear combination of two other columns to
             # guarantee that the determinant is zero
             for ro in range(size):
                 A[ro, size-1] = A[ro, 0] - A[ro,size-2]
             if A.determinant() != 0:
                 error = True
                 print "Determinant of", size, "x", size, "matrix", A
                 print "was not found to vanish!"
     self.assertEqual(error,0)
 def test_rational_matrix_determinants(self):
     """
     >>> rational_matrix_determinants()
     Rational matrix determinants test passed.
     """
     error = False
     a = symbol('a')
     b = symbol('b')
     c = symbol('c')
     for size in range( 3, 9):
         A = matrix(size, size)
         for r in range(size-1):
             # Populate one or two elements in each row:
             for ec in range(2):
                 numer = sparse_tree( a, b, c, randrange(1, 4), 
                     False, False, False)
                 denom = numeric(0)
                 while denom == 0:
                     denom = sparse_tree( a, b, c, randrange(2), 
                         False, False, False)
                 A[r, randrange(size)] = numer/denom
         # set the last row to a linear combination of two other lines
         # to guarantee that the determinant is zero:        
         for co in range(size):
             A[size-1, co] = A[0,co] - A[size-2,co]
         if A.determinant() != 0:
             error = True
             print "Determinant of", size, "x", size," matrix ", A
             print "was not found to vanish!"
     self.assertEqual(error,0)
 def test_compare_matrix_determinants(self):
     """
     >>> compare_matrix_determinants()
     Matrix determinant comparison test passed.
     """
     error = False
     a = symbol('a')
     for size in range(2, 8):
         A = matrix(size, size)
         for co in range(size):
             for ro in range(size):
                 if randrange(size/2) == 0:
                     A[ro, co] = sparse_tree(a, a, a, randrange(3), 
                         False, True, False)
         det_gauss = A.determinant( determinant_algo.gauss)
         det_laplace = A.determinant( determinant_algo.laplace)
         det_divfree = A.determinant( determinant_algo.divfree)
         det_bareiss = A.determinant( determinant_algo.bareiss)
         if not (det_gauss - det_laplace).normal().is_zero() \
             or not (det_bareiss - det_laplace).normal().is_zero() \
             or not (det_divfree - det_laplace).normal().is_zero():
                 error = True
                 print "Determinant of", size, "x", size, "matrix", A
                 print "is inconsistent between different algorithms:"
                 print "Gauss elimination:   ", det_gauss
                 print "Minor elimination:   ", det_laplace
                 print "Division-free elim.: ", det_divfree
                 print "Fraction-free elim.: ", det_bareiss
     self.assertEqual(error,0)
    def test_symbolic_matrix_inverse(self):
        """
        >>> symbolic_matrix_inverse()
        Symbolic matrix inverse test passed.
        """
        error = False
        a = symbol('a')
        b = symbol('b')
        c = symbol('c')
        for size in range(2, 6):
            A = matrix(size, size)
            while A.determinant() == 0:
                for co in range(size):
                    for ro in range(size):
                        if randrange(size/2) == 0:
                            A[ro, co] = sparse_tree(a, b, c, randrange(2), 
                                False, True, False)
            B = A.inverse()
            C = (A * B).evalm().normal()
            ok = True
            for ro in range(size):
                for co in range(size):
                    elem = C[ro, co]
                    if ro == co and elem != 1:
                        print "non-unity", "(", ro, ",", co, ")", elem
                        ok = False
                    if ro != co and elem != 0:
                        print "nonzero", "(", ro, ",", co, ")", elem
                        ok = False
            if not ok:
                print "Inverse of ", size, "x", size, "matrix", A
                print "errooneously returned:", B
                print "Its determinant is: ", A.determinant()
                error = True

        self.assertEqual(error,0)