Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pyttb/sptensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,29 @@ def __add__(self, other):
# Otherwise return negated sub
return self.__sub__(-other)

def __radd__(self, other):
"""
Right binary addition operator (+).

Parameters
----------
other:
Object to add to the sparse tensor.

Examples
--------
Add a scalar value, returning a dense tensor:

>>> S = ttb.sptensor(shape=(2, 2))
>>> S[1, 1] = 1.0
>>> 1 + S
tensor of shape (2, 2) with order F
data[:, :] =
[[1. 1.]
[1. 2.]]
"""
return self.__add__(other)

def __pos__(self):
"""
Unary plus operator (+).
Expand Down
8 changes: 8 additions & 0 deletions tests/test_sptensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,14 @@ def test_sptensor__add__(sample_sptensor):
assert np.array_equal(subSptensor.data, sptensorInstance.to_tensor().data)


def test_sptensor__radd__(sample_sptensor):
(data, sptensorInstance) = sample_sptensor

# scalar + Sptensor
subSptensor = 0 + sptensorInstance
assert np.array_equal(subSptensor.data, sptensorInstance.to_tensor().data)


def test_sptensor_isequal(sample_sptensor):
(data, sptensorInstance) = sample_sptensor

Expand Down
Loading