@@ -76,16 +76,16 @@ public Matrix Forward(Matrix input1, Matrix input2, Matrix weights)
7676 double [ ] dSumY_dResultMagnitude = new double [ input2 . Rows / 2 ] ;
7777 double [ ] dResultMagnitude_dWeight = new double [ input2 . Rows / 2 ] ;
7878
79- double dInputMag_dOutputMag = 0.0d ;
80- double dInputMag_dOutputAngle = 0.0d ;
81- double dInputAngle_dOutputMag = 0.0d ;
82- double dInputAngle_dOutputAngle = 0.0d ;
83- double dInput2Mag_dOutputMag = 0.0d ;
84- double dInput2Mag_dOutputAngle = 0.0d ;
85- double dInput2Angle_dOutputMag = 0.0d ;
86- double dInput2Angle_dOutputAngle = 0.0d ;
87- double dWeight_dOutputMag = 0.0d ;
88- double dWeight_dOutputAngle = 0.0d ;
79+ double [ ] dInputMag_dOutputMag = new double [ input2 . Rows / 2 ] ;
80+ double [ ] dInputMag_dOutputAngle = new double [ input2 . Rows / 2 ] ;
81+ double [ ] dInputAngle_dOutputMag = new double [ input2 . Rows / 2 ] ;
82+ double [ ] dInputAngle_dOutputAngle = new double [ input2 . Rows / 2 ] ;
83+ double [ ] dInput2Mag_dOutputMag = new double [ input2 . Rows / 2 ] ;
84+ double [ ] dInput2Mag_dOutputAngle = new double [ input2 . Rows / 2 ] ;
85+ double [ ] dInput2Angle_dOutputMag = new double [ input2 . Rows / 2 ] ;
86+ double [ ] dInput2Angle_dOutputAngle = new double [ input2 . Rows / 2 ] ;
87+ double [ ] dWeight_dOutputMag = new double [ input2 . Rows / 2 ] ;
88+ double [ ] dWeight_dOutputAngle = new double [ input2 . Rows / 2 ] ;
8989
9090 for ( int k = 0 ; k < input2 . Rows / 2 ; k ++ )
9191 {
@@ -183,63 +183,63 @@ public Matrix Forward(Matrix input1, Matrix input2, Matrix weights)
183183
184184 for ( int k = 0 ; k < input2 . Rows / 2 ; k ++ )
185185 {
186- dInputMag_dOutputMag + =
186+ dInputMag_dOutputMag [ k ] =
187187 dCombinedMagnitude_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dMagnitude [ k ] +
188188 dCombinedMagnitude_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dMagnitude [ k ] +
189189 dCombinedMagnitude_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dMagnitude [ k ] +
190190 dCombinedMagnitude_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dMagnitude [ k ] ;
191191
192- dInput2Mag_dOutputMag + =
192+ dInput2Mag_dOutputMag [ k ] =
193193 dCombinedMagnitude_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX2_dWMagnitude [ k ] +
194194 dCombinedMagnitude_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY2_dWMagnitude [ k ] +
195195 dCombinedMagnitude_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY2_dWMagnitude [ k ] +
196196 dCombinedMagnitude_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX2_dWMagnitude [ k ] ;
197197
198- dInputMag_dOutputAngle + =
198+ dInputMag_dOutputAngle [ k ] =
199199 dCombinedAngle_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dMagnitude [ k ] +
200200 dCombinedAngle_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dMagnitude [ k ] +
201201 dCombinedAngle_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dMagnitude [ k ] +
202202 dCombinedAngle_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dMagnitude [ k ] ;
203203
204- dInput2Mag_dOutputAngle + =
204+ dInput2Mag_dOutputAngle [ k ] =
205205 dCombinedAngle_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX2 [ k ] * dX2_dWMagnitude [ k ] +
206206 dCombinedAngle_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY2 [ k ] * dY2_dWMagnitude [ k ] +
207207 dCombinedAngle_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY2 [ k ] * dY2_dWMagnitude [ k ] +
208208 dCombinedAngle_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX2 [ k ] * dX2_dWMagnitude [ k ] ;
209209
210- dInputAngle_dOutputMag + =
210+ dInputAngle_dOutputMag [ k ] =
211211 dCombinedMagnitude_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dAngle [ k ] +
212212 dCombinedMagnitude_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dAngle [ k ] +
213213 dCombinedMagnitude_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dAngle [ k ] +
214214 dCombinedMagnitude_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dAngle [ k ] ;
215215
216- dInput2Angle_dOutputMag + =
216+ dInput2Angle_dOutputMag [ k ] =
217217 dCombinedMagnitude_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX2 [ k ] * dX2_dWAngle [ k ] +
218218 dCombinedMagnitude_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY2 [ k ] * dY2_dWAngle [ k ] +
219219 dCombinedMagnitude_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY2 [ k ] * dY2_dWAngle [ k ] +
220220 dCombinedMagnitude_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX2 [ k ] * dX2_dWAngle [ k ] ;
221221
222- dInputAngle_dOutputAngle + =
222+ dInputAngle_dOutputAngle [ k ] =
223223 dCombinedAngle_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dAngle [ k ] +
224224 dCombinedAngle_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dAngle [ k ] +
225225 dCombinedAngle_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY1 [ k ] * dY1_dAngle [ k ] +
226226 dCombinedAngle_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX1 [ k ] * dX1_dAngle [ k ] ;
227227
228- dInput2Angle_dOutputAngle + =
228+ dInput2Angle_dOutputAngle [ k ] =
229229 dCombinedAngle_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dX2 [ k ] * dX2_dWAngle [ k ] +
230230 dCombinedAngle_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dY2 [ k ] * dY2_dWAngle [ k ] +
231231 dCombinedAngle_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dY2 [ k ] * dY2_dWAngle [ k ] +
232232 dCombinedAngle_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dX2 [ k ] * dX2_dWAngle [ k ] ;
233233
234- dWeight_dOutputMag + =
234+ dWeight_dOutputMag [ k ] =
235235 dCombinedMagnitude_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dWeight [ k ] +
236236 dCombinedMagnitude_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dWeight [ k ] +
237237 dCombinedMagnitude_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dWeight [ k ] +
238238 dCombinedMagnitude_dSumY * dSumY_dDeltaX [ k ] * dDeltaX_dWeight [ k ] +
239239 dCombinedMagnitude_dSumX * dSumX_dResultMagnitude [ k ] * dResultMagnitude_dWeight [ k ] +
240240 dCombinedMagnitude_dSumY * dSumY_dResultMagnitude [ k ] * dResultMagnitude_dWeight [ k ] ;
241241
242- dWeight_dOutputAngle + =
242+ dWeight_dOutputAngle [ k ] =
243243 dCombinedAngle_dSumX * dSumX_dDeltaX [ k ] * dDeltaX_dWeight [ k ] +
244244 dCombinedAngle_dSumY * dSumY_dDeltaY [ k ] * dDeltaY_dWeight [ k ] +
245245 dCombinedAngle_dSumX * dSumX_dDeltaY [ k ] * dDeltaY_dWeight [ k ] +
@@ -276,24 +276,27 @@ public override BackwardResult Backward(Matrix dOutput)
276276 Matrix dWeights = new Matrix ( weights . Rows , weights . Cols ) ;
277277
278278 // Loop through each element in input1
279- Parallel . For ( 0 , input1 . Rows , i =>
279+ Parallel . For ( 0 , this . input1 . Rows , i =>
280280 {
281281 // Loop through each half of the columns in input1 (representing magnitudes and angles)
282- for ( int j = 0 ; j < input2 . Cols / 2 ; j ++ )
282+ for ( int j = 0 ; j < this . input2 . Cols / 2 ; j ++ )
283283 {
284284 var calculatedValues = this . calculatedValues [ i , j ] ;
285- dInput1 [ i , j ] += dOutput [ i , j ] * calculatedValues . DInputMag_dOutputMag ;
286- dInput1 [ i , j ] += dOutput [ i , j + ( input2 . Cols / 2 ) ] * calculatedValues . DInputMag_dOutputAngle ;
287- dInput1 [ i , j + ( input1 . Cols / 2 ) ] += dOutput [ i , j ] * calculatedValues . DInputAngle_dOutputMag ;
288- dInput1 [ i , j + ( input1 . Cols / 2 ) ] += dOutput [ i , j + ( input2 . Cols / 2 ) ] * calculatedValues . DInputAngle_dOutputAngle ;
289-
290- dInput2 [ i , j ] += dOutput [ i , j ] * calculatedValues . DInput2Mag_dOutputMag ;
291- dInput2 [ i , j ] += dOutput [ i , j + ( input2 . Cols / 2 ) ] * calculatedValues . DInput2Mag_dOutputAngle ;
292- dInput2 [ i , j + ( input2 . Cols / 2 ) ] += dOutput [ i , j ] * calculatedValues . DInput2Angle_dOutputMag ;
293- dInput2 [ i , j + ( input2 . Cols / 2 ) ] += dOutput [ i , j + ( input2 . Cols / 2 ) ] * calculatedValues . DInput2Angle_dOutputAngle ;
294-
295- dWeights [ i , j ] += dOutput [ i , j ] * calculatedValues . DWeight_dOutputMag ;
296- dWeights [ i , j ] += dOutput [ i , j + ( input2 . Cols / 2 ) ] * calculatedValues . DWeight_dOutputAngle ;
285+ for ( int k = 0 ; k < this . input2 . Rows / 2 ; k ++ )
286+ {
287+ dInput1 [ i , k ] += dOutput [ i , j ] * calculatedValues . DInputMag_dOutputMag [ k ] ;
288+ dInput1 [ i , k ] += dOutput [ i , j + ( this . input2 . Cols / 2 ) ] * calculatedValues . DInputMag_dOutputAngle [ k ] ;
289+ dInput1 [ i , k + ( this . input1 . Cols / 2 ) ] += dOutput [ i , j ] * calculatedValues . DInputAngle_dOutputMag [ k ] ;
290+ dInput1 [ i , k + ( this . input1 . Cols / 2 ) ] += dOutput [ i , j + ( this . input2 . Cols / 2 ) ] * calculatedValues . DInputAngle_dOutputAngle [ k ] ;
291+
292+ dInput2 [ k , j ] += dOutput [ i , j ] * calculatedValues . DInput2Mag_dOutputMag [ k ] ;
293+ dInput2 [ k , j ] += dOutput [ i , j + ( this . input2 . Cols / 2 ) ] * calculatedValues . DInput2Mag_dOutputAngle [ k ] ;
294+ dInput2 [ k , j + ( this . input2 . Cols / 2 ) ] += dOutput [ i , j ] * calculatedValues . DInput2Angle_dOutputMag [ k ] ;
295+ dInput2 [ k , j + ( this . input2 . Cols / 2 ) ] += dOutput [ i , j + ( this . input2 . Cols / 2 ) ] * calculatedValues . DInput2Angle_dOutputAngle [ k ] ;
296+
297+ dWeights [ k , j ] += dOutput [ i , j ] * calculatedValues . DWeight_dOutputMag [ k ] ;
298+ dWeights [ k , j ] += dOutput [ i , j + ( this . input2 . Cols / 2 ) ] * calculatedValues . DWeight_dOutputAngle [ k ] ;
299+ }
297300 }
298301 } ) ;
299302
@@ -306,25 +309,25 @@ public override BackwardResult Backward(Matrix dOutput)
306309
307310 private struct CalculatedValues
308311 {
309- public double DInputMag_dOutputMag { get ; internal set ; }
312+ public double [ ] DInputMag_dOutputMag { get ; internal set ; }
310313
311- public double DInputMag_dOutputAngle { get ; internal set ; }
314+ public double [ ] DInputMag_dOutputAngle { get ; internal set ; }
312315
313- public double DInputAngle_dOutputMag { get ; internal set ; }
316+ public double [ ] DInputAngle_dOutputMag { get ; internal set ; }
314317
315- public double DInputAngle_dOutputAngle { get ; internal set ; }
318+ public double [ ] DInputAngle_dOutputAngle { get ; internal set ; }
316319
317- public double DInput2Mag_dOutputMag { get ; internal set ; }
320+ public double [ ] DInput2Mag_dOutputMag { get ; internal set ; }
318321
319- public double DInput2Mag_dOutputAngle { get ; internal set ; }
322+ public double [ ] DInput2Mag_dOutputAngle { get ; internal set ; }
320323
321- public double DInput2Angle_dOutputMag { get ; internal set ; }
324+ public double [ ] DInput2Angle_dOutputMag { get ; internal set ; }
322325
323- public double DInput2Angle_dOutputAngle { get ; internal set ; }
326+ public double [ ] DInput2Angle_dOutputAngle { get ; internal set ; }
324327
325- public double DWeight_dOutputMag { get ; internal set ; }
328+ public double [ ] DWeight_dOutputMag { get ; internal set ; }
326329
327- public double DWeight_dOutputAngle { get ; internal set ; }
330+ public double [ ] DWeight_dOutputAngle { get ; internal set ; }
328331 }
329332 }
330333}
0 commit comments