Ejemplo n.º 1
0
 def test_submatrix_specialcases(self):
   mat = matrix.assemble(numpy.array([1,2,3,4]), numpy.array([[0,0,2,2],[0,2,0,2]]), (3,3))
   self.assertAllEqual(mat.export('dense'), [[1,0,2],[0,0,0],[3,0,4]])
   self.assertAllEqual(mat.submatrix([0,2],[0,1,2]).export('dense'), [[1,0,2],[3,0,4]])
   self.assertAllEqual(mat.submatrix([0,1,2],[0,2]).export('dense'), [[1,2],[0,0],[3,4]])
   self.assertAllEqual(mat.submatrix([0,2],[0,2]).export('dense'), [[1,2],[3,4]])
   self.assertAllEqual(mat.submatrix([1],[1]).export('dense'), [[0]])
Ejemplo n.º 2
0
 def test_transpose(self):
     asym = matrix.assemble(numpy.array([1, 2, 3, 4], dtype=float),
                            numpy.array([[0, 0, 1, 1], [0, 1, 1, 2]]),
                            shape=(2, 3))
     exact = numpy.array([[1, 2, 0], [0, 3, 4]], dtype=float)
     transpose = asym.T
     numpy.testing.assert_equal(actual=transpose.export('dense'),
                                desired=exact.T)
Ejemplo n.º 3
0
 def test_singular(self):
     singularmatrix = matrix.assemble(
         numpy.arange(self.n) - self.n // 2,
         numpy.arange(self.n)[numpy.newaxis].repeat(2, 0),
         shape=(self.n, self.n))
     rhs = numpy.ones(self.n)
     with self.assertRaises(matrix.MatrixError):
         lhs = singularmatrix.solve(rhs, **self.args)
         print(lhs)
Ejemplo n.º 4
0
 def test_singular(self):
     singularmatrix = matrix.assemble(
         numpy.arange(self.n) - self.n // 2,
         numpy.arange(self.n)[numpy.newaxis].repeat(2, 0),
         shape=(self.n, self.n))
     rhs = numpy.ones(self.n)
     for args in self.args:
         with self.subTest(args.get('solver', 'direct')), self.assertRaises(
                 matrix.MatrixError):
             lhs = singularmatrix.solve(rhs, **args)
Ejemplo n.º 5
0
 def setUp(self):
   super().setUp()
   self._backend = matrix.backend(self.backend)
   self._backend.__enter__()
   r = numpy.arange(self.n)
   index = numpy.concatenate([[r, r], [r[:-1], r[1:]], [r[1:], r[:-1]]], axis=1)
   data = numpy.hstack([2.] * self.n + [-1.] * (2*self.n-2))
   self.matrix = matrix.assemble(data, index, shape=(self.n, self.n))
   self.exact = 2 * numpy.eye(self.n) - numpy.eye(self.n, self.n, -1) - numpy.eye(self.n, self.n, +1)
   self.tol = self.args.get('atol', 1e-10)
Ejemplo n.º 6
0
 def test_add(self):
   j = self.n//2
   v = 10.
   other = matrix.assemble(numpy.array([v]*self.n), numpy.array([numpy.arange(self.n),[j]*self.n]), shape=(self.n, self.n))
   add = self.matrix + other
   numpy.testing.assert_equal(actual=add.export('dense'), desired=self.exact + numpy.eye(self.n)[j]*v)
   with self.assertRaises(TypeError):
     self.matrix + 'foo'
   with self.assertRaises(matrix.MatrixError):
     self.matrix + matrix.eye(self.n+1)
Ejemplo n.º 7
0
 def test_sub(self):
     j = self.n // 2
     v = 10.
     other = matrix.assemble(numpy.array([v] * self.n),
                             numpy.array(
                                 [numpy.arange(self.n), [j] * self.n]),
                             shape=(self.n, self.n))
     sub = self.matrix - other
     numpy.testing.assert_equal(actual=sub.export('dense'),
                                desired=self.exact -
                                numpy.eye(self.n)[j] * v)
Ejemplo n.º 8
0
 def setUp(self):
     super().setUp()
     self._backend = matrix.backend(self.backend)
     self._backend.__enter__()
     r = numpy.arange(self.n)
     index = numpy.concatenate([[r, r], [r[:-1], r[1:]], [r[1:], r[:-1]]],
                               axis=1)
     data = numpy.hstack([2.] * self.n + [-1.] * (2 * self.n - 2))
     self.matrix = matrix.assemble(data, index, shape=(self.n, self.n))
     self.exact = 2 * numpy.eye(self.n) - numpy.eye(
         self.n, self.n, -1) - numpy.eye(self.n, self.n, +1)
     self.tol = self.args.get('atol', 1e-10)
Ejemplo n.º 9
0
 def setUpContext(self, stack):
     super().setUpContext(stack)
     if self.backend:
         stack.enter_context(self.backend)
         index = numpy.empty([2, (self.n - 1) * 4], dtype=int)
         data = numpy.empty([(self.n - 1) * 4], dtype=float)
         for i in range(self.n - 1):
             index[:, i * 4:(i + 1) * 4] = [i, i, i + 1,
                                            i + 1], [i, i + 1, i, i + 1]
             data[i * 4:(i + 1) *
                  4] = 1 if i else 2, -1, -1, 1 if i < self.n - 2 else 2
         self.matrix = matrix.assemble(data, index, shape=(self.n, self.n))
         self.exact = 2 * numpy.eye(self.n) - numpy.eye(
             self.n, self.n, -1) - numpy.eye(self.n, self.n, +1)
Ejemplo n.º 10
0
 def test_scalar(self):
     s = matrix.assemble(numpy.array([1., 2.]),
                         index=numpy.empty((0, 2), dtype=int),
                         shape=())
     self.assertEqual(s, 3.)
Ejemplo n.º 11
0
 def test_transpose(self):
   asym = matrix.assemble(numpy.array([1,2,3,4], dtype=float), numpy.array([[0,0,1,1],[0,1,1,2]]), shape=(2,3))
   exact = numpy.array([[1,2,0],[0,3,4]], dtype=float)
   transpose = asym.T
   numpy.testing.assert_equal(actual=transpose.export('dense'), desired=exact.T)
Ejemplo n.º 12
0
 def test_sub(self):
   j = self.n//2
   v = 10.
   other = matrix.assemble(numpy.array([v]*self.n), numpy.array([numpy.arange(self.n),[j]*self.n]), shape=(self.n, self.n))
   sub = self.matrix - other
   numpy.testing.assert_equal(actual=sub.export('dense'), desired=self.exact - numpy.eye(self.n)[j]*v)
Ejemplo n.º 13
0
 def test_rowsupp(self):
   sparse = matrix.assemble(numpy.array([1e-10,0,1,1]), numpy.array([[0,0,2,2],[0,1,1,2]]), shape=(3,3))
   self.assertEqual(tuple(sparse.rowsupp(tol=1e-5)), (False,False,True))
   self.assertEqual(tuple(sparse.rowsupp(tol=0)), (True,False,True))
Ejemplo n.º 14
0
 def test_vector(self):
   v = matrix.assemble(numpy.array([1.,2.,3.]), index=numpy.array([[0,2,0]]), shape=(3,))
   self.assertEqual(tuple(v), (4.,0.,2.))
Ejemplo n.º 15
0
 def test_scalar(self):
   s = matrix.assemble(numpy.array([1.,2.]), index=numpy.empty((0,2), dtype=int), shape=())
   self.assertEqual(s, 3.)
Ejemplo n.º 16
0
 def test_singular(self):
   singularmatrix = matrix.assemble(numpy.arange(self.n)-self.n//2, numpy.arange(self.n)[numpy.newaxis].repeat(2,0), shape=(self.n, self.n))
   rhs = numpy.ones(self.n)
   with self.assertRaises(matrix.MatrixError):
     lhs = singularmatrix.solve(rhs, **self.args)
     print(lhs)
Ejemplo n.º 17
0
 def test_rowsupp(self):
   sparse = matrix.assemble(numpy.array([1e-10,0,1,1]), numpy.array([[0,0,2,2],[0,1,1,2]]), shape=(3,3))
   self.assertEqual(tuple(sparse.rowsupp(tol=1e-5)), (False,False,True))
   self.assertEqual(tuple(sparse.rowsupp(tol=0)), (True,False,True))
Ejemplo n.º 18
0
 def test_vector(self):
     v = matrix.assemble(numpy.array([1., 2., 3.]),
                         index=numpy.array([[0, 2, 0]]),
                         shape=(3, ))
     self.assertEqual(tuple(v), (4., 0., 2.))