Đến nội dung

sans_amour

sans_amour

Đăng ký: 13-11-2006
Offline Đăng nhập: 11-03-2008 - 00:41
-----

Nghịch đảo ma trận bằng các ngôn ngữ

24-08-2007 - 00:54

Đây là chương trình viết bằng ngôn ngữ basic

File inv.bas

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This code is generated by the AlgoPascal translator
'
'This code is distributed under the ALGLIB license
'	(see http://www.alglib.net/copyrules.php for details)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This routines must be defined by the programmer
' Sub LUDecomposition(ByRef A() As Double, _
'		 ByVal M As Long, _
'		 ByVal N As Long, _
'		 ByRef Pivots() As Long)
' Function InvTriangular(ByRef A() As Double, _
'		 ByVal N As Long, _
'		 ByVal IsUpper As Boolean, _
'		 ByVal IsUnitTriangular As Boolean) As Boolean


'Routines
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Inversion of a matrix given by its LU decomposition.
'
'Input parameters:
'	A	   -   LU decomposition of the matrix (output of LUDecomposition subroutine).
'	Pivots  -   table of permutations which were made during the LU decomposition
'				(the output of LUDecomposition subroutine).
'	N	   -   size of matrix A.
'
'Output parameters:
'	A	   -   inverse of matrix A.
'				Array whose indexes range within [1..N, 1..N].
'
'Result:
'	True, if the matrix is not singular.
'	False, if the matrix is singular.
'
'  -- LAPACK routine (version 3.0) --
'	 Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
'	 Courant Institute, Argonne National Lab, and Rice University
'	 February 29, 1992
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function InverseLU(ByRef A() As Double, _
		 ByRef Pivots() As Long, _
		 ByVal N As Long) As Boolean
	Dim Result As Boolean
	Dim WORK() As Double
	Dim I As Long
	Dim IWS As Long
	Dim J As Long
	Dim JB As Long
	Dim JJ As Long
	Dim JP As Long
	Dim JP1 As Long
	Dim V As Double
	Dim i_ As Long

	Result = True
	
	'
	' Quick return if possible
	'
	If N=0# then
		InverseLU = Result
		Exit Function
	End If
	ReDim WORK(1# To N)
	
	'
	' Form inv(U)
	'
	If  Not InvTriangular(A, N, True, False) then
		Result = False
		InverseLU = Result
		Exit Function
	End If
	
	'
	' Solve the equation inv(A)*L = inv(U) for inv(A).
	'
	For J=N To 1# Step -1
		
		'
		' Copy current column of L to WORK and replace with zeros.
		'
		For I=J+1# To N Step 1
			WORK(I) = A(I,J)
			A(I,J) = 0#
		Next I
		
		'
		' Compute current column of inv(A).
		'
		If J<N then
			JP1 = J+1#
			For I=1# To N Step 1
				V = 0.0
				For i_ = JP1 To N Step 1
					V = V + A&#40;I,i_&#41;*WORK&#40;i_&#41;
				Next i_
				A&#40;I,J&#41; = A&#40;I,J&#41;-V
			Next I
		End If
	Next J
	
	&#39;
	&#39; Apply column interchanges.
	&#39;
	For J=N-1# To 1# Step -1
		JP = Pivots&#40;J&#41;
		If JP<>J then
			For i_ = 1# To N Step 1
				WORK&#40;i_&#41; = A&#40;i_,J&#41;
			Next i_
			For i_ = 1# To N Step 1
				A&#40;i_,J&#41; = A&#40;i_,JP&#41;
			Next i_
			For i_ = 1# To N Step 1
				A&#40;i_,JP&#41; = WORK&#40;i_&#41;
			Next i_
		End If
	Next J

	InverseLU = Result
End Function


&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;
&#39;Inversion of a general matrix.
&#39;
&#39;Input parameters&#58;
&#39;	A   -   matrix. Array whose indexes range within &#91;1..N, 1..N&#93;.
&#39;	N   -   size of matrix A.
&#39;
&#39;Output parameters&#58;
&#39;	A   -   inverse of matrix A.
&#39;			Array whose indexes range within &#91;1..N, 1..N&#93;.
&#39;
&#39;Result&#58;
&#39;	True, if the matrix is not singular.
&#39;	False, if the matrix is singular.
&#39;
&#39;  -- ALGLIB --
&#39;	 Copyright 2005 by Bochkanov Sergey
&#39;
&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;
Public Function Inverse&#40;ByRef A&#40;&#41; As Double, ByVal N As Long&#41; As Boolean
	Dim Result As Boolean
	Dim Pivots&#40;&#41; As Long

	Call LUDecomposition&#40;A, N, N, Pivots&#41;
	Result = InverseLU&#40;A, Pivots, N&#41;

	Inverse = Result
End Function

Còn đây là bằng C# :

File inv.cs

/***********************************************************************
This code is generated by the AlgoPascal translator

This code is distributed under the ALGLIB license
	&#40;see http&#58;//www.alglib.net/copyrules.php for details&#41;
***********************************************************************/
/*
This routines must be defined by the programmer&#58;
static void ludecomposition&#40;ref double&#91;,&#93; a,
	int m,
	int n,
	ref int&#91;&#93; pivots&#41;
static bool invtriangular&#40;ref double&#91;,&#93; a,
	int n,
	bool isupper,
	bool isunittriangular&#41;
*/


/*************************************************************************
Inversion of a matrix given by its LU decomposition.

Input parameters&#58;
	A	   -   LU decomposition of the matrix &#40;output of LUDecomposition subroutine&#41;.
	Pivots  -   table of permutations which were made during the LU decomposition
				&#40;the output of LUDecomposition subroutine&#41;.
	N	   -   size of matrix A.

Output parameters&#58;
	A	   -   inverse of matrix A.
				Array whose indexes range within &#91;1..N, 1..N&#93;.

Result&#58;
	True, if the matrix is not singular.
	False, if the matrix is singular.

  -- LAPACK routine &#40;version 3.0&#41; --
	 Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
	 Courant Institute, Argonne National Lab, and Rice University
	 February 29, 1992
*************************************************************************/
public static bool inverselu&#40;ref double&#91;,&#93; a,
	ref int&#91;&#93; pivots,
	int n&#41;
{
	bool result = new bool&#40;&#41;;
	double&#91;&#93; work = new double&#91;0&#93;;
	int i = 0;
	int iws = 0;
	int j = 0;
	int jb = 0;
	int jj = 0;
	int jp = 0;
	int jp1 = 0;
	double v = 0;
	int i_ = 0;


	result = true;
	
	//
	// Quick return if possible
	//
	if&#40; n==0 &#41;
	{
		return result;
	}
	work = new double&#91;n+1&#93;;
	
	//
	// Form inv&#40;U&#41;
	//
	if&#40; !invtriangular&#40;ref a, n, true, false&#41; &#41;
	{
		result = false;
		return result;
	}
	
	//
	// Solve the equation inv&#40;A&#41;*L = inv&#40;U&#41; for inv&#40;A&#41;.
	//
	for&#40;j=n; j>=1; j--&#41;
	{
		
		//
		// Copy current column of L to WORK and replace with zeros.
		//
		for&#40;i=j+1; i<=n; i++&#41;
		{
			work&#91;i&#93; = a&#91;i,j&#93;;
			a&#91;i,j&#93; = 0;
		}
		
		//
		// Compute current column of inv&#40;A&#41;.
		//
		if&#40; j<n &#41;
		{
			jp1 = j+1;
			for&#40;i=1; i<=n; i++&#41;
			{
				v = 0.0;
				for&#40;i_=jp1; i_<=n;i_++&#41;
				{
					v += a&#91;i,i_&#93;*work&#91;i_&#93;;
				}
				a&#91;i,j&#93; = a&#91;i,j&#93;-v;
			}
		}
	}
	
	//
	// Apply column interchanges.
	//
	for&#40;j=n-1; j>=1; j--&#41;
	{
		jp = pivots&#91;j&#93;;
		if&#40; jp!=j &#41;
		{
			for&#40;i_=1; i_<=n;i_++&#41;
			{
				work&#91;i_&#93; = a&#91;i_,j&#93;;
			}
			for&#40;i_=1; i_<=n;i_++&#41;
			{
				a&#91;i_,j&#93; = a&#91;i_,jp&#93;;
			}
			for&#40;i_=1; i_<=n;i_++&#41;
			{
				a&#91;i_,jp&#93; = work&#91;i_&#93;;
			}
		}
	}
	return result;
}


/*************************************************************************
Inversion of a general matrix.

Input parameters&#58;
	A   -   matrix. Array whose indexes range within &#91;1..N, 1..N&#93;.
	N   -   size of matrix A.

Output parameters&#58;
	A   -   inverse of matrix A.
			Array whose indexes range within &#91;1..N, 1..N&#93;.

Result&#58;
	True, if the matrix is not singular.
	False, if the matrix is singular.

  -- ALGLIB --
	 Copyright 2005 by Bochkanov Sergey
*************************************************************************/
public static bool inverse&#40;ref double&#91;,&#93; a,
	int n&#41;
{
	bool result = new bool&#40;&#41;;
	int&#91;&#93; pivots = new int&#91;0&#93;;


	ludecomposition&#40;ref a, n, n, ref pivots&#41;;
	result = inverselu&#40;ref a, ref pivots, n&#41;;
	return result;
}

C++ :

inv.cpp

/***********************************************************************
This code is generated by the AlgoPascal translator

This code is distributed under the ALGLIB license
	&#40;see http&#58;//www.alglib.net/copyrules.php for details&#41;
***********************************************************************/

#include &#34;ap.h&#34;

/*-----------------------------------------------
This routines must be defined by the programmer&#58;

void ludecomposition&#40;ap&#58;&#58;real_2d_array& a,
	 int m,
	 int n,
	 ap&#58;&#58;integer_1d_array& pivots&#41;;
bool invtriangular&#40;ap&#58;&#58;real_2d_array& a,
	 int n,
	 bool isupper,
	 bool isunittriangular&#41;;
-----------------------------------------------*/

bool inverselu&#40;ap&#58;&#58;real_2d_array& a,
	 const ap&#58;&#58;integer_1d_array& pivots,
	 int n&#41;;
bool inverse&#40;ap&#58;&#58;real_2d_array& a, int n&#41;;

/*************************************************************************
Inversion of a matrix given by its LU decomposition.

Input parameters&#58;
	A	   -   LU decomposition of the matrix &#40;output of LUDecomposition subroutine&#41;.
	Pivots  -   table of permutations which were made during the LU decomposition
				&#40;the output of LUDecomposition subroutine&#41;.
	N	   -   size of matrix A.

Output parameters&#58;
	A	   -   inverse of matrix A.
				Array whose indexes range within &#91;1..N, 1..N&#93;.

Result&#58;
	True, if the matrix is not singular.
	False, if the matrix is singular.

  -- LAPACK routine &#40;version 3.0&#41; --
	 Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
	 Courant Institute, Argonne National Lab, and Rice University
	 February 29, 1992
*************************************************************************/
bool inverselu&#40;ap&#58;&#58;real_2d_array& a,
	 const ap&#58;&#58;integer_1d_array& pivots,
	 int n&#41;
{
	bool result;
	ap&#58;&#58;real_1d_array work;
	int i;
	int iws;
	int j;
	int jb;
	int jj;
	int jp;
	int jp1;
	double v;

	result = true;
	
	//
	// Quick return if possible
	//
	if&#40; n==0 &#41;
	{
		return result;
	}
	work.setbounds&#40;1, n&#41;;
	
	//
	// Form inv&#40;U&#41;
	//
	if&#40; !invtriangular&#40;a, n, true, false&#41; &#41;
	{
		result = false;
		return result;
	}
	
	//
	// Solve the equation inv&#40;A&#41;*L = inv&#40;U&#41; for inv&#40;A&#41;.
	//
	for&#40;j = n; j >= 1; j--&#41;
	{
		
		//
		// Copy current column of L to WORK and replace with zeros.
		//
		for&#40;i = j+1; i <= n; i++&#41;
		{
			work&#40;i&#41; = a&#40;i,j&#41;;
			a&#40;i,j&#41; = 0;
		}
		
		//
		// Compute current column of inv&#40;A&#41;.
		//
		if&#40; j<n &#41;
		{
			jp1 = j+1;
			for&#40;i = 1; i <= n; i++&#41;
			{
				v = ap&#58;&#58;vdotproduct&#40;a.getrow&#40;i, jp1, n&#41;, work.getvector&#40;jp1, n&#41;&#41;;
				a&#40;i,j&#41; = a&#40;i,j&#41;-v;
			}
		}
	}
	
	//
	// Apply column interchanges.
	//
	for&#40;j = n-1; j >= 1; j--&#41;
	{
		jp = pivots&#40;j&#41;;
		if&#40; jp!=j &#41;
		{
			ap&#58;&#58;vmove&#40;work.getvector&#40;1, n&#41;, a.getcolumn&#40;j, 1, n&#41;&#41;;
			ap&#58;&#58;vmove&#40;a.getcolumn&#40;j, 1, n&#41;, a.getcolumn&#40;jp, 1, n&#41;&#41;;
			ap&#58;&#58;vmove&#40;a.getcolumn&#40;jp, 1, n&#41;, work.getvector&#40;1, n&#41;&#41;;
		}
	}
	return result;
}


/*************************************************************************
Inversion of a general matrix.

Input parameters&#58;
	A   -   matrix. Array whose indexes range within &#91;1..N, 1..N&#93;.
	N   -   size of matrix A.

Output parameters&#58;
	A   -   inverse of matrix A.
			Array whose indexes range within &#91;1..N, 1..N&#93;.

Result&#58;
	True, if the matrix is not singular.
	False, if the matrix is singular.

  -- ALGLIB --
	 Copyright 2005 by Bochkanov Sergey
*************************************************************************/
bool inverse&#40;ap&#58;&#58;real_2d_array& a, int n&#41;
{
	bool result;
	ap&#58;&#58;integer_1d_array pivots;

	ludecomposition&#40;a, n, n, pivots&#41;;
	result = inverselu&#40;a, pivots, n&#41;;
	return result;
}

GraphCalc 4.0.1

24-08-2007 - 00:42

Hjx , đăng ký mem lâu roài mà giờ mới có dịp vào lại 4rum :">

Nay post cái soft này cho anh em xài, không biết có đụng hàng không nữa :)

Không cho upload thôi đành up lên rapid :"> anh em chịu khó nhá :D

Nếu ai không down được thì liên hệ với email tui là : [email protected]

http://rapidshare.co...phCalc4.0.1.zip