Example #1
0
    def test_val_grad_hess(self):
        us   = [ l2.STO(1.0, 2, z) for z in [1.1, 1.2]]
        driv = l2.STO(1.0 / np.sqrt(2.0), 2, 1.0)

        h_atom = l2.HAtom(1.0)
        l_op = h_atom.h_minus_ene_op(1, 0.5)

        (val, grad, hess, datas) = opt_cbf.val_grad_hess(us, driv, l_op)

        """
        These values are taken from 2015/9/1_**.ipynb
        """

        self.assertAlmostEqual(0.608906838305313,  datas['a0'][0])
        self.assertAlmostEqual(0.599798428001011,  datas['a0'][1])

        self.assertAlmostEqual(-0.445,             datas['A00'][0][0])
        self.assertAlmostEqual(-0.413041532083526, datas['A00'][1][0])
        self.assertAlmostEqual(-0.413041532083526, datas['A00'][0][1])
        self.assertAlmostEqual(-0.380000000000000, datas['A00'][1][1])

        self.assertAlmostEqual(-0.478372504129766, val)
        self.assertAlmostEqual(-1.01381241031413, grad[0])
        self.assertAlmostEqual(-0.915041952889624, grad[1])

        self.assertAlmostEqual(6.42894956208815, hess[0][0])
        self.assertAlmostEqual(5.10430865981922, hess[0][1])
        self.assertAlmostEqual(5.10430865981922, hess[1][0])
        self.assertAlmostEqual(5.77073843522357, hess[1][1])
Example #2
0
    def test_val_grad_hess_partial(self):
        us = [l2.STO(1.0, 2, z) for z in [1.1, 1.3, 1.5, 1.8]]
        driv = l2.HAtom(1.0).length(1, 0, 1)
        l_op = l2.HAtom(1.0).h_minus_ene_op(1, 0.5)
        
        (vf, gf, hf, ds) = opt_cbf.val_grad_hess(us, driv, l_op)
        (v, g, h, ds) = opt_cbf.val_grad_hess(us, driv, l_op, opt_index = [0, 2])

        self.assertAlmostEqual(vf, v)

        self.assertEqual(2, len(g))
        self.assertAlmostEqual(gf[0], g[0])
        self.assertAlmostEqual(gf[2], g[1])

        self.assertEqual((2, 2), h.shape)
        self.assertAlmostEqual(hf[0,0], h[0, 0])
        self.assertAlmostEqual(hf[2,0], h[1, 0])
        self.assertAlmostEqual(hf[0,2], h[0, 1])
        self.assertAlmostEqual(hf[2,2], h[1, 1])