source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/circuits/PhasorMatrix.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 45.3 KB
Line 
1/*
2* Class PhasorMatrix
3*
4* Defines a complex matrix and includes the methods
5* needed for standard matrix manipulations, e.g. multiplation,
6* and related procedures, e.g. solution of complex linear
7* simultaneous equations
8*
9* See class ComplexMatrix for rectangular complex matrix manipulations
10*
11* WRITTEN BY: Dr Michael Thomas Flanagan
12*
13* DATE: July 2007
14* AMENDED: 19 April 2008, 14 November 2010
15*
16* DOCUMENTATION:
17* See Michael Thomas Flanagan's Java library on-line web pages:
18* http://www.ee.ucl.ac.uk/~mflanaga/java/PhasorMatrix.html
19* http://www.ee.ucl.ac.uk/~mflanaga/java/
20*
21* Copyright (c) 2007 - 2010
22*
23* PERMISSION TO COPY:
24* Permission to use, copy and modify this software and its documentation for
25* NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
26* to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
27*
28* Dr Michael Thomas Flanagan makes no representations about the suitability
29* or fitness of the software for any or for a particular purpose.
30* Michael Thomas Flanagan shall not be liable for any damages suffered
31* as a result of using, modifying or distributing this software or its derivatives.
32*
33***************************************************************************************/
34
35package agents.anac.y2015.agentBuyogV2.flanagan.circuits;
36
37import agents.anac.y2015.agentBuyogV2.flanagan.complex.Complex;
38import agents.anac.y2015.agentBuyogV2.flanagan.complex.ComplexMatrix;
39import agents.anac.y2015.agentBuyogV2.flanagan.math.Fmath;
40import agents.anac.y2015.agentBuyogV2.flanagan.math.Matrix;
41
42public class PhasorMatrix{
43
44 private int nrow = 0; // number of rows
45 private int ncol = 0; // number of columns
46 private Phasor matrix[][] = null; // 2-D Phasor Matrix
47 private int index[] = null; // row permutation index
48 private double dswap = 1.0D; // row swap index
49 private static final double TINY = 1.0e-30;
50
51/*********************************************************/
52
53 // CONSTRUCTORS
54 // Construct a nrow x ncol matrix of complex variables all equal to zero
55 public PhasorMatrix(int nrow, int ncol){
56 this.nrow = nrow;
57 this.ncol = ncol;
58 this.matrix = Phasor.twoDarray(nrow, ncol);
59 this.index = new int[nrow];
60 for(int i=0;i<nrow;i++)this.index[i]=i;
61 this.dswap=1.0;
62 }
63
64 // Construct a nrow x ncol matrix of complex variables all equal to the complex number const
65 public PhasorMatrix(int nrow, int ncol, Phasor constant){
66 this.nrow = nrow;
67 this.ncol = ncol;
68 this.matrix = Phasor.twoDarray(nrow, ncol, constant);
69 this.index = new int[nrow];
70 for(int i=0;i<nrow;i++)this.index[i]=i;
71 this.dswap=1.0;
72 }
73
74 // Construct matrix with a reference to an existing nrow x ncol 2-D array of complex variables
75 public PhasorMatrix(Phasor[][] twoD){
76 this.nrow = twoD.length;
77 this.ncol = twoD[0].length;
78 for(int i=0; i<nrow; i++){
79 if(twoD[i].length!=ncol)throw new IllegalArgumentException("All rows must have the same length");
80 }
81 this.matrix = twoD;
82 this.index = new int[nrow];
83 for(int i=0;i<nrow;i++)this.index[i]=i;
84 this.dswap=1.0;
85 }
86
87 // Construct matrix with a reference to the 2D matrix and permutation index of an existing PhasorMatrix bb.
88 public PhasorMatrix(PhasorMatrix bb){
89 this.nrow = bb.nrow;
90 this.ncol = bb.ncol;
91 this.matrix = bb.matrix;
92 this.index = bb.index;
93 this.dswap = bb.dswap;
94 }
95
96
97 // SET VALUES
98 // Set the matrix with a copy of an existing nrow x ncol 2-D matrix of Phasor variables
99 public void setTwoDarray(Phasor[][] aarray){
100 if(this.nrow != aarray.length)throw new IllegalArgumentException("row length of this PhasorMatrix differs from that of the 2D array argument");
101 if(this.ncol != aarray[0].length)throw new IllegalArgumentException("column length of this PhasorMatrix differs from that of the 2D array argument");
102 for(int i=0; i<nrow; i++){
103 if(aarray[i].length!=ncol)throw new IllegalArgumentException("All rows must have the same length");
104 for(int j=0; j<ncol; j++){
105 this.matrix[i][j]=Phasor.copy(aarray[i][j]);
106 }
107 }
108 }
109
110 // Set an individual array element
111 // i = row index
112 // j = column index
113 // aa = value of the element
114 public void setElement(int i, int j, Phasor aa){
115 this.matrix[i][j]=Phasor.copy(aa);
116 }
117
118 // Set an individual array element
119 // i = row index
120 // j = column index
121 // aa = magnitude of the element
122 // bb = phase of the element
123 public void setElement(int i, int j, double aa, double bb){
124 this.matrix[i][j].reset(aa, bb);
125 }
126
127 // Set a sub-matrix starting with row index i, column index j
128
129 public void setSubMatrix(int i, int j, Phasor[][] subMatrix){
130 int k = subMatrix.length;
131 int l = subMatrix[0].length;
132 if(i>k)throw new IllegalArgumentException("row indices inverted");
133 if(j>l)throw new IllegalArgumentException("column indices inverted");
134 int n=k-i+1, m=l-j+1;
135 for(int p=0; p<n; p++){
136 for(int q=0; q<m; q++){
137 this.matrix[i+p][j+q] = Phasor.copy(subMatrix[p][q]);
138 }
139 }
140 }
141
142 // Set a sub-matrix starting with row index i, column index j
143 // and ending with row index k, column index l
144 public void setSubMatrix(int i, int j, int k, int l, Phasor[][] subMatrix){
145 if(i+k-1>=this.nrow)throw new IllegalArgumentException("Sub-matrix position is outside the row bounds of this Matrix");
146 if(j+l-1>=this.ncol)throw new IllegalArgumentException("Sub-matrix position is outside the column bounds of this Matrix");
147 int n=k-i+1, m=l-j+1;
148 for(int p=0; p<n; p++){
149 for(int q=0; q<m; q++){
150 this.matrix[i+p][j+q] = Phasor.copy(subMatrix[p][q]);
151 }
152 }
153 }
154
155
156 // Set a sub-matrix
157 // row = array of row indices
158 // col = array of column indices
159 public void setSubMatrix(int[] row, int[] col, Phasor[][] subMatrix){
160 int n=row.length;
161 int m=col.length;
162 for(int p=0; p<n; p++){
163 for(int q=0; q<m; q++){
164 this.matrix[row[p]][col[q]] = Phasor.copy(subMatrix[p][q]);
165 }
166 }
167 }
168
169 // SPECIAL MATRICES
170 // Construct a Phasor identity matrix
171 public static PhasorMatrix identityMatrix(int nrow){
172 PhasorMatrix u = new PhasorMatrix(nrow, nrow);
173 for(int i=0; i<nrow; i++){
174 u.matrix[i][i]=Phasor.plusOne();
175 }
176 return u;
177 }
178
179 // Construct a Phasor scalar matrix
180 public static PhasorMatrix scalarMatrix(int nrow, Phasor diagconst){
181 PhasorMatrix u = new PhasorMatrix(nrow, nrow);
182 Phasor[][] uarray = u.getArrayReference();
183 for(int i=0; i<nrow; i++){
184 for(int j=i; j<nrow; j++){
185 if(i==j){
186 uarray[i][j]=Phasor.copy(diagconst);
187 }
188 }
189 }
190 return u;
191 }
192
193 // Construct a Phasor diagonal matrix
194 public static PhasorMatrix diagonalMatrix(int nrow, Phasor[] diag){
195 if(diag.length!=nrow)throw new IllegalArgumentException("matrix dimension differs from diagonal array length");
196 PhasorMatrix u = new PhasorMatrix(nrow, nrow);
197 Phasor[][] uarray = u.getArrayReference();
198 for(int i=0; i<nrow; i++){
199 for(int j=i; j<nrow; j++){
200 if(i==j){
201 uarray[i][j]=Phasor.copy(diag[i]);
202 }
203 }
204 }
205 return u;
206 }
207
208 // COLUMN MATRICES
209 // Converts a 1-D array of Phasor to a column matrix
210 public static PhasorMatrix columnMatrix(Phasor[] darray){
211 int nr = darray.length;
212 PhasorMatrix pp = new PhasorMatrix(nr, 1);
213 for(int i=0; i<nr; i++)pp.matrix[i][0] = darray[i];
214 return pp;
215 }
216
217 // ROW MATRICES
218 // Converts a 1-D array of Phasor to a row matrix
219 public static PhasorMatrix rowMatrix(Phasor[] darray){
220 int nc = darray.length;
221 PhasorMatrix pp = new PhasorMatrix(1, nc);
222 for(int i=0; i<nc; i++)pp.matrix[0][i] = darray[i];
223 return pp;
224 }
225
226
227 // GET VALUES
228 // Return the number of rows
229 public int getNrow(){
230 return this.nrow;
231 }
232
233 // Return the number of columns
234 public int getNcol(){
235 return this.ncol;
236 }
237
238 // Return a reference to the internal 2-D array
239 public Phasor[][] getArrayReference(){
240 return this.matrix;
241 }
242
243 // Return a reference to the internal 2-D array
244 public Phasor[][] getArray(){
245 return this.matrix;
246 }
247
248 // Return a reference to the internal 2-D array
249 // included for backward compatibility with earlier incorrect documentation
250 public Phasor[][] getArrayPointer(){
251 return this.matrix;
252 }
253
254 // Return a copy of the internal 2-D array
255 public Phasor[][] getArrayCopy(){
256 Phasor[][] c = new Phasor[this.nrow][this.ncol];
257 for(int i=0; i<nrow; i++){
258 for(int j=0; j<ncol; j++){
259 c[i][j]=Phasor.copy(matrix[i][j]);
260 }
261 }
262 return c;
263 }
264
265 // Return a single element of the internal 2-D array
266 public Phasor getElementReference(int i, int j){
267 return this.matrix[i][j];
268 }
269
270 // Return a reference to a single element of the internal 2-D array
271 // included for backward compatibility with earlier incorrect documentation
272 public Phasor getElementPointer(int i, int j){
273 return this.matrix[i][j];
274 }
275
276 // Return a copy of a single element of the internal 2-D array
277 public Phasor getElementCopy(int i, int j){
278 return Phasor.copy(this.matrix[i][j]);
279 }
280
281 // Return a sub-matrix starting with row index i, column index j
282 // and ending with column index k, row index l
283 public PhasorMatrix getSubMatrix(int i, int j, int k, int l){
284 if(i+k-1>=this.nrow)throw new IllegalArgumentException("Sub-matrix position is outside the row bounds of this Matrix");
285 if(j+l-1>=this.ncol)throw new IllegalArgumentException("Sub-matrix position is outside the column bounds of this Matrix");
286
287 int n=k-i+1, m=l-j+1;
288 PhasorMatrix subMatrix = new PhasorMatrix(n, m);
289 Phasor[][] sarray = subMatrix.getArrayReference();
290 for(int p=0; p<n; p++){
291 for(int q=0; q<m; q++){
292 sarray[p][q]=Phasor.copy(this.matrix[i+p][j+q]);
293 }
294 }
295 return subMatrix;
296 }
297
298 // Return a sub-matrix
299 // row = array of row indices
300 // col = array of column indices
301 public PhasorMatrix getSubMatrix(int[] row, int[] col){
302 int n = row.length;
303 int m = col.length;
304 PhasorMatrix subMatrix = new PhasorMatrix(n, m);
305 Phasor[][] sarray = subMatrix.getArrayReference();
306 for(int i=0; i<n; i++){
307 for(int j=0; j<m; j++){
308 sarray[i][j]=Phasor.copy(this.matrix[row[i]][col[j]]);
309 }
310 }
311 return subMatrix;
312 }
313
314 // Return a reference to the permutation index array
315 public int[] getIndexReference(){
316 return this.index;
317 }
318
319 // Return a reference to the permutation index array
320 public int[] getIndexPointer(){
321 return this.index;
322 }
323
324 // Return a copy of the permutation index array
325 public int[] getIndexCopy(){
326 int[] indcopy = new int[this.nrow];
327 for(int i=0; i<this.nrow; i++){
328 indcopy[i]=this.index[i];
329 }
330 return indcopy;
331 }
332
333 // Return the row swap index
334 public double getSwap(){
335 return this.dswap;
336 }
337
338 // COPY
339 // Copy a PhasorMatrix [static method]
340 public static PhasorMatrix copy(PhasorMatrix a){
341 if(a==null){
342 return null;
343 }
344 else{
345 int nr = a.getNrow();
346 int nc = a.getNcol();
347 Phasor[][] aarray = a.getArrayReference();
348 PhasorMatrix b = new PhasorMatrix(nr,nc);
349 b.nrow = nr;
350 b.ncol = nc;
351 Phasor[][] barray = b.getArrayReference();
352 for(int i=0; i<nr; i++){
353 for(int j=0; j<nc; j++){
354 barray[i][j]=Phasor.copy(aarray[i][j]);
355 }
356 }
357 for(int i=0; i<nr; i++)b.index[i] = a.index[i];
358 return b;
359 }
360 }
361
362 // Copy a PhasorMatrix [instance method]
363 public PhasorMatrix copy(){
364 if(this==null){
365 return null;
366 }
367 else{
368 int nr = this.nrow;
369 int nc = this.ncol;
370 PhasorMatrix b = new PhasorMatrix(nr,nc);
371 Phasor[][] barray = b.getArrayReference();
372 b.nrow = nr;
373 b.ncol = nc;
374 for(int i=0; i<nr; i++){
375 for(int j=0; j<nc; j++){
376 barray[i][j]=Phasor.copy(this.matrix[i][j]);
377 }
378 }
379 for(int i=0; i<nr; i++)b.index[i] = this.index[i];
380 return b;
381 }
382 }
383
384 // Clone a PhasorMatrix
385 public Object clone(){
386 if(this==null){
387 return null;
388 }
389 else{
390 int nr = this.nrow;
391 int nc = this.ncol;
392 PhasorMatrix b = new PhasorMatrix(nr,nc);
393 Phasor[][] barray = b.getArrayReference();
394 b.nrow = nr;
395 b.ncol = nc;
396 for(int i=0; i<nr; i++){
397 for(int j=0; j<nc; j++){
398 barray[i][j]=Phasor.copy(this.matrix[i][j]);
399 }
400 }
401 for(int i=0; i<nr; i++)b.index[i] = this.index[i];
402 return (Object) b;
403 }
404 }
405
406 // CONVERSIONS
407 // Converts a 1-D array of Phasors to a row phasor matrix
408 public static PhasorMatrix toPhasorRowMatrix(Phasor[] parray){
409 int nc = parray.length;
410 PhasorMatrix pp = new PhasorMatrix(1, nc);
411 for(int i=0; i<nc; i++)pp.matrix[0][i] = parray[i].copy();
412 return pp;
413 }
414
415 // Converts a 1-D array of Complex to a row phasor matrix
416 public static PhasorMatrix toPhasorRowMatrix(Complex[] carray){
417 int nc = carray.length;
418 PhasorMatrix pp = new PhasorMatrix(1, nc);
419 for(int i=0; i<nc; i++)pp.matrix[0][i] = Phasor.toPhasor(carray[i]).copy();
420 return pp;
421 }
422
423 // Converts a 1-D array of doubles to a row phasor matrix
424 public static PhasorMatrix toPhasorRowMatrix(double[] darray){
425 int nc = darray.length;
426 PhasorMatrix pp = new PhasorMatrix(1, nc);
427 for(int i=0; i<nc; i++)pp.matrix[0][i] = new Phasor(darray[i], 0.0D);
428 return pp;
429 }
430
431 // Converts a 1-D array of Phasors to a column phasor matrix
432 public static PhasorMatrix toPhasorColumnMatrix(Phasor[] parray){
433 int nr = parray.length;
434 PhasorMatrix pp = new PhasorMatrix(nr, 1);
435 for(int i=0; i<nr; i++)pp.matrix[i][0] = parray[i].copy();
436 return pp;
437 }
438
439 // Converts a 1-D array of Complex to a column phasor matrix
440 public static PhasorMatrix toPhasorColumnMatrix(Complex[] carray){
441 int nr = carray.length;
442 PhasorMatrix pp = new PhasorMatrix(nr, 1);
443 for(int i=0; i<nr; i++)pp.matrix[i][0] = Phasor.toPhasor(carray[i]).copy();
444 return pp;
445 }
446
447 // Converts a 1-D array of doubles to a column phasor matrix
448 public static PhasorMatrix toPhasorColumnMatrix(double[] darray){
449 int nr = darray.length;
450 PhasorMatrix pp = new PhasorMatrix(nr, 1);
451 for(int i=0; i<nr; i++)pp.matrix[i][0] = new Phasor(darray[i], 0.0D);
452 return pp;
453 }
454
455 // Converts a complex matrix (ComplexMatrix) to a phasor matrix (PhasorMatix)
456 public static PhasorMatrix toPhasorMatrix(ComplexMatrix cc){
457 PhasorMatrix pp = new PhasorMatrix(cc.getNrow(), cc.getNcol() );
458 pp.index = cc.getIndexCopy();
459 pp.dswap = cc.getSwap();
460 for(int i=0; i<pp.nrow; i++){
461 for(int j=0; j<pp.ncol; i++){
462 pp.matrix[i][j] = Phasor.toPhasor(cc.getElementCopy(i,j));
463 }
464 }
465 return pp;
466 }
467
468 // Converts a 2D complex array to a phasor matrix (PhasorMatix)
469 public static PhasorMatrix toPhasorMatrix(Complex[][] carray){
470 ComplexMatrix cc = new ComplexMatrix(carray);
471 PhasorMatrix pp = new PhasorMatrix(cc.getNrow(), cc.getNcol() );
472 for(int i=0; i<pp.nrow; i++){
473 for(int j=0; j<pp.ncol; i++){
474 pp.matrix[i][j] = Phasor.toPhasor(cc.getElementCopy(i,j));
475 }
476 }
477 return pp;
478 }
479
480 // Converts a matrix of doubles (Matrix) to a phasor matrix (PhasorMatix)
481 public static PhasorMatrix toPhasorMatrix(Matrix marray){
482 int nr = marray.getNrow();
483 int nc = marray.getNcol();
484
485 PhasorMatrix pp = new PhasorMatrix(nr, nc);
486 for(int i=0; i<nr; i++){
487 for(int j=0; j<nc; j++){
488 pp.matrix[i][j].reset(marray.getElementCopy(i, j), 0.0D);
489 }
490 }
491 return pp;
492 }
493
494 // Converts a 2D array of doubles to a phasor matrix (PhasorMatix)
495 public static PhasorMatrix toPhasorMatrix(double[][] darray){
496 int nr = darray.length;
497 int nc = darray[0].length;
498 for(int i=1; i<nr; i++){
499 if(darray[i].length!=nc)throw new IllegalArgumentException("All rows must have the same length");
500 }
501 PhasorMatrix pp = new PhasorMatrix(nr, nc);
502 for(int i=0; i<pp.nrow; i++){
503 for(int j=0; j<pp.ncol; j++){
504 pp.matrix[i][j].reset(darray[i][j], 0.0D);
505 }
506 }
507 return pp;
508 }
509
510 // Converts a phasor matrix (PhasorMatix) to a complex matrix (ComplexMatrix) - instance method
511 public ComplexMatrix toComplexMatrix(){
512 int nr = this.getNrow();
513 int nc = this.getNcol();
514 ComplexMatrix cc = new ComplexMatrix(nr, nc);
515 for(int i=0; i<nr; i++){
516 for(int j=0; j<nc; i++){
517 cc.setElement(i, j, this.matrix[i][j].toRectangular());
518 }
519 }
520 return cc;
521 }
522
523 // Converts a phasor matrix (PhasorMatix) to a complex matrix (ComplexMatrix) - static method
524 public static ComplexMatrix toComplexMatrix(PhasorMatrix pp){
525 int nr = pp.getNrow();
526 int nc = pp.getNcol();
527 ComplexMatrix cc = new ComplexMatrix(nr, nc);
528 for(int i=0; i<nr; i++){
529 for(int j=0; j<nc; i++){
530 cc.setElement(i, j, pp.matrix[i][j].toRectangular());
531 }
532 }
533 return cc;
534 }
535
536
537 // ADDITION
538 // Add this matrix to matrix B. This matrix remains unaltered
539 public PhasorMatrix plus(PhasorMatrix bmat){
540 if((this.nrow!=bmat.nrow)||(this.ncol!=bmat.ncol)){
541 throw new IllegalArgumentException("Array dimensions do not agree");
542 }
543 int nr=bmat.nrow;
544 int nc=bmat.ncol;
545 PhasorMatrix cmat = new PhasorMatrix(nr,nc);
546 Phasor[][] carray = cmat.getArrayReference();
547 for(int i=0; i<nr; i++){
548 for(int j=0; j<nc; j++){
549 carray[i][j]=this.matrix[i][j].plus(bmat.matrix[i][j]);
550 }
551 }
552 return cmat;
553 }
554
555 // Add this matrix to a Phasor 2-D array.
556 public PhasorMatrix plus(Phasor[][] bmat){
557 int nr=bmat.length;
558 int nc=bmat[0].length;
559 if((this.nrow!=nr)||(this.ncol!=nc)){
560 throw new IllegalArgumentException("Array dimensions do not agree");
561 }
562 PhasorMatrix cmat = new PhasorMatrix(nr,nc);
563 Phasor[][] carray = cmat.getArrayReference();
564 for(int i=0; i<nr; i++){
565 for(int j=0; j<nc; j++){
566 carray[i][j]=this.matrix[i][j].plus(bmat[i][j]);
567 }
568 }
569 return cmat;
570 }
571
572 // Add this PhasorMatrix to a ComplexMatrix.
573 public PhasorMatrix plus(ComplexMatrix bmat){
574 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
575 return this.plus(pmat);
576 }
577
578 // Add this PhasorMatrix to a 2D array of Complex.
579 public PhasorMatrix plus(Complex[][] bmat){
580 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
581 return this.plus(pmat);
582 }
583
584 // Add this PhasorMatrix to a Matrix.
585 public PhasorMatrix plus(Matrix bmat){
586 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
587 return this.plus(pmat);
588 }
589
590 // Add this PhasorMatrix to a 2D array of double.
591 public PhasorMatrix plus(double[][] bmat){
592 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
593 return this.plus(pmat);
594 }
595
596 // Add a PhasorMatrix to this matrix [equivalence of +=]
597 public void plusEquals(PhasorMatrix bmat){
598 if((this.nrow!=bmat.nrow)||(this.ncol!=bmat.ncol)){
599 throw new IllegalArgumentException("Array dimensions do not agree");
600 }
601 int nr=bmat.nrow;
602 int nc=bmat.ncol;
603
604 for(int i=0; i<nr; i++){
605 for(int j=0; j<nc; j++){
606 this.matrix[i][j].plusEquals(bmat.matrix[i][j]);
607 }
608 }
609 }
610
611 // Add a 2D array of Phasors to this matrix [equivalence of +=]
612 public void plusEquals(Phasor[][] bmat){
613 PhasorMatrix pmat = new PhasorMatrix(bmat);
614 this.plusEquals(pmat);
615 }
616
617 // Add a ComplexMatrix of complex to this matrix [equivalence of +=]
618 public void plusEquals(ComplexMatrix bmat){
619 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
620 this.plusEquals(pmat);
621 }
622
623 // Add a 2D array of complex to this matrix [equivalence of +=]
624 public void plusEquals(Complex[][] bmat){
625 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
626 this.plusEquals(pmat);
627 }
628
629 // Add a Matrix to this PhasorMatrix [equivalence of +=]
630 public void plusEquals(Matrix bmat){
631 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
632 this.plusEquals(pmat);
633 }
634
635 // Add a 2D array of doubles to this matrix [equivalence of +=]
636 public void plusEquals(double[][] bmat){
637 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
638 this.plusEquals(pmat);
639 }
640
641 // SUBTRACTION
642 // Subtract matrix B from this matrix. This matrix remains unaltered [instance method]
643 public PhasorMatrix minus(PhasorMatrix bmat){
644 if((this.nrow!=bmat.nrow)||(this.ncol!=bmat.ncol)){
645 throw new IllegalArgumentException("Array dimensions do not agree");
646 }
647 int nr=this.nrow;
648 int nc=this.ncol;
649 PhasorMatrix cmat = new PhasorMatrix(nr,nc);
650 Phasor[][] carray = cmat.getArrayReference();
651 for(int i=0; i<nr; i++){
652 for(int j=0; j<nc; j++){
653 carray[i][j]=this.matrix[i][j].minus(bmat.matrix[i][j]);
654 }
655 }
656 return cmat;
657 }
658
659 // Subtract Phasor 2-D array from this matrix. [instance method]
660 public PhasorMatrix minus(Phasor[][] bmat){
661 int nr=bmat.length;
662 int nc=bmat[0].length;
663 if((this.nrow!=nr)||(this.ncol!=nc)){
664 throw new IllegalArgumentException("Array dimensions do not agree");
665 }
666 PhasorMatrix cmat = new PhasorMatrix(nr,nc);
667 Phasor[][] carray = cmat.getArrayReference();
668 for(int i=0; i<nr; i++){
669 for(int j=0; j<nc; j++){
670 carray[i][j]=this.matrix[i][j].minus(bmat[i][j]);
671 }
672 }
673 return cmat;
674 }
675
676 // Subtract a ComplexMatrix from this PhasorMatrix
677 public PhasorMatrix minus(ComplexMatrix bmat){
678 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
679 return this.minus(pmat);
680 }
681
682 // Subtract a 2D array of Complex from this PhasorMatrix.
683 public PhasorMatrix minus(Complex[][] bmat){
684 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
685 return this.minus(pmat);
686 }
687
688 // Subtract a Matrix from this PhasorMatrix.
689 public PhasorMatrix minus(Matrix bmat){
690 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
691 return this.minus(pmat);
692 }
693
694 // Subtract a 2D array of doubles from this PhasorMatrix.
695 public PhasorMatrix minus(double[][] bmat){
696 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
697 return this.minus(pmat);
698 }
699
700
701 // Subtract a PhasorMatrix from this matrix [equivalence of -=]
702 public void minusEquals(PhasorMatrix bmat){
703 if((this.nrow!=bmat.nrow)||(this.ncol!=bmat.ncol)){
704 throw new IllegalArgumentException("Array dimensions do not agree");
705 }
706 int nr=bmat.nrow;
707 int nc=bmat.ncol;
708
709 for(int i=0; i<nr; i++){
710 for(int j=0; j<nc; j++){
711 this.matrix[i][j].minusEquals(bmat.matrix[i][j]);
712 }
713 }
714 }
715
716 // Subtract a 2D array of Phasors from this matrix [equivalence of -=]
717 public void minusEquals(Phasor[][] bmat){
718 PhasorMatrix pmat = new PhasorMatrix(bmat);
719 this.minusEquals(pmat);
720 }
721
722 // Subtract a ComplexMatrix from this matrix [equivalence of -=]
723 public void minusEquals(ComplexMatrix bmat){
724 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
725 this.minusEquals(pmat);
726 }
727
728 // Subtract a 2D array of complex from this matrix [equivalence of -=]
729 public void minusEquals(Complex[][] bmat){
730 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
731 this.minusEquals(pmat);
732 }
733
734 // Subtract a Matrix from this phasorMatrix [equivalence of -=]
735 public void minusEquals(Matrix bmat){
736 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
737 this.minusEquals(pmat);
738 }
739
740 // Subtract a 2D array of doubles from this matrix [equivalence of -=]
741 public void minusEquals(double[][] bmat){
742 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
743 this.minusEquals(pmat);
744 }
745
746 // MULTIPLICATION
747 // Multiply this Phasor matrix by a Phasor matrix.
748 // This matrix remains unaltered.
749 public PhasorMatrix times(PhasorMatrix bmat){
750 if(this.ncol!=bmat.nrow)throw new IllegalArgumentException("Nonconformable matrices");
751
752 PhasorMatrix cmat = new PhasorMatrix(this.nrow, bmat.ncol);
753 Phasor [][] carray = cmat.getArrayReference();
754 Phasor sum = new Phasor();
755
756 for(int i=0; i<this.nrow; i++){
757 for(int j=0; j<bmat.ncol; j++){
758 sum=Phasor.zero();
759 for(int k=0; k<this.ncol; k++){
760 sum.plusEquals(this.matrix[i][k].times(bmat.matrix[k][j]));
761 }
762 carray[i][j]=Phasor.copy(sum);
763 }
764 }
765 return cmat;
766 }
767
768 // Multiply this Phasor matrix by a Phasor 2-D array.
769 public PhasorMatrix times(Phasor[][] bmat){
770 int nr=bmat.length;
771 int nc=bmat[0].length;
772 if(this.ncol!=nr)throw new IllegalArgumentException("Nonconformable matrices");
773
774 PhasorMatrix cmat = new PhasorMatrix(this.nrow, nc);
775 Phasor [][] carray = cmat.getArrayReference();
776 Phasor sum = new Phasor();
777
778 for(int i=0; i<this.nrow; i++){
779 for(int j=0; j<nc; j++){
780 sum=Phasor.zero();
781 for(int k=0; k<this.ncol; k++){
782 sum.plusEquals(this.matrix[i][k].times(bmat[k][j]));
783 }
784 carray[i][j]=Phasor.copy(sum);
785 }
786 }
787 return cmat;
788 }
789
790 // Multiply a ComplexMatrix by this PhasorMatrix
791 public PhasorMatrix times(ComplexMatrix bmat){
792 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
793 return this.times(pmat);
794 }
795
796 // Multiply a 2D array of Complex by this PhasorMatrix.
797 public PhasorMatrix times(Complex[][] bmat){
798 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
799 return this.times(pmat);
800 }
801
802 // Multiply a Matrix by this PhasorMatrix.
803 public PhasorMatrix times(Matrix bmat){
804 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
805 return this.times(pmat);
806 }
807
808 // Multiply a 2D array of doubles by this PhasorMatrix.
809 public PhasorMatrix times(double[][] bmat){
810 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
811 return this.times(pmat);
812 }
813
814 // Multiply this Phasor matrix by a Phasor constant
815 // This matrix remains unaltered
816 public PhasorMatrix times(Phasor constant){
817 PhasorMatrix cmat = new PhasorMatrix(this.nrow, this.ncol);
818 Phasor [][] carray = cmat.getArrayReference();
819
820 for(int i=0; i<this.nrow; i++){
821 for(int j=0; j<this.ncol; j++){
822 carray[i][j] = this.matrix[i][j].times(constant);
823 }
824 }
825 return cmat;
826 }
827
828 // Multiply this Phasor matrix by a real (double) constant
829 // This matrix remains unaltered.
830 public PhasorMatrix times(double constant){
831 PhasorMatrix cmat = new PhasorMatrix(this.nrow, this.ncol);
832 Phasor [][] carray = cmat.getArrayReference();
833 Phasor cconstant = new Phasor(constant, 0.0);
834
835 for(int i=0; i<this.nrow; i++){
836 for(int j=0; j<this.ncol; j++){
837 carray[i][j] = this.matrix[i][j].times(cconstant);
838 }
839 }
840 return cmat;
841 }
842
843
844 // Multiply this matrix by a Phasor matrix [equivalence of *=]
845 public void timesEquals(PhasorMatrix bmat){
846 if(this.ncol!=bmat.nrow)throw new IllegalArgumentException("Nonconformable matrices");
847
848 Phasor sum = new Phasor();
849
850 for(int i=0; i<this.nrow; i++){
851 for(int j=0; j<bmat.ncol; j++){
852 sum=Phasor.zero();
853 for(int k=0; k<this.ncol; k++){
854 sum.plusEquals(this.matrix[i][k].times(bmat.matrix[k][j]));
855 }
856 this.matrix[i][j] = Phasor.copy(sum);
857 }
858 }
859 }
860
861 // Multiply a 2D array of Phasors by this matrix [equivalence of *=]
862 public void timesEquals(Phasor[][] bmat){
863 PhasorMatrix pmat = new PhasorMatrix(bmat);
864 this.timesEquals(pmat);
865 }
866
867 // Multiply a ComplexMatrix of complex by this matrix [equivalence of *=]
868 public void timesEquals(ComplexMatrix bmat){
869 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
870 this.timesEquals(pmat);
871 }
872
873 // Multiply a 2D array of complex by this matrix [equivalence of *=]
874 public void timesEquals(Complex[][] bmat){
875 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
876 this.timesEquals(pmat);
877 }
878
879 // Multiply a Matrix by this PhasorMatrix [equivalence of *=]
880 public void timesEquals(Matrix bmat){
881 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
882 this.timesEquals(pmat);
883 }
884
885 // Multiply a 2D array of doubles by this matrix [equivalence of *=]
886 public void timesEquals(double[][] bmat){
887 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
888 this.timesEquals(pmat);
889 }
890
891 // Multiply this matrix by a Phasor constant [equivalence of *=]
892 public void timesEquals(Phasor constant){
893
894 for(int i=0; i<this.nrow; i++){
895 for(int j=0; j<this.ncol; j++){
896 this.matrix[i][j].timesEquals(constant);
897 }
898 }
899 }
900
901 // Multiply this matrix by a Complex constant [equivalence of *=]
902 public void timesEquals(Complex constant){
903
904 for(int i=0; i<this.nrow; i++){
905 for(int j=0; j<this.ncol; j++){
906 this.matrix[i][j].timesEquals(constant);
907 }
908 }
909 }
910
911 // Multiply this matrix by a real (double) constant [equivalence of *=]
912 public void timesEquals(double constant){
913 Phasor cconstant = new Phasor(constant, 0.0);
914
915 for(int i=0; i<this.nrow; i++){
916 for(int j=0; j<this.ncol; j++){
917 this.matrix[i][j].timesEquals(cconstant);
918 }
919 }
920 }
921
922 // Multiply this matrix by a real integer(int) constant [equivalence of *=]
923 public void timesEquals(int constant){
924 Phasor cconstant = new Phasor((double)constant, 0.0);
925
926 for(int i=0; i<this.nrow; i++){
927 for(int j=0; j<this.ncol; j++){
928 this.matrix[i][j].timesEquals(cconstant);
929 }
930 }
931 }
932
933 // DIVISION
934 // Divide this PhasorMatrix by a PhasorMatrix.
935 public PhasorMatrix over(PhasorMatrix bmat){
936 if((this.nrow!=bmat.nrow)||(this.ncol!=bmat.ncol)){
937 throw new IllegalArgumentException("Array dimensions do not agree");
938 }
939 return this.times(bmat.inverse());
940 }
941
942 // Divide this matrix by a Phasor 2-D array.
943 public PhasorMatrix over(Phasor[][] bmat){
944 int nr=bmat.length;
945 int nc=bmat[0].length;
946 if((this.nrow!=nr)||(this.ncol!=nc)){
947 throw new IllegalArgumentException("Array dimensions do not agree");
948 }
949
950 PhasorMatrix cmat = new PhasorMatrix(bmat);
951 return this.times(cmat.inverse());
952 }
953
954 // Divide this PhasorMatrix by a ComplexMatrix.
955 public PhasorMatrix over(ComplexMatrix bmat){
956 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
957 return this.over(pmat);
958 }
959
960 // Divide this PhasorMatrix by a 2D array of Complex.
961 public PhasorMatrix over(Complex[][] bmat){
962 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
963 return this.over(pmat);
964 }
965
966 // Divide this PhasorMatrix by a Matrix.
967 public PhasorMatrix over(Matrix bmat){
968 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
969 return this.over(pmat);
970 }
971
972 // Divide this PhasorMatrix by a 2D array of double.
973 public PhasorMatrix over(double[][] bmat){
974 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
975 return this.over(pmat);
976 }
977
978 // Divide this matrix by a PhasorMatrix [equivalence of /=]
979 public void overEquals(PhasorMatrix bmat){
980 if((this.nrow!=bmat.nrow)||(this.ncol!=bmat.ncol)){
981 throw new IllegalArgumentException("Array dimensions do not agree");
982 }
983 PhasorMatrix cmat = new PhasorMatrix(bmat);
984 this.timesEquals(cmat.inverse());
985 }
986
987 // Divide this matrix by a 2D array of Phasors [equivalence of /=]
988 public void overEquals(Phasor[][] bmat){
989 PhasorMatrix pmat = new PhasorMatrix(bmat);
990 this.overEquals(pmat);
991 }
992
993 // Divide this matrix by a ComplexMatrix [equivalence of /=]
994 public void overEquals(ComplexMatrix bmat){
995 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
996 this.overEquals(pmat);
997 }
998
999 // Divide this matrix by a 2D array of complex [equivalence of /=]
1000 public void overEquals(Complex[][] bmat){
1001 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
1002 this.overEquals(pmat);
1003 }
1004
1005 // Divide this PhasorMatrix a Matrix [equivalence of /=]
1006 public void overEquals(Matrix bmat){
1007 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
1008 this.overEquals(pmat);
1009 }
1010
1011 // Divide this matrix by a 2D array of doubles [equivalence of /=]
1012 public void overEquals(double[][] bmat){
1013 PhasorMatrix pmat = PhasorMatrix.toPhasorMatrix(bmat);
1014 this.overEquals(pmat);
1015 }
1016
1017 // INVERSE
1018 // Inverse of a square Phasor matrix
1019 public PhasorMatrix inverse(){
1020 int n = this.nrow;
1021 if(n!=this.ncol)throw new IllegalArgumentException("Matrix is not square");
1022 Phasor[] col = new Phasor[n];
1023 Phasor[] xvec = new Phasor[n];
1024 PhasorMatrix invmat = new PhasorMatrix(n, n);
1025 Phasor[][] invarray = invmat.getArrayReference();
1026 PhasorMatrix ludmat;
1027
1028 ludmat = this.luDecomp();
1029 for(int j=0; j<n; j++){
1030 for(int i=0; i<n; i++)col[i]=Phasor.zero();
1031 col[j]=Phasor.plusOne();
1032 xvec=ludmat.luBackSub(col);
1033 for(int i=0; i<n; i++)invarray[i][j]=Phasor.copy(xvec[i]);
1034 }
1035 return invmat;
1036 }
1037
1038
1039
1040 // TRANSPOSE
1041 // Transpose of a Phasor matrix
1042 public PhasorMatrix transpose(){
1043 PhasorMatrix tmat = new PhasorMatrix(this.ncol, this.nrow);
1044 Phasor[][] tarray = tmat.getArrayReference();
1045 for(int i=0; i<this.ncol; i++){
1046 for(int j=0; j<this.nrow; j++){
1047 tarray[i][j]=Phasor.copy(this.matrix[j][i]);
1048 }
1049 }
1050 return tmat;
1051 }
1052
1053
1054 // COMPLEX CONJUGATE
1055 // Complex Conjugate of a Phasor matrix
1056 public PhasorMatrix conjugate(){
1057 PhasorMatrix conj = PhasorMatrix.copy(this);
1058 for(int i=0; i<this.nrow; i++){
1059 for(int j=0; j<this.ncol; j++){
1060 conj.matrix[i][j]=this.matrix[i][j].conjugate();
1061 }
1062 }
1063 return conj;
1064 }
1065
1066
1067 // ADJOIN
1068 // Adjoin of a Phasor matrix
1069 public PhasorMatrix adjoin(){
1070 PhasorMatrix adj = PhasorMatrix.copy(this);
1071 adj=adj.transpose();
1072 adj=adj.conjugate();
1073 return adj;
1074 }
1075
1076
1077 // OPPOSITE
1078 // Opposite of a Phasor matrix
1079 public PhasorMatrix opposite(){
1080 PhasorMatrix opp = PhasorMatrix.copy(this);
1081 for(int i=0; i<this.nrow; i++){
1082 for(int j=0; j<this.ncol; j++){
1083 opp.matrix[i][j]=this.matrix[i][j].times(Phasor.minusOne());
1084 }
1085 }
1086 return opp;
1087 }
1088
1089
1090 // TRACE
1091 // Trace of a Phasor matrix
1092 public Phasor trace(){
1093 Phasor trac = new Phasor(0.0, 0.0);
1094 for(int i=0; i<Math.min(this.ncol,this.ncol); i++){
1095 trac.plusEquals(this.matrix[i][i]);
1096 }
1097 return trac;
1098 }
1099
1100 // DETERMINANT
1101 // Returns the determinant of a Phasor square matrix
1102 public Phasor determinant(){
1103 int n = this.nrow;
1104 if(n!=this.ncol)throw new IllegalArgumentException("Matrix is not square");
1105 Phasor det = new Phasor();
1106 PhasorMatrix ludmat;
1107
1108 ludmat = this.luDecomp();
1109 det.reset(ludmat.dswap,0.0);
1110 for(int j=0; j<n; j++){
1111 det.timesEquals(ludmat.matrix[j][j]);
1112 }
1113 return det;
1114 }
1115
1116 // Returns the log(determinant) of a Phasor square matrix
1117 // Useful if determinant() underflows or overflows.
1118 public Phasor logDeterminant(){
1119 int n = this.nrow;
1120 if(n!=this.ncol)throw new IllegalArgumentException("Matrix is not square");
1121 Phasor det = new Phasor();
1122 PhasorMatrix ludmat;
1123
1124 ludmat = this.luDecomp();
1125 det.reset(ludmat.dswap,0.0);
1126 det=Phasor.log(det);
1127 for(int j=0; j<n; j++){
1128 det.plusEquals(Phasor.log(ludmat.matrix[j][j]));
1129 }
1130 return det;
1131 }
1132
1133
1134 // FROBENIUS (EUCLIDEAN) NORM of a Phasor matrix
1135 public double frobeniusNorm(){
1136 double norm=0.0D;
1137 for(int i=0; i<this.nrow; i++){
1138 for(int j=0; j<this.ncol; j++){
1139 norm=Fmath.hypot(norm, matrix[i][j].abs());
1140 }
1141 }
1142 return norm;
1143 }
1144
1145 // ONE NORM of a Phasor matrix
1146 public double oneNorm(){
1147 double norm=0.0D;
1148 double sum = 0.0D;
1149 for(int i=0; i<this.nrow; i++){
1150 sum=0.0D;
1151 for(int j=0; j<this.ncol; j++){
1152 sum+=this.matrix[i][j].abs();
1153 }
1154 norm=Math.max(norm,sum);
1155 }
1156 return norm;
1157 }
1158
1159 // INFINITY NORM of a Phasor matrix
1160 public double infinityNorm(){
1161 double norm=0.0D;
1162 double sum=0.0D;
1163 for(int i=0; i<this.nrow; i++){
1164 sum=0.0D;
1165 for(int j=0; j<this.ncol; j++){
1166 sum+=this.matrix[i][j].abs();
1167 }
1168 norm=Math.max(norm,sum);
1169 }
1170 return norm;
1171 }
1172
1173
1174 // LU DECOMPOSITION OF COMPLEX MATRIX A
1175 // For details of LU decomposition
1176 // See Numerical Recipes, The Art of Scientific Computing
1177 // by W H Press, S A Teukolsky, W T Vetterling & B P Flannery
1178 // Cambridge University Press, http://www.nr.com/
1179 // PhasorMatrix ludmat is the returned LU decompostion
1180 // int[] index is the vector of row permutations
1181 // dswap returns +1.0 for even number of row interchanges
1182 // returns -1.0 for odd number of row interchanges
1183 public PhasorMatrix luDecomp(){
1184 if(this.nrow!=this.ncol)throw new IllegalArgumentException("A matrix is not square");
1185 int n=this.nrow;
1186 int imax=0;
1187 double dum=0.0D, temp=0.0D, big=0.0D;
1188 double[] vv = new double[n];
1189 Phasor sum = new Phasor();
1190 Phasor dumm = new Phasor();
1191
1192 PhasorMatrix ludmat=PhasorMatrix.copy(this);
1193 Phasor[][] ludarray = ludmat.getArrayReference();
1194
1195 ludmat.dswap=1.0;
1196 for (int i=0;i<n;i++) {
1197 big=0.0;
1198 for (int j=0;j<n;j++){
1199 if ((temp=ludarray[i][j].abs()) > big) big=temp;
1200 }
1201 if (big == 0.0) throw new ArithmeticException("Singular matrix");
1202 vv[i]=1.0/big;
1203 }
1204 for (int j=0;j<n;j++) {
1205 for (int i=0;i<j;i++) {
1206 sum=Phasor.copy(ludarray[i][j]);
1207 for (int k=0;k<i;k++) sum.minusEquals(ludarray[i][k].times(ludarray[k][j]));
1208 ludarray[i][j]=Phasor.copy(sum);
1209 }
1210 big=0.0;
1211 for (int i=j;i<n;i++) {
1212 sum=Phasor.copy(ludarray[i][j]);
1213 for (int k=0;k<j;k++){
1214 sum.minusEquals(ludarray[i][k].times(ludarray[k][j]));
1215 }
1216 ludarray[i][j]=Phasor.copy(sum);
1217 if ((dum=vv[i]*sum.abs()) >= big) {
1218 big=dum;
1219 imax=i;
1220 }
1221 }
1222 if (j != imax) {
1223 for (int k=0;k<n;k++) {
1224 dumm=Phasor.copy(ludarray[imax][k]);
1225 ludarray[imax][k]=Phasor.copy(ludarray[j][k]);
1226 ludarray[j][k]=Phasor.copy(dumm);
1227 }
1228 ludmat.dswap = -ludmat.dswap;
1229 vv[imax]=vv[j];
1230 }
1231 ludmat.index[j]=imax;
1232
1233 if(ludarray[j][j].isZero()){
1234 ludarray[j][j].reset(TINY, 0.0D);
1235 }
1236 if(j != n-1) {
1237 dumm=ludarray[j][j].inverse();
1238 for (int i=j+1;i<n;i++){
1239 ludarray[i][j].timesEquals(dumm);
1240 }
1241 }
1242 }
1243 return ludmat;
1244 }
1245
1246 // Solves the set of n linear Phasor equations A.X=B using not A but its LU decomposition
1247 // Phasor bvec is the vector B (input)
1248 // Phasor xvec is the vector X (output)
1249 // index is the permutation vector produced by luDecomp()
1250 public Phasor[] luBackSub(Phasor[] bvec){
1251 int ii=0,ip=0;
1252 int n=bvec.length;
1253 if(n!=this.ncol)throw new IllegalArgumentException("vector length is not equal to matrix dimension");
1254 if(this.ncol!=this.nrow)throw new IllegalArgumentException("matrix is not square");
1255 Phasor sum=new Phasor();
1256 Phasor[] xvec=new Phasor[n];
1257 for(int i=0; i<n; i++){
1258 xvec[i]=Phasor.copy(bvec[i]);
1259 }
1260 for (int i=0;i<n;i++) {
1261 ip=this.index[i];
1262 sum=Phasor.copy(xvec[ip]);
1263 xvec[ip]=Phasor.copy(xvec[i]);
1264 if (ii==0){
1265 for (int j=ii;j<=i-1;j++){
1266 sum.minusEquals(this.matrix[i][j].times(xvec[j]));
1267 }
1268 }
1269 else{
1270 if(sum.isZero()) ii=i;
1271 }
1272 xvec[i]=Phasor.copy(sum);
1273 }
1274 for(int i=n-1;i>=0;i--) {
1275 sum=Phasor.copy(xvec[i]);
1276 for (int j=i+1;j<n;j++){
1277 sum.minusEquals(this.matrix[i][j].times(xvec[j]));
1278 }
1279 xvec[i]= sum.over(this.matrix[i][i]);
1280 }
1281 return xvec;
1282 }
1283
1284 // Solves the set of n linear Phasor equations A.X=B
1285 // Phasor bvec is the vector B (input)
1286 // Phasor xvec is the vector X (output)
1287 public Phasor[] solveLinearSet(Phasor[] bvec){
1288 PhasorMatrix ludmat;
1289
1290 ludmat=this.luDecomp();
1291 return ludmat.luBackSub(bvec);
1292 }
1293}
1294
Note: See TracBrowser for help on using the repository browser.