Esempio n. 1
0
 def test_multiple_vector_valued(self):
     """
     For a function that returns a vector rather than a scalar, make
     sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac() behave correctly.
     """
     for nparams1 in range(1, 5):
         for nvals1 in range(1, 5):
             nparams2 = np.random.randint(1, 6)
             nparams3 = np.random.randint(1, 6)
             nvals2 = np.random.randint(1, 6)
             nvals3 = np.random.randint(1, 6)
             o1 = Affine(nparams=nparams1, nvals=nvals1)
             o2 = Affine(nparams=nparams2, nvals=nvals2)
             o3 = Affine(nparams=nparams3, nvals=nvals3)
             dofs = Dofs([o1, o2, o3], diff_method="centered")
             dofs.set(
                 (np.random.rand(nparams1 + nparams2 + nparams3) - 0.5) * 4)
             f1 = np.matmul(o1.A, o1.x) + o1.B
             f2 = np.matmul(o2.A, o2.x) + o2.B
             f3 = np.matmul(o3.A, o3.x) + o3.B
             np.testing.assert_allclose(dofs.f(), np.concatenate((f1, f2, f3)), \
                                        rtol=1e-13, atol=1e-13)
             true_jac = np.zeros(
                 (nvals1 + nvals2 + nvals3, nparams1 + nparams2 + nparams3))
             true_jac[0:nvals1, 0:nparams1] = o1.A
             true_jac[nvals1:nvals1 + nvals2,
                      nparams1:nparams1 + nparams2] = o2.A
             true_jac[nvals1 + nvals2:nvals1 + nvals2 + nvals3, \
                      nparams1 + nparams2:nparams1 + nparams2 + nparams3] = o3.A
             np.testing.assert_allclose(dofs.jac(),
                                        true_jac,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        true_jac, rtol=1e-7, atol=1e-7)
Esempio n. 2
0
 def test_mixed_vector_valued(self):
     """
     For a mixture of functions that return a scalar vs return a
     vector, make sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac()
     behave correctly.
     """
     for nparams1 in range(1, 5):
         for nvals1 in range(1, 5):
             nparams2 = np.random.randint(1, 6)
             nparams3 = np.random.randint(1, 6)
             nvals2 = np.random.randint(1, 6)
             nvals3 = np.random.randint(1, 6)
             o1 = Affine(nparams=nparams1, nvals=nvals1)
             o2 = Affine(nparams=nparams2, nvals=nvals2)
             o3 = Affine(nparams=nparams3, nvals=nvals3)
             a1 = Adder(n=2)
             a2 = Adder(n=3)
             dofs = Dofs([o1, o2, a1, o3, a2], diff_method="centered")
             dofs.set(
                 (np.random.rand(nparams1 + nparams2 + nparams3 + 5) - 0.5)
                 * 4)
             f1 = np.matmul(o1.A, o1.x) + o1.B
             f2 = np.matmul(o2.A, o2.x) + o2.B
             f3 = np.array([a1.f])
             f4 = np.matmul(o3.A, o3.x) + o3.B
             f5 = np.array([a2.f])
             np.testing.assert_allclose(dofs.f(), np.concatenate((f1, f2, f3, f4, f5)), \
                                        rtol=1e-13, atol=1e-13)
             true_jac = np.zeros((nvals1 + nvals2 + nvals3 + 2,
                                  nparams1 + nparams2 + nparams3 + 5))
             true_jac[0:nvals1, 0:nparams1] = o1.A
             true_jac[nvals1:nvals1 + nvals2,
                      nparams1:nparams1 + nparams2] = o2.A
             true_jac[nvals1 + nvals2:nvals1 + nvals2 + 1, \
                      nparams1 + nparams2:nparams1 + nparams2 + 2] = np.ones(2)
             true_jac[nvals1 + nvals2 + 1:nvals1 + nvals2 + 1 + nvals3, \
                      nparams1 + nparams2 + 2:nparams1 + nparams2 + 2 + nparams3] = o3.A
             true_jac[nvals1 + nvals2 + 1 + nvals3:nvals1 + nvals2 + nvals3 + 2, \
                      nparams1 + nparams2 + nparams3 + 2:nparams1 + nparams2 + nparams3 + 5] = np.ones(3)
             np.testing.assert_allclose(dofs.jac(),
                                        true_jac,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        true_jac, rtol=1e-7, atol=1e-7)