@@ -18,147 +18,34 @@ public partial class NDArray
1818 /// <returns>Scalarproduct or matrix prod</returns>
1919 public NDArray dot ( NDArray nd2 )
2020 {
21- var pufferShape = nd2 . Storage . Shape ;
22-
23- // in case must do a reshape
24- var oldStorage1 = this . Storage ;
25- var oldStorage2 = nd2 . Storage ;
26- if ( ( this . ndim == 0 ) & ( nd2 . ndim == 0 ) )
21+ if ( ndim == 2 && nd2 . ndim == 1 )
2722 {
28- if ( this . dtype == typeof ( int ) )
23+ var nd = new NDArray ( dtype , new Shape ( shape [ 0 ] ) ) ;
24+ switch ( dtype . Name )
2925 {
30- var ret = this . Data < int > ( ) [ 0 ] * nd2 . Data < int > ( ) [ 0 ] ;
31- return new NDArray ( new int [ ] { ret } ) . reshape ( ) ;
26+ case "Int32" :
27+ for ( int i = 0 ; i < shape [ 0 ] ; i ++ )
28+ for ( int j = 0 ; j < nd2 . shape [ 0 ] ; j ++ )
29+ nd . Data < int > ( ) [ i ] += Data < int > ( i , j ) * nd2 . Data < int > ( j ) ;
30+ break ;
3231 }
33-
32+ return nd ;
3433 }
35-
36- if ( ( this . ndim == 1 ) & ( nd2 . ndim == 1 ) )
37- if ( this . shape [ 0 ] != nd2 . shape [ 0 ] )
38- throw new IncorrectShapeException ( ) ;
39- else
40- {
41- this . Storage = new NDStorage ( ) ;
42- this . Storage . Allocate ( oldStorage1 . DType , new Shape ( 1 , oldStorage1 . GetData ( ) . Length ) , 1 ) ;
43- this . Storage . SetData ( oldStorage1 . GetData ( ) ) ;
44-
45- nd2 . Storage = new NDStorage ( ) ;
46- nd2 . Storage . Allocate ( oldStorage2 . DType , new Shape ( oldStorage2 . GetData ( ) . Length , 1 ) , 1 ) ;
47- nd2 . Storage . SetData ( oldStorage2 . GetData ( ) ) ;
48- }
49- else
50- if ( this . shape [ 1 ] != nd2 . shape [ 0 ] )
51- throw new IncorrectShapeException ( ) ;
52-
53- if ( ( this . ndim == 2 ) & ( nd2 . ndim == 1 ) )
34+ else if ( ndim == 2 && nd2 . ndim == 2 )
5435 {
55- var pufferList = pufferShape . Dimensions . ToList ( ) ;
56- pufferList . Add ( 1 ) ;
57- nd2 . Storage . Reshape ( pufferList . ToArray ( ) ) ;
58- }
59-
60- int iterator = this . shape [ 1 ] ;
61- int dim0 = this . shape [ 0 ] ;
62- int dim1 = nd2 . shape [ 1 ] ;
63-
64- var prod = new NDArray ( this . Storage . DType , new Shape ( dim0 , dim1 ) ) ;
65-
66- Array nd1SystemArray = this . Storage . GetData ( ) ;
67-
68- switch ( nd1SystemArray )
69- {
70- case int [ ] nd1Array :
36+ var nd = new NDArray ( dtype , new Shape ( shape [ 0 ] , nd2 . shape [ 1 ] ) ) ;
37+ switch ( dtype . Name )
7138 {
72- int [ ] result = prod . Storage . GetData < int > ( ) ;
73- int [ ] nd2Array = nd2 . Storage . GetData < int > ( ) ;
74-
75- for ( int idx = 0 ; idx < prod . size ; idx ++ )
76- {
77- int puffer1 = idx % dim0 ;
78- int puffer2 = idx / dim0 ;
79- int puffer3 = puffer2 * iterator ;
80- for ( int kdx = 0 ; kdx < iterator ; kdx ++ )
81- result [ idx ] += nd2Array [ puffer3 + kdx ] * nd1Array [ dim0 * kdx + puffer1 ] ;
82- }
83- break ;
84- }
85- case double [ ] nd1Array :
86- {
87- double [ ] result = prod . Storage . GetData < double > ( ) ;
88- double [ ] nd2Array = nd2 . Storage . GetData < double > ( ) ;
89-
90- for ( int idx = 0 ; idx < prod . size ; idx ++ )
91- {
92- int puffer1 = idx % dim0 ;
93- int puffer2 = idx / dim0 ;
94- int puffer3 = puffer2 * iterator ;
95- for ( int kdx = 0 ; kdx < iterator ; kdx ++ )
96- result [ idx ] += nd2Array [ puffer3 + kdx ] * nd1Array [ dim0 * kdx + puffer1 ] ;
97- }
98- break ;
99- }
100- case float [ ] nd1Array :
101- {
102- float [ ] result = prod . Storage . GetData < float > ( ) ;
103- float [ ] nd2Array = nd2 . Storage . GetData < float > ( ) ;
104-
105- for ( int idx = 0 ; idx < prod . size ; idx ++ )
106- {
107- int puffer1 = idx % dim0 ;
108- int puffer2 = idx / dim0 ;
109- int puffer3 = puffer2 * iterator ;
110- for ( int kdx = 0 ; kdx < iterator ; kdx ++ )
111- result [ idx ] += nd2Array [ puffer3 + kdx ] * nd1Array [ dim0 * kdx + puffer1 ] ;
112- }
113- break ;
114- }
115- case Complex [ ] nd1Array :
116- {
117- Complex [ ] result = prod . Storage . GetData < Complex > ( ) ;
118- Complex [ ] nd2Array = nd2 . Storage . GetData < Complex > ( ) ;
119-
120- for ( int idx = 0 ; idx < prod . size ; idx ++ )
121- {
122- int puffer1 = idx % dim0 ;
123- int puffer2 = idx / dim0 ;
124- int puffer3 = puffer2 * iterator ;
125- for ( int kdx = 0 ; kdx < iterator ; kdx ++ )
126- result [ idx ] += nd2Array [ puffer3 + kdx ] * nd1Array [ dim0 * kdx + puffer1 ] ;
127- }
128- break ;
129- }
130- case Quaternion [ ] nd1Array :
131- {
132- Quaternion [ ] result = prod . Storage . GetData < Quaternion > ( ) ;
133- Quaternion [ ] nd2Array = nd2 . Storage . GetData < Quaternion > ( ) ;
134-
135- for ( int idx = 0 ; idx < prod . size ; idx ++ )
136- {
137- int puffer1 = idx % dim0 ;
138- int puffer2 = idx / dim0 ;
139- int puffer3 = puffer2 * iterator ;
140- for ( int kdx = 0 ; kdx < iterator ; kdx ++ )
141- result [ idx ] += nd2Array [ puffer3 + kdx ] * nd1Array [ dim0 * kdx + puffer1 ] ;
142- }
143- break ;
144- }
145- default :
146- {
147- throw new NotImplementedException ( ) ;
39+ case "Int32" :
40+ for ( int i = 0 ; i < shape [ 1 ] ; i ++ )
41+ for ( int j = 0 ; j < nd2 . shape [ 0 ] ; j ++ )
42+ nd [ i , j ] = nd . Data < int > ( i , j ) + Data < int > ( i , j ) * nd2 . Data < int > ( j ) ;
43+ break ;
14844 }
45+ return nd ;
14946 }
15047
151- if ( ( this . ndim == 1 ) & ( nd2 . ndim == 1 ) )
152- {
153- this . Storage . Reshape ( this . Storage . GetData ( ) . Length ) ;
154- nd2 . Storage . Reshape ( nd2 . Storage . GetData ( ) . Length ) ;
155- prod . Storage . Reshape ( 1 ) ;
156- }
157-
158- this . Storage = oldStorage1 ;
159- nd2 . Storage = oldStorage2 ;
160-
161- return prod ;
48+ throw new NotImplementedException ( $ "dot { ndim } * { nd2 . ndim } ") ;
16249 }
16350 }
16451}
0 commit comments