def gen_1d(mat,e,i,d): w = mat for j in xrange(i): w = tt.kron(e,w) for j in xrange(d-i-1): w = tt.kron(w,e) return w
def full_matrix(A, Qs, t0, pt, dt): support = -tt.delta(2, pt, center = 1) + tt.delta(2, pt, center = 0) G_t = tt.Toeplitz(support,kind='L').T G_t = G_t.round(1e-14) support = tt.delta(2, pt, center = 1) + tt.delta(2, pt, center = 0) M_t = tt.Toeplitz(support,kind='L').T M_t = M_t.round(1e-14) px = A.n.shape[0] Id = tt.eye(2, px) A_full = tt.kron(Id, G_t) - 0.5 * dt * tt.kron(A, M_t) A_full = A_full.round(1e-14) # collected full matrix A e1 = tt.delta(2, pt, center = 0) left = t0 + 0.5* dt * tt.matvec(A, t0) left = left.round(1e-14) left = tt.kron(left, e1) left = left.round(1e-14) right = dt * Qs Qs_full = left + right Qs_full = Qs_full.round(1e-14) return A_full, Qs_full
def gen_1d(mat, e, i, d): """Generate 1D operator in TT form""" w = mat for j in range(i): w = tt.kron(e, w) for j in range(d - i - 1): w = tt.kron(w, e) return w
def matrix(dt, dx, rho, Cv, k_t): Idx = tt.eye(2, 3*dx) # identity tensor Idt = tt.eye(2, dt) delta = tt.qlaplace_dd([dx, dx, dx]) # laplasian support = tt.delta(2, dt, center = 1) # first time derivative deriv = tt.Toeplitz(support,kind='L') - tt.Toeplitz(support,kind='L').T deriv = deriv.round(1e-6) return rho * Cv * tt.kron(Idx, deriv) - k_t * tt.kron(delta, Idt)
def ObservationOperator(y, P, t): sigma = 0.3 PO = tt.tensor(np.exp(-0.5 * (y[0] - np.arange(N[0]))**2 / sigma**2)) PO = tt.kron( PO, tt.tensor(np.exp(-0.5 * (y[1] - np.arange(N[1]))**2 / sigma**2))) PO = tt.kron( PO, tt.tensor(np.exp(-0.5 * (y[2] - np.arange(N[2]))**2 / sigma**2))) PO = tt.kron( PO, tt.tensor(np.exp(-0.5 * (y[3] - np.arange(N[3]))**2 / sigma**2))) # PO = PO * (1/tt.sum(PO)) return PO
def ObservationOperator(y, P, t): sigma = 1 / 2 PO = tt.kron( tt.tensor(np.exp(-0.5 * (y[0] - np.arange(N[0]))**2 / sigma**2)), tt.tensor(np.exp(-0.5 * (y[1] - np.arange(N[1]))**2 / sigma**2))) * (1 / (sigma**2 * 2 * np.pi)) # PO = PO * (1/tt.sum(PO)) return PO
def gen_1site_1d_operator(matrix, spacer, site_num, dimension): """ Generate 1D operator in TT form. Parameters ---------- matrix: tt.matrix Matrix of the operator (which acts upon the site) spacer: tt.matrix Spacer used for all other sites (usually identity) site_num: int Number of site where matrix acts dimension: total number of sites """ result = matrix for j in range(site_num): result = tt.kron(spacer, result) for j in range(dimension - site_num - 1): result = tt.kron(result, spacer) return result
lp = np.zeros((N,N)) for i in xrange(N): for j in xrange(N): if i is not j: lp[i,j] = (-1)**(i - j)*(2*(x[i] - x[j])**(-2) - 0.5) else: lp[i,j] = 1.0/6*(4*N - 1 - 2 * x[i]**2) lp = tt.matrix(lp) e = tt.eye([N]) lp2 = None eps = 1e-8 for i in xrange(f): w = lp for j in xrange(i): w = tt.kron(e,w) for j in xrange(i+1,f): w = tt.kron(w,e) lp2 = lp2 + w lp2 = lp2.round(eps) #Now we will compute Henon-Heiles stuff xx = [] t = tt.tensor(x) ee = tt.ones([N]) for i in xrange(f): t0 = t for j in xrange(i): t0 = tt.kron(ee,t0) for j in xrange(i+1,f):
#------------------converting to TT-format------------------ #convert right-hand side b to TT-format b1_tt = tt.vector(b1) b2_tt = tt.vector(b2) #convert Identity matrix to TT-format Identity_tt = tt.matrix(Identity) #convert laplacian matrix to TT-format L_tt = tt.matrix(L) #------------------building 3d laplacian matrix in TT-format with TT-kronecker product------------------ L_tt1 = tt.kron(tt.kron(L_tt, Identity_tt), Identity_tt) L_tt2 = tt.kron(tt.kron(Identity_tt, L_tt), Identity_tt) L_tt3 = tt.kron(tt.kron(Identity_tt, Identity_tt), L_tt) L_tt = L_tt1 + L_tt2 + L_tt3 #------------------solving higher order linear system in TT-format------------------ #defining initial guess vector x = tt.ones(11, d) #solving the higher order linear system with AMEN print("") print("Solving Problem 1 with AMEN") print("") u1 = amen_solve(L_tt, b1_tt, x, 1e-6)
plt.figure() plt.title('Means') plt.plot(time_sample, np.mean(sample[:, 0, :], 1), 'b') plt.plot(time_sample, np.mean(sample[:, 1, :], 1), 'orange') plt.plot(time_sample, np.mean(sample[:, 2, :], 1), 'r') plt.plot(time_sample, np.mean(sample[:, 3, :], 1), 'g') plt.legend(['Susceptible', 'Exposed', 'Infected', 'Recovered']) plt.ylabel(r'#individuals') plt.xlabel(r'$t$ [d]') #%% Integrate ODE A_tt = mdl.construct_generator_tt() # A_tt = tt.reshape(A_tt,np.array(2*[[15]*8]).transpose()) P = tt.kron(tt.kron(tt.tensor(p1), tt.tensor(p2)), tt.kron(tt.tensor(p3), tt.tensor(p4))) P = P * (1 / tt.sum(P)) P0 = P # P = tt.reshape(P,[15]*8) x_S = tt.kron(tt.tensor(np.arange(N[0])), tt.ones(N[1:])) x_E = tt.kron(tt.ones([N[0]]), tt.kron(tt.tensor(np.arange(N[1])), tt.ones(N[2:]))) x_I = tt.kron(tt.ones(N[:2]), tt.kron(tt.tensor(np.arange(N[2])), tt.ones([N[3]]))) x_R = tt.kron(tt.ones(N[:3]), tt.tensor(np.arange(N[3]))) epsilon = 1e-10 rmax = 30 #%% reference ode solution
# basis = [LegendreBasis(Nl,[p[0],p[1]]) for p in param_range] basis = [BSplineBasis(Nl, [p[0], p[1]], deg=2) for p in param_range] pts = [b.integration_points(4)[0] for b in basis] ws = [b.integration_points(4)[1] for b in basis] lint = pts[0].size WS = tt.mkron([tt.tensor(b.get_integral()) for b in basis]) A_tt = extend_cme(Atts, pts) A_tt = A_tt.round(1e-10, 20) mass_tt, mass_inv_tt = get_mass(basis) stiff_tt = get_stiff(A_tt, N, pts, ws, basis) M_tt = tt.kron(tt.eye(N), mass_inv_tt) @ stiff_tt #%% Get observation np.random.seed(34548) # reaction_time,reaction_jumps,reaction_indices = Gillespie(np.array(Initial),time_observation[-1],Pre,Post-Pre,rates) # observations = Observations_grid(time_observation, reaction_time, reaction_jumps) # observations_noise = observations+np.random.normal(0,sigma,observations.shape) with open(r"simplegene_64_500k.pickle", "rb") as input_file: dct = pickle.load(input_file) No = dct['time_observation'].size time_observation = dct['time_observation'] reaction_time = dct['reaction_time'] reaction_jumps = dct['reaction_jumps']
def solve(self, initial_tt, T, intervals=None, return_all=False, nswp=40, qtt=False, verb=False, rounding=True): if intervals == None: pass else: x_tt = initial_tt dT = T / intervals Nt = self.N_max S, P, ev, basis = self.get_SP(dT, Nt) if qtt: nqtt = int(np.log2(Nt)) S = ttm2qttm(tt.matrix(S)) P = ttm2qttm(tt.matrix(P)) I_tt = tt.eye(self.A_tt.n) B_tt = tt.kron(I_tt, tt.matrix(S)) - tt.kron( I_tt, P) @ tt.kron(self.A_tt, ttm2qttm(tt.eye([Nt]))) else: nqtt = 1 I_tt = tt.eye(self.A_tt.n) B_tt = tt.kron(I_tt, tt.matrix(S)) - tt.kron( I_tt, tt.matrix(P)) @ tt.kron(self.A_tt, tt.matrix(np.eye(Nt))) # print(dT,T,intervals) returns = [] for i in range(intervals): # print(i) if qtt: f_tt = tt.kron(x_tt, tt2qtt(tt.tensor(ev))) else: f_tt = tt.kron(x_tt, tt.tensor(ev)) # print(B_tt.n,f_tt.n) try: # xs_tt = xs_tt.round(1e-10,5) # tme = datetime.datetime.now() xs_tt = tt.amen.amen_solve(B_tt, f_tt, self.xs_tt, self.epsilon, verb=1 if verb else 0, nswp=nswp, kickrank=8, max_full_size=50, local_prec='n') # tme = datetime.datetime.now() - tme # print(tme) self.xs_tt = xs_tt except: # tme = datetime.datetime.now() xs_tt = tt.amen.amen_solve(B_tt, f_tt, f_tt, self.epsilon, verb=1 if verb else 0, nswp=nswp, kickrank=8, max_full_size=50, local_prec='n') # tme = datetime.datetime.now() - tme # print(tme) self.xs_tt = xs_tt # print('SIZE',tt_size(xs_tt)/1e6) # print('PLMMM',tt.sum(xs_tt),xs_tt.r) if basis == None: if return_all: returns.append(xs_tt) x_tt = xs_tt[tuple([slice(None, None, None)] * len(self.A_tt.n) + [-1] * nqtt)] x_tt = x_tt.round(self.epsilon / 10) else: if return_all: if qtt: beval = basis(np.array([0])).flatten() temp1 = xs_tt * tt.kron(tt.ones(self.A_tt.n), tt2qtt(tt.tensor(beval))) for l in range(nqtt): temp1 = tt.sum(temp1, len(temp1.n) - 1) beval = basis(np.array([dT])).flatten() temp2 = xs_tt * tt.kron(tt.ones(self.A_tt.n), tt2qtt(tt.tensor(beval))) for l in range(nqtt): temp2 = tt.sum(temp2, len(temp2.n) - 1) returns.append( tt.kron(temp1, tt.tensor(np.array([1, 0]))) + tt.kron(temp2, tt.tensor(np.array([0, 1])))) else: beval = basis(np.array([0])).flatten() temp1 = xs_tt * tt.kron(tt.ones(self.A_tt.n), tt.tensor(beval)) temp1 = tt.sum(temp1, len(temp1.n) - 1) beval = basis(np.array([dT])).flatten() temp2 = xs_tt * tt.kron(tt.ones(self.A_tt.n), tt.tensor(beval)) temp2 = tt.sum(temp2, len(temp2.n) - 1) returns.append( tt.kron(temp1, tt.tensor(np.array([1, 0]))) + tt.kron(temp2, tt.tensor(np.array([0, 1])))) beval = basis(np.array([dT])).flatten() if qtt: x_tt = xs_tt * tt.kron(tt.ones(self.A_tt.n), tt2qtt(tt.tensor(beval))) for l in range(nqtt): x_tt = tt.sum(x_tt, len(x_tt.n) - 1) if rounding: x_tt = x_tt.round(self.epsilon / 10) else: x_tt = tt.sum( xs_tt * tt.kron(tt.ones(self.A_tt.n), tt.tensor(beval)), len(xs_tt.n) - 1) if rounding: x_tt = x_tt.round(self.epsilon / 10) # print('SIZE 2 ',tt_size(x_tt)/1e6) if not return_all: returns = x_tt return returns
else: fwd_int = ttInt(A_tt, epsilon=1e-6, N_max=8, dt_max=1.0, method='cheby') P_fwd = [P.copy()] ranks_fwd = [[0, max(P.r)]] time = 0 for i in range(1, No): y = observations[i, :] Po1 = noise_model(np.arange(N[0]), y[0], s1) Po2 = noise_model(np.arange(N[1]), y[1], s2) Po3 = noise_model(np.arange(N[2]), y[2], s3) Po4 = noise_model(np.arange(N[3]), y[3], s4) Po = tt.kron(tt.kron(tt.tensor(Po1), tt.tensor(Po2)), tt.kron(tt.tensor(Po3), tt.tensor(Po4))) Po = Po * (1 / tt.sum(Po)) if qtt: Po = tt2qtt(Po) tme = timeit.time.time() P = fwd_int.solve(P, dT, intervals=6, return_all=True, qtt=qtt) # for p in P: # print('\t',p.r) if qtt: P_fwd += [ p[tuple([slice(None, None, None)] * len(A_qtt.n) + [-1])].round(1e-8) for p in P ] for k in range(len(P)):
plt.plot( np.repeat(reaction_time, 2)[1:], np.repeat(reaction_jumps[:, 3], 2)[:-1]) plt.plot( np.repeat(reaction_time, 2)[1:], np.repeat(reaction_jumps[:, 4], 2)[:-1]) plt.plot( np.repeat(reaction_time, 2)[1:], np.repeat(reaction_jumps[:, 5], 2)[:-1]) # import sys # sys.exit() evector = lambda n, i: np.eye(n)[:, i] P = tt.kron(tt.tensor(evector(N[0], s0[0])), tt.tensor(evector(N[1], s0[1]))) P = tt.kron(P, tt.tensor(evector(N[2], s0[2]))) P = tt.kron(P, tt.tensor(evector(N[3], s0[3]))) P = tt.kron(P, tt.tensor(evector(N[4], s0[4]))) P = tt.kron(P, tt.tensor(evector(N[5], s0[5]))) if qtt: A_qtt = ttm2qttm(Att) integrator = ttInt(A_qtt, epsilon=1e-7, N_max=8, dt_max=1.0, method='cheby') P = tt2qtt(P) else: integrator = ttInt(Att,
# basis = [LegendreBasis(Nl,[p[0],p[1]]) for p in param_range] basis = [BSplineBasis(Nl,[p[0],p[1]],deg = 3) for p in param_range] pts = [b.integration_points(4)[0] for b in basis] ws = [b.integration_points(4)[1] for b in basis] lint = pts[0].size WS = tt.mkron([tt.tensor(b.get_integral()) for b in basis]) A_tt = extend_cme(Atts,pts+[np.array([rates[-1]])]) A_tt = A_tt.round(1e-10,20) mass_tt,mass_inv_tt = get_mass(basis) stiff_tt = get_stiff(A_tt,N,pts,ws,basis) M_tt = tt.kron(tt.eye(N),mass_inv_tt) @ stiff_tt #%% Get observation np.random.seed(34548) # reaction_time,reaction_jumps,reaction_indices = Gillespie(np.array(Initial),time_observation[-1],Pre,Post-Pre,rates) # observations = Observations_grid(time_observation, reaction_time, reaction_jumps) # observations_noise = observations+np.random.normal(0,sigma,observations.shape) with open(r"3stage2_45_500k.pickle", "rb") as input_file: dct = pickle.load(input_file) No = dct['time_observation'].size time_observation = dct['time_observation']
x = h * np.arange(1,N+1) x = L * (x - pi)/pi column = -0.5*((-1)**(np.arange(1,N)))/(np.sin(h*np.arange(1,N)/2)**2) column = np.concatenate(([-pi**2/(3*h**2)-1.0/6],column)) lp = (pi/L)**2*toeplitz(column) lp = -lp e = tt.eye([N]) lp = tt.matrix(lp) #Calculate the kinetic energy (Laplace) operator lp2 = None eps = 1e-8 for i in xrange(f): w = lp for j in xrange(i): w = tt.kron(e,w) for j in xrange(i+1,f): w = tt.kron(w,e) lp2 = lp2 + w lp2 = lp2.round(eps) #Now we will compute Henon-Heiles stuff xx = [] t = tt.tensor(x) ee = tt.ones([N]) for i in xrange(f): t0 = t for j in xrange(i): t0 = tt.kron(ee,t0) for j in xrange(i+1,f):
# #convert Identity matrix to TT-format Identity_qtt = tt.matrix(Identity_qtt) Identity_bd_qtt=tt.matrix(Identity_bd_qtt) # #convert laplacian matrix to TT-format L_qtt = tt.matrix(L_qtt) Lbd_qtt = tt.matrix(Lbd_qtt) Lf_qtt = tt.matrix(Lf_qtt) #------------------building 3d Lbd_tt matrixmatrix in TT-format with TT-kronecker product------------------ Lbd_qtt= tt.kron(tt.kron(Lbd_qtt,Identity_bd_qtt),Identity_bd_qtt)+tt.kron(tt.kron(Identity_bd_qtt,Lbd_qtt),Identity_bd_qtt)+tt.kron(tt.kron(Identity_bd_qtt,Identity_bd_qtt),Lbd_qtt) #------------------building 3d lf matrix in TT-format with TT-kronecker product Lf_qtt = tt.kron(tt.kron(Lf_qtt,Identity_qtt),Identity_qtt)+tt.kron(tt.kron(Identity_qtt,Lf_qtt),Identity_qtt)+tt.kron(tt.kron(Identity_qtt,Identity_qtt),Lf_qtt) #------------------building 3d laplacian matrix in TT-format with TT-kronecker product------------------ L_qtt = tt.kron(tt.kron(L_qtt,Identity_qtt),Identity_qtt)+tt.kron(tt.kron(Identity_qtt,L_qtt),Identity_qtt)+tt.kron(tt.kron(Identity_qtt,Identity_qtt),L_qtt) # #adding boundary matrix to laplacian matrix L_qtt = L_qtt+Lbd_qtt # #matrix-vector multiplication to maintain f and g and sum both to get the complete righside b
for i in xrange(N): for j in xrange(N): if i is not j: lp[i,j] = (-1)**(i - j)*(2*(x[i] - x[j])**(-2) - 0.5) else: lp[i,j] = 1.0/6*(4*N - 1 - 2 * x[i]**2) lp = tt.matrix(lp) e = tt.eye([N]) #Calculate the kinetic energy (Laplace) operator lp2 = None eps = 1e-8 for i in xrange(f): w = lp for j in xrange(i): w = tt.kron(e,w) for j in xrange(i+1,f): w = tt.kron(w,e) lp2 = lp2 + w lp2 = lp2.round(eps) #Now we will compute Henon-Heiles stuff xx = [] t = tt.tensor(x) ee = tt.ones([N]) for i in xrange(f): t0 = t for j in xrange(i): t0 = tt.kron(ee,t0) for j in xrange(i+1,f):
qtt = True # Set up model mdl = CME(N, Pre, Post, rates * 0 + 1, Props) Atts = mdl.construct_generator_tt(as_list=True) Nl = 64 mult = 4 param_range = [(0, r * 5) for r in rates[:-1]] pts1, ws1 = points_weights(param_range[0][0], param_range[0][1], Nl) pts2, ws2 = points_weights(param_range[1][0], param_range[1][1], Nl) pts3, ws3 = points_weights(param_range[2][0], param_range[2][1], Nl) pts4, ws4 = points_weights(param_range[3][0], param_range[3][1], Nl) pts5, ws5 = points_weights(param_range[4][0], param_range[4][1], Nl) A_tt = tt.kron(Atts[0] , tt.kron(tt.matrix(np.diag(pts1)),tt.eye([Nl]*4)) ) \ + tt.kron(Atts[1] , tt.kron(tt.kron(tt.eye([Nl]),tt.matrix(np.diag(pts2))),tt.eye([Nl]*3)) ) \ + tt.kron(Atts[2] , tt.kron(tt.kron(tt.eye([Nl]*2),tt.matrix(np.diag(pts3))),tt.eye([Nl]*2)) ) \ + tt.kron(Atts[3] , tt.kron(tt.kron(tt.eye([Nl]*3),tt.matrix(np.diag(pts4))),tt.eye([Nl]*1)) ) \ + tt.kron(Atts[4] , tt.kron(tt.eye([Nl]*4),tt.matrix(np.diag(pts5))) ) \ + tt.kron(Atts[5], tt.eye([Nl]*5) )*rates[5] A_tt = A_tt.round(1e-10, 20) No = 64 # Nt = 64 dT = 0.2 Nbs = 8 time_observation = np.arange(No) * dT #%% Get observation
A = gen_heisen(d) n = A.n d = A.tt.d #In this case we will start from the rank-1 tensor "all spins up" #It is not good, since it leads to an eigenvector #Random start also seems to be quite useless #Maybe first d/2 is up other d/2 are down? v0 = np.array([1,0],dtype=np.float); v0 = tt.tensor(v0,1e-12) v1 = np.array([0,1],dtype=np.float); v1 = tt.tensor(v1,1e-12) e1 = None for j in xrange(d): if j % 2: e0 = v0#np.random.rand(2) else: e0 = v1#e0 = tt.tensor(e0,1e-12) e1 = tt.kron(e1,e0) r = [1]*(d+1) r[0] = 1 r[d] = 1 x0 = tt.rand(n,d,r) tau = 1e-2 tf = 100 t = 0 start = e1 psi = start + 0 * x0 psi1 = start + 0 * x0 cf = [] while t <= tf: print '%f/%f' % (t,tf) psi = ksl(-1.0j*A,psi,tau) #psi1 = kls(-1.0j*A,psi1,tau)