Stavo provando ad implementare questo algoritmo in java, però dubito fortemente di esserte vicino alla soluzione. Per chi non sapesse cos'è è un metodo per ridurre una matrice qualsiasi in una matrice a gradini. Questo è l'algoritmo:
[quote=Wikipedia]
L'algoritmo di Gauss trasforma una qualsiasi matrice in una matrice a scalini tramite mosse di Gauss. Funziona nel modo seguente:
Se la prima riga ha il primo elemento nullo, scambiala con una riga che ha il primo elemento non nullo. Se tutte le righe hanno il primo elemento nullo, vai al punto 3.
Per ogni riga Ai con primo elemento non nullo, eccetto la prima (i>1), moltiplica la prima riga per un coefficiente scelto in maniera tale che la somma tra la prima riga e Ai abbia il primo elemento nullo (quindi coefficiente = −Ai1/A11). Sostituisci Ai con la somma appena ricavata.
Adesso sulla prima colonna tutte le cifre, eccetto forse la prima, sono nulle. A questo punto ritorna al punto 1 considerando la sottomatrice che ottieni cancellando la prima riga e la prima colonna.
[/quote]
Non posterò tutta la classe ma solo le funzioni incriminate perché ve ne sono molte altre non utilizzate da questo metodo.
Alcuni metodi di classe:
Codice: Seleziona tutto
public class Matrix
{
private final AtomicInteger nrows;
private final AtomicInteger ncolumns;
private final ArrayList<ArrayList<Double>> matrix = new ArrayList<ArrayList<Double>>();
public Matrix(int rows, int cols)
{
this.nrows = new AtomicInteger(rows);
this.ncolumns = new AtomicInteger(cols);
for(int i = 0; i < this.nrows.intValue(); i++)
{
this.matrix.add(new ArrayList<Double>());
for(int j = 0; j < this.ncolumns.intValue(); j++)
{
this.matrix.get(i).add(null);
}
}
}
public Matrix(Matrix mat)
{
this.matrix.clear();
for(ArrayList<Double> subList: mat.getValue())
{
this.matrix.add(subList);
}
this.nrows = new AtomicInteger(mat.getLength()[0]);
this.ncolumns = new AtomicInteger(mat.getLength()[1]);
}
public final void init()
{
Scanner sc = new Scanner(System.in);
for(int i = 0; i < this.nrows.intValue(); i++)
{
for(int j = 0; j < this.ncolumns.intValue(); j++)
{
this.matrix.get(i).set(j, (Double) sc.nextDouble());
}
}
//sc.close();
}
public final Double getValue(int row, int col)
{
return this.matrix.get(row).get(col);
}
public final ArrayList<Double> getValue(int row)
{
return this.matrix.get(row);
}
public final ArrayList<ArrayList<Double>> getValue()
{
return this.matrix;
}
public final void setValue(int row, int col, Double val)
{
this.matrix.get(row).set(col, val);
}
public final void setValue(int row, ArrayList<Double> val)
{
this.matrix.set(row, val);
}
public final void setValue(ArrayList<ArrayList<Double>> val)
{
this.matrix.clear();
for(ArrayList<Double> subList: val)
{
this.matrix.add(subList);
}
this.nrows.lazySet(this.matrix.size());
this.ncolumns.lazySet(this.matrix.get(0).size());
}
public final int[] getLength()
{
int[] len = new int[2];
ArrayList<Double> subMatrix = this.matrix.get(0);
len[0] = this.matrix.size();
len[1] = subMatrix.size();
return len;
}
private final int getMinDimension()
{
int[] dim = this.getLength();
if(dim[0] <= dim[1])
{
return dim[0];
}
else
{
return dim[1];
}
}
private final void exchangeRows(int i1, int i2)
{
ArrayList<Double> temp = this.getValue(i1);
this.setValue(i1, this.getValue(i2));
this.setValue(i2, temp);
}
Codice: Seleziona tutto
public final Matrix gaussSubMatrix()
{
Matrix s = new Matrix(this.getLength()[0] - 1, this.getLength()[1] - 1);
for(int i = 1; i < this.getLength()[0]; i++)
{
for(int j = 1; j < this.getLength()[1]; j++)
{
s.setValue(i - 1 , j - 1, this.getValue(i, j));
}
}
return s;
}
private static Matrix gauss1(Matrix a)
{
for(int i = 0; i < a.getLength()[0]; i++)
{
if((double) a.getValue(i, 0) == 0.0)
{
for(int x = i; x < a.getLength()[0]; x++)
{
if((double) a.getValue(i, 0) != 0.0)
{
a.exchangeRows(i, x);
return gauss2(a,false);
}
}
}
}
return gauss2(a,true);
}
private static Matrix gauss2(Matrix a, boolean allZeroPivot)
{
if(allZeroPivot)
{
return a;
}
Double pivot = a.getValue(0, 0);
Matrix fRow = new Matrix(1, a.getLength()[1]);
fRow.setValue(0, a.getValue(0));
for(int i = 1; i < a.getLength()[0]; i++)
{
if((double) a.getValue(i, 0) != 0.0)
{
Matrix coef = Matrix.scalarProduct(fRow, -a.getValue(i, 0)/pivot);
for(int j = 0; j < a.getLength()[1]; j++)
{
a.setValue(i, j, a.getValue(i, j) + coef.getValue(0, j));
}
}
}
return a;
}
public static Matrix rowEchelonForm(Matrix a)
{
int ncut = 0;
Matrix cmat = new Matrix(a);
while(cmat.getMinDimension() != 1)
{
cmat = new Matrix(gauss1(cmat));
for(int i = 0; i < cmat.getLength()[0]; i++)
{
for(int j = 0; j < cmat.getLength()[1]; j++)
{
a.setValue(i + ncut , j + ncut, cmat.getValue(i, j));
}
}
ncut += 1;
cmat = cmat.gaussSubMatrix();
}
return a;
}
}
So che è un codice bello lungo, ma grazie in anticipo