A package for Numerical Analysis 2 course I wrote for my class, including:
- Matrix and vector operations( addition, multiplication..)
- Models for complex numbers, complex matrix and complex vectors.
- Infinity norm, euclidian norm(for vectors), Frobenious norm(matrices).
- Matrix convergence test.
- Power method for finding eiugenvalues and eiguenvector of a Matrix.
- Expansion of a matrix A with complex numbers to a matrix twice as big with a matrix with real numbers.
- Jacobi Iterative and Gauss Seidel methods for solving system of linear equations.
A
Input: Matrix A, n0 (max number of iterations), TOL
Output: true or false
S1: set A1 = A
S2: set k = 1
S3: while k<=n0 do S4 - S6
S4: set A = A*A1
S5: if $|a_i|$ < TOL, for each $i = 1, 2, . . . , n$ and $j = 1, 2, . . . n$, then
Output(The matrix A converges)
STOP
S6 k := k+1;
S7 Output(Tolerance was not met within the max number of iterations).
And in java the implentation went
public boolean converges(double tol, int max) {
Matrix M1 = new Matrix(M);
double[][] A;
boolean found = false;
int k = 1;
while (k <= max && !found) {
M1 = M1.multiply(this);
A = M1.getPrimitive();
boolean converge = true;
for (int i = 0; i < rows && converge; i++) {
for (int j = 0; j < columns; j++) {
if (Math.abs(A[i][j]) > tol) {
converge = false;
break;
}
}
}
if (converge) {
found = true;
}
k = k + 1;
}
return found;
}
The number
This homogeneous system of equations has nonzero solutions if and only if the coefficient matrix
When
Finding euiguen vector when
Setting
Which is equivalent to solving
The 6x6 matrix was obtained by getting the constats of each row near
Let
So for the i-th row of 3x3 matrix in (1), we know now the coefficients near
public Matrix expandToReal() {
int n = rows;
double[][] M = new double[2 * n][2 * n];
for (int i = 0; i < CM.length; i++) {
for (int j = 0; j < CM.length; j++) {
Complex Z = this.get(i, j);
double c_ij = Z.getReal();
double d_ij = Z.getImaginary();
M[2 * i][2 * j] = c_ij;
M[2 * i + 1][2 * j] = d_ij;
M[2 * i][2 * j + 1] = -1.0 * d_ij;
M[2 * i + 1][2 * j + 1] = c_ij;
}
}
return new Matrix(M);
}
And voila, testing the algorithm we get the same 6x6 expanded matrix we got above
Yes, I was lazy to try and print the matrix better.