Repository: scipy/scipy
Pull request: Duplicate entries are added together in csr_matrix constructor
<!-- 
Thanks for contributing a pull request! Please ensure that
your PR satisfies the checklist before submitting:
http://scipy.github.io/devdocs/dev/contributor/development_workflow.html#checklist-before-submitting-a-pr

Also, please name and describe your PR as you would write a
commit message:
https://docs.scipy.org/doc/numpy/dev/development_workflow.html#writing-the-commit-message

Note that we are a team of volunteers; we appreciate your
patience during the review process.

Again, thanks for contributing!
-->

#### Reference issue
<!--Example: Closes gh-WXYZ.-->
Closes #10558 

#### What does this implement/fix?
<!--Please explain your changes.-->
This documents implicit summation in csr_sum matrix constructor

#### Additional information
<!--Any additional information you think is important.-->
I'm in doubt whether to delete another example with the same constructor or not, but decided not to.

Problem statement:
Document implicit sum in csr_matrix() constructor
<!-- 
Thank you for taking the time to file a bug report. 
Please fill in the fields below, deleting the sections that 
don't apply to your issue. You can view the final output
by clicking the preview button above.
Note: This is a comment, and won't appear in the output.
-->

My issue is about csr_matrix().

#### Reproducing code example:
<!-- 
If you place your code between the triple backticks below, 
it will be rendered as a code block. 
-->

```
from scipy.sparse import csr_matrix
csr_matrix(([1,1,1],([0,0,0],[0,0,0])), shape=(1,1)).todense()
```
Result: matrix([[3]])
Expected: matrix([[1]])
This is an implicit behaviour. I can not find anything about this in the documentation.
I assume, there are 3 options:
1. Raise a error.
2. Use only last value in the list.
3. Document current behaviour.

Hints from issue discussion:
This is indeed the intended behavior, and it's documented (albeit not very prominently) here:
https://docs.scipy.org/doc/scipy/reference/sparse.html#example-2

I agree that we should make this more prominent in the docs.
I think, it will be ok if you add notice right to https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
@aikho I would love to work on this. Do you want me to add the above example as a notice to https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
@sameshl would be great to add something similar to example 2 from https://docs.scipy.org/doc/scipy/reference/sparse.html#example-2 right to https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
Hi! My guess is that @sameshl is not working on this, and wouldn't be angry if I'll make the PR
@erheron please go ahead!

Commit message: Duplicate entries are added together in csr_matrix

Patch to review:
diff --git a/scipy/sparse/csr.py b/scipy/sparse/csr.py
index 86869f8e43c1..8ab81633c53b 100644
--- a/scipy/sparse/csr.py
+++ b/scipy/sparse/csr.py
@@ -100,6 +100,16 @@ class csr_matrix(_cs_matrix):
            [0, 0, 3],
            [4, 5, 6]])
 
+    Duplicate entries are summed together:
+
+    >>> row = np.array(0, 1, 2, 0)
+    >>> col = np.array(0, 1, 1, 0)
+    >>> data = np.array(1, 2, 4, 8)
+    >>> csr_matrix((data, (row, col)), shape=(3,3)).todense()
+    array([[9, 0, 0],
+           [0, 2, 0],
+           [4, 0, 0]])
+
     As an example of how to construct a CSR matrix incrementally,
     the following snippet builds a term-document matrix from texts:
 


Code review comments:
diff_hunk: @@ -100,6 +100,16 @@ class csr_matrix(_cs_matrix):
            [0, 0, 3],
            [4, 5, 6]])
 
+    Duplicate entries are summed together:
+
+    >>> row = np.array(0, 1, 2, 0)
path: scipy/sparse/csr.py
text: @user1:
the numbers should be in square brackets: `np.array([0, 1, 2, 0])`

@author:
Yep, sorry

diff_hunk: @@ -100,6 +100,16 @@ class csr_matrix(_cs_matrix):
            [0, 0, 3],
            [4, 5, 6]])
 
+    Duplicate entries are summed together:
+
+    >>> row = np.array(0, 1, 2, 0)
+    >>> col = np.array(0, 1, 1, 0)
+    >>> data = np.array(1, 2, 4, 8)
+    >>> csr_matrix((data, (row, col)), shape=(3,3)).todense()
path: scipy/sparse/csr.py
text: @user1:
Use `.toarray()` instead of `.todense()`

Also, add a space after the comma: `shape=(3, 3)`

@author:
Done