前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java之矩阵求秩

Java之矩阵求秩

作者头像
全栈程序员站长
发布2022-08-30 18:58:24
7360
发布2022-08-30 18:58:24
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
public class MatrixRank {

   public static int Rank(double[][] Matrix,int error_,int List)
   {
	   int n=List;
	   int m=Matrix.length ;
	   int i=0;
	   int i1;
	   int j=0;
	   int j1;
	   double temp1;
	   if(m>n)
	   {
		   i=m;
		   m=n;
		   n=i;
		   i=1;
	   }
	   m-=1;
	   n-=1;
	   double[][]temp=new double[m+1][n+1];
	   if(i==0)
	   {
		 for(i=0;i<=m;i++)
		 {
			 for(j=0;j<=n;j++)
			 {
				 temp[i][j]=Matrix[i][j];
			 }
				 
		 }
	   }
	   else
	   {
		   for(i=0;i<=m;i++)
		   {
			   for(j=0;j<=n;j++)
			   {
				   temp[i][j]=Matrix[j][i];
			   }
		   }
	   }
	   if(m==0)
	   {
		   i=0;
		   while(i<=n)
		   {
			   if(Matrix[0][i]!=0)
			   {
				   return 1;
			   }
			   i+=1;
		   }
		   return 0;
	   }
	   double error0;
	   if(error_==-1)
	   {
		   error0=Math.pow(0.1, 10);
	   }
	   else
	   {
		   error0=Math.pow(0.1, error_);
	   }
	   i=0;
	   while(i<=m)
	   {
		   j=0;
		   while(j<=n)
		   {
			   if(temp[i][j]!=0)
			   {
				   error0*=temp[i][j];
				   i=m;
				   break;
			   }
			   j+=1;
		   }
		   i+=1;
	   }
	   double error1;
	   for(i=0;i<=m;i++)
	   {
		   j=0;
		   while(j<=n)
		   {
			   if(temp[i][j]!=0)
			   {
				   break;
			   }
			   j+=1;
		   }
		   if(j<=n)
		   {
			   i1=0;
			   while(i1<=m)
			   {
				   if(temp[i1][j]!=0&&i1!=i)
				   {
					   temp1=temp[i][j]/temp[i1][j];
					   error1=Math.abs((temp[i][j]-temp[i1][j]*temp1))*100;
					   error1+=error0;
					   for(j1=0;j1<=n;j1++)
					   {
						   temp[i1][j1]=temp[i][j1]-temp[i1][j1]*temp1;
						   if(Math.abs(temp[i1][j1])<error1)
						   {
							   temp[i1][j1]=0;
						   }
					   }
					   
				   }
				   i1+=1;
			   }
		   }
	   }
	   
	   i1=0;
	   for(i=0;i<=m;i++)
	   {
		   for(j=0;j<=n;j++)
		   {
			   if(temp[i][j]!=0)
			   {
				   i1+=1;
				   break;
			   }
		   }
	   }
	   return i1;
   }
   public static void main(String[]args)//测试
   {
   	double[][] TestMatrix = {
   			   {1, 22, 34,22}, 
   			   {1, 11,5,21} ,
   			   {0,1,5,11},
   			   {7,2,13,19}};	 
   	double[][]TestMatrix2={
   			{1,2,3},{4,7,8},{2,9,1}
   	};
   	double[][]TestMatrix3={
   			{1,2},{3,3}	
   	};
   		
   	  String strr=String.valueOf(Rank(TestMatrix,-1,4));
   	 String strr2=String.valueOf(Rank(TestMatrix2,-1,3));
   	 String strr3=String.valueOf(Rank(TestMatrix3,-1,2));
   	
      System.out.println(strr);
      System.out.println(strr2);
      System.out.println(strr3);
     
   	
   }
   
}

运行结果:

Java之矩阵求秩
Java之矩阵求秩

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/145006.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档