@@ -8918,7 +8918,7 @@ class TestDotFamilyFallback:
89188918 # when available, end-to-end through a real new-style DType (quaddtype).
89198919
89208920 @pytest .mark .parametrize ("sa,sb" , [
8921- ((), ()), ((), ( 3 , 4 )), (( 4 , 3 ), ()), (( 5 ,), (5 ,)), ((3 ,), (3 , 4 )),
8921+ ((5 ,), (5 ,)), ((3 ,), (3 , 4 )),
89228922 ((4 , 3 ), (3 ,)), ((4 , 3 ), (3 , 5 )), ((2 , 3 , 4 ), (4 ,)),
89238923 ((2 , 3 , 4 ), (4 , 5 )), ((4 ,), (2 , 4 , 5 )),
89248924 ((0 , 3 ), (3 , 4 )), ((4 , 0 ), (0 , 5 )),
@@ -8943,6 +8943,14 @@ def test_dot_fallback_rejects_stacked(self, sa, sb):
89438943 with assert_raises_regex (ValueError , "tensordot" ):
89448944 _dot_fallback (a , b )
89458945
8946+ def test_dot_fallback_rejects_scalar_operands (self ):
8947+ # matmul has no 0-D form; `dot` keeps 0-D on its own multiply path.
8948+ a = np .arange (6 , dtype = np .float64 ).reshape (2 , 3 )
8949+ for x , y in [(np .array (3.0 ), a ), (a , np .array (3.0 )),
8950+ (np .array (3.0 ), np .array (4.0 ))]:
8951+ with assert_raises_regex (ValueError , "enough dimensions" ):
8952+ _dot_fallback (x , y )
8953+
89468954 def test_dot_fallback_unsupported_type (self ):
89478955 s = np .array (["a" , "b" , "c" ])
89488956 assert_raises (ValueError , _dot_fallback , s , s )
@@ -8951,12 +8959,10 @@ def test_dot_fallback_unsupported_type(self):
89518959 assert_raises (ValueError , np .vdot , s , s )
89528960
89538961 def test_dot_scalar_operands_bypass_fallback (self ):
8954- # `dot` handles a 0-D operand with `multiply` for every dtype, and
8955- # casts both operands to their common dtype first. That must keep
8956- # happening for dtypes without a dotfunc: routing them through the
8957- # fallback would skip the cast and reach `multiply`'s heterogeneous
8958- # loops, so `np.dot` would start repeating strings instead of
8959- # raising. See the `out=` cases below.
8962+ # `dot` handles 0-D with `multiply`, casting to the common dtype
8963+ # first. That must keep happening for dtypes without a dotfunc: the
8964+ # errors below come from `multiply`, whereas the fallback would raise
8965+ # a plain ValueError -- which is what proves 0-D never reaches it.
89608966 s = np .array ("ab" )
89618967 assert_raises (_UFuncNoLoopError , np .dot , s , 3 )
89628968 assert_raises (_UFuncNoLoopError , np .dot , 3 , s )
@@ -8983,7 +8989,6 @@ def test_dot_fallback_noncontiguous_inputs(self):
89838989 a = np .arange (12 , dtype = np .float64 ).reshape (4 , 3 )
89848990 b = np .arange (15 , dtype = np .float64 ).reshape (3 , 5 )
89858991 inputs = [
8986- (np .array (3.0 ), np .asfortranarray (a )),
89878992 (np .asfortranarray (a ), np .asfortranarray (b )),
89888993 (np .arange (24. ).reshape (4 , 6 )[:, ::2 ],
89898994 np .arange (30. ).reshape (6 , 5 )[::2 ]),
@@ -8992,8 +8997,7 @@ def test_dot_fallback_noncontiguous_inputs(self):
89928997 ref = np .dot (x , y )
89938998 got = _dot_fallback (x , y )
89948999 assert_array_equal (got , ref , strict = True )
8995- if x .ndim and y .ndim :
8996- assert got .flags ["C_CONTIGUOUS" ]
9000+ assert got .flags ["C_CONTIGUOUS" ]
89979001
89989002 def test_dot_fallback_does_not_conjugate (self ):
89999003 # dot (unlike vdot) must not conjugate, even for complex input
@@ -9051,6 +9055,13 @@ def f(arr):
90519055 assert np .ndim (scalar ) == 0
90529056 assert float (scalar ) == np .dot (a , b )
90539057
9058+ # 0-D stays on dot's own multiply path, for user dtypes too
9059+ assert_array_equal (f (np .dot (q (3.0 ), q (A ))), np .dot (3.0 , A ))
9060+ assert_array_equal (f (np .dot (q (A ), q (3.0 ))), np .dot (A , 3.0 ))
9061+ scalar_out = np .empty (A .shape , dtype = qd )
9062+ assert np .dot (q (3.0 ), q (A ), scalar_out ) is scalar_out
9063+ assert_array_equal (f (scalar_out ), np .dot (3.0 , A ))
9064+
90549065 assert_array_equal (f (np .dot (q (A ), q (B ))), np .dot (A , B ))
90559066 assert_array_equal (f (np .inner (q (A ), q (A ))), np .inner (A , A ))
90569067 assert float (np .vdot (q (a ), q (b ))) == np .vdot (a , b )
0 commit comments