1+ using NumSharp . Interfaces ;
2+ using System ;
3+ using System . Linq ;
4+ using System . Runtime . InteropServices ;
5+ using System . Threading . Tasks ;
6+
7+ namespace NumSharp . Backends
8+ {
9+ public abstract partial class DefaultEngine
10+ {
11+ public NDArray Dot ( NDArray x , NDArray y )
12+ {
13+ var dtype = x . dtype ;
14+
15+ if ( x . ndim == 0 && y . ndim == 0 )
16+ {
17+ switch ( dtype . Name )
18+ {
19+ case "Int32" :
20+ return y . Data < int > ( 0 ) * x . Data < int > ( 0 ) ;
21+ case "Single" :
22+ return y . Data < float > ( 0 ) * x . Data < float > ( 0 ) ;
23+ }
24+ }
25+ else if ( x . ndim == 1 && x . ndim == 1 )
26+ {
27+
28+ switch ( dtype . Name )
29+ {
30+ case "Int32" :
31+ {
32+ int sum = 0 ;
33+ for ( int i = 0 ; i < x . size ; i ++ )
34+ sum += x . Data < int > ( i ) * y . Data < int > ( i ) ;
35+ return sum ;
36+ }
37+
38+ case "Single" :
39+ {
40+ float sum = 0 ;
41+ for ( int i = 0 ; i < x . size ; i ++ )
42+ sum += x . Data < float > ( i ) * y . Data < float > ( i ) ;
43+ return sum ;
44+ }
45+ }
46+ }
47+ else if ( x . ndim == 2 && y . ndim == 1 )
48+ {
49+ // check size
50+ if ( x . shape [ 1 ] != y . shape [ 0 ] )
51+ throw new IncorrectSizeException ( $ "shapes ({ x . shape [ 0 ] } ,{ x . shape [ 1 ] } ) and ({ y . shape [ 0 ] } ,) not aligned: { x . shape [ 1 ] } (dim 1) != { y . shape [ 0 ] } (dim 0)") ;
52+ var nd = new NDArray ( dtype , new Shape ( x . shape [ 0 ] ) ) ;
53+ switch ( dtype . Name )
54+ {
55+ case "Int32" :
56+ for ( int i = 0 ; i < x . shape [ 0 ] ; i ++ )
57+ for ( int j = 0 ; j < y . shape [ 0 ] ; j ++ )
58+ nd . Data < int > ( ) [ i ] += x . Data < int > ( i , j ) * y . Data < int > ( j ) ;
59+ break ;
60+ case "Single" :
61+ for ( int i = 0 ; i < x . shape [ 0 ] ; i ++ )
62+ for ( int j = 0 ; j < y . shape [ 0 ] ; j ++ )
63+ nd . Data < float > ( ) [ i ] += x . Data < float > ( i , j ) * y . Data < float > ( j ) ;
64+ break ;
65+ }
66+ return nd ;
67+ }
68+ else if ( x . ndim == 2 && y . ndim == 2 )
69+ {
70+ // check size
71+ if ( x . shape [ 1 ] != y . shape [ 0 ] )
72+ throw new IncorrectSizeException ( $ "shapes ({ x . shape [ 0 ] } ,{ x . shape [ 1 ] } ) and ({ y . shape [ 0 ] } ,{ y . shape [ 1 ] } ) not aligned: { x . shape [ 1 ] } (dim 1) != { y . shape [ 0 ] } (dim 0)") ;
73+ return np . matmul ( x , y ) ;
74+ }
75+
76+ throw new NotImplementedException ( $ "dot { x . ndim } * { y . ndim } ") ;
77+ }
78+ }
79+ }
0 commit comments