Java之求逆矩阵

</pre><pre code_snippet_id="1597537" snippet_file_name="blog_20160304_2_3739580" name="code" class="java">public class MatrixInverse {
	public static double Det(double [][]Matrix,int N)//计算n阶行列式(N=n-1)
	{
		int T0;
		int T1;
		int T2;
		double Num;
		int Cha;
		double [][] B;
		if(N>0)
		{
			Cha=0;
			B=new double[N][N];
			Num=0;
			
			if(N==1)
			{
				return Matrix[0][0]*Matrix[1][1]-Matrix[0][1]*Matrix[1][0];
			}
			for(T0=0;T0<=N;T0++)//T0循环
			{
				for(T1=1;T1<=N;T1++)//T1循环
				{
					for(T2=0;T2<=N-1;T2++)//T2循环
					{
						if(T2==T0)
						{
							Cha=1;
							
						}
						B[T1-1][T2]=Matrix[T1][T2+Cha];
					}//T2循环
					Cha=0;
				}//T1循环
				Num=Num+Matrix[0][T0]*Det(B,N-1)*Math.pow((-1),T0);
			}//T0循环
			return Num;
		}
		else if(N==0)
		{
			return Matrix[0][0];
		}
		
		return 0;
	}
public static double Inverse(double[][]Matrix,int N,double[][]MatrixC){
	int T0;
	int T1;
	int T2;
	int T3;
	double [][]B;
	double Num=0;
	int Chay=0;
	int Chax=0;
	B=new double[N][N];
	double add;
	add=1/Det(Matrix,N);
	for( T0=0;T0<=N;T0++)
	{
		for(T3=0;T3<=N;T3++)
		{
			for(T1=0;T1<=N-1;T1++)
			{
				if(T1<T0)
				{
					Chax=0;
				}
				else
				{
					Chax=1;
				}
				for(T2=0;T2<=N-1;T2++)
				{
					if(T2<T3)
					{
						Chay=0;
					}
					else
					{
						Chay=1;
					}
					B[T1][T2]=Matrix[T1+Chax][T2+Chay];
				}//T2循环
			}//T1循环
			Det(B,N-1);
			MatrixC[T3][T0]=Det(B,N-1)*add*(Math.pow(-1, T0+T3));
			
		}
	}
	return 0;
}
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[][]InMatrix=new double[4][4];
	Inverse(TestMatrix,3,InMatrix);
	String str=new String("");
	
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
	        String strr=String.valueOf(InMatrix[i][j]);
	        str+=strr;
	        str+=" ";
		}
		str+="\n";
	}
   System.out.println(str);
	
}
}




 

运行结果:



推荐文章:那些年,做的几个应用


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值