# Path: tt/core/utils.py
# def my_chop2(sv, eps):  # from ttpy/multifuncr.py
#     if eps <= 0.0:
#         r = len(sv)
#         return r
#     sv0 = _np.cumsum(abs(sv[::-1]) ** 2)[::-1]
#     ff = [i for i in range(len(sv0)) if sv0[i] < eps ** 2]
#     if len(ff) == 0:
#         return len(sv)
#     else:
#         return _np.amin(ff)


from six.moves import xrange
from numbers import Number as _Number
from . import tt_f90 as _tt_f90
from tt.core.utils import my_chop2
        from . import matrix as _matrix
import numpy as _np
import warnings
                if j==0:
                    r[j]=r1
                    r[j+1] = r2*2
                    y[j] = _np.zeros((r[j], 2, r[j+1]),order='F',dtype=complex)
                    y[j][0:r1, :, 0:r2] = cr 
                    y[j][0:r1, 0, r2 :r[j+1]] = cr[:,0,:] 
                    y[j][0:r1, 1, r2 :r[j+1]] = twiddle**(1.0/(2**(i-j)))*cr[:,1,:]
                else:
                    r[j]=r1*2
                    r[j+1] = r2*2
                    y[j] = _np.zeros((r[j], 2, r[j+1]),order='F',dtype=complex)
                    y[j][0:r1, :, 0:r2] = cr 
                    y[j][r1:r[j], 0, r2 :r[j+1]] = cr[:,0,:] 
                    y[j][r1:r[j], 1, r2 :r[j+1]] = twiddle**(1.0/(2**(i-j)))*cr[:,1,:]
                        
                    
                y[j] = _np.reshape(y[j],( r[j], 2*r[j+1]),order='F')
                y[j] = _np.dot(rv,y[j])
                r[j] = y[j].shape[0]
                y[j] = _np.reshape(y[j],( 2*r[j],  r[j+1]),order='F')
                
                y[j], rv = _np.linalg.qr(y[j])
                y[j] = _np.reshape(y[j], (r[j], 2, rv.shape[0]),order='F')
            
            y[i] = _np.reshape(y[i], (r[i], 2*r[i+1]),order='F')  
            y[i] = _np.dot(rv,y[i])
            r[i] = rv.shape[0]
            # backward svd
            for j in range(i, 0,-1):
                u,s,v = _np.linalg.svd(y[j], full_matrices=False)

# Path: tt/core/utils.py
# def my_chop2(sv, eps):  # from ttpy/multifuncr.py
#     if eps <= 0.0:
#         r = len(sv)
#         return r
#     sv0 = _np.cumsum(abs(sv[::-1]) ** 2)[::-1]
#     ff = [i for i in range(len(sv0)) if sv0[i] < eps ** 2]
#     if len(ff) == 0:
#         return len(sv)
#     else:
#         return _np.amin(ff)


# Path: tt/core/vector.py
from six.moves import xrange
from numbers import Number as _Number
from . import tt_f90 as _tt_f90
from tt.core.utils import my_chop2
        from . import matrix as _matrix
import numpy as _np
import warnings

                if j==0:
                    r[j]=r1
                    r[j+1] = r2*2
                    y[j] = _np.zeros((r[j], 2, r[j+1]),order='F',dtype=complex)
                    y[j][0:r1, :, 0:r2] = cr 
                    y[j][0:r1, 0, r2 :r[j+1]] = cr[:,0,:] 
                    y[j][0:r1, 1, r2 :r[j+1]] = twiddle**(1.0/(2**(i-j)))*cr[:,1,:]
                else:
                    r[j]=r1*2
                    r[j+1] = r2*2
                    y[j] = _np.zeros((r[j], 2, r[j+1]),order='F',dtype=complex)
                    y[j][0:r1, :, 0:r2] = cr 
                    y[j][r1:r[j], 0, r2 :r[j+1]] = cr[:,0,:] 
                    y[j][r1:r[j], 1, r2 :r[j+1]] = twiddle**(1.0/(2**(i-j)))*cr[:,1,:]
                        
                    
                y[j] = _np.reshape(y[j],( r[j], 2*r[j+1]),order='F')
                y[j] = _np.dot(rv,y[j])
                r[j] = y[j].shape[0]
                y[j] = _np.reshape(y[j],( 2*r[j],  r[j+1]),order='F')
                
                y[j], rv = _np.linalg.qr(y[j])
                y[j] = _np.reshape(y[j], (r[j], 2, rv.shape[0]),order='F')
            
            y[i] = _np.reshape(y[i], (r[i], 2*r[i+1]),order='F')  
            y[i] = _np.dot(rv,y[i])
            r[i] = rv.shape[0]
            # backward svd
            for j in range(i, 0,-1):
                u,s,v = _np.linalg.svd(y[j], full_matrices=False)

                rnew = my_chop2(s, _np.linalg.norm(s)*tol/_np.sqrt(i))