Repository: scipy/scipy
Pull request: DOC: sparse: Correct `todense` documentation
<!-- 
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:
http://scipy.github.io/devdocs/dev/contributor/development_workflow.html#writing-the-commit-message

Depending on your changes, you can skip CI operations and save time and energy: 
http://scipy.github.io/devdocs/dev/contributor/continuous_integration.html#skipping

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 #20239 

#### What does this implement/fix?
<!--Please explain your changes.-->
Changed the return documentation correct the return type of `todense`

#### Additional information
<!--Any additional information you think is important.-->
The return type documentation has been copied from the `toarray` method. There may be some difference between how these methods work that I'm not aware of so the documentation may not be accurate. If someone more knowledgeable could check that would be appreciated. 

Problem statement:
DOC: Sparse arrays: todense() does not return numpy.matrix
### Issue with current documentation:

The documentation states that the `todense()` method of each of the sparse array classes returns a `numpy.matrix` but they return a `numpy.ndarray`.

### Idea or request for content:

Modify the documentation to state the return type is `numpy.ndarray`.

### Additional context (e.g. screenshots, GIFs)

_No response_

Hints from issue discussion:
@henry50 would you be interested in opening a PR?
See also #18898

Commit message: Added optional parameters to todense method

Patch to review:
diff --git a/scipy/sparse/_base.py b/scipy/sparse/_base.py
index 3c0e9bb209fd..94d8b21ee665 100644
--- a/scipy/sparse/_base.py
+++ b/scipy/sparse/_base.py
@@ -948,13 +948,12 @@ def todense(self, order=None, out=None):
 
         Returns
         -------
-        arr : numpy.matrix, 2-D
-            A NumPy matrix object with the same shape and containing
-            the same data represented by the sparse array/matrix, with the
-            requested memory order. If `out` was passed and was an
-            array (rather than a `numpy.matrix`), it will be filled
-            with the appropriate values and returned wrapped in a
-            `numpy.matrix` object that shares the same memory.
+        arr : ndarray, 2-D
+            An array with the same shape and containing the same
+            data represented by the sparse array/matrix, with the requested
+            memory order. If `out` was passed, the same object is
+            returned after being modified in-place to contain the
+            appropriate values.
         """
         return self._ascontainer(self.toarray(order=order, out=out))
 
diff --git a/scipy/sparse/_matrix.py b/scipy/sparse/_matrix.py
index 1ab874942383..230d230c1249 100644
--- a/scipy/sparse/_matrix.py
+++ b/scipy/sparse/_matrix.py
@@ -111,3 +111,35 @@ def getrow(self, i):
         matrix (row vector).
         """
         return self._getrow(i)
+
+    def todense(self, order=None, out=None):
+        """
+        Return a dense representation of this sparse matrix.
+
+        Parameters
+        ----------
+        order : {'C', 'F'}, optional
+            Whether to store multi-dimensional data in C (row-major)
+            or Fortran (column-major) order in memory. The default
+            is 'None', which provides no ordering guarantees.
+            Cannot be specified in conjunction with the `out`
+            argument.
+
+        out : ndarray, 2-D, optional
+            If specified, uses this array (or `numpy.matrix`) as the
+            output buffer instead of allocating a new array to
+            return. The provided array must have the same shape and
+            dtype as the sparse matrix on which you are calling the
+            method.
+
+        Returns
+        -------
+        arr : numpy.matrix, 2-D
+            A NumPy matrix object with the same shape and containing
+            the same data represented by the sparse matrix, with the
+            requested memory order. If `out` was passed and was an
+            array (rather than a `numpy.matrix`), it will be filled
+            with the appropriate values and returned wrapped in a
+            `numpy.matrix` object that shares the same memory.
+        """
+        return self.todense(order, out)


Code review comments:
diff_hunk: @@ -111,3 +111,35 @@ def getrow(self, i):
         matrix (row vector).
         """
         return self._getrow(i)
+
+    def todense(self, order=None, out=None):
+        """
+        Return a dense representation of this sparse matrix.
+
+        Parameters
+        ----------
+        order : {'C', 'F'}, optional
+            Whether to store multi-dimensional data in C (row-major)
+            or Fortran (column-major) order in memory. The default
+            is 'None', which provides no ordering guarantees.
+            Cannot be specified in conjunction with the `out`
+            argument.
+
+        out : ndarray, 2-D, optional
+            If specified, uses this array (or `numpy.matrix`) as the
+            output buffer instead of allocating a new array to
+            return. The provided array must have the same shape and
+            dtype as the sparse matrix on which you are calling the
+            method.
+
+        Returns
+        -------
+        arr : numpy.matrix, 2-D
+            A NumPy matrix object with the same shape and containing
+            the same data represented by the sparse matrix, with the
+            requested memory order. If `out` was passed and was an
+            array (rather than a `numpy.matrix`), it will be filled
+            with the appropriate values and returned wrapped in a
+            `numpy.matrix` object that shares the same memory.
+        """
+        return self.todense(order, out)
path: scipy/sparse/_matrix.py
text: @user1:
I think you'll need to use "super" instead of "self" so it looks to the method defined in the base class.
As currently set up it leads to an infinite loop of calling itself.
```suggestion
        return super().todense(order, out)
```

@author:
I didn't think about that, thanks for the suggestion!