# Path: tt/core/utils.py
# def ind2sub(siz, idx):
#     '''
#     Translates full-format index into tt.vector one's.
#     ----------
#     Parameters:
#         siz - tt.vector modes
#         idx - full-vector index
#     Note: not vectorized.
#     '''
#     n = len(siz)
#     subs = _np.empty((n))
#     k = _np.cumprod(siz[:-1])
#     k = _np.concatenate((_np.ones(1), k))
#     for i in xrange(n - 1, -1, -1):
#         subs[i] = _np.floor(idx / k[i])
#         idx = idx % k[i]
#     return subs.astype(_np.int32)
# 
# Path: tt/core/utils.py
# def gcd(a, b):
#     '''Greatest common divider'''
#     f = _np.frompyfunc(_gcd, 2, 1)
#     return f(a, b)
# 
# 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)


import six
import numpy as _np
import math as _math
import copy as _cp
from six.moves import xrange
from . import tt_f90 as _tt_f90
from . import vector as _vector
from . import matrix as _matrix
from .utils import ind2sub as _ind2sub
from .utils import gcd as _gcd
from .utils import my_chop2 as _my_chop2
    tt1 = tt1.to_list(tt1)

    if needQRs:  # We have to split some cores -> perform QRs
        for i in range(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, dtype=_np.int32)

    i1 = 0  # Working index in tt1
    i2 = 0  # Working index in tt2
    core2 = _np.zeros((0))
    curcr2 = 1
    restn2 = sz
    n2 = _np.ones(d2, dtype=_np.int32)
    if ismatrix:
        n2_n = _np.ones(d2, dtype=_np.int32)
        n2_m = _np.ones(d2, dtype=_np.int32)

    while i1 < d1:
        curcr1 = tt1[i1]

# Path: tt/core/utils.py
# def ind2sub(siz, idx):
#     '''
#     Translates full-format index into tt.vector one's.
#     ----------
#     Parameters:
#         siz - tt.vector modes
#         idx - full-vector index
#     Note: not vectorized.
#     '''
#     n = len(siz)
#     subs = _np.empty((n))
#     k = _np.cumprod(siz[:-1])
#     k = _np.concatenate((_np.ones(1), k))
#     for i in xrange(n - 1, -1, -1):
#         subs[i] = _np.floor(idx / k[i])
#         idx = idx % k[i]
#     return subs.astype(_np.int32)
# 
# Path: tt/core/utils.py
# def gcd(a, b):
#     '''Greatest common divider'''
#     f = _np.frompyfunc(_gcd, 2, 1)
#     return f(a, b)
# 
# 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/tools.py
import six
import numpy as _np
import math as _math
import copy as _cp
from six.moves import xrange
from . import tt_f90 as _tt_f90
from . import vector as _vector
from . import matrix as _matrix
from .utils import ind2sub as _ind2sub
from .utils import gcd as _gcd
from .utils import my_chop2 as _my_chop2

    tt1 = tt1.to_list(tt1)

    if needQRs:  # We have to split some cores -> perform QRs
        for i in range(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, dtype=_np.int32)

    i1 = 0  # Working index in tt1
    i2 = 0  # Working index in tt2
    core2 = _np.zeros((0))
    curcr2 = 1
    restn2 = sz
    n2 = _np.ones(d2, dtype=_np.int32)
    if ismatrix:
        n2_n = _np.ones(d2, dtype=_np.int32)
        n2_m = _np.ones(d2, dtype=_np.int32)

    while i1 < d1:
        curcr1 = tt1[i1]

        if _gcd(restn2[i2], n1[i1]) == n1[i1]: