Esempio n. 1
0
    def test_issue_25():
        def g_fun(x):
            out = np.zeros((2, 2))
            out[0] = x
            out[1] = x
            return out

        dg_dx = nd.Jacobian(g_fun)
        x = np.array([1., 2.])
        v0 = nd.approx_fprime(x, g_fun)
        assert_allclose(v0, [[[1., 0.],
                             [0., 1.]],
                            [[1., 0.],
                             [0., 1.]]])

        dg = dg_dx(x)
        assert_allclose(dg, [[[1., 0.],
                             [0., 1.]],
                            [[1., 0.],
                             [0., 1.]]])
        fun3 = lambda x : np.vstack((x[0]*x[1]*x[2]**2, x[0]*x[1]*x[2]))
        jfun3 = nd.Jacobian(fun3)
        x = np.array([[1.,2.,3.], [4., 5., 6.]]).T
        tv = [[[18., 180.],
               [9., 144.],
               [12., 240.]],
              [[6., 30.],
               [3., 24.],
               [2., 20.]]]
        assert_allclose(jfun3(x), tv)
        assert_allclose(nd.approx_fprime(x, fun3), tv)
Esempio n. 2
0
    def test_on_matrix_valued_function():
        def fun(x):
            x = np.atleast_1d(x)
            f0 = x[0] ** 2 + x[1] ** 2
            f1 = x[0] ** 3 + x[1] ** 3
            return np.array([f0, f1])

        def dfun(x):
            x = np.atleast_1d(x)
            f0_d0 = np.atleast_1d(x[0] * 2)
            f0_d1 = np.atleast_1d(x[1] * 2)
            f1_d0 = np.atleast_1d(3 * x[0] ** 2)
            f1_d1 = np.atleast_1d(3 * x[1] ** 2)
            # algopy way:
            # df0 = np.hstack([np.diag(f0_d0), np.diag(f0_d1)])
            # df1 = np.hstack([np.diag(f1_d0), np.diag(f1_d1)])
            # numdifftools way:
            df0 = np.vstack([f0_d0, f0_d1])
            df1 = np.vstack([f1_d0, f1_d1])

            return np.array([df0, df1]).squeeze()

        jaca = nd.Jacobian(fun)
        assert_allclose(jaca([1, 2]), [[2., 4.],
                                       [3., 12.]])
        assert_allclose(jaca([3, 4]), [[6., 8.],
                                       [27., 48.]])
        assert_allclose(jaca([1, 2]), dfun([1, 2]))
        assert_allclose(jaca([3, 4]), dfun([3, 4]))

        v0 = jaca([[1, 2], [3, 4]])
        print(v0)
        assert_allclose(v0,
                        dfun([[1, 2],
                              [3, 4]]))

        x = np.array([(1, 2, 3, 4),
                      (5, 6, 7, 8)], dtype=float)

        y = fun(x)
        assert_allclose(y, [[26., 40., 58., 80.],
                            [126., 224., 370., 576.]])
        tval = dfun(x)
        assert_allclose(tval, [[[2., 4., 6., 8.],
                                [10., 12., 14., 16.]],
                               [[3., 12., 27., 48.],
                                [75., 108., 147., 192.]]])
        v0 = nd.approx_fprime(x, fun)
        val = jaca(x)
        assert_allclose(v0, tval)
        assert_allclose(val, tval)
Esempio n. 3
0
    def test_on_vector_valued_function():
        xdata = np.arange(0, 1, 0.1) #.reshape((-1, 1))
        ydata = 1 + 2 * np.exp(0.75 * xdata)

        def fun(c):
            return (c[0] + c[1] * np.exp(c[2] * xdata) - ydata) ** 2

        _j_0 = nd.approx_fprime([1, 2, 0.75], fun)

        for method in ['complex', 'central', 'forward', 'backward']:
            for order in [2, 4]:
                j_fun = nd.Jacobian(fun, method=method, order=order)
                j_val = j_fun([1, 2, 0.75])  # should be numerically zero
                assert_allclose(j_val, np.zeros((ydata.size, 3)), atol=1e-12)