def __func__(vs): zs = [u.z for u in basis_set] for (i, v) in zip(opt_index, vs): zs[i] = v us = [l2.d0_basis(type(u), u.n, z) for (u, z) in zip(basis_set, zs)] (val, grad, hess, dum) = val_grad_hess(us, driv, l_op, opt_index) return (val, grad, hess)
def optimize_partial(init_basis_set, driven_term, l_op, eps=0.00001, max_iter = 30, opt_index=None): if(opt_index == None): return optimize_partial(init_basis_set, driven_term, l_op, eps=eps, max_iter = max_iter) conv_q = convergence_q(eps) res_list = [] basis_set = init_basis_set for i in range(max_iter): # compute value, gradient, hessian and other (v, g, h, data) = val_grad_hess(basis_set, driven_term, l_op, opt_index = opt_index) # update dz_list = la.solve(h, g) dz_list_full = list_indexed(zip(opt_index, dz_list), len(basis_set)) basis_set = [ l2.d0_basis(type(basis_set[0]), basis.n, basis.z - dz) for (basis, dz) in zip(basis_set, dz_list_full)] if conv_q(g, dz_list): return (True, basis_set, i+1, res_list) return (False, basis_set, max_iter, max_iter, res_list)
def optimize_simple(init_basis_set, driven_term, l_op, **keyargs): res_list = [] ns = [u.n for u in init_basis_set] z0s= [u.z for u in init_basis_set] bt = type(init_basis_set[0]) def one(zs): us = [bt(1.0, n, z) for (n, z) in zip(ns, zs)] (val, grad, hess, data) = val_grad_hess(us, driven_term, l_op) return (val, grad, hess) (res_conv, zs_opt, val_opt, grad_opt) = newton(one, z0s, **keyargs) basis_opt = [l2.d0_basis(bt, n, z) for (n, z) in zip(ns, zs_opt)] return (res_conv, basis_opt, res_list)
def val_only(us, driv, l_op, opt_index=None): def make_vec(flag_basis_set, ip): return np.array([ip(u, driv) if f else 0 for (f, u) in flag_basis_set]) def make_mat(flag_basis_set1, flag_basis_set2, ip): return np.array([[ip(a, l_op, b) if fa and fb else 0 for (fb, b) in flag_basis_set2] for (fa, a) in flag_basis_set1]) if opt_index == None: opt_index = range(len(us)) bt = type(us[0]) us = [(True, l2.d0_basis(bt, u.n, u.z)) for (u, i) in with_index(us)] a0 = make_vec(us, l2.cip) A00 = make_mat(us, us, l2.cip) val = np.dot(a0, la.solve(A00, a0)) return val
def optimize_even_temp(bt, et_num, et_n, et_z0, et_r0, nz0_list, driv, lop, **keyargs): """ Inputs ------ et_num : int number of the even tempered basis et_n: int princinple number of the even tempered basis et_z0: complex initial guess for first complex orbital exponents of the even-tempered basis et_r: complex initial guess for ratio of the even tempered basis nz_list: [(int, complex)] principle numbers and initial guess orbital exponents of basis to be optimize """ res_list = [] def one(vars): a = vars[0] b = vars[1] et_ns = range(et_num) zs = [a*b**n for n in et_ns] + list(vars[2:]) ns = [et_n for n in et_ns] + [n for (n, z) in nz0_list] us = [bt(1.0, n, z) for (n, z) in zip(ns, zs)] (v0, g0, h0, data) = val_grad_hess(us, driv, lop) grad = geometric_grad(g0, [a, b], et_num) hess = geometric_hess(g0, h0, [a, b], et_num) return (v0, grad, hess) guess = [et_z0, et_r0] + [z for (n, z) in nz0_list] (convq, zs_o, val_o, grad_o) = newton(one, guess, **keyargs) a_o = zs_o[0] b_o = zs_o[1] zs_o= zs_o[2:] n_o = [n for (n, z) in nz0_list] nz_o = [(et_n, a_o*b_o**k) for k in range(et_num)] +\ [(n, z) for (n, z) in zip(n_o, zs_o)] basis_o = [l2.d0_basis(bt, n, z) for (n, z) in nz_o] return (convq, basis_o, res_list)
def val_grad_hess(basis_set, driven_term, l_op, opt_index=None): """ compute (value,grad,hess) of aA^{-1}a""" def make_vec(flag_basis_set, ip): return np.array([ip(u, driven_term) if f else 0 for (f, u) in flag_basis_set]) def make_mat(flag_basis_set1, flag_basis_set2, ip): return np.array([[ip(a, l_op, b) if fa and fb else 0 for (fb, b) in flag_basis_set2] for (fa, a) in flag_basis_set1]) if opt_index == None: opt_index = range(len(basis_set)) bt = type(basis_set[0]) us = [(True, l2.d0_basis(bt, u.n, u.z)) for (u, i) in with_index(basis_set)] d_us = [(i in opt_index, l2.d1_basis(bt, u.n, u.z)) for (u, i) in with_index(basis_set)] dd_us = [(i in opt_index, l2.d2_basis(bt, u.n, u.z)) for (u, i) in with_index(basis_set)] a0 = make_vec(us, l2.cip) A00 = make_mat(us, us, l2.cip) a1 = make_vec(d_us, l2.cip) A10 = make_mat(d_us, us, l2.cip) a2 = make_vec(dd_us, l2.cip) A11 = make_mat(d_us, d_us, l2.cip) A20 = make_mat(dd_us, us, l2.cip) datas = { 'A00': A00, 'A10':A10, 'A11':A11, 'A20':A20, 'a0':a0, 'a1':a1, 'a2':a2 } (val, grad, hess) = l_algebra.calc_val_grad_hess_aAm1a(a0, a1, a2, A00, A10, A20, A11) return (val, grad[opt_index], hess[opt_index].T[opt_index], datas)
def basis(a): zs = [(a*xi+b)*sigmoid(v, -xi+xth) + xi *sigmoid(v, +xi-xth) for xi in xs] return [l2.d0_basis(bt, n, z) for (n, z) in zip(ns, zs)]
def basis(scale_z): return [l2.d0_basis(bt, n, z * cond(i in opt_index, scale_z, 1)) for (n, z, i) in zip(ns, zs, range(len(zs)))]
def basis(shift_z): return [l2.d0_basis(bt, n, z + cond(i in opt_index, shift_z, 0)) for (n, z, i) in zip(ns, zs, range(len(zs)))]
def basis_scale(basis_set, scale_z, scale_index): return [l2.d0_basis(type(u), u.n, u.z * cond(i in shift_index, scale_z, 1)) for (u, i) in with_index(basis_set)]
def basis_shift(basis_set, shift_z, shift_index): return [l2.d0_basis(type(u), u.n, u.z + cond(i in shift_index, shift_z, 0)) for (u, i) in with_index(basis_set)]