Skip to content

Commit

Permalink
Add full and full_like (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and hameerabbasi committed Sep 19, 2018
1 parent 0cba310 commit 7abe667
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 33 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
Upcoming Release
----------------

* Added :code:`sparse.full` and :code:`sparse.full_like` (:pr:`189`).
* Added :code:`COO.clip` method (:pr:`185`).
* Added :code:`COO.copy` method, and changed pickle of :code:`COO` to not
include its cache (:pr:`184`).
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/sparse.full.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
full
====

.. currentmodule:: sparse

.. autofunction:: full
6 changes: 6 additions & 0 deletions docs/generated/sparse.full_like.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
full_like
=========

.. currentmodule:: sparse

.. autofunction:: full_like
8 changes: 6 additions & 2 deletions docs/generated/sparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ API

concatenate

eye

dot

elemwise

eye

full

full_like

load_npz

nanmax
Expand Down
2 changes: 1 addition & 1 deletion sparse/coo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .umath import elemwise
from .common import (tensordot, dot, concatenate, stack, triu, tril, where,
nansum, nanprod, nanmin, nanmax, nanreduce, roll, eye,
zeros, zeros_like, ones, ones_like)
full, full_like, zeros, zeros_like, ones, ones_like)
83 changes: 67 additions & 16 deletions sparse/coo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,71 @@ def eye(N, M=None, k=0, dtype=float):
sorted=True)


def full(shape, fill_value, dtype=None):
"""Return a COO array of given shape and type, filled with `fill_value`.
Parameters
----------
shape : int or tuple of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
fill_value : scalar
Fill value.
dtype : data-type, optional
The desired data-type for the array. The default, `None`, means
`np.array(fill_value).dtype`.
Returns
-------
out : COO
Array of `fill_value` with the given shape and dtype.
Examples
--------
>>> full(5, 9).todense() # doctest: +NORMALIZE_WHITESPACE
array([9, 9, 9, 9, 9])
>>> full((2, 2), 9, dtype=float).todense() # doctest: +SKIP
array([[9., 9.],
[9., 9.]])
"""
from .core import COO

if dtype is None:
dtype = np.array(fill_value).dtype
if not isinstance(shape, tuple):
shape = (shape,)
data = np.empty(0, dtype=dtype)
coords = np.empty((len(shape), 0), dtype=np.intp)
return COO(coords, data=data, shape=shape,
fill_value=fill_value, has_duplicates=False,
sorted=True)


def full_like(a, fill_value, dtype=None):
"""Return a full array with the same shape and type as a given array.
Parameters
----------
a : array_like
The shape and data-type of the result will match those of `a`.
dtype : data-type, optional
Overrides the data type of the result.
Returns
-------
out : COO
Array of `fill_value` with the same shape and type as `a`.
Examples
--------
>>> x = np.ones((2, 3), dtype='i8')
>>> full_like(x, 9.0).todense() # doctest: +NORMALIZE_WHITESPACE
array([[9, 9, 9],
[9, 9, 9]])
"""
return full(a.shape, fill_value, dtype=(a.dtype if dtype is None else dtype))


def zeros(shape, dtype=float):
"""Return a COO array of given shape and type, filled with zeros.
Expand All @@ -793,14 +858,7 @@ def zeros(shape, dtype=float):
array([[0, 0],
[0, 0]])
"""
from .core import COO

if not isinstance(shape, tuple):
shape = (shape,)
data = np.empty(0, dtype=dtype)
coords = np.empty((len(shape), 0), dtype=np.intp)
return COO(coords, data=data, shape=shape, has_duplicates=False,
sorted=True)
return full(shape, 0, np.dtype(dtype))


def zeros_like(a, dtype=None):
Expand Down Expand Up @@ -853,14 +911,7 @@ def ones(shape, dtype=float):
array([[1, 1],
[1, 1]])
"""
from .core import COO

if not isinstance(shape, tuple):
shape = (shape,)
data = np.empty(0, dtype=dtype)
coords = np.empty((len(shape), 0), dtype=np.intp)
return COO(coords, data=data, shape=shape, fill_value=1,
has_duplicates=False, sorted=True)
return full(shape, 1, np.dtype(dtype))


def ones_like(a, dtype=None):
Expand Down
40 changes: 26 additions & 14 deletions sparse/tests/test_coo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,25 +1808,37 @@ def test_eye(N, M):
assert_eq(sparse.eye(N, M=M, k=k, dtype='i4'), np.eye(N, M=M, k=k, dtype='i4'))


def test_zeros():
assert_eq(sparse.zeros(5), np.zeros(5))
assert_eq(sparse.zeros((5, 4)), np.zeros((5, 4)))
assert_eq(sparse.zeros((5, 4), dtype='i4'), np.zeros((5, 4), dtype='i4'))
@pytest.mark.parametrize('funcname', ['ones', 'zeros'])
def test_ones_zeros(funcname):
sp_func = getattr(sparse, funcname)
np_func = getattr(np, funcname)

assert_eq(sp_func(5), np_func(5))
assert_eq(sp_func((5, 4)), np_func((5, 4)))
assert_eq(sp_func((5, 4), dtype='i4'), np_func((5, 4), dtype='i4'))
assert_eq(sp_func((5, 4), dtype=None), np_func((5, 4), dtype=None))


@pytest.mark.parametrize('funcname', ['ones_like', 'zeros_like'])
def test_ones_zeros_like(funcname):
sp_func = getattr(sparse, funcname)
np_func = getattr(np, funcname)

def test_zeros_like():
x = np.ones((5, 5), dtype='i8')
assert_eq(sparse.zeros_like(x), np.zeros_like(x))
assert_eq(sparse.zeros_like(x, dtype='f8'), np.zeros_like(x, dtype='f8'))

assert_eq(sp_func(x), np_func(x))
assert_eq(sp_func(x, dtype='f8'), np_func(x, dtype='f8'))
assert_eq(sp_func(x, dtype=None), np_func(x, dtype=None))


def test_ones():
assert_eq(sparse.ones(5), np.ones(5))
assert_eq(sparse.ones((5, 4)), np.ones((5, 4)))
assert_eq(sparse.ones((5, 4), dtype='i4'), np.ones((5, 4), dtype='i4'))
def test_full():
assert_eq(sparse.full(5, 9), np.full(5, 9))
assert_eq(sparse.full(5, 9, dtype='f8'), np.full(5, 9, dtype='f8'))
assert_eq(sparse.full((5, 4), 9.5), np.full((5, 4), 9.5))
assert_eq(sparse.full((5, 4), 9.5, dtype='i4'), np.full((5, 4), 9.5, dtype='i4'))


def test_ones_like():
def test_full_like():
x = np.zeros((5, 5), dtype='i8')
assert_eq(sparse.ones_like(x), np.ones_like(x))
assert_eq(sparse.ones_like(x, dtype='f8'), np.ones_like(x, dtype='f8'))
assert_eq(sparse.full_like(x, 9.5), np.full_like(x, 9.5))
assert_eq(sparse.full_like(x, 9.5, dtype='f8'), np.full_like(x, 9.5, dtype='f8'))

0 comments on commit 7abe667

Please sign in to comment.