1
1
2
2
package org .drip .numerical .linearalgebra ;
3
3
4
+ import org .drip .numerical .common .NumberUtil ;
5
+ import org .drip .service .common .FormatUtil ;
6
+
4
7
/*
5
8
* -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
6
9
*/
120
123
121
124
public class SuccessiveOverRelaxation
122
125
{
126
+ public static final double TOLERANCE = 1.0e-04 ;
127
+
128
+ private double [] _rhsArray = null ;
129
+ private double _omega = Double .NaN ;
130
+ private double [][] _squareMatrix = null ;
123
131
private double [][] _diagonalMatrix = null ;
124
132
private double [][] _strictlyLowerTriangularMatrix = null ;
125
133
private double [][] _strictlyUpperTriangularMatrix = null ;
126
134
135
+ private static final boolean VectorsMatch (
136
+ final double [] array1 ,
137
+ final double [] array2 )
138
+ throws Exception
139
+ {
140
+ for (int i = 0 ; i < array1 .length ; ++i ) {
141
+ System .out .println ("Arrays => " + array1 [i ] + " | " + array2 [i ]);
142
+
143
+ if (Math .abs (array1 [i ] - array2 [i ]) > TOLERANCE ) {
144
+ System .out .println ("FALSE => " + Math .abs (array1 [i ] - array2 [i ]));
145
+
146
+ return false ;
147
+ }
148
+ }
149
+
150
+ System .out .println ("TRUE" );
151
+
152
+ return true ;
153
+ }
154
+
127
155
/**
128
156
* Construct a Standard <i>SuccessiveOverRelaxation</i> Instance from the Square Matrix
129
157
*
130
158
* @param squareMatrix Square Matrix
159
+ * @param rhsArray RHS Array
160
+ * @param omega SOR Omega Parameter
131
161
*
132
162
* @return <i>SuccessiveOverRelaxation</i> Instance
133
163
*/
134
164
135
165
public static final SuccessiveOverRelaxation Standard (
136
- final double [][] squareMatrix )
166
+ final double [][] squareMatrix ,
167
+ final double [] rhsArray ,
168
+ final double omega )
137
169
{
138
170
try {
139
171
int size = squareMatrix .length ;
@@ -160,9 +192,12 @@ public static final SuccessiveOverRelaxation Standard (
160
192
}
161
193
162
194
return new SuccessiveOverRelaxation (
195
+ squareMatrix ,
163
196
diagonalMatrix ,
164
197
strictlyLowerTriangularMatrix ,
165
- strictlyUpperTriangularMatrix
198
+ strictlyUpperTriangularMatrix ,
199
+ rhsArray ,
200
+ omega
166
201
);
167
202
} catch (Exception e ) {
168
203
e .printStackTrace ();
@@ -174,27 +209,47 @@ public static final SuccessiveOverRelaxation Standard (
174
209
/**
175
210
* <i>SuccessiveOverRelaxation</i> Constructor
176
211
*
212
+ * @param squareMatrix Square Matrix
177
213
* @param diagonalMatrix Diagonal Matrix
178
214
* @param strictlyLowerTriangularMatrix Strictly Lower Triangular Matrix
179
215
* @param strictlyUpperTriangularMatrix Strictly Upper Triangular Matrix
216
+ * @param rhsArray RHS Array
217
+ * @param omega SOR Omega Parameter
180
218
*
181
219
* @throws Exception Thrown if the Inputs are Invalid
182
220
*/
183
221
184
222
public SuccessiveOverRelaxation (
223
+ final double [][] squareMatrix ,
185
224
final double [][] diagonalMatrix ,
186
225
final double [][] strictlyLowerTriangularMatrix ,
187
- final double [][] strictlyUpperTriangularMatrix )
226
+ final double [][] strictlyUpperTriangularMatrix ,
227
+ final double [] rhsArray ,
228
+ final double omega )
188
229
throws Exception
189
230
{
190
- if (null == (_diagonalMatrix = diagonalMatrix ) ||
231
+ if (null == (_squareMatrix = squareMatrix ) ||
232
+ null == (_diagonalMatrix = diagonalMatrix ) ||
191
233
null == (_strictlyLowerTriangularMatrix = strictlyLowerTriangularMatrix ) ||
192
- null == (_strictlyUpperTriangularMatrix = strictlyUpperTriangularMatrix ))
234
+ null == (_strictlyUpperTriangularMatrix = strictlyUpperTriangularMatrix ) ||
235
+ null == (_rhsArray = rhsArray ) ||
236
+ !NumberUtil .IsValid (_omega = omega ))
193
237
{
194
238
throw new Exception ("SuccessiveOverRelaxation Construction => Invalid Inputs" );
195
239
}
196
240
}
197
241
242
+ /**
243
+ * Retrieve the Square Matrix
244
+ *
245
+ * @return Square Matrix
246
+ */
247
+
248
+ public double [][] squareMatrix ()
249
+ {
250
+ return _squareMatrix ;
251
+ }
252
+
198
253
/**
199
254
* Retrieve the Diagonal Matrix
200
255
*
@@ -227,4 +282,112 @@ public double[][] strictlyUpperTriangularMatrix()
227
282
{
228
283
return _strictlyUpperTriangularMatrix ;
229
284
}
285
+
286
+ /**
287
+ * Retrieve the RHS Array
288
+ *
289
+ * @return RHS Array
290
+ */
291
+
292
+ public double [] rhsArray ()
293
+ {
294
+ return _rhsArray ;
295
+ }
296
+
297
+ /**
298
+ * Retrieve the SOR Omega
299
+ *
300
+ * @return SOR Omega
301
+ */
302
+
303
+ public double omega ()
304
+ {
305
+ return _omega ;
306
+ }
307
+
308
+ public void forwardSubstitution (
309
+ final double [] startingUnknownArray )
310
+ {
311
+ if (null == startingUnknownArray || _rhsArray .length != startingUnknownArray .length ) {
312
+ return ;
313
+ }
314
+
315
+ // int iteration = 0;
316
+ double [] previousUnknownArray = startingUnknownArray ;
317
+ double [] updatedUnknownArray = new double [previousUnknownArray .length ];
318
+
319
+ for (int i = 0 ; i < updatedUnknownArray .length ; ++i ) {
320
+ updatedUnknownArray [i ] = Math .random ();
321
+ }
322
+
323
+ try {
324
+ do {
325
+ previousUnknownArray = updatedUnknownArray ;
326
+
327
+ for (int i = 0 ; i < previousUnknownArray .length ; ++i ) {
328
+ updatedUnknownArray [i ] = _rhsArray [i ];
329
+
330
+ for (int j = 0 ; j < previousUnknownArray .length ; ++j ) {
331
+ if (j < i ) {
332
+ updatedUnknownArray [i ] -= _squareMatrix [i ][j ] * updatedUnknownArray [j ];
333
+ } else if (j > i ) {
334
+ updatedUnknownArray [i ] -= _squareMatrix [i ][j ] * previousUnknownArray [j ];
335
+ }
336
+ }
337
+
338
+ updatedUnknownArray [i ] = (1. - _omega ) * previousUnknownArray [i ] + (
339
+ _omega * updatedUnknownArray [i ] / _squareMatrix [i ][i ]
340
+ );
341
+
342
+ System .out .println (i + " => " + previousUnknownArray [i ] + " | " + updatedUnknownArray [i ]);
343
+ }
344
+
345
+ /* String dump = "[Iteration = " + iteration++ + "] {";
346
+
347
+ for (int i = 0; i < previousUnknownArray.length; ++i) {
348
+ dump += FormatUtil.FormatDouble (updatedUnknownArray[i], 2, 4, 1.) + " | " +
349
+ FormatUtil.FormatDouble (previousUnknownArray[i], 2, 4, 1.) + ", ";
350
+ }
351
+
352
+ System.out.println (dump + "}"); */
353
+ } while (!VectorsMatch (previousUnknownArray , updatedUnknownArray ));
354
+ } catch (Exception e ) {
355
+ e .printStackTrace ();
356
+ }
357
+ }
358
+
359
+ public static final void main (
360
+ final String [] argumentArray )
361
+ {
362
+ double [][] squareMatrix = new double [][] {
363
+ { 4. , -1. , -6. , 0. },
364
+ {-5. , -4. , 10. , 8. },
365
+ { 0. , 9. , 4. , -2. },
366
+ { 1. , 0. , -7. , 5. },
367
+ };
368
+
369
+ double [] rhsArray = new double [] {
370
+ 2. ,
371
+ 21. ,
372
+ -12. ,
373
+ -6.
374
+ };
375
+
376
+ double omega = 0.5 ;
377
+
378
+ double [] startingUnknownArray = new double [] {
379
+ 0. ,
380
+ 0. ,
381
+ 0. ,
382
+ 0.
383
+ };
384
+
385
+ SuccessiveOverRelaxation successiveOverRelaxation = SuccessiveOverRelaxation .Standard (
386
+ squareMatrix ,
387
+ rhsArray ,
388
+ omega
389
+ );
390
+
391
+ successiveOverRelaxation .forwardSubstitution (startingUnknownArray );
392
+ }
230
393
}
0 commit comments