-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.java
More file actions
131 lines (112 loc) · 3.57 KB
/
Copy pathMatrix.java
File metadata and controls
131 lines (112 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.lang.Math;
class Matrix {
private Complex innerArray[][];
protected int totalRows;
protected int totalColumns;
public Matrix(int n){
totalRows=n;
totalColumns=n;
FillAllInnerArrayWithZeros();
}
public Matrix(int n, int m){
totalRows=n;
totalColumns=m;
FillAllInnerArrayWithZeros();
}
private void FillAllInnerArrayWithZeros (){
innerArray = new Complex [totalRows][totalColumns];
for (int i=0; i<=totalRows-1; i++){
for (int j=0; j<=totalColumns-1; j++){
innerArray[i][j]= new Complex (0.0f,0.0f);
//System.out.println("["+i+","+j+"]:"+innerArray[i][j].ToString()); //uncomment for debug
}
}
}
public void IdentityMatrix (){
for (int i=0; i<=totalRows-1; i++){
for (int j=0; j<=totalColumns-1; j++){
if(i==j)
innerArray[i][j]= new Complex (1.0f,0.0f);
else
innerArray[i][j]= new Complex (0.0f,0.0f);
//System.out.println("["+i+","+j+"]:"+innerArray[i][j].ToString()); //uncomment for debug
}
}
}
public void SwitchRow(int rowA, int rowB){
float temporaryRe;
float temporaryIm;
for (int i=0; i<=totalColumns-1; i++){
temporaryRe=innerArray[rowA][i].GetRe();
temporaryIm=innerArray[rowA][i].GetIm();
innerArray[rowA][i]=innerArray[rowB][i];
innerArray[rowB][i]= new Complex(temporaryRe,temporaryIm);
}
}
public Matrix Add (Matrix mat1){
Matrix result = new Matrix(totalRows,totalColumns);
for (int i=0; i<=totalRows-1; i++){
for (int j=0; j<=totalColumns-1; j++){
result.Set(i,j,this.Get(i,j).Add(mat1.Get(i,j)));
//System.out.println("["+i+","+j+"]:"+result.Get(i,j).ToString()); //uncomment for debug
}
}
return result;
}
public Matrix Substract (Matrix mat1){
Matrix result = new Matrix(totalRows,totalColumns);
for (int i=0; i<=totalRows-1; i++){
for (int j=0; j<=totalColumns-1; j++){
result.Set(i,j,this.Get(i,j).Substract(mat1.Get(i,j)));
//System.out.println("["+i+","+j+"]:"+result.Get(i,j).ToString()); //uncomment for debug
}
}
return result;
}
public Matrix MultiplyBy (Matrix mat1){
Matrix result = new Matrix(this.totalRows,mat1.totalColumns);
try{
if (this.totalColumns != mat1.totalRows)
{
throw new ArrayIndexOutOfBoundsException();
}
for (int i=0; i<=this.totalRows-1; i++){
for (int j=0; j<=mat1.totalColumns-1; j++){
for (int k=0; k<=this.totalColumns-1; k++){
result.Set(i,j,result.Get(i,j).Add(this.Get(i,k).MultiplyBy(mat1.Get(k,j))));
}
//System.out.println("["+i+","+j+"]:"+result.Get(i,j).ToString()); //uncomment for debug
}
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error: Column of first Matrix should be the same lenght as row of second Matrix.");
System.exit(-1);
}
return result;
}
public Complex Get(int row, int column){
return innerArray[row][column];
}
public void Set(int row, int column, Complex value){
innerArray[row][column]=value;
}
public void DebugPrintMatrixDetailsOnlyRealValues(){
for (int i=0; i<totalRows; i++){
System.out.print("["+i+"]");
for (int j=0; j<totalColumns; j++){
System.out.print(" "+(int)this.Get(i,j).GetRe());
}
System.out.println(" ");
}
}
public void DebugPrintMatrixDetailsComplexValues(){
for (int i=0; i<totalRows; i++){
System.out.print("["+i+"]");
for (int j=0; j<totalColumns; j++){
System.out.print(" ["+this.Get(i,j).ToString()+"]");
}
System.out.println(" ");
}
}
}