1、初等变换法:有固定方法,设方程的系数矩阵为A,未知数矩阵为X,常数矩阵为B,即AX=B,要求X,则等式两端同时左乘A^(-1),有X=A^(-1)B。又因为(A,E)~(E,A^(-1)),所以可用初等行变换求A^(-1),从而所有未知数都求出来了。
2、逆矩阵求解法:求解方法:容易算出已知矩阵的行列式等于-1。然后计算伴随阵,具体方法是对于编号为mn的元素,划去原阵的第m行和第n列,原阵退化为n-1阶矩阵,求出这个n-1阶阵的行列式,然后填入伴随阵的第n行第m列位置,最后乘以-1的m+n次幂。下面是做法:
拓展资料:初等变换。一般采用消元法来解线性方程组,而消元法实际上是反复对方程进行变换,而所做的变换也只是以下三种基本的变换所构成:
(1)用一非零的数乘以某一方程
(2)把一个方程的倍数加到另一个方程
(3)互换两个方程的位置
于是,将变换(1)、(2)、(3)称为线性方程组的初等变换。
//矩阵乘积.cpp#include<stdio.h>
void main()
{
//定义矩阵1、2为输入数据矩阵,3为乘积后的矩阵
int cJuZhen1[4][3],cJuZhen2[3][2],cJuZhen3[4][2]
//定义两个指针*p1,*p2分别指向矩阵1、2
int *p1,*p2,i,j
p1=&cJuZhen1[0][0]
p2=&cJuZhen2[0][0]
//输入矩阵1的数据
printf("请输入4*3的矩阵A: \n")
for(i=0i<4i++)
{
for(j=0j<3j++)
{
scanf("%d",cJuZhen1[i][j])
}
}
printf("\n")
//输入矩阵2的数据
printf("请输入3*2的矩阵B: \n")
for(i=0i<3i++)
{
for(j=0j<2j++)
{
scanf("%d",cJuZhen2[i][j])
}
}
printf("\n")
//输出矩阵1
printf("矩阵A:"\n)
for(i=0i<4i++)
{
for(j=0j<3j++)
{
printf("%d",cJuZhen1[i][j])
}
if(j==3)
{
printf("\n")
}
}
printf("\n")
//输出矩阵2
printf("矩阵B: \n")
for(i=0i<3i++)
{
for(j=0j<2j++)
{
printf("%d",cJuZhen2[i][j])
}
if(j==2)
{
printf("\n")
}
}
printf("\n")
//计算矩阵1、2的乘积
printf("两矩阵乘积为: \n")
for(i=0i<4i++)
{
cJuZhen3[i][0]=(*p1)*(*p2)+(*(p1+1))*(*(p2+2))+(*(p1+2))*(*(p2+4))
cJuZhen3[i][1]=(*p1)*(*(p2+1))+(*(p1+1))*(*(p2+3))+(*(p1+2))*(*(p2+5))
p1=p1+3
}
//输出矩阵3(即矩阵乘积)数据
printf("矩阵A与B乘积为: \n")
for(i=0i<4i++)
{
for(j=0j<2j++)
{
printf("%d",cJuZhen3[i][j])
}
if(j==2)
{
printf("\n")
}
}
printf("\n")
}
我写个4X4矩阵的例子,游戏里面经常用的,你自己去改造下即可#include <stdio.h>
#include <memory.h>
#include <math.h>#define EPSILON 1.e-3
class matrix4x4
{
public:
union
{
struct
{
float M11, M12, M13, M14
float M21, M22, M23, M24
float M31, M32, M33, M34
float M41, M42, M43, M44
} float M[4][4]
}
static matrix4x4 ms_Identity
public: matrix4x4( )
{ } matrix4x4(
float _11, float _12, float _13, float _14,
float _21, float _22, float _23, float _24,
float _31, float _32, float _33, float _34,
float _41, float _42, float _43, float _44 )
:
M11( _11 ), M12( _12 ), M13( _13 ), M14( _14 ),
M21( _21 ), M22( _22 ), M23( _23 ), M24( _24 ),
M31( _31 ), M32( _32 ), M33( _33 ), M34( _34 ),
M41( _41 ), M42( _42 ), M43( _43 ), M44( _44 )
{ } matrix4x4( const matrix4x4&Mat )
{
memcpy( &M11, &Mat, sizeof( matrix4x4 ) )
} matrix4x4&operator = ( const matrix4x4&Mat )
{
memcpy( &M11, &Mat, sizeof( matrix4x4 ) )
return *this
} bool operator == ( const matrix4x4&Mat )
{
return ( abs( M[0][0] - Mat.M[0][0] ) <= EPSILON ) &&
( abs( M[0][1] - Mat.M[0][1] ) <= EPSILON ) &&
( abs( M[0][2] - Mat.M[0][2] ) <= EPSILON ) &&
( abs( M[0][3] - Mat.M[0][3] ) <= EPSILON ) &&
( abs( M[1][0] - Mat.M[1][0] ) <= EPSILON ) &&
( abs( M[1][1] - Mat.M[1][1] ) <= EPSILON ) &&
( abs( M[1][2] - Mat.M[1][2] ) <= EPSILON ) &&
( abs( M[1][3] - Mat.M[1][3] ) <= EPSILON ) &&
( abs( M[2][0] - Mat.M[2][0] ) <= EPSILON ) &&
( abs( M[2][1] - Mat.M[2][1] ) <= EPSILON ) &&
( abs( M[2][2] - Mat.M[2][2] ) <= EPSILON ) &&
( abs( M[2][3] - Mat.M[2][3] ) <= EPSILON ) &&
( abs( M[3][0] - Mat.M[3][0] ) <= EPSILON ) &&
( abs( M[3][1] - Mat.M[3][1] ) <= EPSILON ) &&
( abs( M[3][2] - Mat.M[3][2] ) <= EPSILON ) &&
( abs( M[3][3] - Mat.M[3][3] ) <= EPSILON )
} matrix4x4 operator * ( const matrix4x4&Other )
{
matrix4x4 Result
Result.M[0][0] = M[0][0] * Other.M[0][0] + M[0][1] * Other.M[1][0] + M[0][2] * Other.M[2][0] + M[0][3] * Other.M[3][0]
Result.M[0][1] = M[0][0] * Other.M[0][1] + M[0][1] * Other.M[1][1] + M[0][2] * Other.M[2][1] + M[0][3] * Other.M[3][1]
Result.M[0][2] = M[0][0] * Other.M[0][2] + M[0][1] * Other.M[1][2] + M[0][2] * Other.M[2][2] + M[0][3] * Other.M[3][2]
Result.M[0][3] = M[0][0] * Other.M[0][3] + M[0][1] * Other.M[1][3] + M[0][2] * Other.M[2][3] + M[0][3] * Other.M[3][3] Result.M[1][0] = M[1][0] * Other.M[0][0] + M[1][1] * Other.M[1][0] + M[1][2] * Other.M[2][0] + M[1][3] * Other.M[3][0]
Result.M[1][1] = M[1][0] * Other.M[0][1] + M[1][1] * Other.M[1][1] + M[1][2] * Other.M[2][1] + M[1][3] * Other.M[3][1]
Result.M[1][2] = M[1][0] * Other.M[0][2] + M[1][1] * Other.M[1][2] + M[1][2] * Other.M[2][2] + M[1][3] * Other.M[3][2]
Result.M[1][3] = M[1][0] * Other.M[0][3] + M[1][1] * Other.M[1][3] + M[1][2] * Other.M[2][3] + M[1][3] * Other.M[3][3] Result.M[2][0] = M[2][0] * Other.M[0][0] + M[2][1] * Other.M[1][0] + M[2][2] * Other.M[2][0] + M[2][3] * Other.M[3][0]
Result.M[2][1] = M[2][0] * Other.M[0][1] + M[2][1] * Other.M[1][1] + M[2][2] * Other.M[2][1] + M[2][3] * Other.M[3][1]
Result.M[2][2] = M[2][0] * Other.M[0][2] + M[2][1] * Other.M[1][2] + M[2][2] * Other.M[2][2] + M[2][3] * Other.M[3][2]
Result.M[2][3] = M[2][0] * Other.M[0][3] + M[2][1] * Other.M[1][3] + M[2][2] * Other.M[2][3] + M[2][3] * Other.M[3][3] Result.M[3][0] = M[3][0] * Other.M[0][0] + M[3][1] * Other.M[1][0] + M[3][2] * Other.M[2][0] + M[3][3] * Other.M[3][0]
Result.M[3][1] = M[3][0] * Other.M[0][1] + M[3][1] * Other.M[1][1] + M[3][2] * Other.M[2][1] + M[3][3] * Other.M[3][1]
Result.M[3][2] = M[3][0] * Other.M[0][2] + M[3][1] * Other.M[1][2] + M[3][2] * Other.M[2][2] + M[3][3] * Other.M[3][2]
Result.M[3][3] = M[3][0] * Other.M[0][3] + M[3][1] * Other.M[1][3] + M[3][2] * Other.M[2][3] + M[3][3] * Other.M[3][3]
return Result
}
matrix4x4 Inverse( )float Determinant( )
}float matrix4x4::Determinant( )
{
return M[0][0] * (
M[1][1] * (M[2][2] * M[3][3] - M[2][3] * M[3][2]) -
M[2][1] * (M[1][2] * M[3][3] - M[1][3] * M[3][2]) +
M[3][1] * (M[1][2] * M[2][3] - M[1][3] * M[2][2])
) -
M[1][0] * (
M[0][1] * (M[2][2] * M[3][3] - M[2][3] * M[3][2]) -
M[2][1] * (M[0][2] * M[3][3] - M[0][3] * M[3][2]) +
M[3][1] * (M[0][2] * M[2][3] - M[0][3] * M[2][2])
) +
M[2][0] * (
M[0][1] * (M[1][2] * M[3][3] - M[1][3] * M[3][2]) -
M[1][1] * (M[0][2] * M[3][3] - M[0][3] * M[3][2]) +
M[3][1] * (M[0][2] * M[1][3] - M[0][3] * M[1][2])
) -
M[3][0] * (
M[0][1] * (M[1][2] * M[2][3] - M[1][3] * M[2][2]) -
M[1][1] * (M[0][2] * M[2][3] - M[0][3] * M[2][2]) +
M[2][1] * (M[0][2] * M[1][3] - M[0][3] * M[1][2])
)
}
matrix4x4 matrix4x4::Inverse( )
{
matrix4x4 Result
//求行列式
float Det = Determinant( )
if(Det == 0.0f)
{
//没有逆矩阵
return matrix4x4::ms_Identity
} //计算逆矩阵,详细见msdn d3dxmath中描述
float RDet = 1.0f / DetResult.M[0][0] = RDet * (
M[1][1] * (M[2][2] * M[3][3] - M[2][3] * M[3][2]) -
M[2][1] * (M[1][2] * M[3][3] - M[1][3] * M[3][2]) +
M[3][1] * (M[1][2] * M[2][3] - M[1][3] * M[2][2])
)
Result.M[0][1] = -RDet * (
M[0][1] * (M[2][2] * M[3][3] - M[2][3] * M[3][2]) -
M[2][1] * (M[0][2] * M[3][3] - M[0][3] * M[3][2]) +
M[3][1] * (M[0][2] * M[2][3] - M[0][3] * M[2][2])
)
Result.M[0][2] = RDet * (
M[0][1] * (M[1][2] * M[3][3] - M[1][3] * M[3][2]) -
M[1][1] * (M[0][2] * M[3][3] - M[0][3] * M[3][2]) +
M[3][1] * (M[0][2] * M[1][3] - M[0][3] * M[1][2])
)
Result.M[0][3] = -RDet * (
M[0][1] * (M[1][2] * M[2][3] - M[1][3] * M[2][2]) -
M[1][1] * (M[0][2] * M[2][3] - M[0][3] * M[2][2]) +
M[2][1] * (M[0][2] * M[1][3] - M[0][3] * M[1][2])
)Result.M[1][0] = -RDet * (
M[1][0] * (M[2][2] * M[3][3] - M[2][3] * M[3][2]) -
M[2][0] * (M[1][2] * M[3][3] - M[1][3] * M[3][2]) +
M[3][0] * (M[1][2] * M[2][3] - M[1][3] * M[2][2])
)
Result.M[1][1] = RDet * (
M[0][0] * (M[2][2] * M[3][3] - M[2][3] * M[3][2]) -
M[2][0] * (M[0][2] * M[3][3] - M[0][3] * M[3][2]) +
M[3][0] * (M[0][2] * M[2][3] - M[0][3] * M[2][2])
)
Result.M[1][2] = -RDet * (
M[0][0] * (M[1][2] * M[3][3] - M[1][3] * M[3][2]) -
M[1][0] * (M[0][2] * M[3][3] - M[0][3] * M[3][2]) +
M[3][0] * (M[0][2] * M[1][3] - M[0][3] * M[1][2])
)
Result.M[1][3] = RDet * (
M[0][0] * (M[1][2] * M[2][3] - M[1][3] * M[2][2]) -
M[1][0] * (M[0][2] * M[2][3] - M[0][3] * M[2][2]) +
M[2][0] * (M[0][2] * M[1][3] - M[0][3] * M[1][2])
)Result.M[2][0] = RDet * (
M[1][0] * (M[2][1] * M[3][3] - M[2][3] * M[3][1]) -
M[2][0] * (M[1][1] * M[3][3] - M[1][3] * M[3][1]) +
M[3][0] * (M[1][1] * M[2][3] - M[1][3] * M[2][1])
)
Result.M[2][1] = -RDet * (
M[0][0] * (M[2][1] * M[3][3] - M[2][3] * M[3][1]) -
M[2][0] * (M[0][1] * M[3][3] - M[0][3] * M[3][1]) +
M[3][0] * (M[0][1] * M[2][3] - M[0][3] * M[2][1])
)
Result.M[2][2] = RDet * (
M[0][0] * (M[1][1] * M[3][3] - M[1][3] * M[3][1]) -
M[1][0] * (M[0][1] * M[3][3] - M[0][3] * M[3][1]) +
M[3][0] * (M[0][1] * M[1][3] - M[0][3] * M[1][1])
)
Result.M[2][3] = -RDet * (
M[0][0] * (M[1][1] * M[2][3] - M[1][3] * M[2][1]) -
M[1][0] * (M[0][1] * M[2][3] - M[0][3] * M[2][1]) +
M[2][0] * (M[0][1] * M[1][3] - M[0][3] * M[1][1])
)Result.M[3][0] = -RDet * (
M[1][0] * (M[2][1] * M[3][2] - M[2][2] * M[3][1]) -
M[2][0] * (M[1][1] * M[3][2] - M[1][2] * M[3][1]) +
M[3][0] * (M[1][1] * M[2][2] - M[1][2] * M[2][1])
)
Result.M[3][1] = RDet * (
M[0][0] * (M[2][1] * M[3][2] - M[2][2] * M[3][1]) -
M[2][0] * (M[0][1] * M[3][2] - M[0][2] * M[3][1]) +
M[3][0] * (M[0][1] * M[2][2] - M[0][2] * M[2][1])
)
Result.M[3][2] = -RDet * (
M[0][0] * (M[1][1] * M[3][2] - M[1][2] * M[3][1]) -
M[1][0] * (M[0][1] * M[3][2] - M[0][2] * M[3][1]) +
M[3][0] * (M[0][1] * M[1][2] - M[0][2] * M[1][1])
)
Result.M[3][3] = RDet * (
M[0][0] * (M[1][1] * M[2][2] - M[1][2] * M[2][1]) -
M[1][0] * (M[0][1] * M[2][2] - M[0][2] * M[2][1]) +
M[2][0] * (M[0][1] * M[1][2] - M[0][2] * M[1][1])
)
return Result
}matrix4x4 matrix4x4::ms_Identity = matrix4x4( 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 )int main( )
{
matrix4x4 A( 1.0f, 122.0f, 222, 233,
123.f, 23,32,32,
22,33,44,55,
112, 33, 44,33 )matrix4x4 B( 231.0f, 22,32,65,
565.7, 76, 76,44,
54, 645,54, 6,
23,32,56,777 )matrix4x4 X
//AX = B, X = ( A的逆矩阵 ) * Bmatrix4x4 AA = A.Inverse( )
X = AA * Bmatrix4x4 Result = A * X if( Result == B )
{
printf( "结果正确\n" )
}
else
{
if( A.Determinant( ) == 0 )
{
printf( "矩阵A没有逆矩阵\n")
}
else
{
printf( "未知错误?难道是我算法有问题,这不可能,我就是神!!?????!!\n")
}
} return 0}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)