Salve a tutti sono nuovo del forum e vorrei porvi un problema che mi è sorto nel generare una classe per invertire una matrice. Dovendo realizzare una classe per risolvere un sistema di equazioni lineari e dovendolo risolvere in forma matriciale ho trovato su internet una classe che effettua operazioni con le matrici tra cui l'inversa. Dopo aver copiato il codice eliminando le parti che non mi servivano quando vado ad avviarlo mi da il problema: java.lang.NullPointerException sulla riga 12 e 53 del codice seguente (mi scuso per la lunghezza del messaggio ma non sapevo come sintetizzarlo ):
import system.System;
import system.Scanner;
import javax.swing.JLabel;
public class Matriceinversa {
public static void main (String [] args) throws Exception{
Scanner read =new Scanner(System.in);
float [][] M=new float [5][5];
for(int i=0;i<M.length;i++)
for(int j=0;j<M[0].length;j++)
M[i][j]=read.nextFloat();
float [][] ZK=Inverse(M);
for(int i=0; i<ZK.length;i++)
for(int j=0;j<ZK[0].length;j++)
System.out.println(ZK[i][j]+"");
}
public static boolean DEBUG = true;
public static boolean INFO = true;
public static int iDF = 0;
public static JLabel statusBar;
public static float[][] Transpose (float[][] a) {
if (INFO) {
System.out.println("Performing Transpose...");
}
float m[][] = new float[a[0].length][a.length];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
m[j][i] = a[i][j];
return m;
}
// --------------------------------------------------------------
public static float[][] Inverse(float[][] a) throws Exception {
// Formula used to Calculate Inverse:
// inv(A) = 1/det(A) * adj(A)
if (INFO) {
System.out.println("Performing Inverse...");
}
int tms = a.length;
float m[][] = new float[tms][tms];
float mm[][] = Adjoint(a);
float det = Determinant(a);
float dd = 0;
if (det == 0) {
statusBar.setText("Determinant Equals 0, Not Invertible.");
if (INFO) {
System.out.println("Determinant Equals 0, Not Invertible.");
}
} else {
dd = 1 / det;
}
for (int i = 0; i < tms; i++)
for (int j = 0; j < tms; j++) {
m[i][j] = dd * mm[i][j];
}
return m;
}
public static float[][] Adjoint(float[][] a) throws Exception {
if (INFO) {
System.out.println("Performing Adjoint...");
}
int tms = a.length;
float m[][] = new float[tms][tms];
int ii, jj, ia, ja;
float det;
for (int i = 0; i < tms; i++)
for (int j = 0; j < tms; j++) {
ia = ja = 0;
float ap[][] = new float[tms - 1][tms - 1];
for (ii = 0; ii < tms; ii++) {
for (jj = 0; jj < tms; jj++) {
if ((ii != i) && (jj != j)) {
ap[ia][ja] = a[ii][jj];
ja++;
}
}
if ((ii != i) && (jj != j)) {
ia++;
}
ja = 0;
}
det = Determinant(ap);
m[i][j] = (float) Math.pow(-1, i + j) * det;
}
m = Transpose(m);
return m;
}
// --------------------------------------------------------------
public static float[][] UpperTriangle(float[][] m) {
if (INFO) {
System.out.println("Converting to Upper Triangle...");
}
float f1 = 0;
float temp = 0;
int tms = m.length; // get This Matrix Size (could be smaller than
// global)
int v = 1;
iDF = 1;
for (int col = 0; col < tms - 1; col++) {
for (int row = col + 1; row < tms; row++) {
v = 1;
outahere: while (m[col][col] == 0) // check if 0 in diagonal
{ // if so switch until not
if (col + v >= tms) // check if switched all rows
{
iDF = 0;
break outahere;
} else {
for (int c = 0; c < tms; c++) {
temp = m[col][c];
m[col][c] = m[col + v][c]; // switch rows
m[col + v][c] = temp;
}
v++; // count row switchs
iDF = iDF * -1; // each switch changes determinant
// factor
}
}
if (m[col][col] != 0) {
if (DEBUG) {
System.out.println("tms = " + tms + " col = " + col
+ " row = " + row);
}
try {
f1 = (-1) * m[row][col] / m[col][col];
for (int i = col; i < tms; i++) {
m[row][i] = f1 * m[col][i] + m[row][i];
}
} catch (Exception e) {
System.out.println("Still Here!!!");
}
}
}
}
return m;
}
public static float Determinant(float[][] matrix) {
if (INFO) {
System.out.println("Getting Determinant...");
}
int tms = matrix.length;
float det = 1;
matrix = UpperTriangle(matrix);
for (int i = 0; i < tms; i++) {
det = det * matrix[i][i];
} // multiply down diagonal
det = det * iDF; // adjust w/ determinant factor
if (INFO) {
System.out.println("Determinant: " + det);
}
return det;
}
}
Ho provato a fare il debug ma non ho capito dove sbaglio. Qualcuno può aiutarmi?
Giuseppe