Beispiel #1
0
def _lange(x, norm, axis):
    order = 'F' if x.flags.f_contiguous and not x.flags.c_contiguous else 'C'
    dtype = 'f' if x.dtype.char in 'fF' else 'd'
    if x.size == 0:
        shape = [x.shape[i] for i in set(range(x.ndim)) - set(axis)]
        return nlcpy.zeros(shape, dtype=dtype)
    if norm in (None, 'fro', 'f'):
        if x.dtype.kind == 'c':
            x = abs(x)
        return nlcpy.sqrt(nlcpy.sum(x * x, axis=axis))
    if norm == nlcpy.inf:
        norm = 'I'
    else:
        norm = '1'
    lwork = x.shape[0] if norm == 'I' else 1
    x = nlcpy.asarray(nlcpy.moveaxis(x, (axis[0], axis[1]), (0, 1)), order='F')
    y = nlcpy.empty(x.shape[2:], dtype=dtype, order='F')
    work = nlcpy.empty(lwork, dtype=dtype)
    fpe = request._get_fpe_flag()
    args = (
        ord(norm),
        x._ve_array,
        y._ve_array,
        work._ve_array,
        veo.OnStack(fpe, inout=veo.INTENT_OUT),
    )

    request._push_and_flush_request(
        'nlcpy_norm',
        args,
    )

    return nlcpy.asarray(y, order=order)
Beispiel #2
0
    def test_multi_ndarray_5(self, dtype):
        xin = nlcpy.random.rand(5, 5).astype(dtype=dtype)
        yin = nlcpy.random.rand(5, 5).astype(dtype=dtype)
        zin = nlcpy.random.rand(5, 5).astype(dtype=dtype)
        # compute with sca
        dxin, dyin, dzin = nlcpy.sca.create_descriptor((xin, yin, zin))
        desc = (
            dxin[-1, 0] +
            dxin[0, 1] +
            dxin[1, 0] +
            dxin[0, -1] +
            dyin[-1, -1] +
            dzin[1, 1])
        res_sca = nlcpy.sca.create_kernel(desc).execute()
        # compute with naive
        res_naive = nlcpy.zeros((5, 5), dtype=dtype)
        res_naive[1:-1, 1:-1] = (
            xin[:-2, 1:-1] +
            xin[1:-1, 2:] +
            xin[2:, 1:-1] +
            xin[1:-1, :-2] +
            yin[:-2, :-2] +
            zin[2:, 2:])

        rtol = TOL_SINGLE if dtype == numpy.float32 else TOL_DOUBLE
        testing.assert_allclose(res_sca, res_naive, rtol=rtol)
Beispiel #3
0
def _warmup():
    xx = []
    # allocate VE memory as heap
    for i in range(130):
        xx.append(nlcpy.zeros(int((1 * 1e8) / 8), dtype='f8'))
    nlcpy.request.flush()
    xx = []
Beispiel #4
0
 def test_adv_getitem_nlcpy_indices2(self):
     a = nlcpy.zeros(self.shape)
     index = nlcpy.array([1, 0])
     original_index = index.copy()
     b = a[(slice(None), index)]
     b_cpu = a.get()[(slice(None), index.get())]
     testing.assert_array_equal(b, b_cpu)
     testing.assert_array_equal(original_index, index)
Beispiel #5
0
 def test_nlcpy_indices_boolean_array(self):
     a = nlcpy.zeros(self.shape)
     index = nlcpy.array([True, False])
     original_index = index.copy()
     a[index] = nlcpy.array(1.)
     testing.assert_array_equal(a, nlcpy.array([[1., 1., 1.], [0., 0.,
                                                               0.]]))
     testing.assert_array_almost_equal(original_index, index)
Beispiel #6
0
 def test_nlcpy_indices_integer_array_3(self):
     a = nlcpy.zeros(self.shape)
     index = nlcpy.array([3, -5])
     original_index = index.copy()
     a[[1, 1], index] = nlcpy.array(1.)
     testing.assert_array_equal(a, nlcpy.array([[0., 0., 0.], [1., 1.,
                                                               0.]]))
     testing.assert_array_equal(index, original_index)
Beispiel #7
0
 def test_adv_getitem_nlcpy_indices5(self):
     a = nlcpy.zeros(self.shape)
     index = nlcpy.array([4, -5])
     original_index = index.copy()
     b = a[[1, 0], index]
     b_cpu = a.get()[[1, 0], index.get() % self.shape[1]]
     testing.assert_array_equal(b, b_cpu)
     testing.assert_array_equal(original_index, index)
Beispiel #8
0
 def test_adv_getitem_nlcpy_indices3(self):
     a = nlcpy.zeros(self.shape)
     index = nlcpy.array([True, False])
     original_index = index.copy()
     b = a[index]
     b_cpu = a.get()[index.get()]
     testing.assert_array_equal(b, b_cpu)
     testing.assert_array_equal(original_index, index)
Beispiel #9
0
def _prep(dtype):
    NX = 50
    NY = 60
    NZ = 40
    x1 = nlcpy.arange(NZ * NY * NX, dtype=dtype).reshape(NZ, NY, NX)
    x2 = nlcpy.ones((NZ, NY, NX), dtype=dtype)
    y = nlcpy.zeros((NZ, NY, NX), dtype=dtype)
    return x1, x2, y
Beispiel #10
0
def use_nlcpy_jit_haversine(lat1, lon1, lat2, lon2, size):
    from nlcpy.ve_types import (uint32, uint64, int32, int64, float32, float64,
                                void_p, void)

    vp.request.flush()
    start = time.time()

    mod =  vp.jit.CustomVELibrary(
        code=string.Template(
            _test_source1_c
        ).substitute(
            dtype=_pytype2ctype(DT)
        ),
        compiler='/opt/nec/ve/bin/ncc',
        cflags=vp.jit.get_default_cflags(openmp=True) + \
                    (('-DFLOAT32',) if DT is np.float32 else ()),
        ldflags=vp.jit.get_default_ldflags(openmp=True),
        # ftrace=True,
    )

    if DT is np.float32:
        args_type = (float32, float32, uint64, uint64, int64, uint64)
    elif DT is np.float64:
        args_type = (float64, float64, uint64, uint64, int64, uint64)
    else:
        raise ValueError

    kern = mod.get_function('calc_harv', args_type=args_type, ret_type=float32)

    end = time.time()
    intime["pre"] = end - start

    mi = vp.zeros(size, dtype=DT)
    vp.request.flush()
    start = time.time()
    ve_elapsed = kern(lat1,
                      lon1,
                      lat2.ve_adr,
                      lon2.ve_adr,
                      size,
                      mi.ve_adr,
                      callback=None,
                      sync=True)
    end = time.time()

    intime["exec(VE+VH)"] = end - start
    intime["exec(VE)"] = ve_elapsed

    return mi
Beispiel #11
0
    def test_multi_ndarray_3(self, dtype):
        xin = nlcpy.random.rand(7, 7).astype(dtype=dtype)
        yin = nlcpy.random.rand(6, 8).astype(dtype=dtype)
        # compute with sca
        dxin, dyin = nlcpy.sca.create_descriptor((xin, yin))
        desc = dxin[-2, 2] + dxin[1, -1] + dyin[0, 0]
        res_sca = nlcpy.sca.create_kernel(desc).execute()
        # compute with naive
        x_tmp = xin[:6, :]
        y_tmp = yin[:, :7]
        res_naive = nlcpy.zeros((6, 7), dtype=dtype)
        res_naive[2:-1, 1:-2] = x_tmp[:-3, 3:] + x_tmp[3:, :-3] + y_tmp[2:-1, 1:-2]

        rtol = TOL_SINGLE if dtype == numpy.float32 else TOL_DOUBLE
        testing.assert_allclose(res_sca, res_naive, rtol=rtol)
Beispiel #12
0
    def test_multi_ndarray_2(self, dtype):
        xin = nlcpy.random.rand(5, 5).astype(dtype=dtype)
        yin = nlcpy.random.rand(7, 7).astype(dtype=dtype)
        # compute with sca
        dxin, dyin = nlcpy.sca.create_descriptor((xin, yin))
        desc = dxin[-1, 1] + dxin[1, -1] + dyin[-1, -1] + dyin[1, 1]
        res_sca = nlcpy.sca.create_kernel(desc).execute()
        # compute with naive
        x_tmp = xin[:, :]
        y_tmp = yin[:5, :5]
        res_naive = nlcpy.zeros((5, 5), dtype=dtype)
        res_naive[1:-1, 1:-1] = (x_tmp[:-2, 2:] + x_tmp[2:, :-2] +
                                 y_tmp[:-2, :-2] + y_tmp[2:, 2:])

        rtol = TOL_SINGLE if dtype == numpy.float32 else TOL_DOUBLE
        testing.assert_allclose(res_sca, res_naive, rtol=rtol)
Beispiel #13
0
def roll(a, shift, axis=None):
    """Rolls array elements along a given axis.

    Elements that roll beyond the last position are re-introduced at the first.

    Parameters
    ----------
    a : array_like
        Input array.
    shift : int or tuple of ints
        The number of places by which elements are shifted. If a tuple, then *axis* must
        be a tuple of the same size, and each of the given axes is shifted by the
        corresponding number. If an int while *axis* is a tuple of ints, then the
        same value is used for all given axes.
    axis : int or tuple of ints, optional
        Axis or axes along which elements are shifted. By default, the array is
        flattened before shifting, after which the original shape is restored.

    Returns
    -------
    res : ndarray
        Output array, with the same shape as *a*.

    See Also
    --------
    rollaxis : Rolls the specified axis backwards, until it lies in a given position.

    Examples
    --------
    >>> import nlcpy as vp
    >>> x = vp.arange(10)
    >>> vp.roll(x, 2)
    array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
    >>> vp.roll(x, -2)
    array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])

    >>> x2 = vp.reshape(x, (2,5))
    >>> x2
    array([[0, 1, 2, 3, 4],
           [5, 6, 7, 8, 9]])
    >>> vp.roll(x2, 1)
    array([[9, 0, 1, 2, 3],
           [4, 5, 6, 7, 8]])
    >>> vp.roll(x2, -1)
    array([[1, 2, 3, 4, 5],
           [6, 7, 8, 9, 0]])
    >>> vp.roll(x2, 1, axis=0)
    array([[5, 6, 7, 8, 9],
           [0, 1, 2, 3, 4]])
    >>> vp.roll(x2, -1, axis=0)
    array([[5, 6, 7, 8, 9],
           [0, 1, 2, 3, 4]])
    >>> vp.roll(x2, 1, axis=1)
    array([[4, 0, 1, 2, 3],
           [9, 5, 6, 7, 8]])
    >>> vp.roll(x2, -1, axis=1)
    array([[1, 2, 3, 4, 0],
           [6, 7, 8, 9, 5]])
    """
    a = nlcpy.asanyarray(a)
    if axis is None:
        return roll(a.ravel(), shift, 0).reshape(a.shape)

    if type(axis) not in (tuple, list):
        try:
            axis = [operator.index(axis)]
        except TypeError:
            pass

    _axis = axis.get() if isinstance(axis, nlcpy.ndarray) else axis
    axis = [ax + a.ndim if ax < 0 else ax for ax in _axis]
    for ax in axis:
        if ax < 0 or ax >= a.ndim:
            raise AxisError(
                'axis {} is out of bounds for array of dimension {}'.format(
                    ax, a.ndim))

    shift = nlcpy.asanyarray(shift)
    axis = nlcpy.asanyarray(axis)
    if shift.ndim > 1 or axis.ndim > 1:
        raise ValueError(
            "'shift' and 'axis' should be scalars or 1D sequences")

    if shift.size > axis.size:
        axis = nlcpy.broadcast_to(axis, shift.shape)
    else:
        shift = nlcpy.broadcast_to(shift, axis.shape)

    shift = nlcpy.array(shift, dtype='l')
    axis = nlcpy.array(axis, dtype='l')
    result = nlcpy.empty(a.shape, dtype=a.dtype)
    work = nlcpy.zeros(a.ndim, dtype='l')
    request._push_request('nlcpy_roll', 'manipulation_op',
                          (a, shift, axis, work, result))

    return result
Beispiel #14
0
def lstsq(a, b, rcond='warn'):
    """Returns the least-squares solution to a linear matrix equation.

    Solves the equation :math:`ax = b` by computing a vector *x* that minimizes the
    squared Euclidean 2-norm :math:`|b-ax|^2_2`. The equation may be under-, well-, or
    over-determined (i.e., the number of linearly independent rows of *a* can be less
    than, equal to, or greater than its number of linearly independent columns). If *a*
    is square and of full rank, then *x* (but for round-off error) is the "exact"
    solution of the equation.

    Parameters
    ----------
    a : (M,N) array_like
        "Coefficient" matrix.
    b : {(M,), (M, K)} array_like
        Ordinate or "dependent variable" values. If *b* is two-dimensional, the
        least-squares solution is calculated for each of the *K* columns of *b*.
    rcond : float, optional
        Cut-off ratio for small singular values of *a*. For the purposes of rank
        determination, singular values are treated as zero if they are smaller than
        *rcond* times the largest singular value of *a*.

    Returns
    -------
    x : {(N,), (N, K)} ndarray
        Least-squares solution. If *b* is two-dimensional, the solutions are in the *K*
        columns of *x*.
    residuals : {(1,), (K,), (0,)} ndarray
        Sums of residuals; squared Euclidean 2-norm for each column in ``b - a@x``. If
        the rank of *a* is < N or M <= N, this is an empty array. If *b* is
        1-dimensional, this is a (1,) shape array. Otherwise the shape is (K,).
    rank : *int*
        Rank of matrix *a*.
    s : (min(M, N),) ndarray
        Singular values of *a*.

    Note
    ----
    If `b` is a matrix, then all array results are returned as matrices.

    Examples
    --------
    .. plot::
        :align: center

        Fit a line, ``y = mx + c``, through some noisy data-points:

        >>> import nlcpy as vp
        >>> x = vp.array([0, 1, 2, 3])
        >>> y = vp.array([-1, 0.2, 0.9, 2.1])

        By examining the coefficients, we see that the line should have a gradient of
        roughly 1 and cut the y-axis at, more or less, -1.
        We can rewrite the line equation as ``y = Ap``, where ``A = [[x 1]]`` and
        ``p = [[m], [c]]``. Now use lstsq to solve for *p*:

        >>> A = vp.array((x, vp.ones(len(x)))).T
        >>> A
        array([[0., 1.],
               [1., 1.],
               [2., 1.],
               [3., 1.]])
        >>> m, c = vp.linalg.lstsq(A, y, rcond=None)[0]
        >>> m, c  # doctest: +SKIP
        (array(1.), array(-0.95)) # may vary

        Plot the data along with the fitted line:

        >>> import matplotlib.pyplot as plt
        >>> _ = plt.plot(x, y, 'o', label='Original data', markersize=15)
        >>> _ = plt.plot(x, m*x + c, 'r', label='Fitted line')
        >>> _ = plt.legend()
        >>> plt.show()

    """
    a = nlcpy.asarray(a)
    b = nlcpy.asarray(b)
    b_ndim = b.ndim
    if b_ndim == 1:
        b = b[:, nlcpy.newaxis]
    util._assertRank2(a, b)

    if a.shape[-2] != b.shape[-2]:
        raise util.LinAlgError('Incompatible dimensions')

    a_complex = a.dtype.char in 'FD'
    b_complex = b.dtype.char in 'FD'
    if a_complex or b_complex:
        if a.dtype.char in 'fF' and b.dtype.char in 'fF':
            x_dtype = 'F'
            f_dtype = 'f'
        else:
            x_dtype = 'D'
            f_dtype = 'd'
    else:
        if a.dtype.char == 'f' and b.dtype.char == 'f':
            x_dtype = 'f'
            f_dtype = 'f'
        else:
            x_dtype = 'd'
            f_dtype = 'd'

    m = a.shape[-2]
    n = a.shape[-1]
    k = b.shape[-1]
    minmn = min(m, n)
    maxmn = max(m, n)
    k_extend = (k == 0)
    if k_extend:
        b = nlcpy.zeros([m, 1], dtype=b.dtype)
        k = 1
    if minmn == 0:
        if n > 0:
            x = nlcpy.zeros([n, k], dtype=x_dtype)
            residuals = nlcpy.array([], dtype=f_dtype)
        else:
            x = nlcpy.array([], dtype=x_dtype)
            if b_complex:
                br = nlcpy.asarray(b.real, dtype='d')
                bi = nlcpy.asarray(b.imag, dtype='d')
                square_b = nlcpy.power(br, 2) + nlcpy.power(bi, 2)
            else:
                square_b = nlcpy.power(b, 2, dtype='d')
            residuals = nlcpy.add.reduce(square_b, dtype=f_dtype)
        return (x, residuals, 0, nlcpy.array([], dtype=f_dtype))

    if rcond == 'warn':
        rcond = -1.0
    if rcond is None:
        rcond = nlcpy.finfo(x_dtype).eps * max(m, n)
    rcond = nlcpy.asarray(rcond, dtype=f_dtype)

    a = nlcpy.array(a, dtype=x_dtype, order='F')
    if m < n:
        _b = nlcpy.empty([n, k], dtype=x_dtype, order='F')
        _b[:m, :] = b
        b = _b
    else:
        b = nlcpy.array(b, dtype=x_dtype, order='F')
    s = nlcpy.empty(min(m, n), dtype=f_dtype)
    rank = numpy.empty(1, dtype='l')

    nlvl = max(0, int(nlcpy.log(minmn / 26.0) / nlcpy.log(2)) + 1)
    mnthr = int(minmn * 1.6)
    mm = m
    lwork = 1
    if a_complex:
        _tmp = 2
        wlalsd = minmn * k
        lrwork = 10 * maxmn + 2 * maxmn * 25 + 8 * maxmn * nlvl + 3 * 25 * k \
                            + max(26 ** 2, n * (1 + k) + 2 * k)
    else:
        _tmp = 3
        wlalsd = 9 * minmn + 2 * minmn * 25 + 8 * minmn * nlvl + minmn * k + 26**2
        lrwork = 1

    if m >= n:
        if m >= mnthr:
            mm = n
            lwork = max(65 * n, n + 32 * k)
        lwork = max(lwork, _tmp * n + (mm + n) * 64, _tmp * n + 32 * k,
                    _tmp * n + 32 * n - 32, _tmp * n + wlalsd)
    else:
        if n >= mnthr:
            lwork = max(lwork, m * m + 4 * m + wlalsd, m * m + 132 * m,
                        m * m + 4 * m + 32 * k, m * m + (k + 1) * m,
                        m * m + n + m)
        else:
            lwork = max(lwork, 3 * m + wlalsd, 3 * m + 64 * (n + m),
                        3 * m + 32 * k)

    liwork = minmn * (11 + 3 * nlvl)
    work = nlcpy.empty(lwork)
    iwork = nlcpy.empty(liwork, dtype='l')
    rwork = nlcpy.empty(lrwork, dtype=f_dtype)
    info = numpy.empty(1, dtype='l')
    fpe = request._get_fpe_flag()
    args = (
        a._ve_array,
        b._ve_array,
        s._ve_array,
        work._ve_array,
        iwork._ve_array,
        rwork._ve_array,
        rcond._ve_array,
        veo.OnStack(rank, inout=veo.INTENT_OUT),
        veo.OnStack(info, inout=veo.INTENT_OUT),
        veo.OnStack(fpe, inout=veo.INTENT_OUT),
    )

    request._push_and_flush_request('nlcpy_lstsq',
                                    args,
                                    callback=_lstsq_errchk(info),
                                    sync=True)

    if rank < n or m <= n:
        residuals = nlcpy.array([], dtype=f_dtype)
    else:
        _b = b[n:]
        square_b = nlcpy.power(_b.real, 2) + nlcpy.power(_b.imag, 2)
        residuals = nlcpy.add.reduce(square_b, dtype=f_dtype)
    if k_extend:
        x = b[..., :0]
        residuals = residuals[..., :0]
    elif b_ndim == 1:
        x = nlcpy.asarray(b[:n, 0], dtype=x_dtype, order='C')
    else:
        x = nlcpy.asarray(b[:n, :], dtype=x_dtype, order='C')

    return (x, residuals, rank[0], s)
Beispiel #15
0
def resize(a, new_shape):
    """Returns a new array with the specified shape.

    If the new array is larger than the original array, then the new array
    is filled with repeated copies of *a*.
    Note that this behavior is different from a.resize(new_shape) which fills
    with zeros instead of repeated copies of *a*.

    Parameters
    ----------
    a : array_like
        Array to be resized.
    new_shape : int or sequence of ints
        Shape of resized array.

    Returns
    -------
    reshaped_array : ndarray
        The new array is formed from the data in the old array, repeated if necessary to
        fill out the required number of elements. The data are repeated in the order that
        they are stored in memory.

    Note
    ----

    Warning: This functionality does **not** consider axes separately, i.e. it does
    not apply interpolation/extrapolation.
    It fills the return array with the required number of elements, taken from
    `a` as they are laid out in memory, disregarding strides and
    axes.
    (This is in case the new shape is smaller. For larger, see above.)
    This functionality is therefore not suitable to resize images, or data where
    each axis represents a separate and distinct entity.

    Examples
    --------
    >>> import nlcpy as vp
    >>> a=vp.array([[0,1],[2,3]])
    >>> vp.resize(a,(2,3))
    array([[0, 1, 2],
           [3, 0, 1]])
    >>> vp.resize(a,(1,4))
    array([[0, 1, 2, 3]])
    >>> vp.resize(a,(2,4))
    array([[0, 1, 2, 3],
           [0, 1, 2, 3]])
    """
    if isinstance(new_shape, int):
        new_shape = (new_shape,)
    a = nlcpy.ravel(a)
    Na = a.size
    total_size = core.internal.prod(new_shape)
    if Na == 0 or total_size == 0:
        return nlcpy.zeros(new_shape, a.dtype)

    n_copies = int(total_size / Na)
    extra = total_size % Na
    if extra != 0:
        n_copies = n_copies + 1
        extra = Na - extra

    a = nlcpy.concatenate((a,) * n_copies)
    if extra > 0:
        a = a[:-extra]

    return nlcpy.reshape(a, new_shape)
Beispiel #16
0
def insert(arr, obj, values, axis=None):
    """Inserts values along the given axis before the given indices.

    Parameters
    ----------
    arr : array_like
        Input array.
    obj : int, slice or sequence of ints
        Object that defines the index or indices before which values is inserted.
        Support for multiple insertions when obj is a single scalar or a sequence
        with one element (similar to calling insert multiple times).
    values : array_like
        Values to insert into arr. If the type of values is different from that of
        arr, values is converted to the type of arr. values should be shaped so that
        arr[...,obj,...] = values is legal.
    axis : int, optional
        Axis along which to insert values. If axis is None then arr is flattened
        first.

    Returns
    -------
    out : ndarray
        A copy of arr with values inserted. Note that insert does not occur in-place:
        a new array is returned. If axis is None, out is a flattened array.

    Note:
        Note that for higher dimensional inserts obj=0 behaves very different from
        obj=[0] just like arr[:,0,:] = values is different from arr[:,[0],:] = values.

    See Also
    --------
    append : Appends values to the end of an array.
    concatenate : Joins a sequence of arrays along an existing axis.
    delete : Returns a new array with sub-arrays along an axis deleted.

    Examples
    --------
    >>> import nlcpy as vp
    >>> from nlcpy import testing
    >>> a = vp.array([[1, 1], [2, 2], [3, 3]])
    >>> a
    array([[1, 1],
           [2, 2],
           [3, 3]])
    >>> vp.insert(a, 1, 5)
    array([1, 5, 1, 2, 2, 3, 3])
    >>> vp.insert(a, 1, 5, axis=1)
    array([[1, 5, 1],
           [2, 5, 2],
           [3, 5, 3]])

    Difference between sequence and scalars:

    >>> vp.insert(a, [1], [[1],[2],[3]], axis=1)
    array([[1, 1, 1],
           [2, 2, 2],
           [3, 3, 3]])
    >>> vp.testing.assert_array_equal(
    ...                vp.insert(a, 1, [1, 2, 3], axis=1),
    ...                vp.insert(a, [1], [[1],[2],[3]], axis=1))
    >>> b = a.flatten()
    >>> b
    array([1, 1, 2, 2, 3, 3])
    >>> vp.insert(b, [2, 2], [5, 6])
    array([1, 1, 5, 6, 2, 2, 3, 3])
    >>> vp.insert(b, slice(2, 4), [5, 6])
    array([1, 1, 5, 2, 6, 2, 3, 3])
    >>> vp.insert(b, [2, 2], [7.13, False]) # type casting
    array([1, 1, 7, 0, 2, 2, 3, 3])
    >>> x = vp.arange(8).reshape(2, 4)
    >>> idx = (1, 3)
    >>> vp.insert(x, idx, 999, axis=1)
    array([[  0, 999,   1,   2, 999,   3],
           [  4, 999,   5,   6, 999,   7]])

    """
    a = nlcpy.asarray(arr)
    if axis is None:
        if a.ndim != 1:
            a = a.ravel()
        axis = 0
    elif isinstance(axis, nlcpy.ndarray) or isinstance(axis, numpy.ndarray):
        axis = int(axis)
    elif not isinstance(axis, int):
        raise TypeError("an integer is required "
                        "(got type {0})".format(type(axis).__name__))

    if axis < -a.ndim or axis >= a.ndim:
        raise nlcpy.AxisError(
            "axis {0} is out of bounds for array of dimension {1}".format(axis, a.ndim))

    if axis < 0:
        axis += a.ndim

    if type(obj) is slice:
        start, stop, step = obj.indices(a.shape[axis])
        obj = nlcpy.arange(start, stop, step)
    else:
        obj = nlcpy.array(obj)
        if obj.dtype.char == '?':
            warnings.warn(
                "in the future insert will treat boolean arrays and "
                "array-likes as a boolean index instead of casting it to "
                "integer", FutureWarning, stacklevel=3)
        elif obj.dtype.char in 'fdFD':
            if obj.size == 1:
                raise TypeError(
                    "slice indices must be integers or "
                    "None or have an __index__ method")
            elif obj.size > 0:
                raise IndexError(
                    'arrays used as indices must be of integer (or boolean) type')
        elif obj.dtype.char in 'IL':
            if obj.size == 1:
                objval = obj[()] if obj.ndim == 0 else obj[0]
                if objval > a.shape[axis]:
                    raise IndexError(
                        "index {0} is out of bounds for axis {1} with size {2}".format(
                            objval, axis, a.shape[axis]))
            else:
                tmp = 'float64' if obj.dtype.char == 'L' else 'int64'
                raise UFuncTypeError(
                    "Cannot cast ufunc 'add' output from dtype('{0}') to "
                    "dtype('{1}') with casting rule 'same_kind'".format(tmp, obj.dtype))
        obj = obj.astype('l')
        if obj.ndim > 1:
            raise ValueError(
                "index array argument obj to insert must be one dimensional or scalar")

    if obj.ndim == 0:
        if obj > a.shape[axis] or obj < -a.shape[axis]:
            raise IndexError(
                "index {0} is out of bounds for axis {1} with size {2}".format(
                    obj[()] if obj > 0 else obj[()] + a.shape[axis],
                    axis, a.shape[axis]))

    newshape = list(a.shape)
    if obj.size == 1:
        values = nlcpy.array(values, copy=False, ndmin=a.ndim, dtype=a.dtype)
        if obj.ndim == 0:
            values = nlcpy.moveaxis(values, 0, axis)
        newshape[axis] += values.shape[axis]
        obj = nlcpy.array(nlcpy.broadcast_to(obj, values.shape[axis]))
        val_shape = list(a.shape)
        val_shape[axis] = values.shape[axis]
        values = nlcpy.broadcast_to(values, val_shape)
    else:
        newshape[axis] += obj.size
        values = nlcpy.array(values, copy=False, ndmin=a.ndim, dtype=a.dtype)
        val_shape = list(a.shape)
        val_shape[axis] = obj.size
        values = nlcpy.broadcast_to(values, val_shape)

    out = nlcpy.empty(newshape, dtype=a.dtype)
    work = nlcpy.zeros(obj.size + out.shape[axis] + 2, dtype='l')
    work[-1] = -1
    request._push_request(
        'nlcpy_insert',
        'manipulation_op',
        (a, obj, values, out, axis, work)
    )
    if work[-1] != -1:
        raise IndexError(
            "index {0} is out of bounds for axis {1} with size {2}"
            .format(obj[work[-1]], axis, out.shape[axis]))
    return out
Beispiel #17
0
def delete(arr, obj, axis=None):
    """Returns a new array with sub-arrays along an axis deleted.

    For a one dimensional array, this returns those entries not returned by arr[obj].

    Parameters
    ----------
    arr : array_like
        Input array.
    obj : slice, int or array of ints
        Indicate indices of sub-arrays to remove along the specified axis.
    axis : int, optional
        The axis along which to delete the subarray defined by obj.
        If axis is None, obj is applied to the flattened array.

    Returns
    -------
    out : ndarray
        A copy of arr with the elements specified by obj removed.
        Note that delete does not occur in-place. If axis is None, out is a flattened
        array.

    Note
    ----
    Often it is preferable to use a boolean mask. For example:

    >>> import nlcpy as vp
    >>> arr = vp.arange(12) + 1
    >>> mask = vp.ones(len(arr), dtype=bool)
    >>> mask[[0,2,4]] = False
    >>> result = arr[mask,...]

    Is equivalent to vp.delete(arr, [0,2,4], axis=0), but allows further use of mask.

    See Also
    --------
    insert : Inserts values along the given axis before the given indices.
    append : Appends values to the end of an array.

    Examples
    --------
    >>> import nlcpy as vp
    >>> arr = vp.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
    >>> arr
    array([[ 1,  2,  3,  4],
           [ 5,  6,  7,  8],
           [ 9, 10, 11, 12]])
    >>> vp.delete(arr, 1, 0)
    array([[ 1,  2,  3,  4],
           [ 9, 10, 11, 12]])
    >>> vp.delete(arr, slice(None, None, 2), 1)
    array([[ 2,  4],
           [ 6,  8],
           [10, 12]])
    >>> vp.delete(arr, [1,3,5], None)
    array([ 1,  3,  5,  7,  8,  9, 10, 11, 12])

    """

    input_arr = nlcpy.asarray(arr)
    ndim = input_arr.ndim

    if input_arr._f_contiguous and not input_arr._c_contiguous:
        order_out = 'F'
    else:
        order_out = 'C'

    if axis is None:
        if ndim != 1:
            input_arr = input_arr.ravel()
        ndim = input_arr.ndim
        axis = ndim - 1

    if isinstance(axis, numpy.ndarray) or isinstance(axis, nlcpy.ndarray):
        axis = int(axis)
    elif not isinstance(axis, int):
        raise TypeError("an integer is required (got type "
                        + str(type(axis).__name__) + ")")

    if axis < -ndim or axis > ndim - 1:
        raise AxisError(
            "axis {} is out of bounds for array of dimension {}".format(axis, ndim))
    if axis < 0:
        axis += ndim

    N = input_arr.shape[axis]
    if isinstance(obj, slice):
        start, stop, step = obj.indices(N)
        xr = range(start, stop, step)
        if len(xr) == 0:
            return input_arr.copy(order=order_out)
        else:
            del_obj = nlcpy.arange(start, stop, step)
    else:
        del_obj = nlcpy.asarray(obj)
        if del_obj.ndim != 1:
            del_obj = del_obj.ravel()

        if del_obj.dtype == bool:
            if del_obj.ndim != 1 or del_obj.size != input_arr.shape[axis]:
                raise ValueError(
                    'boolean array argument obj to delete must be one dimensional and '
                    'match the axis length of {}'.format(input_arr.shape[axis]))
            del_obj = del_obj.astype(nlcpy.intp)

        if isinstance(obj, (int, nlcpy.integer)):
            if (obj < -N or obj >= N):
                raise IndexError(
                    "index %i is out of bounds for axis %i with "
                    "size %i" % (obj, axis, N))
            if (obj < 0):
                del_obj += N
        elif del_obj.size > 0 and del_obj.dtype != int:
            raise IndexError(
                'arrays used as indices must be of integer (or boolean) type')

    if del_obj.size == 0:
        new = nlcpy.array(input_arr)
        return new
    else:
        new = nlcpy.empty(input_arr.shape, input_arr.dtype, order_out)
        idx = nlcpy.ones(input_arr.shape[axis], dtype=del_obj.dtype)
        obj_count = nlcpy.zeros([3], dtype='l')
        request._push_request(
            'nlcpy_delete',
            'manipulation_op',
            (input_arr, del_obj, axis, idx, new, obj_count)
        )
        count = obj_count.get()
        if count[1] != 0:
            raise IndexError(
                "index out of bounds for axis {}".format(axis))
        if count[2] != 0:
            warnings.warn(
                "in the future negative indices will not be ignored by "
                "`numpy.delete`.", FutureWarning, stacklevel=3)
        sl = [slice(N - count[0]) if i == axis
              else slice(None) for i in range(new.ndim)]
        return new[sl].copy()
Beispiel #18
0
 def test_zeros_like_reshape_nlcpy_only(self, dtype, order):
     a = testing.shaped_arange((2, 3, 4), nlcpy, dtype)
     b = nlcpy.zeros_like(a, shape=self.shape)
     c = nlcpy.zeros(self.shape, order=order, dtype=dtype)
     testing.assert_array_equal(b, c)
Beispiel #19
0
 def test_zeros_strides(self, order):
     a = numpy.zeros((2, 3), dtype='d', order=order)
     b = nlcpy.zeros((2, 3), dtype='d', order=order)
     self.assertEqual(b.strides, a.strides)
Beispiel #20
0
def diag(v, k=0):
    """Extracts a diagonal or constructs a diagonal array.

    Parameters
    ----------
    v : array_like
        If *v* is a 2-D array, return a copy of its *k-th* diagonal. If *v* is a 1-D
        array, return a 2-D array with *v* on the k-th diagonal.
    k : int, optional
        Diagonal in question. The default is 0. Use *k>0* for diagonals above the main
        diagonal, and *k<0* for diagonals below the main diagonal.

    Returns
    -------
    out : ndarray
        The extracted diagonal or constructed diagonal array.

    See Also
    --------
    diagonal : Returns specified diagonals.
    diagflat : Creates a two-dimensional array with the flattened input as a diagonal.

    Examples
    --------
    >>> import nlcpy as vp
    >>> x = vp.arange(9).reshape((3,3))
    >>> x
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    >>> vp.diag(x)
    array([0, 4, 8])
    >>> vp.diag(x, k=1)
    array([1, 5])
    >>> vp.diag(x, k=-1)
    array([3, 7])
    >>> vp.diag(vp.diag(x))
    array([[0, 0, 0],
           [0, 4, 0],
           [0, 0, 8]])

    """
    if isinstance(v, nlcpy.ndarray):
        ndim = v.ndim
    else:
        ndim = numpy.ndim(v)
        if ndim == 1:
            v = nlcpy.array(v)
        if ndim == 2:
            # to save bandwidth, don't copy non-diag elements to GPU
            v = numpy.array(v)

    if ndim == 1:
        size = v.size + abs(k)
        ret = nlcpy.zeros((size, size), dtype=v.dtype)
        ret.diagonal(k)[:] = v
        return ret
    elif ndim == 2:
        return v.diagonal(k).copy()
    else:
        raise ValueError('Input must be 1- or 2-d.')
Beispiel #21
0
def qr(a, mode='reduced'):
    """Computes the qr factorization of a matrix.

    Factor the matrix *a* as *qr*, where *q* is orthonormal and *r* is upper-triangular.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to be factored.
    mode : {'reduced', 'complete', 'r', 'raw', 'full', 'economic'}, optional
        If K = min(M, N), then

        - 'reduced' : returns q, r with dimensions (M, K), (K, N) (default)
        - 'complete' : returns q, r with dimensions (M, M), (M, N)
        - 'r' : returns r only with dimensions (K, N)
        - 'raw' : returns h, tau with dimensions (N, M), (K,)
        - 'full' or 'f' : alias of 'reduced', deprecated
        - 'economic' or 'e' : returns h from 'raw', deprecated.

    Returns
    -------
    q : ndarray, optional
        A matrix with orthonormal columns. When mode = 'complete' the result is an
        orthogonal/unitary matrix depending on whether or not a is real/complex. The
        determinant may be either +/- 1 in that case.
    r : ndarray, optional
        The upper-triangular matrix.
    (h, tau) : ndarray, optional
        The array h contains the Householder reflectors that generate q along with r. The
        tau array contains scaling factors for the reflectors. In the deprecated
        'economic' mode only h is returned.

    Note
    ----
    This is an interface to the LAPACK routines ``dgeqrf``, ``zgeqrf``, ``dorgqr``,
    and ``zungqr``.

    For more information on the qr factorization, see for example:
    https://en.wikipedia.org/wiki/QR_factorization

    Note that when 'raw' option is specified the returned arrays are of type "float64" or
    "complex128" and the h array is transposed to be FORTRAN compatible.

    Examples
    --------
    >>> import numpy as np
    >>> import nlcpy as vp
    >>> from nlcpy import testing
    >>> a = vp.random.randn(9, 6)
    >>> q, r = vp.linalg.qr(a)
    >>> vp.testing.assert_allclose(a, vp.dot(q, r))  # a does equal qr
    >>> r2 = vp.linalg.qr(a, mode='r')
    >>> r3 = vp.linalg.qr(a, mode='economic')
    >>> # mode='r' returns the same r as mode='full'
    >>> vp.testing.assert_allclose(r, r2)
    >>> # But only triu parts are guaranteed equal when mode='economic'
    >>> vp.testing.assert_allclose(r, np.triu(r3[:6,:6], k=0))

    Example illustrating a common use of qr: solving of least squares problems

    What are the least-squares-best *m* and *y0* in ``y = y0 + mx`` for the following
    data: {(0,1), (1,0), (1,2), (2,1)}. (Graph the points and you’ll see that it should
    be y0 = 0, m = 1.) The answer is provided by solving the over-determined matrix
    equation ``Ax = b``, where::

        A = array([[0, 1], [1, 1], [1, 1], [2, 1]])
        x = array([[y0], [m]])
        b = array([[1], [0], [2], [1]])

    If A = qr such that q is orthonormal (which is always possible via Gram-Schmidt),
    then ``x = inv(r) * (q.T) * b``. (In practice, however, we simply use :func:`lstsq`.)

    >>> A = vp.array([[0, 1], [1, 1], [1, 1], [2, 1]])
    >>> A
    array([[0, 1],
           [1, 1],
           [1, 1],
           [2, 1]])
    >>> b = vp.array([1, 0, 2, 1])
    >>> q, r = vp.linalg.qr(A)
    >>> p = vp.dot(q.T, b)
    >>> vp.dot(vp.linalg.inv(r), p)
    array([1.1102230246251565e-16, 1.0000000000000002e+00])

    """
    if mode not in ('reduced', 'complete', 'r', 'raw'):
        if mode in ('f', 'full'):
            msg = "".join(
                ("The 'full' option is deprecated in favor of 'reduced'.\n",
                 "For backward compatibility let mode default."))
            warnings.warn(msg, DeprecationWarning, stacklevel=3)
            mode = 'reduced'
        elif mode in ('e', 'economic'):
            msg = "The 'economic' option is deprecated."
            warnings.warn(msg, DeprecationWarning, stacklevel=3)
            mode = 'economic'
        else:
            raise ValueError("Unrecognized mode '%s'" % mode)

    a = nlcpy.asarray(a)
    util._assertRank2(a)
    if a.dtype == 'F':
        dtype = 'D'
        a_dtype = 'F'
    elif a.dtype == 'D':
        dtype = 'D'
        a_dtype = 'D'
    elif a.dtype == 'f':
        dtype = 'd'
        a_dtype = 'f'
    else:
        dtype = 'd'
        a_dtype = 'd'

    m, n = a.shape
    if a.size == 0:
        if mode == 'reduced':
            return nlcpy.empty((m, 0), a_dtype), nlcpy.empty((0, n), a_dtype)
        elif mode == 'complete':
            return nlcpy.identity(m, a_dtype), nlcpy.empty((m, n), a_dtype)
        elif mode == 'r':
            return nlcpy.empty((0, n), a_dtype)
        elif mode == 'raw':
            return nlcpy.empty((n, m), dtype), nlcpy.empty((0, ), dtype)
        else:
            return nlcpy.empty((m, n), a_dtype), nlcpy.empty((0, ), a_dtype)

    a = nlcpy.asarray(a, dtype=dtype, order='F')
    k = min(m, n)
    if mode == 'complete':
        if m > n:
            x = nlcpy.empty((m, m), dtype=dtype, order='F')
            x[:m, :n] = a
            a = x
        r_shape = (m, n)
    elif mode in ('r', 'reduced', 'economic'):
        r_shape = (k, n)
    else:
        r_shape = 1
    jobq = 0 if mode in ('r', 'raw', 'economic') else 1
    tau = nlcpy.empty(k, dtype=dtype)
    r = nlcpy.zeros(r_shape, dtype=dtype)
    work = nlcpy.empty(n * 64, dtype=dtype)
    fpe = request._get_fpe_flag()
    args = (
        m,
        n,
        jobq,
        a._ve_array,
        tau._ve_array,
        r._ve_array,
        work._ve_array,
        veo.OnStack(fpe, inout=veo.INTENT_OUT),
    )

    request._push_and_flush_request(
        'nlcpy_qr',
        args,
    )

    if mode == 'raw':
        return a.T, tau

    if mode == 'r':
        return nlcpy.asarray(r, dtype=a_dtype)

    if mode == 'economic':
        return nlcpy.asarray(a, dtype=a_dtype)

    mc = m if mode == 'complete' else k
    q = nlcpy.asarray(a[:, :mc], dtype=a_dtype, order='C')
    r = nlcpy.asarray(r, dtype=a_dtype, order='C')
    return q, r
Beispiel #22
0
def linspace(start,
             stop,
             num=50,
             endpoint=True,
             retstep=False,
             dtype=None,
             axis=0):
    """Returns evenly spaced numbers over a specified interval.

    Returns *num* evenly spaced samples, calculated over the interval ``[start, stop]``.
    The endpoint of the interval can optionally be excluded.

    Parameters
    ----------
    start : array_like
        The starting value of the sequence.
    stop : array_like
        The end value of the sequence, unless *endpoint* is set to False. In that case,
        the sequence consists of all but the last of ``num + 1`` evenly spaced samples,
        so that *stop* is excluded. Note that the step size changes when *endpoint* is
        False.
    num : int, optional
        Number of samples to generate. Default is 50. Must be non-negative.
    endpoint : bool, optional
        If True, *stop* is the last sample. Otherwise, it is not included. Default is
        True.
    retstep : bool, optional
        If True, return (*samples*, *step*) where *step* is the spacing between samples.
    dtype : dtype, optional
        The type of the output array. If *dtype* is not given, infer the data type from
        the other input arguments.
    axis : int, optional
        The axis in the result to store the samples. Relevant only if start or stop are
        array-like. By default (0), the samples will be along a new axis inserted at the
        beginning. Use -1 to get an axis at the end.

    Returns
    -------
    samples : ndarray
        There are *num* equally spaced samples in the closed interval ``[start, stop]``
        or the half-open interval ``[start, stop)`` (depending on whether *endpoint* is
        True or False).
    step : float, optional
        Only returned if *retstep* is True
        Size of spacing between samples.

    See Also
    --------
    arange : Returns evenly spaced values within a given interval.

    Examples
    --------
    >>> import nlcpy as vp
    >>> vp.linspace(2.0, 3.0, num=5)
    array([2.  , 2.25, 2.5 , 2.75, 3.  ])
    >>> vp.linspace(2.0, 3.0, num=5, endpoint=False)
    array([2. , 2.2, 2.4, 2.6, 2.8])
    >>> vp.linspace(2.0, 3.0, num=5, retstep=True)
    (array([2.  , 2.25, 2.5 , 2.75, 3.  ]), array([0.25]))

    """
    num = operator.index(num)
    if num < 0:
        raise ValueError("Number of samples, %s, must be non-negative." % num)

    dtype_kind = numpy.dtype(dtype).kind
    if dtype_kind == 'V':
        raise NotImplementedError(
            'void dtype in linspace is not implemented yet.')

    start = nlcpy.asarray(start)
    stop = nlcpy.asarray(stop)
    dt = numpy.result_type(start, stop, float(num))
    if start.dtype.char in '?iIlL' or stop.dtype.char in '?iIlL':
        dt = 'D' if dt.char in 'FD' else 'd'

    if dtype is None:
        dtype = dt

    start = nlcpy.asarray(start, dtype=dt)
    stop = nlcpy.asarray(stop, dtype=dt)
    delta = stop - start
    div = (num - 1) if endpoint else num
    if num == 0:
        ret = nlcpy.empty((num, ) + delta.shape, dtype=dtype)
        if retstep:
            ret = (ret, nlcpy.NaN)
        return ret
    elif div == 0 or num == 1:
        ret = nlcpy.resize(start, (1, ) + delta.shape).astype(dtype)
        if retstep:
            ret = (ret, stop)
        return ret
    else:
        ret = nlcpy.empty((num, ) + delta.shape, dtype=dtype)
    retdata = ret

    delta = delta[nlcpy.newaxis]
    start = nlcpy.array(nlcpy.broadcast_to(start, delta.shape))
    stop = nlcpy.array(nlcpy.broadcast_to(stop, delta.shape))
    step = delta / div if div > 1 else delta
    if retdata._memloc in {on_VE, on_VE_VH}:
        denormal = nlcpy.zeros(1, dtype='l')
        request._push_request(
            "nlcpy_linspace", "creation_op",
            (ret, start, stop, delta, step, int(endpoint), denormal))
        if axis != 0:
            ret = nlcpy.moveaxis(ret, 0, axis)
        if retstep:
            ret = (ret, step)

    if retdata._memloc in {on_VH, on_VE_VH}:
        del retdata.vh_data
        del step.vh_data
        typ = numpy.dtype(dtype).type
        if retstep:
            (retdata.vh_data,
             step.vh_data) = numpy.linspace(typ(start),
                                            typ(stop), num, endpoint,
                                            typ(retstep), dtype, axis)
        else:
            retdata.vh_data = numpy.linspace(typ(start),
                                             typ(stop), num, endpoint,
                                             typ(retstep), dtype, axis)
    return ret
Beispiel #23
0
 def test_lstsq_not_converge(self):
     a = nlcpy.ones([200, 200])
     a[0, 0] = nlcpy.nan
     with self.assertRaises(nlcpy.linalg.LinAlgError):
         nlcpy.linalg.lstsq(a, nlcpy.zeros([200, 1]))