def test_on_scalar_function(): def f2(x): return x[0] * x[1] * x[2] + np.exp(x[0]) * x[1] for method in ['forward', 'reverse']: Jfun3 = nd.Jacobian(f2, method=method) x = Jfun3([3., 5., 7.]) assert_array_almost_equal(x, [[135.42768462, 41.08553692, 15.]])
def test_on_vector_valued_function(self): xdata = np.arange(0, 1, 0.1) ydata = 1 + 2 * np.exp(0.75 * xdata) def fun(c): return (c[0] + c[1] * np.exp(c[2] * xdata) - ydata) ** 2 for method in ['reverse', ]: j_fun = nd.Jacobian(fun, method=method) J = j_fun([1, 2, 0.75]) # should be numerically zero assert_allclose(J, np.zeros((ydata.size, 3))) # TODO: 'forward' not implemented j_fun = nd.Jacobian(fun, method='forward') with pytest.raises(NotImplementedError): j_fun([1, 2, 0.75])
def test_on_scalar_function(): def fun(x): return x[0] * x[1] * x[2] + np.exp(x[0]) * x[1] for method in ['forward', 'reverse']: j_fun = nd.Jacobian(fun, method=method) x = j_fun([3., 5., 7.]) assert_allclose(x, [[135.42768462, 41.08553692, 15.]])
def test_scalar_to_vector(val): def fun(x): out = algopy.zeros((3, ), dtype=x) out[0] = x out[1] = x**2 out[2] = x**3 return out for method in ['reverse', 'forward']: j0 = nd.Jacobian(fun, method=method)(val).T assert_allclose(j0, [[1., 2 * val, 3 * val**2]], atol=1e-14)
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 for method in ['reverse']: # TODO: 'forward' fails Jfun = nd.Jacobian(fun, method=method) J = Jfun([1, 2, 0.75]) # should be numerically zero assert_array_almost_equal(J, np.zeros((ydata.size, 3)))
def test_issue_25(): def g_fun(x): out = algopy.zeros((2, 2), dtype=x) out[0, 0] = x[0] out[0, 1] = x[1] out[1, 0] = x[0] out[1, 1] = x[1] return out dg_dx = nd.Jacobian(g_fun) # TODO: method='reverse' fails x = [1, 2] dg = dg_dx(x) assert_allclose(dg, [[[1., 0.], [0., 1.]], [[1., 0.], [0., 1.]]])
def main(problem_sizes=(4, 8, 16, 32, 64, 96)): fixed_step = MinStepGenerator(num_steps=1, use_exact_steps=True, offset=0) epsilon = MaxStepGenerator(num_steps=14, use_exact_steps=True, step_ratio=1.6, offset=0) adaptiv_txt = '_adaptive_{0:d}_{1!s}_{2:d}'.format(epsilon.num_steps, str(epsilon.step_ratio), epsilon.offset) gradient_funs = OrderedDict() hessian_funs = OrderedDict() hessian_fun = 'Hessdiag' hessian_fun = 'Hessian' if nda is not None: nda_method = 'forward' nda_txt = 'algopy_' + nda_method gradient_funs[nda_txt] = nda.Jacobian(1, method=nda_method) hessian_funs[nda_txt] = getattr(nda, hessian_fun)(1, method=nda_method) ndc_hessian = getattr(nd, hessian_fun) order = 2 for method in ['forward', 'central', 'complex']: method2 = method + adaptiv_txt options = dict(method=method, order=order) gradient_funs[method] = nd.Jacobian(1, step=fixed_step, **options) gradient_funs[method2] = nd.Jacobian(1, step=epsilon, **options) hessian_funs[method] = ndc_hessian(1, step=fixed_step, **options) hessian_funs[method2] = ndc_hessian(1, step=epsilon, **options) hessian_funs['forward_statsmodels'] = nds.Hessian(1, method='forward') hessian_funs['central_statsmodels'] = nds.Hessian(1, method='central') hessian_funs['complex_statsmodels'] = nds.Hessian(1, method='complex') gradient_funs['forward_statsmodels'] = nds.Jacobian(1, method='forward') gradient_funs['central_statsmodels'] = nds.Jacobian(1, method='central') gradient_funs['complex_statsmodels'] = nds.Jacobian(1, method='complex') gradient_funs['forward_scipy'] = nsc.Jacobian(1, method='forward') gradient_funs['central_scipy'] = nsc.Jacobian(1, method='central') gradient_funs['complex_scipy'] = nsc.Jacobian(1, method='complex') run_gradient_and_hessian_benchmarks(gradient_funs, hessian_funs, problem_sizes)
def test_on_matrix_valued_function(): def fun(x): f0 = x[0] ** 2 + x[1] ** 2 f1 = x[0] ** 3 + x[1] ** 3 s0 = f0.size s1 = f1.size out = algopy.zeros((2, (s0 + s1) // 2), dtype=x) out[0, :] = f0 out[1, :] = f1 return out 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.]]) for method in ['forward', ]: # TODO: 'reverse' fails jaca = nd.Jacobian(fun, method=method) assert_allclose(jaca([1, 2]), [[[2., 4.]], [[3., 12.]]]) assert_allclose(jaca([3, 4]), [[[6., 8.]], [[27., 48.]]]) assert_allclose(jaca([[1, 2], [3, 4]]), [[[2., 0., 6., 0.], [0., 4., 0., 8.]], [[3., 0., 27., 0.], [0., 12., 0., 48.]]]) val = jaca(x) assert_allclose(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.]]])
def test_on_matrix_valued_function(): def f(x): f0 = x[0] ** 2 + x[1] ** 2 f1 = x[0] ** 3 + x[1] ** 3 s0 = f0.size s1 = f1.size out = algopy.zeros((2, (s0 + s1) / 2), dtype=x) out[0, :] = f0 out[1, :] = f1 return out 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], [3, 4]]), [[[2., 0., 6., 0.], [0., 4., 0., 8.]], [[3., 0., 27., 0.], [0., 12., 0., 48.]]]) # v0 = df([1, 2]) 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.]]])
import numpy as np import numdifftools as nd import numdifftools.nd_algopy as nda num_dimensions = 2 #################################### function = lambda c: c[0]**10 + c[1] - 5 #################################### jacobian = nda.Jacobian(function, method='reverse') inverse_jacobian = np.zeros((num_dimensions - 1, 1)) def generate_inverse_jac(jac): inverse_jac = np.zeros((num_dimensions, 1)) for i in range(len(jac)): if not (jac[i] == 0): #print "Changing " + str(jac[i]) + " to " + str(1/jac[i]) inverse_jac[i] = 1 / jac[i] else: inverse_jac[i] = 0 return inverse_jac def vector_scaler_mult(in_vector, scaler_value): out_vector = np.zeros((num_dimensions, 1)) for i in range(len(in_vector)): out_vector[i] = in_vector[i] * scaler_value return out_vector def vector_add(vector_one, vector_two): out_vector = np.zeros((num_dimensions, 1)) for i in range(len(vector_one)):
def loglimits(data, border=0.05): low, high = np.min(data), np.max(data) scale = (high/low)**border return low/scale, high*scale fixed_step = MinStepGenerator(num_steps=1, use_exact_steps=True, offset=0) epsilon = MaxStepGenerator(num_steps=14, use_exact_steps=True, step_ratio=1.6, offset=0) adaptiv_txt = '_adaptive_{0:d}_{1!s}_{2:d}'.format(epsilon.num_steps, str(epsilon.step_ratio), epsilon.offset) gradient_funs = OrderedDict() nda_method = 'forward' nda_txt = 'algopy_' + nda_method gradient_funs[nda_txt] = nda.Jacobian(1, method=nda_method) hessian_fun = 'Hessdiag' hessian_fun = 'Hessian' ndc_hessian = getattr(nd, hessian_fun) hessian_funs = OrderedDict() hessian_funs[nda_txt] = getattr(nda, hessian_fun)(1, method=nda_method) order = 2 for method in ['forward', 'central', 'complex']: method2 = method + adaptiv_txt options = dict(method=method, order=order) gradient_funs[method] = nd.Jacobian(1, step=fixed_step, **options) gradient_funs[method2] = nd.Jacobian(1, step=epsilon, **options) hessian_funs[method] = ndc_hessian(1, step=fixed_step, **options) hessian_funs[method2] = ndc_hessian(1, step=epsilon, **options)
h_C = 10.**(-s.pH) * 1e3 * mM hco3_A = 42.9 * mM hco3_C = 42.9 * mM co2_A = 1.288 * mM co2_C = 1.228 * mM x_0 = [ w_C, na_A, cl_A, k_A, h_A, hco3_A, co2_A, na_C, cl_C, k_C, h_C, hco3_C, co2_C ] x_0 = s.fix_initial_conditions(x_0) x_cur = x_0 # note forward mode ist faster than reverse mode in our case! ode_jac = nda.Jacobian(s.ode, method='forward') t0 = 0 t_bound = 10e4 ode = BDF(lambda t, x: s.ode(x), y0=x_0, t0=t0, t_bound=t_bound, jac=lambda t, x: ode_jac(x)) T = np.array([t0]) Y = np.array(x_0) Y = Y[:, np.newaxis] F = np.array(s.ode(x_0))
sys.exit() grad_fct = grad(sphere) print(grad_fct([beta_syntetic[0], beta_syntetic[0], beta_syntetic[0]])) sphere(beta_syntetic + epsilon) print(1) grad_fct = jacobian(mixedSchwefel) print(grad_fct(beta_syntetic)) np.gradient([sphere], beta_syntetic) print(2) import numdifftools.nd_algopy as nda import numdifftools as nd fd = nda.Derivative(sphere) # 1'st derivative fd = nda.Jacobian(sphere) fd(beta_syntetic) np.allclose(fd(1), 2.7182818284590424) True # #bnds = list(zip(np.repeat(-500,Nparam),np.repeat(500,Nparam))) #((0, None), (0, None)) #values=pd.DataFrame(np.zeros((Iter*N+1,N)),columns=np.arange(N),index=np.arange(Iter*N+1)) # #target=TargetClass(dim=Nparam, minf=-500.0, maxf=500.0,target_function=target_function) #x01=np.random.randint(-5,5,size=Nparam) ##print (x0[index]) #index=np.setdiff1d(np.arange(Nparam),np.array([])) ##