Я хочу отсортировать матрицу так, чтобы ненулевые элементы были на диагонали. Я хочу повернуть матрицу для решения линейных уравнений. Но чтобы убедиться, что все работает, мне нужно отсортировать его, прежде чем мой алгоритм сможет это сделать.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package positioning;
/**
*
* @author Andreas
*/
public class lineareq {
public static double[][] gaussjordan(double[][] mat){
//http://people.richland.edu/james/lecture/m116/matrices/pivot.html
double factor1 =0;
double factor2 =0;
for(int i=0; i<mat.length; i++){
factor1 = mat[i][i];
if(factor1!=0){
for(int j=0; j<mat.length; j++){
factor2 = mat[j][i];
if(i!=j && factor2!=0){
System.out.println(factor1+";"+factor2);
for(int k=0; k<mat.length+1; k++){
mat[j][k] = factor1*mat[j][k]-factor2*mat[i][k];
}
}
}
}
}
for(int i=0; i<mat.length; i++){
factor1=mat[i][i];
if(mat[i][i]!=0){
for(int j=0; j<mat.length+1; j++){
if(mat[i][j]!=0){
mat[i][j]=mat[i][j]/factor1;
}
}
}
}
return mat;
}
public static double[][] mat3x3(double[][] mat){
int[] diagon = new int[mat.length];
int[] diagony = new int[mat.length];
int[] checkx = new int[mat.length];
int[] checky = new int[mat.length];
int[] changer = new int[mat.length];
int checkcount = 0;
int[][] find = new int[mat.length][mat[0].length-1];
for(int i=0; i<find.length; i++){
for(int j=0; j<find[i].length; j++){
if(mat[i][j]!=0){
find[i][j] = 1;
diagon[j] = diagon[j]+1;
diagony[i] = diagony[i]+1;
}
// System.out.print(find[i][j]+";");
}
// System.out.println();
}
/*
for(int i=0; i<diagon.length; i++){
System.out.print(diagon[i]+";");
}
System.out.println("xxx");
for(int i=0; i<diagony.length; i++){
System.out.print(diagony[i]+";");
}
System.out.println("yyy");
*/
int count = 0;
for(int i=1; i<=diagon.length; i++){
for(int j=0; j<diagon.length; j++){
if(diagon[j]==i){
// System.out.println("x"+i+";"+j+";"+diagon[j]);
int k=0;
int stop=0;
while(k<diagon.length && stop==0){
// System.out.println(find[k][j]);
if(find[k][j]==1 && checkx[j]==0 && checky[k]==0){
// System.out.println("t");
changer[j] = k;
checkx[j]=1;
checky[k]=1;
stop=1;
}
k=k+1;
}
}
}
for(int j=0; j<diagony.length; j++){
if(diagony[j]==i){
// System.out.println("y"+i+";"+j+";"+diagony[j]);
int k=0;
int stop=0;
while(k<diagon.length && stop==0){
// System.out.println(find[j][k]);
if(find[j][k]==1 && checkx[k]==0 && checky[j]==0){
// System.out.println("t");
changer[k] = j;
checkx[k]=1;
checky[j]=1;
stop=1;
}
k=k+1;
}
}
}
}
// System.out.println();
/*
for(int i=0; i<changer.length; i++){
System.out.print(changer[i]+";");
}
System.out.println();
*/
double[][] mat_change = new double[mat.length][mat[0].length];
for(int i=0; i<mat.length; i++){
for(int j=0; j<mat[i].length; j++){
mat_change[i][j] = mat[changer[i]][j];
}
}
return mat_change;
}
}