Ahoj,
mam dost zmatek v tech ruznych vychytavkach objektoveho programovani a momentalne jsem se v jednom projektu zabrzdil na jedne veci..
package maticeproject2009;
final public class Matrix {
private final int M; // number of rows
private final int N; // number of columns
private final double[][] data; // M-by-N array
// create M-by-N matrix of 0's
public Matrix(int M, int N) {
this.M = M;
this.N = N;
data = new double[M][N];
}
// create matrix based on 2d array
public Matrix(double[][] data) {
M = data.length;
N = data[0].length;
this.data = new double[M][N];
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
this.data[i][j] = data[i][j];
}
// copy constructor
private Matrix(Matrix A) { this(A.data); }
// print matrix to standard output
public void show() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
System.out.printf("%9.4f ", data[i][j]);
System.out.println();
}
}
public Matrix gaussian() {
// create copies of the data
Matrix A = new Matrix(this);
// Gaussian elimination with partial pivoting
for (int i = 0; i < N; i++) {
// find pivot row and swap
int max = i;
for (int j = i + 1; j < N; j++)
if (Math.abs(A.data[j][i]) > Math.abs(A.data[max][i]))
max = j;
A.swap(i, max);
// pivot within A
for (int j = i + 1; j < N; j++) {
double m = A.data[j][i] / A.data[i][i];
for (int k = i+1; k < N; k++) {
A.data[j][k] -= A.data[i][k] * m;
}
A.data[j][i] = 0.0;
}
}
return A;
}
// swap rows i and j
private void swap(int i, int j) {
double[] temp = data[i];
data[i] = data[j];
data[j] = temp;
}
// test client
public static void main(String[] args) {
double[][] d = { { 8, -1, -2, 0 }, { -1, 7, -1, 10 }, { -2, -1, 9, 23} };
Matrix A = new Matrix(d);
A.show();
System.out.println();
A.gaussian();
System.out.println("Uprava na schodovy tvar:");
A.show();
}
}
Hazi mi to: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
takze tipuji, ze mam spatne inicializovaneou matici A v metode gaussian, jelikoz me to odkazuje na radek 47
if (Math.abs(A.data[j][i]) > Math.abs(A.data[max][i]))