python3-scipy: fix determinant regression and openblas option handling

This commit is contained in:
Andrew J. Hesford 2023-06-28 20:36:47 -04:00
parent e0aefd77af
commit ccaf2ea792
2 changed files with 77 additions and 4 deletions

View File

@ -0,0 +1,74 @@
From 61d892c9faa543ad80bd5e2d0bf69821188487e0 Mon Sep 17 00:00:00 2001
From: Ilhan Polat <ilhanpolat@gmail.com>
Date: Tue, 27 Jun 2023 12:00:38 +0200
Subject: [PATCH] MAINT:linalg.det:Return scalars for singleton inputs (#18763)
---
scipy/linalg/_basic.py | 17 ++++++++++++-----
scipy/linalg/tests/test_basic.py | 17 +++++++++++++++++
2 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/scipy/linalg/_basic.py b/scipy/linalg/_basic.py
index 327ba1ca36ce..6faa3898f720 100644
--- 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"
diff --git a/scipy/linalg/tests/test_basic.py b/scipy/linalg/tests/test_basic.py
index 983530b63ff2..40308500ec4a 100644
--- 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)

View File

@ -1,9 +1,10 @@
# Template file for 'python3-scipy'
pkgname=python3-scipy
version=1.11.0
revision=1
revision=2
build_style=meson
build_helper="python3"
configure_args="$(vopt_if openblas "" "-Dblas=blas -Dlapack=lapack")"
hostmakedepends="python3-build python3-installer python3-meson-python
python3-wheel python3-Cython python3-pybind11 pythran python3-numpy
gcc-fortran pkg-config"
@ -22,7 +23,7 @@ build_options="openblas"
if [ "$CROSS_BUILD" ]; then
_pybind11_dir="${py3_sitelib}/pybind11"
configure_args="--cross-file=python.cross"
configure_args+=" --cross-file=python.cross"
export PKG_CONFIG_PATH="${XBPS_CROSS_BASE}/${_pybind11_dir}/share/pkgconfig"
# pybind11 uses a path relative to the pkgconfig file to set $prefix,
# which causes the wrapper to double-include $XBPS_CROSS_BASE; override
@ -43,8 +44,6 @@ if [ "$build_option_openblas" ]; then
ppc64*) ;;
ppc*) broken="numpy can't be built with openblas";;
esac
else
configure_args+=" -Dblas=blas -Dlapack=lapack"
fi
pre_patch() {