# 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
        r[kk] = tr
    k = 0
    while (True):
        # Find next inversion
        nk = k
        while (nk < d - 1) and (idx[nk] < idx[nk + 1]):
            nk += 1
        if (nk == d - 1):
            break

        # Move orthogonal centre there
        for kk in xrange(k, nk - 1):  #############
            new_shape = [r[kk] * n[kk], r[kk + 1]]
            Q, R = _np.linalg.qr(_reshape(cores[kk], new_shape))
            tr = min(new_shape)
            new_shape = [r[kk], n[kk], tr]
            cores[kk] = _reshape(Q, new_shape)
            tmp = _reshape(cores[kk + 1], [r[kk + 1], n[kk + 1] * r[kk + 2]])
            tmp = _np.dot(R, tmp)
            cores[kk + 1] = _reshape(tmp, [tr, n[kk + 1], r[kk + 2]])
            r[kk + 1] = tr
        k = nk

        # Swap dimensions
        tmp = _reshape(cores[k], [r[k] * n[k], r[k + 1]])
        tmp = _np.dot(tmp, _reshape(cores[k + 1], [r[k + 1], n[k + 1] * r[k + 2]]))
        c = _reshape(tmp, [r[k], n[k], n[k + 1], r[k + 2]])
        c = _np.transpose(c, [0, 2, 1, 3])
        tmp = _reshape(c, [r[k] * n[k + 1], n[k] * r[k + 2]])
        U, S, Vt = _np.linalg.svd(tmp, full_matrices=False)

# 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

        r[kk] = tr
    k = 0
    while (True):
        # Find next inversion
        nk = k
        while (nk < d - 1) and (idx[nk] < idx[nk + 1]):
            nk += 1
        if (nk == d - 1):
            break

        # Move orthogonal centre there
        for kk in xrange(k, nk - 1):  #############
            new_shape = [r[kk] * n[kk], r[kk + 1]]
            Q, R = _np.linalg.qr(_reshape(cores[kk], new_shape))
            tr = min(new_shape)
            new_shape = [r[kk], n[kk], tr]
            cores[kk] = _reshape(Q, new_shape)
            tmp = _reshape(cores[kk + 1], [r[kk + 1], n[kk + 1] * r[kk + 2]])
            tmp = _np.dot(R, tmp)
            cores[kk + 1] = _reshape(tmp, [tr, n[kk + 1], r[kk + 2]])
            r[kk + 1] = tr
        k = nk

        # Swap dimensions
        tmp = _reshape(cores[k], [r[k] * n[k], r[k + 1]])
        tmp = _np.dot(tmp, _reshape(cores[k + 1], [r[k + 1], n[k + 1] * r[k + 2]]))
        c = _reshape(tmp, [r[k], n[k], n[k + 1], r[k + 2]])
        c = _np.transpose(c, [0, 2, 1, 3])
        tmp = _reshape(c, [r[k] * n[k + 1], n[k] * r[k + 2]])
        U, S, Vt = _np.linalg.svd(tmp, full_matrices=False)

        r[k + 1] = max(_my_chop2(S, _np.linalg.norm(S) * eps), 1)