# 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
            center //= n0[i]
        if center > 0:
            cind = [0] * d
    cr = []
    for i in xrange(d):
        cur_core = _np.zeros((1, n0[i], 1))
        cur_core[0, cind[i], 0] = 1
        cr.append(cur_core)
    return _vector.vector.from_list(cr)


def stepfun(n, d=None, center=1, direction=1):
    """ Create TT-vector for Heaviside step function :math:`\chi(x - x_0)`.

    Heaviside step function is defined as

    .. math::

        \chi(x) = \\left\{ \\begin{array}{l} 1 \mbox{ when } x \ge 0, \\\\ 0 \mbox{ when } x < 0. \\end{array} \\right.

    For negative value of ``direction`` :math:`\chi(x_0 - x)` is approximated. """
    if isinstance(n, six.integer_types):
        n = [n]
    if d is None:
        n0 = _np.asanyarray(n, dtype=_np.int32)
    else:
        n0 = _np.array(n * d, dtype=_np.int32)
    d = n0.size
    N = _np.prod(n0)


# 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

            center //= n0[i]
        if center > 0:
            cind = [0] * d
    cr = []
    for i in xrange(d):
        cur_core = _np.zeros((1, n0[i], 1))
        cur_core[0, cind[i], 0] = 1
        cr.append(cur_core)
    return _vector.vector.from_list(cr)


def stepfun(n, d=None, center=1, direction=1):
    """ Create TT-vector for Heaviside step function :math:`\chi(x - x_0)`.

    Heaviside step function is defined as

    .. math::

        \chi(x) = \\left\{ \\begin{array}{l} 1 \mbox{ when } x \ge 0, \\\\ 0 \mbox{ when } x < 0. \\end{array} \\right.

    For negative value of ``direction`` :math:`\chi(x_0 - x)` is approximated. """
    if isinstance(n, six.integer_types):
        n = [n]
    if d is None:
        n0 = _np.asanyarray(n, dtype=_np.int32)
    else:
        n0 = _np.array(n * d, dtype=_np.int32)
    d = n0.size
    N = _np.prod(n0)


    if center >= N and direction < 0 or center <= 0 and direction > 0: