How to use the sparse._coo.common.linear_loc function in sparse

To help you get started, we’ve selected a few sparse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pydata / sparse / sparse / _compressed / compressed.py View on Github external
compressed_axes = (np.argmin(x.shape),)

    check_compressed_axes(x.shape, compressed_axes)

    axis_order = list(compressed_axes)
    # array location where the uncompressed dimensions start
    axisptr = len(compressed_axes)
    axis_order.extend(np.setdiff1d(np.arange(len(x.shape)), compressed_axes))
    reordered_shape = tuple(x.shape[i] for i in axis_order)
    row_size = np.prod(reordered_shape[:axisptr])
    col_size = np.prod(reordered_shape[axisptr:])
    compressed_shape = (row_size, col_size)
    shape = x.shape

    x = x.transpose(axis_order)
    linear = linear_loc(x.coords, reordered_shape)
    order = np.argsort(linear)
    # linearizing twice is unnecessary, fix needed
    coords = x.reshape((compressed_shape)).coords
    indptr = np.empty(row_size + 1, dtype=np.intp)
    indptr[0] = 0
    np.cumsum(np.bincount(coords[0], minlength=row_size), out=indptr[1:])
    indices = coords[1]
    data = x.data[order]
    return ((data, indices, indptr), shape, compressed_axes, x.fill_value)
github pydata / sparse / sparse / _coo / core.py View on Github external
See Also
        --------
        :obj:`numpy.flatnonzero` : Equivalent Numpy function.

        Examples
        --------
        >>> x = np.eye(5)
        >>> s = COO.from_numpy(x)
        >>> s.linear_loc()  # doctest: +NORMALIZE_WHITESPACE
        array([ 0,  6, 12, 18, 24])
        >>> np.array_equal(np.flatnonzero(x), s.linear_loc())
        True
        """
        from .common import linear_loc

        return linear_loc(self.coords, self.shape)
github pydata / sparse / sparse / _coo / umath.py View on Github external
cargs = [matched_arrays[0], arg2]
            current_shape = _get_broadcast_shape(matched_arrays[0].shape, arg2.shape)
            params = [
                _get_broadcast_parameters(arg.shape, current_shape) for arg in cargs
            ]
            reduced_params = [all(p) for p in zip(*params)]
            reduced_shape = _get_reduced_shape(
                arg2.shape, _rev_idx(reduced_params, arg2.ndim)
            )

            reduced_coords = [
                _get_reduced_coords(arg.coords, _rev_idx(reduced_params, arg.ndim))
                for arg in cargs
            ]

            linear = [linear_loc(rc, reduced_shape) for rc in reduced_coords]
            sorted_idx = [np.argsort(idx) for idx in linear]
            linear = [idx[s] for idx, s in zip(linear, sorted_idx)]
            matched_idx = _match_arrays(*linear)

            if return_midx:
                matched_idx = [
                    sidx[midx] for sidx, midx in zip(sorted_idx, matched_idx)
                ]
                return matched_idx

            coords = [arg.coords[:, s] for arg, s in zip(cargs, sorted_idx)]
            mcoords = [c[:, idx] for c, idx in zip(coords, matched_idx)]
            mcoords = _get_matching_coords(mcoords, params)
            mdata = [arg.data[sorted_idx[0]][matched_idx[0]] for arg in matched_arrays]
            mdata.append(arg2.data[sorted_idx[1]][matched_idx[1]])
            # The coords aren't truly sorted, but we don't need them, so it's
github pydata / sparse / sparse / _compressed / convert.py View on Github external
def _transpose(x, shape, axes, compressed_axes, transpose=False):
    """an algorithm for reshaping, resizing, changing compressed axes, and transposing"""

    check_compressed_axes(shape, compressed_axes)
    uncompressed = uncompress_dimension(x.indptr)
    coords = np.stack((uncompressed, x.indices))
    linear = linear_loc(coords, x._compressed_shape)
    sorted_axis_order = np.argsort(x._axis_order)
    if len(shape) == 1:
        c_linear = np.empty(x.nnz, dtype=np.intp)
        _c_ordering(
            linear,
            c_linear,
            np.asarray(x._reordered_shape),
            np.asarray(sorted_axis_order),
            np.asarray(x.shape),
        )
        order = np.argsort(c_linear, kind="mergesort")
        data = x.data[order]
        indices = c_linear[order]
        return (data, indices, [])

    new_axis_order = list(compressed_axes)