Repository: scipy/scipy

Issue:
determinant of a 1x1 matrix returns an array, not a scalar
### Describe your issue.

With 1.11, the determinant of a 1x1 matrix returns an array instead of a scalar

### Reproducing Code Example

```python
>>> import numpy
>>> from scipy import linalg
>>> linalg.det(numpy.array([[1]]))
array(1.)
>>> linalg.det(numpy.array([[1,0],[0,1]]))
1.0
```


### Error message

```shell
See above
```


### SciPy/NumPy/Python version and system information

```shell
>>> import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info); scipy.show_config()
1.11.0 1.25.0 sys.version_info(major=3, minor=11, micro=3, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/include
    lib directory: /usr/lib
    name: blas
    openblas configuration: unknown
    pc file directory: /usr/lib/pkgconfig
    version: 3.11.0
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/include
    lib directory: /usr/lib
    name: lapack
    openblas configuration: unknown
    pc file directory: /usr/lib/pkgconfig
    version: 3.11.0
  pybind11:
    detection method: pkgconfig
    include directory: /usr/lib/pkgconfig/../..//include
    name: pybind11
    version: 2.10.4
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 13.1.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 13.1.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 0.29.35
  fortran:
    commands: gfortran
    linker: ld.bfd
    name: gcc
    version: 13.1.1
  pythran:
    include directory: ../../../../../usr/lib/python3.11/site-packages/pythran
    version: 0.13.1
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /usr/bin/python
  version: '3.11'
```



Discussion:
Sigh... apparently I wrote every other test case except this one https://github.com/scipy/scipy/blob/main/scipy/linalg/tests/test_basic.py#L929-L1048

Resolving patch:
diff --git a/scipy/linalg/_basic.py b/scipy/linalg/_basic.py
--- a/scipy/linalg/_basic.py
+++ b/scipy/linalg/_basic.py
@@ -1001,7 +1001,8 @@ def det(a, overwrite_a=False, check_finite=True):
     det : (...) float or complex
         Determinant of `a`. For stacked arrays, a scalar is returned for each
         (m, m) slice in the last two dimensions of the input. For example, an
-        input of shape (p, q, m, m) will produce a result of shape (p, q).
+        input of shape (p, q, m, m) will produce a result of shape (p, q). If
+        all dimensions are 1 a scalar is returned regardless of ndim.
 
     Notes
     -----
@@ -1066,11 +1067,17 @@ def det(a, overwrite_a=False, check_finite=True):
 
     # Scalar case
     if a1.shape[-2:] == (1, 1):
-        if a1.dtype.char in 'dD':
-            return np.squeeze(a1)
+        # Either ndarray with spurious singletons or a single element
+        if max(*a1.shape) > 1:
+            temp = np.squeeze(a1)
+            if a1.dtype.char in 'dD':
+                return temp
+            else:
+                return (temp.astype('d') if a1.dtype.char == 'f' else
+                        temp.astype('D'))
         else:
-            return (np.squeeze(a1).astype('d') if a1.dtype.char == 'f' else
-                    np.squeeze(a1).astype('D'))
+            return (np.float64(a1.item()) if a1.dtype.char in 'fd' else
+                    np.complex128(a1.item()))
 
     # Then check overwrite permission
     if not _datacopied(a1, a):  # "a"  still alive through "a1"


Test patch:
diff --git a/scipy/linalg/tests/test_basic.py b/scipy/linalg/tests/test_basic.py
--- a/scipy/linalg/tests/test_basic.py
+++ b/scipy/linalg/tests/test_basic.py
@@ -930,6 +930,23 @@ class TestDet:
     def setup_method(self):
         self.rng = np.random.default_rng(1680305949878959)
 
+    def test_1x1_all_singleton_dims(self):
+        a = np.array([[1]])
+        deta = det(a)
+        assert deta.dtype.char == 'd'
+        assert np.isscalar(deta)
+        assert deta == 1.
+        a = np.array([[[[1]]]], dtype='f')
+        deta = det(a)
+        assert deta.dtype.char == 'd'
+        assert np.isscalar(deta)
+        assert deta == 1.
+        a = np.array([[[1 + 3.j]]], dtype=np.complex64)
+        deta = det(a)
+        assert deta.dtype.char == 'D'
+        assert np.isscalar(deta)
+        assert deta == 1.+3.j
+
     def test_1by1_stacked_input_output(self):
         a = self.rng.random([4, 5, 1, 1], dtype=np.float32)
         deta = det(a)