-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolicMatrix.java
More file actions
233 lines (207 loc) · 8.21 KB
/
Copy pathSymbolicMatrix.java
File metadata and controls
233 lines (207 loc) · 8.21 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
public class SymbolicMatrix {// real-values matrix class
private int size;
private Polynomial[][] values;
public SymbolicMatrix(int size){
this.setSize(size);
}
public void setSize(int newSize){
this.size = newSize;
this.values = null;// clear the values because size has changed
}
public int getSize(){
return this.size;
}
public void setValues(Polynomial[][] newValues){
// first check new array is the right size
if (newValues.length != this.size){
System.err.println("Wrong size array for the matrix");
}
for (int i = 0; i < this.size; i++){
if (newValues[i].length != this.size){
System.err.println("Wrong size array for the matrix");
}
}
this.values = newValues;
}
public Polynomial[][] getValues(){
return this.values;
}
public void print(){// print a matrix in a nice format rounded to 3 d.p
for (int i = 0; i < this.size; i++){
System.out.print("[");
for (int j = 0; j < this.size; j++){
Polynomial value = this.values[i][j];
value.print();
System.out.print(" ");
}
System.out.println("]");
}
System.out.println();
}
public Matrix matMultiply(Matrix B){// multiply two matrices where A.matMultiply(B) = AB and B.matMultiply(A) = BA
Matrix result = new Matrix(this.size);
result.setSize(this.size);
double[][] resultValues = new double[this.size][this.size];
for (int i = 0; i < this.size; i++){
for (int j = 0; j < this.size; j++){
resultValues[i][j] = 0;
for (int count = 0; count < this.size; count++)
resultValues[i][j] += this.values[i][count]*B.values[count][j];
}
}
result.setValues(resultValues);
return result;
}
public Matrix matAddition(Matrix B){// add two matrices
Matrix result = new Matrix(this.size);
result.setSize(this.size);
double[][] resultValues = new double[this.size][this.size];
for (int i = 0; i < this.size; i++){
for (int j = 0; j < this.size; j++){
resultValues[i][j] = this.values[i][j] + B.values[i][j];
}
}
result.setValues(resultValues);
return result;
}
public Matrix multiply(double a){// multiply by a real factor
Matrix result = new Matrix(this.size);
result.setSize(this.size);
double[][] resultValues = new double[this.size][this.size];
for (int i = 0; i < this.size; i++){
for (int j = 0; j < this.size; j++){
resultValues[i][j] = a * this.values[i][j];
}
}
result.setValues(resultValues);
return result;
}
public Matrix matSubtraction(Matrix B){
return this.matAddition(B.multiply(-1));
}
public double determinant(){
PermutationSet allPermutations = new PermutationSet(this.size);// generate an instance of the PermutationSet class
double total = 0;
int[][] sn = allPermutations.getPermutations();
for (int i = 0; i<sn.length; i++){// loop through the set of permutations, Sn
double[] matrixEntries = new double[this.size];// a list of the matrix entries that are selected by this permutation
for (int j = 0; j<this.size; j++){// loop through to find the values of the selected entries
matrixEntries[j] = this.values[j][sn[i][j]];// sn[i][j] is the value that j gets mapped to in permutation i
}
total += allPermutations.sgn(sn[i]) * product(matrixEntries);// add the signed product of these entries to the running total
}
return total;
}
private double product(double[] nums){// calculates the product of all numbers in array
double total = 1;
for (int i = 0; i<nums.length; i++){
total *= nums[i];
}
return total;
}
public Matrix adjugate(){// find the adjugate of the matrix
Matrix result = new Matrix(this.size);
double[][] values = new double[this.size][this.size];
for (int i=0; i < this.size; i++){
for (int j=0; j < this.size; j++){
values[i][j] = (Math.pow(-1, i+j)) * (this.minor(j,i)).determinant();// each entry of the adj matrix is the signed det of the minor matrix
}
}
result.setValues(values);
return result;
}
public Matrix minor(int i, int j){// return the minor(i,j) of a matrix by eliminating column i and row j
Matrix result = new Matrix(this.size - 1);
double[][] values = new double[this.size - 1][this.size - 1];
for (int x=0; x < this.size - 1; x++){
for (int y=0; y < this.size - 1; y++){
int newX;
int newY;
if (x>=i){newX = x+1;}else{newX=x;}
if (y>=j){newY = y+1;}else{newY=y;}
values[x][y] = this.values[newX][newY];
}
}
result.setValues(values);
return result;
}
public Matrix inverse(){// return the inverse of the matrix
Matrix inverseMatrix = this.adjugate().multiply(1 / this.determinant());
return inverseMatrix;
}
public static Matrix identity(int n){// produce nxn identity matrix
Matrix idMatrix = new Matrix(n);
double[][] values = new double[n][n];
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
if(i==j){values[i][j]=1;}
else{values[i][j]=0;}
}
}
idMatrix.setValues(values);
return idMatrix;
}
public Vector getCol(int i){// return column i of the matrix
Vector result = new Vector(this.size);
double[] resultValues = new double[this.size];
for (int j=0; j<this.size; j++){
resultValues[j] = this.values[j][i];
}
result.setValues(resultValues);
return result;
}
public Vector getRow(int i){// return row i of the matrix
Vector result = new Vector(this.size);
double[] resultValues = new double[this.size];
for (int j=0; j<this.size; j++){
resultValues[j] = this.values[i][j];
}
result.setValues(resultValues);
return result;
}
public void setCol(int i, Vector v){// return column i of the matrix
if (v.getSize() != this.size){
System.err.println("vector is not right size for matrix");
}
double[] newColumn = v.getvalues();
for (int j=0; j<this.size; j++){
this.values[i][j] = newColumn[j];
}
}
public void setRow(int i, Vector v){// return column i of the matrix
if (v.getSize() != this.size){
System.err.println("vector is not right size for matrix");
}
double[] newRow = v.getvalues();
for (int j=0; j<this.size; j++){
this.values[j][i] = newRow[j];
}
}
public Matrix adjoint(){// calculate adjoint of the matrix AKA conjugate/hermitian transpose over C
Matrix result = new Matrix(this.size);
double[][] newValues = new double[this.size][this.size];
for (int i=0; i<this.size; i++){
for (int j=0; j<this.size; j++){
newValues[j][i] = this.values[i][j];// would also take complex conjugate here if working over C
}
}
result.setValues(newValues);
return result;
}
public boolean equals(Matrix B){// check two matrices are equal (allowing for 5d.p error)
if (this.size != B.getSize()){return false;}
double[][] Bvals = B.getValues();
for (int i=0; i<this.size; i++){
for (int j=0; j<this.size; j++){
if ((double)Math.round(this.values[i][j]*100000d)/100000d !=
(double)Math.round(Bvals[i][j]*100000d)/100000d){
return false;
}
}
}
return true;
}
public boolean diagonalisable(){// self-adjoint operator <-> symmetric matrix <-> diagonalisable
return (this.equals(this.adjoint()));
}
}