Repository: scipy/scipy

Issue:
BUG: scipy.sparse.lil_matrix division by complex number leads to inconsistent behavior
### Describe your issue.

When dividing a `scipy.sparse.lil_array` by a complex number, and subsequently trying to convert it to `scipy.sparse.csr_array`, a `TypeError` is raised during the conversion.

The same does not happen for other sparse matrix types.

### Reproducing Code Example

```python
from scipy import sparse

matrix = sparse.random(10, 10, 0.1, format="lil")
matrix = matrix / 1j
matrix.tocsr()
```

The above example results in an error, while with `format="coo"` the following works as expected:
```python
from scipy import sparse

matrix = sparse.random(10, 10, 0.1, format="coo")
matrix = matrix / 1j
matrix.tocsr()
```



### Error message

```shell
File ~/.conda/envs/ntcad/lib/python3.11/site-packages/scipy/sparse/_lil.py:474, in lil_matrix.tocsr(self, copy)
    472 data = np.empty(nnz, dtype=self.dtype)
    473 _csparsetools.lil_flatten_to_array(self.rows, indices)
--> 474 _csparsetools.lil_flatten_to_array(self.data, data)
    476 # init csr matrix
    477 return self._csr_container((data, indices, indptr), shape=self.shape)

File _csparsetools.pyx:135, in scipy.sparse._csparsetools.lil_flatten_to_array()

File _csparsetools.pyx:244, in scipy.sparse._csparsetools._lil_flatten_to_array_float64()

TypeError: must be real number, not complex
```


### SciPy/NumPy/Python version and system information

```shell
1.10.1 1.23.5 sys.version_info(major=3, minor=11, micro=3, releaselevel='final', serial=0)
lapack_armpl_info:
  NOT AVAILABLE
lapack_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/home/vetschn/.conda/envs/ntcad/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/vetschn/.conda/envs/ntcad/include']
lapack_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/home/vetschn/.conda/envs/ntcad/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/vetschn/.conda/envs/ntcad/include']
blas_armpl_info:
  NOT AVAILABLE
blas_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/home/vetschn/.conda/envs/ntcad/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/vetschn/.conda/envs/ntcad/include']
blas_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/home/vetschn/.conda/envs/ntcad/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/vetschn/.conda/envs/ntcad/include']
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2,SSE3
    found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
    not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_KNM,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL
```



Discussion:

Resolving patch:
diff --git a/scipy/sparse/_lil.py b/scipy/sparse/_lil.py
--- a/scipy/sparse/_lil.py
+++ b/scipy/sparse/_lil.py
@@ -287,6 +287,7 @@ def _mul_scalar(self, other):
     def __truediv__(self, other):           # self / other
         if isscalarlike(other):
             new = self.copy()
+            new.dtype = np.result_type(self, other)
             # Divide every element by this scalar
             for j, rowvals in enumerate(new.data):
                 new.data[j] = [val/other for val in rowvals]


Test patch:
diff --git a/scipy/sparse/tests/test_base.py b/scipy/sparse/tests/test_base.py
--- a/scipy/sparse/tests/test_base.py
+++ b/scipy/sparse/tests/test_base.py
@@ -4201,6 +4201,14 @@ def test_scalar_mul(self):
         x = x*0
         assert_equal(x[0, 0], 0)
 
+    def test_truediv_scalar(self):
+        A = self.spcreator((3, 2))
+        A[0, 1] = -10
+        A[2, 0] = 20
+
+        assert_array_equal((A / 1j).toarray(), A.toarray() / 1j)
+        assert_array_equal((A / 9).toarray(), A.toarray() / 9)
+
     def test_inplace_ops(self):
         A = lil_matrix([[0, 2, 3], [4, 0, 6]])
         B = lil_matrix([[0, 1, 0], [0, 2, 3]])