示例#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]])
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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.)
示例#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)
示例#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)
示例#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))
示例#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.))
示例#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.)
示例#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)
示例#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))
示例#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.))