コード例 #1
0
 def test_issue_27b():
     """Test for memory-error"""
     n = 1000
     x = np.ones(n)
     assert_allclose(
         nd.Jacobian(lambda x: x**2, method='complex')(x),
         2 * np.diag(np.ones(n)))
コード例 #2
0
 def test_issue_27a():
     """Test for memory-error"""
     n = 500
     x = np.ones(n)
     for method in ['complex', 'central', 'forward', 'backward']:
         assert_allclose(
             nd.Jacobian(lambda x: x**2, method=method)(x),
             2 * np.diag(np.ones(n)))
コード例 #3
0
    def test_on_scalar_function():
        def fun(x):
            return x[0] * x[1] * x[2] + np.exp(x[0]) * x[1]

        for method in ['complex', 'central', 'forward', 'backward']:
            j_fun = nd.Jacobian(fun, method=method)
            x = j_fun([3., 5., 7.])
            assert_allclose(x, [[135.42768462, 41.08553692, 15.]])
コード例 #4
0
 def test_jacobian_fulloutput():
     """test """
     res, info = nd.Jacobian(lambda x, y: x + y, full_output=True)(1, 3)
     assert_allclose(res, 1)
     assert info.error_estimate < 1e-13
     assert info.final_step == 0.015625
     assert info.index == 5
     assert info.f_value == 4
コード例 #5
0
    def test_on_scalar_function():
        def f2(x):
            return x[0] * x[1] * x[2] + np.exp(x[0]) * x[1]

        for method in ['complex', 'central', 'forward', 'backward']:
            Jfun3 = nd.Jacobian(f2, method=method)
            x = Jfun3([3., 5., 7.])
            assert_array_almost_equal(x, [[135.42768462, 41.08553692, 15.]])
コード例 #6
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 = nds.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(nds.approx_fprime(x, fun3), tv)
コード例 #7
0
    def test_scalar_to_vector(val):
        def fun(x):
            return np.array([x, x**2, x**3])

        truth = np.array([[1., 2 * val, 3 * val**2]])
        for method in [
                'multicomplex', 'complex', 'central', 'forward', 'backward'
        ]:
            j0, info = nd.Jacobian(fun, method=method, full_output=True)(val)
            error = np.abs(j0 - truth)
            note('method={}, error={}, error_est={}'.format(
                method, error, info.error_estimate))
            assert_allclose(j0, truth, rtol=1e-3, atol=1e-6)
コード例 #8
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 = nds.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)
コード例 #9
0
    def test_on_matrix_valued_function():
        def f(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 df(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)
            df0 = np.hstack([np.diag(f0_d0), np.diag(f0_d1)])
            df1 = np.hstack([np.diag(f1_d0), np.diag(f1_d1)])
            return np.array([df0, df1]).squeeze()

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

        y = f(x)
        assert_array_almost_equal(
            y, [[26., 40., 58., 80.], [126., 224., 370., 576.]])
        jaca = nd.Jacobian(f)
        assert_array_almost_equal(jaca([1, 2]), [[2., 4.], [3., 12.]])
        assert_array_almost_equal(jaca([3, 4]), [[6., 8.], [27., 48.]])
        assert_array_almost_equal(jaca([1, 2]), df([1, 2]))
        assert_array_almost_equal(jaca([3, 4]), df([3, 4]))

        # v0 = approx_fprime([[1, 2], [3, 4]], f)

        v0 = jaca([[1, 2], [3, 4]])
        assert_array_almost_equal(v0, df([[1, 2], [3, 4]]))
        assert_array_almost_equal(v0,
                                  [[[2., 0., 6., 0.], [0., 4., 0., 8.]],
                                   [[3., 0., 27., 0.], [0., 12., 0., 48.]]])

        v0 = approx_fprime(x, f)
        assert_array_almost_equal(
            v0, [[[2., 4., 6., 8.], [10., 12., 14., 16.]],
                 [[3., 12., 27., 48.], [75., 108., 147., 192.]]])

        val = jaca(x)
        assert_array_almost_equal(val, [[[2., 0., 0., 0., 10., 0., 0., 0.],
                                         [0., 4., 0., 0., 0., 12., 0., 0.],
                                         [0., 0., 6., 0., 0., 0., 14., 0.],
                                         [0., 0., 0., 8., 0., 0., 0., 16.]],
                                        [[3., 0., 0., 0., 75., 0., 0., 0.],
                                         [0., 12., 0., 0., 0., 108., 0., 0.],
                                         [0., 0., 27., 0., 0., 0., 147., 0.],
                                         [0., 0., 0., 48., 0., 0., 0., 192.]]])
コード例 #10
0
    def test_on_vector_valued_function():
        xdata = np.reshape(np.arange(0, 1, 0.1), (-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

        _J0 = approx_fprime([1, 2, 0.75], fun)

        for method in ['complex', 'central', 'forward', 'backward']:
            for order in [2, 4]:
                Jfun = nd.Jacobian(fun, method=method, order=order)
                J = Jfun([1, 2, 0.75])  # should be numerically zero
                assert_array_almost_equal(J, np.zeros((ydata.size, 3)))
コード例 #11
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 = nds.approx_fprime(x, fun)
        val = jaca(x)
        assert_allclose(v0, tval)
        assert_allclose(val, tval)