Exemplo n.º 1
0
    def test_get_fy_on_edge(self):
        for _ in range(10):
            data = np.random.randn(4, 4)
            bicubic = sjs.Bicubic(data)

            cubic = bicubic.get_fy_on_edge(sjs.BicubicVariable.Lambda, 0)
            self.assertAlmostEqual(cubic.f(0), data[0, 2])
            self.assertAlmostEqual(cubic.f(1), data[1, 2])
            for _ in range(10):
                lam = np.random.rand()
                self.assertAlmostEqual(cubic.f(lam), bicubic.fy(lam, 0))

            cubic = bicubic.get_fy_on_edge(sjs.BicubicVariable.Lambda, 1)
            self.assertAlmostEqual(cubic.f(0), data[0, 3])
            self.assertAlmostEqual(cubic.f(1), data[1, 3])
            for _ in range(10):
                lam = np.random.rand()
                self.assertAlmostEqual(cubic.f(lam), bicubic.fy(lam, 1))

            cubic = bicubic.get_fy_on_edge(sjs.BicubicVariable.Mu, 0)
            self.assertAlmostEqual(cubic.f(0), data[0, 2])
            self.assertAlmostEqual(cubic.f(1), data[0, 3])
            for _ in range(10):
                mu = np.random.rand()
                self.assertAlmostEqual(cubic.f(mu), bicubic.fy(0, mu))

            cubic = bicubic.get_fy_on_edge(sjs.BicubicVariable.Mu, 1)
            self.assertAlmostEqual(cubic.f(0), data[1, 2])
            self.assertAlmostEqual(cubic.f(1), data[1, 3])
            for _ in range(10):
                mu = np.random.rand()
                self.assertAlmostEqual(cubic.f(mu), bicubic.fy(1, mu))
Exemplo n.º 2
0
 def test_ctor(self):
     for _ in range(10):
         data = np.random.randn(4, 4)
         bicubic = sjs.Bicubic(data)
         for (i, lam), (j, mu) in it.product(enumerate([0.0,1.0]), repeat=2):
             self.assertAlmostEqual(bicubic.f(lam, mu), data[i, j])
             self.assertAlmostEqual(bicubic.fx(lam, mu), data[2 + i, j])
             self.assertAlmostEqual(bicubic.fy(lam, mu), data[i, 2 + j])
             self.assertAlmostEqual(bicubic.fxy(lam, mu), data[2 + i, 2 + j])
Exemplo n.º 3
0
    def test_evaluate(self):
        eps = 1e-7

        for _ in range(10):
            vx, vy = np.random.uniform(-0.05, 0.05, (2,))
            s_gt = get_linear_speed_s(vx, vy)
            slow = sjs.get_linear_speed_field2(vx, vy)

            data = np.random.randn(4, 4)
            h = np.random.random()
            H = np.diag([1, 1, h, h])
            data = H@data@H

            p = np.random.randn(2)
            p0 = p + h*np.random.randn(2)
            p1 = p + h*np.random.randn(2)

            bicubic = sjs.Bicubic(data)
            T = bicubic.get_f_on_edge(sjs.BicubicVariable.Lambda, 0)
            Tx = bicubic.get_fx_on_edge(sjs.BicubicVariable.Lambda, 0)
            Ty = bicubic.get_fy_on_edge(sjs.BicubicVariable.Lambda, 0)

            a_T = np.array([T.a[i] for i in range(4)])
            a_Tx = np.array([Tx.a[i] for i in range(4)])
            a_Ty = np.array([Ty.a[i] for i in range(4)])

            context_gt = F4(s_gt, a_T, a_Tx, a_Ty, p, p0, p1)

            xy = sjs.Dvec2(*p)
            xy0 = sjs.Dvec2(*p0)
            xy1 = sjs.Dvec2(*p1)

            context = sjs.F4Context(T, Tx, Ty, xy, xy0, xy1, slow)

            for _ in range(10):
                args = np.random.random(2)
                args[1] *= 2*np.pi

                context.compute(*args)

                f4_gt = context_gt.F4(args)
                f4_eta_gt, f4_th_gt = context_gt.grad_F4(args)

                self.assertAlmostEqual(f4_gt, context.F4)
                self.assertAlmostEqual(f4_eta_gt, context.F4_eta)
                self.assertAlmostEqual(f4_th_gt, context.F4_th)
Exemplo n.º 4
0
    def test_bfgs_linear_speed(self):
        for _ in range(10):
            h = 0.1

            x0, y0 = np.random.uniform(-1, 1 - h, (2,))
            x1, y1 = x0 + h, y0 + h
            x, y = x0 - h, y0

            vx, vy = np.random.uniform(-0.05, 0.05, (2,))
            s_gt = get_linear_speed_s(vx, vy)
            slow = sjs.get_linear_speed_field2(vx, vy)

            tau_Omega = get_linear_speed_tau(vx, vy)
            tau = lambda lam, mu: tau_Omega(
                (1 - lam)*x0 + lam*x1,
                (1 -  mu)*y0 +  mu*y1
            )
            grad_tau = autograd.grad(lambda args: tau(args[0], args[1]))
            hess_tau = autograd.hessian(lambda args: tau(args[0], args[1]))
            tau_x = lambda lam, mu: grad_tau(np.array([lam, mu]))[0]
            tau_y = lambda lam, mu: grad_tau(np.array([lam, mu]))[1]
            tau_xy = lambda lam, mu: hess_tau(np.array([lam, mu]))[1][0]

            data = np.array([
                [  tau(0., 0.),    tau(0., 1.),  tau_y(0., 0.),  tau_y(0., 1.)],
                [  tau(1., 0.),    tau(1., 1.),  tau_y(1., 0.),  tau_y(1., 1.)],
                [tau_x(0., 0.),  tau_x(0., 1.), tau_xy(0., 0.), tau_xy(0., 1.)],
                [tau_x(1., 0.),  tau_x(1., 1.), tau_xy(1., 0.), tau_xy(1., 1.)],
            ])
            bicubic = sjs.Bicubic(data)
            T = bicubic.get_f_on_edge(sjs.BicubicVariable.Lambda, 0)
            Tx = bicubic.get_fx_on_edge(sjs.BicubicVariable.Lambda, 0)
            Ty = bicubic.get_fy_on_edge(sjs.BicubicVariable.Lambda, 0)

            a_T = np.array([T.a[i] for i in range(4)])
            a_Tx = np.array([Tx.a[i] for i in range(4)])
            a_Ty = np.array([Ty.a[i] for i in range(4)])

            p, p0, p1 = np.array([x, y]), np.array([x0, y0]), np.array([x0, y1])
            context_gt = F4(s_gt, a_T, a_Tx, a_Ty, p, p0, p1)

            xy = sjs.Dvec2(*p)
            xy0 = sjs.Dvec2(*p0)
            xy1 = sjs.Dvec2(*p1)

            context = sjs.F4Context(T, Tx, Ty, xy, xy0, xy1, slow)

            context3 = sjs.F3Context(T, xy, xy0, xy1, slow)
            def F3(eta):
                context3.compute(eta)
                return context3.F3
            def F3_eta(eta):
                context3.compute(eta)
                return context3.F3_eta
            if np.sign(F3_eta(0.)) == np.sign(F3_eta(1.)):
                argeta3 = 0. if F3(0.) < F3(1.) else 1.
            else:
                argeta3 = brentq(F3_eta, 0, 1)
            lp = (p - p0 - argeta3*(p1 - p0))
            lp /= np.linalg.norm(lp)
            argth3 = np.arctan2(*reversed(lp))

            xk_gt = np.array([argeta3, argth3])

            eps = 1e-7

            hess_gt = context_gt.hess_F4(xk_gt)
            hess_fd = context.hess_fd(argeta3, argth3, eps)

            self.assertTrue(abs(hess_gt[0, 0] - hess_fd[0, 0]) < eps)
            self.assertTrue(abs(hess_gt[1, 0] - hess_fd[1, 0]) < eps)
            self.assertTrue(abs(hess_gt[0, 1] - hess_fd[0, 1]) < eps)
            self.assertTrue(abs(hess_gt[1, 1] - hess_fd[1, 1]) < eps)

            gk_gt = context_gt.grad_F4(xk_gt)
            Hk_gt = np.linalg.inv(hess_gt)

            xk, gk, Hk = context.bfgs_init(*xk_gt)

            eps = 1e-6
            self.assertTrue(abs(Hk_gt[0, 0] - Hk[0, 0]) < eps)
            self.assertTrue(abs(Hk_gt[1, 0] - Hk[1, 0]) < eps)
            self.assertTrue(abs(Hk_gt[0, 1] - Hk[0, 1]) < eps)
            self.assertTrue(abs(Hk_gt[1, 1] - Hk[1, 1]) < eps)