# 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
                                           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

# 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

                                           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])