示例#1
0
 def test_dimensions(self):
     bm = BlockMatrix(2, 2)
     self.assertTrue(bm.has_undefined_row_sizes())
     self.assertTrue(bm.has_undefined_col_sizes())
     with self.assertRaises(NotFullyDefinedBlockMatrixError):
         shape = bm.shape
     with self.assertRaises(NotFullyDefinedBlockMatrixError):
         bm.set_block(0, 0, BlockMatrix(2, 2))
     with self.assertRaises(NotFullyDefinedBlockMatrixError):
         row_sizes = bm.row_block_sizes()
     with self.assertRaises(NotFullyDefinedBlockMatrixError):
         col_sizes = bm.col_block_sizes()
     bm2 = BlockMatrix(2, 2)
     bm2.set_block(0, 0, coo_matrix((2, 2)))
     bm2.set_block(1, 1, coo_matrix((2, 2)))
     bm3 = bm2.copy()
     bm.set_block(0, 0, bm2)
     bm.set_block(1, 1, bm3)
     self.assertFalse(bm.has_undefined_row_sizes())
     self.assertFalse(bm.has_undefined_col_sizes())
     self.assertEqual(bm.shape, (8, 8))
     bm.set_block(0, 0, None)
     self.assertFalse(bm.has_undefined_row_sizes())
     self.assertFalse(bm.has_undefined_col_sizes())
     self.assertEqual(bm.shape, (8, 8))
     self.assertTrue(np.all(bm.row_block_sizes() == np.ones(2) * 4))
     self.assertTrue(np.all(bm.col_block_sizes() == np.ones(2) * 4))
     self.assertTrue(
         np.all(bm.row_block_sizes(copy=False) == np.ones(2) * 4))
     self.assertTrue(
         np.all(bm.col_block_sizes(copy=False) == np.ones(2) * 4))
    def test_simple_model_1(self):
        model = SimpleModel1()
        m = model.make_model()
        m.x.set_value(2.0)
        m.y.set_value(2.0)

        con = m.residual_eqn
        expected_hess = np.array([[2.0, 0.0], [0.0, 2.0]])
        hess = get_hessian_of_constraint(con)
        self.assertTrue(np.all(expected_hess == hess.toarray()))

        expected_hess = np.array([[2.0]])
        hess = get_hessian_of_constraint(con, [m.x])
        self.assertTrue(np.all(expected_hess == hess.toarray()))

        con = m.external_eqn
        expected_hess = np.array([[0.0, 1.0], [1.0, 0.0]])
        hess = get_hessian_of_constraint(con)
        self.assertTrue(np.all(expected_hess == hess.toarray()))