Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
    def construct_generator_tt(self, as_list=False):

        if as_list:
            Att = []
        else:
            Att = tt.eye(self.size) * 0

        for i in range(self.num_r):

            fs = []

            for k in range(len(self.size)):
                if self.Pre[i, k] != 0:
                    fs.append((lambda a: lambda x: x * a)(self.Pre[i, k]))
                else:
                    fs.append(lambda x: 1.0)

            A = self.CME_tt(fs, self.C[i], self.nu[i, :])

            if as_list:
                Att.append(A)
            else:
                Att = Att + A
                Att = Att.round(1e-12)

        return Att
Ejemplo n.º 4
0
def apply_mask(A, f, mask, eps=1e-8):
    """
    Apply boundary mask on tt-stiffness matrix and tt-force vector
    :param A:
    :param f:
    :param mask:
    :return:
    """
    d = A.tt.d
    return (mask * A + tt.eye(4, d) - mask).round(eps), tt.matvec(mask,
                                                                  f).round(eps)
Ejemplo n.º 5
0
 def eye(d, mode=MODE_NP, tau=None, name=DEF_MATRIX_NAME):
     ''' 
     Eye matrix: diag([1, 1,..., 1]).
     '''
     res = Matrix(None, d, mode, tau, False, name=name)
     if mode == MODE_NP:
         res.x = np.eye(res.n)
     if mode == MODE_TT:
         res.x = tt.eye(2, d)
     if mode == MODE_SP:
         res.x = sp_diag([np.ones(res.n)], [0], format='csr')
     return res
Ejemplo n.º 6
0
 def ones(d, mode=MODE_NP, tau=None, name=DEF_MATRIX_NAME):
     ''' 
     Matrix of all ones.
     '''
     res = Matrix(None, d, mode, tau, False, name=name)
     if mode == MODE_NP:
         res.x = np.ones((res.n, res.n))
     if mode == MODE_TT:
         res.x = tt.eye(2, d)
         res.x.tt = tt.ones(4, d)
     if mode == MODE_SP:
         raise NotImplementedError()
     return res
Ejemplo n.º 7
0
 def volterra(d, mode=MODE_NP, tau=None, h=1., name=DEF_MATRIX_NAME):
     '''
     Volterra integral 1D matrix
     [i, j] = h if i>=j and = 0 otherwise.
     '''
     res = Matrix(None, d, mode, tau, False, name)
     if mode == MODE_NP:
         res.x = h * toeplitz(c=np.ones(res.n), r=np.zeros(res.n))
     if mode == MODE_TT:
         res.x = h * (tt.Toeplitz(tt.ones(2, d), kind='U') + tt.eye(2, d))
         res = res.round()
     if mode == MODE_SP:
         raise NotImplementedError()
     return res
Ejemplo n.º 8
0
 def findif(d, mode=MODE_NP, tau=None, h=1., name=DEF_MATRIX_NAME):
     '''
     Finite difference 1D matrix
     [i, j] = 1/h if i=j, [i, j] =-1/h if i=j+1 and = 0 otherwise.
     '''
     res = Matrix(None, d, mode, tau, False, name)
     if mode == MODE_NP or mode == MODE_SP:
         res.x = sp_diag([np.ones(res.n), -np.ones(res.n - 1)], [0, -1],
                         format='csr')
         res.x *= 1. / h
         if mode == MODE_NP:
             res.x = res.x.toarray()
     if mode == MODE_TT:
         e1 = tt.tensor(np.array([0., 1.]))
         e2 = tt.mkron([e1] * d)
         res.x = tt.Toeplitz(-e2, kind='U') + tt.eye(2, d)
         res.x *= (1. / h)
         res = res.round()
     return res
Ejemplo n.º 9
0
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
# 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']
Ejemplo n.º 11
0
def matrix(dx, rho, Cv, k_t):
    Idx = tt.eye(2, 3*dx) # identity tensor
    
    delta = tt.qlaplace_dd([dx, dx, dx]) # laplasian
    
    return - k_t/(rho*Cv) * delta
Ejemplo n.º 12
0
#lm = 0 #The magic constant
#lm = 1e-2
#lm = 
N = 15 # The size of the spectral discretization

x, ws = quadgauss.cdgqf(N,6,0,0.5) #Generation of hermite quadrature
#Generate Laplacian
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 = []
Ejemplo n.º 13
0
def tt_reshape(tt_array, shape, eps=1e-14, rl=1, rr=1):
    ''' Reshape of the TT-tensor
       [TT1]=TT_RESHAPE(TT,SZ) reshapes TT-tensor or TT-matrix into another 
       with mode sizes SZ, accuracy 1e-14

       [TT1]=TT_RESHAPE(TT,SZ,EPS) reshapes TT-tensor/matrix into another with
       mode sizes SZ and accuracy EPS
       
       [TT1]=TT_RESHAPE(TT,SZ,EPS, RL) reshapes TT-tensor/matrix into another 
       with mode size SZ and left tail rank RL

       [TT1]=TT_RESHAPE(TT,SZ,EPS, RL, RR) reshapes TT-tensor/matrix into 
       another with mode size SZ and tail ranks RL*RR
       Reshapes TT-tensor/matrix into a new one, with dimensions specified by SZ.

       If the input is TT-matrix, SZ must have the sizes for both modes, 
       so it is a matrix if sizes d2-by-2.
       If the input is TT-tensor, SZ may be either a column or a row vector.
    '''
    tt1 = deepcopy(tt_array)
    sz = deepcopy(shape)
    ismatrix = False
    if isinstance(tt1, tt.matrix):
        d1 = tt1.tt.d
        d2 = sz.shape[0]
        ismatrix = True
        # The size should be [n,m] in R^{d x 2}
        restn2_n = sz[:, 0]
        restn2_m = sz[:, 1]
        sz_n = copy(sz[:, 0])
        sz_m = copy(sz[:, 1])
        n1_n = tt1.n
        n1_m = tt1.m    
        sz = np.prod(sz, axis = 1) # We will split/convolve using the vector form anyway
        tt1 = tt1.tt
    else:
        d1 = tt1.d
        d2 = len(sz)


    # Recompute sz to include r0,rd,
    # and the items of tt1

    sz[0] = sz[0] * rl
    sz[d2-1] = sz[d2-1] * rr
    tt1.n[0] = tt1.n[0] * tt1.r[0]
    tt1.n[d1-1] = tt1.n[d1-1] * tt1.r[d1]
    if ismatrix: # in matrix: 1st tail rank goes to the n-mode, last to the m-mode
        restn2_n[0] = restn2_n[0] * rl
        restn2_m[d2-1] = restn2_m[d2-1] * rr
        n1_n[0] = n1_n[0] * tt1.r[0]
        n1_m[d1-1] = n1_m[d1-1] * tt1.r[d1]

    tt1.r[0] = 1
    tt1.r[d1] = 1

    n1 = tt1.n

    assert np.prod(n1) == np.prod(sz), 'Reshape: incorrect sizes'

    needQRs = False
    if d2 > d1:
        needQRs = True

    if d2 <= d1:
        i2 = 0
        n2 = sz
        for i1 in xrange(d1):
            if n2[i2] == 1:
                i2 = i2 + 1
                if i2 > d2:
                    break
            if n2[i2] % n1[i1] == 0:
                n2[i2] = n2[i2] / n1[i1]
            else:
                needQRs = True
                break

    r1 = tt1.r
    tt1 = tt1.to_list(tt1)

    if needQRs: # We have to split some cores -> perform QRs
        for i in xrange(d1-1, 0, -1):
            cr = tt1[i]
            cr = np.reshape(cr, (r1[i], n1[i]*r1[i+1]), order = 'F')
            [cr, rv] = np.linalg.qr(cr.T) # Size n*r2, r1new - r1nwe,r1
            cr0 = tt1[i-1]
            cr0 = np.reshape(cr0, (r1[i-1]*n1[i-1], r1[i]), order = 'F')
            cr0 = np.dot(cr0, rv.T) # r0*n0, r1new
            r1[i] = cr.shape[1]        
            cr0 = np.reshape(cr0, (r1[i-1], n1[i-1], r1[i]), order = 'F')
            cr = np.reshape(cr.T, (r1[i], n1[i], r1[i+1]), order = 'F')
            tt1[i] = cr
            tt1[i-1] = cr0  

    r2 = np.ones(d2 + 1)
        
    i1 = 0 # Working index in tt1
    i2 = 0 # Working index in tt2
    core2 = np.zeros((0))
    curcr2 = 1
    restn2 = sz
    n2 = np.ones(d2)
    if ismatrix:
        n2_n = np.ones(d2)
        n2_m = np.ones(d2)

    while i1 < d1:
        curcr1 = tt1[i1]    
        if gcd(restn2[i2], n1[i1]) == n1[i1]:
            # The whole core1 fits to core2. Convolve it
            if (i1 < d1-1) and (needQRs): # QR to the next core - for safety
                curcr1 = np.reshape(curcr1, (r1[i1]*n1[i1], r1[i1+1]), order = 'F')
                [curcr1, rv] = np.linalg.qr(curcr1)
                curcr12 = tt1[i1+1]
                curcr12 = np.reshape(curcr12, (r1[i1+1], n1[i1+1]*r1[i1+2]), order = 'F')
                curcr12 = np.dot(rv, curcr12)
                r1[i1+1] = curcr12.shape[0]
                tt1[i1+1] = np.reshape(curcr12, (r1[i1+1], n1[i1+1], r1[i1+2]), order = 'F')
            # Actually merge is here
            curcr1 = np.reshape(curcr1, (r1[i1], n1[i1]*r1[i1+1]), order = 'F')
            curcr2 = np.dot(curcr2, curcr1) # size r21*nold, dn*r22        
            if ismatrix: # Permute if we are working with tt_matrix
                curcr2 = np.reshape(curcr2, (r2[i2], n2_n[i2], n2_m[i2], n1_n[i1], n1_m[i1], r1[i1+1]), order = 'F')
                curcr2 = np.transpose(curcr2, [0, 1, 3, 2, 4, 5])
                # Update the "matrix" sizes            
                n2_n[i2] = n2_n[i2]*n1_n[i1]
                n2_m[i2] = n2_m[i2]*n1_m[i1]
                restn2_n[i2] = restn2_n[i2] / n1_n[i1]
                restn2_m[i2] = restn2_m[i2] / n1_m[i1]
            r2[i2+1] = r1[i1+1]
            # Update the sizes of tt2
            n2[i2] = n2[i2]*n1[i1]
            restn2[i2] = restn2[i2] / n1[i1]
            curcr2 = np.reshape(curcr2, (r2[i2]*n2[i2], r2[i2+1]), order = 'F')
            i1 = i1+1 # current core1 is over
        else:
            if (gcd(restn2[i2], n1[i1]) !=1 ) or (restn2[i2] == 1):
                # There exists a nontrivial divisor, or a singleton requested
                # Split it and convolve
                n12 = gcd(restn2[i2], n1[i1])
                if ismatrix: # Permute before the truncation
                    # Matrix sizes we are able to split
                    n12_n = gcd(restn2_n[i2], n1_n[i1])
                    n12_m = gcd(restn2_m[i2], n1_m[i1])
                    curcr1 = np.reshape(curcr1, (r1[i1], n12_n, n1_n[i1] / n12_n, n12_m, n1_m[i1] / n12_m, r1[i1+1]), order = 'F')
                    curcr1 = np.transpose(curcr1, [0, 1, 3, 2, 4, 5])
                    # Update the matrix sizes of tt2 and tt1
                    n2_n[i2] = n2_n[i2]*n12_n
                    n2_m[i2] = n2_m[i2]*n12_m
                    restn2_n[i2] = restn2_n[i2] / n12_n
                    restn2_m[i2] = restn2_m[i2] / n12_m
                    n1_n[i1] = n1_n[i1] / n12_n
                    n1_m[i1] = n1_m[i1] / n12_m
                
                curcr1 = np.reshape(curcr1, (r1[i1]*n12, (n1[i1]/n12)*r1[i1+1]), order = 'F')
                [u,s,v] = np.linalg.svd(curcr1, full_matrices = False)
                r = my_chop2(s, eps*np.linalg.norm(s)/(d2-1)**0.5)
                u = u[:, :r]
                v = v.T
                v = v[:, :r]*s[:r]
                u = np.reshape(u, (r1[i1], n12*r), order = 'F')
                # u is our admissible chunk, merge it to core2
                curcr2 = np.dot(curcr2, u) # size r21*nold, dn*r22
                r2[i2+1] = r
                # Update the sizes of tt2
                n2[i2] = n2[i2]*n12
                restn2[i2] = restn2[i2] / n12
                curcr2 = np.reshape(curcr2, (r2[i2]*n2[i2], r2[i2+1]), order = 'F')
                r1[i1] = r
                # and tt1
                n1[i1] = n1[i1] / n12
                # keep v in tt1 for next operations
                curcr1 = np.reshape(v.T, (r1[i1], n1[i1], r1[i1+1]), order = 'F')
                tt1[i1] = curcr1
            else:
                # Bad case. We have to merge cores of tt1 until a common divisor appears
                i1new = i1+1
                curcr1 = np.reshape(curcr1, (r1[i1]*n1[i1], r1[i1+1]), order = 'F')
                while (gcd(restn2[i2], n1[i1]) == 1) and (i1new < d1):
                    cr1new = tt1[i1new]
                    cr1new = np.reshape(cr1new, (r1[i1new], n1[i1new]*r1[i1new+1]), order = 'F')
                    curcr1 = np.dot(curcr1, cr1new) # size r1(i1)*n1(i1), n1new*r1new
                    if ismatrix: # Permutes and matrix size updates
                        curcr1 = np.reshape(curcr1, (r1[i1], n1_n[i1], n1_m[i1], n1_n[i1new], n1_m[i1new], r1[i1new+1]), order = 'F')
                        curcr1 = np.transpose(curcr1, [0, 1, 3, 2, 4, 5])
                        n1_n[i1] = n1_n[i1]*n1_n[i1new]
                        n1_m[i1] = n1_m[i1]*n1_m[i1new]
                    n1[i1] = n1[i1]*n1[i1new]
                    curcr1 = np.reshape(curcr1, (r1[i1]*n1[i1], r1[i1new+1]), order = 'F')
                    i1new = i1new+1
                # Inner cores merged => squeeze tt1 data
                n1 = np.concatenate((n1[:i1], n1[i1new:]))
                r1 = np.concatenate((r1[:i1], r1[i1new:]))
                tt1[i] = np.reshape(curcr1, (r1[i1], n1[i1], r1[i1new]), order = 'F')
                tt1 = tt1[:i1] + tt1[i1new:]
                d1 = len(n1)
        
        if (restn2[i2] == 1) and ((i1 >= d1) or ((i1 < d1) and (n1[i1] != 1))):
            # The core of tt2 is finished
            # The second condition prevents core2 from finishing until we 
            # squeeze all tailing singletons in tt1.
            curcr2 = curcr2.flatten(order = 'F')
            core2 = np.concatenate((core2, curcr2))
            i2 = i2+1
            # Start new core2
            curcr2 = 1

    # If we have been asked for singletons - just add them
    while (i2 < d2):
        core2 = np.concatenate((core2, np.ones(1)))
        r2[i2] = 1
        i2 = i2+1

    tt2 = tt.ones(2, 1) # dummy tensor
    tt2.d = d2
    tt2.n = n2
    tt2.r = r2
    tt2.core = core2
    tt2.ps = np.cumsum(np.concatenate((np.ones(1), r2[:-1] * n2 * r2[1:])))


    tt2.n[0] = tt2.n[0] / rl
    tt2.n[d2-1] = tt2.n[d2-1] / rr
    tt2.r[0] = rl
    tt2.r[d2] = rr

    if ismatrix:
        ttt = tt.eye(1,1) # dummy tt matrix
        ttt.n = sz_n
        ttt.m = sz_m
        ttt.tt = tt2
        return ttt
    else:
        return tt2
Ejemplo n.º 14
0
    tt2.ps = np.cumsum(np.concatenate((np.ones(1), r2[:-1] * n2 * r2[1:])))


    tt2.n[0] = tt2.n[0] / rl
    tt2.n[d2-1] = tt2.n[d2-1] / rr
    tt2.r[0] = rl
    tt2.r[d2] = rr

    if ismatrix:
        ttt = tt.eye(1,1) # dummy tt matrix
        ttt.n = sz_n
        ttt.m = sz_m
        ttt.tt = tt2
        return ttt
    else:
        return tt2
        
        
if __name__ == '__main__':
    a = tt.rand(8, 6)
    sz = np.array([2, 4]*5)
    b = tt_reshape(a, sz, eps=1e-14, rl=2, rr=4)
    print np.linalg.norm(a.full().flatten(order = 'F') - b.full().flatten(order = 'F'))
    
    k = 4
    c = tt.eye(8, k)
    sz = np.array([[2, 4]*(k), [2, 4]*(k)]).T
    d = tt_reshape(c, sz, eps=1e-14, rl=1, rr=1)
    print np.linalg.norm(c.full().flatten(order = 'F') - d.full().flatten(order = 'F'))
        
Ejemplo n.º 15
0
    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