@@ -968,6 +968,63 @@ where
968968 is_blas_2d ( a. _dim ( ) , a. _strides ( ) , BlasOrder :: F )
969969}
970970
971+ /// Dot product for dynamic-dimensional arrays (`ArrayD`).
972+ ///
973+ /// For one-dimensional arrays, computes the vector dot product, which is the sum
974+ /// of the elementwise products (no conjugation of complex operands).
975+ /// Both arrays must have the same length.
976+ ///
977+ /// For two-dimensional arrays, performs matrix multiplication. The array shapes
978+ /// must be compatible in the following ways:
979+ /// - If `self` is *M* × *N*, then `rhs` must be *N* × *K* for matrix-matrix multiplication
980+ /// - If `self` is *M* × *N* and `rhs` is *N*, returns a vector of length *M*
981+ /// - If `self` is *M* and `rhs` is *M* × *N*, returns a vector of length *N*
982+ /// - If both arrays are one-dimensional of length *N*, returns a scalar
983+ ///
984+ /// **Panics** if:
985+ /// - The arrays have dimensions other than 1 or 2
986+ /// - The array shapes are incompatible for the operation
987+ /// - For vector dot product: the vectors have different lengths
988+ impl < A > Dot < ArrayRef < A , IxDyn > > for ArrayRef < A , IxDyn >
989+ where A : LinalgScalar
990+ {
991+ type Output = Array < A , IxDyn > ;
992+
993+ fn dot ( & self , rhs : & ArrayRef < A , IxDyn > ) -> Self :: Output
994+ {
995+ match ( self . ndim ( ) , rhs. ndim ( ) ) {
996+ ( 1 , 1 ) => {
997+ let a = self . view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
998+ let b = rhs. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
999+ let result = a. dot ( & b) ;
1000+ ArrayD :: from_elem ( vec ! [ ] , result)
1001+ }
1002+ ( 2 , 2 ) => {
1003+ // Matrix-matrix multiplication
1004+ let a = self . view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1005+ let b = rhs. view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1006+ let result = a. dot ( & b) ;
1007+ result. into_dimensionality :: < IxDyn > ( ) . unwrap ( )
1008+ }
1009+ ( 2 , 1 ) => {
1010+ // Matrix-vector multiplication
1011+ let a = self . view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1012+ let b = rhs. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
1013+ let result = a. dot ( & b) ;
1014+ result. into_dimensionality :: < IxDyn > ( ) . unwrap ( )
1015+ }
1016+ ( 1 , 2 ) => {
1017+ // Vector-matrix multiplication
1018+ let a = self . view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
1019+ let b = rhs. view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1020+ let result = a. dot ( & b) ;
1021+ result. into_dimensionality :: < IxDyn > ( ) . unwrap ( )
1022+ }
1023+ _ => panic ! ( "Dot product for ArrayD is only supported for 1D and 2D arrays" ) ,
1024+ }
1025+ }
1026+ }
1027+
9711028#[ cfg( test) ]
9721029#[ cfg( feature = "blas" ) ]
9731030mod blas_tests
@@ -1083,60 +1140,3 @@ mod blas_tests
10831140 }
10841141 }
10851142}
1086-
1087- /// Dot product for dynamic-dimensional arrays (`ArrayD`).
1088- ///
1089- /// For one-dimensional arrays, computes the vector dot product, which is the sum
1090- /// of the elementwise products (no conjugation of complex operands).
1091- /// Both arrays must have the same length.
1092- ///
1093- /// For two-dimensional arrays, performs matrix multiplication. The array shapes
1094- /// must be compatible in the following ways:
1095- /// - If `self` is *M* × *N*, then `rhs` must be *N* × *K* for matrix-matrix multiplication
1096- /// - If `self` is *M* × *N* and `rhs` is *N*, returns a vector of length *M*
1097- /// - If `self` is *M* and `rhs` is *M* × *N*, returns a vector of length *N*
1098- /// - If both arrays are one-dimensional of length *N*, returns a scalar
1099- ///
1100- /// **Panics** if:
1101- /// - The arrays have dimensions other than 1 or 2
1102- /// - The array shapes are incompatible for the operation
1103- /// - For vector dot product: the vectors have different lengths
1104- impl < A > Dot < ArrayRef < A , IxDyn > > for ArrayRef < A , IxDyn >
1105- where A : LinalgScalar
1106- {
1107- type Output = Array < A , IxDyn > ;
1108-
1109- fn dot ( & self , rhs : & ArrayRef < A , IxDyn > ) -> Self :: Output
1110- {
1111- match ( self . ndim ( ) , rhs. ndim ( ) ) {
1112- ( 1 , 1 ) => {
1113- let a = self . view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
1114- let b = rhs. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
1115- let result = a. dot ( & b) ;
1116- ArrayD :: from_elem ( vec ! [ ] , result)
1117- }
1118- ( 2 , 2 ) => {
1119- // Matrix-matrix multiplication
1120- let a = self . view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1121- let b = rhs. view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1122- let result = a. dot ( & b) ;
1123- result. into_dimensionality :: < IxDyn > ( ) . unwrap ( )
1124- }
1125- ( 2 , 1 ) => {
1126- // Matrix-vector multiplication
1127- let a = self . view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1128- let b = rhs. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
1129- let result = a. dot ( & b) ;
1130- result. into_dimensionality :: < IxDyn > ( ) . unwrap ( )
1131- }
1132- ( 1 , 2 ) => {
1133- // Vector-matrix multiplication
1134- let a = self . view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
1135- let b = rhs. view ( ) . into_dimensionality :: < Ix2 > ( ) . unwrap ( ) ;
1136- let result = a. dot ( & b) ;
1137- result. into_dimensionality :: < IxDyn > ( ) . unwrap ( )
1138- }
1139- _ => panic ! ( "Dot product for ArrayD is only supported for 1D and 2D arrays" ) ,
1140- }
1141- }
1142- }
0 commit comments