旋转矩阵的排序算法 - 如何确保非零元素位于对角线上?
我想对矩阵进行排序,使非零元素位于对角线上。我想旋转矩阵来求解线性方程。但为了确保一切正常,我必须先对其进行排序,然后我的算法才能做到这一点。
/*
* 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;
}
}
I want to sort matrix so that non zero elements are on the diagonal. I want to pivot the matrix to solve linear equations. But to make sure everything is working, I have to have it sorted before my algo can do that.
/*
* 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地说,
Arrays.sort()
可以使用采用double
的方法。对于学习,我喜欢 JScience 和 Apache Commons 数学。前者承认DenseMatrix
,这可能对高斯-若尔消除法。为了进行调试,您需要一个包含测试用例的 sscce。Trivially, one of the
Arrays.sort()
methods that takesdouble
may serve. For study, I like JScience and Apache Commons Math. The former admitsDenseMatrix<Rational>
, which may prove useful for Gauss–Jordan elimination. For debugging, you'll need an sscce that includes test cases.