代数余子式 C# 伴随矩阵我想用C#编写一个矩阵类,其中的一个函数是求矩阵的伴随矩阵,我是想利用代数余子式来求伴随矩阵,可是如何利用程序来实现呢?请高手帮忙!

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/28 02:14:41
代数余子式 C# 伴随矩阵我想用C#编写一个矩阵类,其中的一个函数是求矩阵的伴随矩阵,我是想利用代数余子式来求伴随矩阵,可是如何利用程序来实现呢?请高手帮忙!
x[}OF*Tp$ǀN:i覵Q49DT$}eBGkZ[K藱9% 8E{{iϜU,,n/<: 7G狿??Z0P._:]_nq5w_ܫC ˛0dG=M=|/|{razFmhVNdzl:G-`;9i03A0/ˠW~vͼB-B/bm:|<+iS*nR,*e84$>%Tv K``ֿ`ePKHf/~ ޗ+%*!aGAV?{^<[?WQx-|@Pz\ I5RmU8YĹ nAa?3Īg@M)~G" K#5x.(_Y}tӕDL88nNU^1%y5nG%>nwD (;(Sx\ : ~0!EA]΍ ELCǻZƱ`((A $7ݜ +BM̦X-iߘ܈(){)agu"1 X4BdC,`D8:DЎBjI;i - NC4Z{ќ}(W 1AOSvrMv #7SNrPƀrSA1p(r$pT)T:C)oZHU//fbdNTInHwR>x%뉣[ :o^ڄH14L:A3JRn>5#t3 u %qdwrE& FIc,PTNqܢUspC0ѹzI,E 2c? ]K\9˚!ڰD:yF[Zz_Ey <.NpKy tQ8N}Z{Tsw!h~%[8߫ZY]j&>ġ/H2e)[oK\;Z[A]!!\&A+ nV u&)2Pln6$GW,HRzS(ޯ`ےC8xAWC1^%x*pu'HV 0R Qz ;{\_WrUvծt{mTe \ X3OXAfl2LQ뫰ezjM\;”mP€ "0+ }Q0ݟG$UwD|x3=t3|ΐT#z@ܗ횚}xC5xTVs3bG0B$/WF[b ^M@!R6V#0d@4yxS0qٌ '3W56'uH99\t7G$VXE%a2n [t o?dusfs\= AYTXw׎j+j.|\iYRA_S8(8r fGO ˛ ,LV&ME~.'t|HW,H !˂sIJ2p}J[J} HJPQ)R% 8Ҭ1҈ jw-7ݑRfگsRC-q=jpw觳wV;ю1 -Mzeɜa Nu' Й ]L_a[p1?Y7DaESXIU6@K^=u9.u1Rny L/HePуD w+/SS 3mzN[]/N0}s*{8m<<ﯳ] ܃j5}*-uV3Fs@

代数余子式 C# 伴随矩阵我想用C#编写一个矩阵类,其中的一个函数是求矩阵的伴随矩阵,我是想利用代数余子式来求伴随矩阵,可是如何利用程序来实现呢?请高手帮忙!
代数余子式 C# 伴随矩阵
我想用C#编写一个矩阵类,其中的一个函数是求矩阵的伴随矩阵,我是想利用代数余子式来求伴随矩阵,可是如何利用程序来实现呢?请高手帮忙!

代数余子式 C# 伴随矩阵我想用C#编写一个矩阵类,其中的一个函数是求矩阵的伴随矩阵,我是想利用代数余子式来求伴随矩阵,可是如何利用程序来实现呢?请高手帮忙!
public class Matrix
{
double[,] A;
//m行n列
int m, n;
string name;
public Matrix(int am, int an)
{
m = am;
n = an;
A = new double[m, n];
name = "Result";
}
public Matrix(int am, int an, string aName)
{
m = am;
n = an;
A = new double[m, n];
name = aName;
}
public int getM
{
get { return m; }
}
public int getN
{
get { return n; }
}
public double[,] Detail
{
get { return A; }
set { A = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
class MatrixOperator
{
MatrixOperator()
{ }
///
/// 矩阵加法
///
///
///
///
public static Matrix MatrixAdd(Matrix Ma, Matrix Mb)
{
int m = Ma.getM;
int n = Ma.getN;
int m2 = Mb.getM;
int n2 = Mb.getN;
if ((m != m2) || (n != n2))
{
Exception myException = new Exception("数组维数不匹配");
throw myException;
}
Matrix Mc = new Matrix(m, n);
double[,] c = Mc.Detail;
double[,] a = Ma.Detail;
double[,] b = Mb.Detail;
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i, j] = a[i, j] + b[i, j];
return Mc;
}
///
/// 矩阵减法
///
///
///
///
public static Matrix MatrixSub(Matrix Ma, Matrix Mb)
{
int m = Ma.getM;
int n = Ma.getN;
int m2 = Mb.getM;
int n2 = Mb.getN;
if ((m != m2) || (n != n2))
{
Exception myException = new Exception("数组维数不匹配");
throw myException;
}
Matrix Mc = new Matrix(m, n);
double[,] c = Mc.Detail;
double[,] a = Ma.Detail;
double[,] b = Mb.Detail;
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i, j] = a[i, j] - b[i, j];
return Mc;
}
///
/// 矩阵打印
///
///
///
public static string MatrixPrint(Matrix Ma)
{
string s;
s = Ma.Name + ":\n";
int m = Ma.getM;
int n = Ma.getN;
double[,] a = Ma.Detail;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
s += a[i, j].ToString("0.0000") + "\t";
}
s += "\n";
}
return s;
}
///
/// 矩阵乘法
///
///
///
///
public static Matrix MatrixMulti(Matrix Ma, Matrix Mb)
{
int m = Ma.getM;
int n = Ma.getN;
int m2 = Mb.getM;
int n2 = Mb.getN;
if (n != m2)
{
Exception myException = new Exception("数组维数不匹配");
throw myException;
}
Matrix Mc = new Matrix(m, n2);
double[,] c = Mc.Detail;
double[,] a = Ma.Detail;
double[,] b = Mb.Detail;
int i, j, k;
for (i = 0; i < m; i++)
for (j = 0; j < n2; j++)
{
c[i, j] = 0;
for (k = 0; k < n; k++)
c[i, j] += a[i, k] * b[k, j];
}
return Mc;
}
///
/// 矩阵数乘
///
///
///
///
public static Matrix MatrixSimpleMulti(double k, Matrix Ma)
{
int n = Ma.getN;
int m = Ma.getM;
Matrix Mc = new Matrix(m, n);
double[,] c = Mc.Detail;
double[,] a = Ma.Detail;
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i, j] = a[i, j] * k;
return Mc;
}
///
/// 矩阵转置
///
///
///
///
public static Matrix MatrixTrans(Matrix Ma)
{
int m = Ma.getM;
int n = Ma.getN;
Matrix Mc = new Matrix(n, m);
double[,] c = Mc.Detail;
double[,] a = Ma.Detail;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
c[i, j] = a[j, i];
return Mc;
}
///
/// 矩阵求逆(高斯法)
///
///
///
public static Matrix MatrixInv(Matrix Ma)
{
int m = Ma.getM;
int n = Ma.getN;
if (m != n)
{
Exception myException = new Exception("数组维数不匹配");
throw myException;
}
Matrix Mc = new Matrix(m, n);
double[,] a0 = Ma.Detail;
double[,] a = (double[,])a0.Clone();
double[,] b = Mc.Detail;
int i, j, row, k;
double max, temp;
//单位矩阵
for (i = 0; i < n; i++)
{
b[i, i] = 1;
}
for (k = 0; k < n; k++)
{
max = 0; row = k;
//找最大元,其所在行为row
for (i = k; i < n; i++)
{
temp = Math.Abs(a[i, k]);
if (max < temp)
{
max = temp;
row = i;
}
}
if (max == 0)
{
Exception myException = new Exception("没有逆矩阵");
throw myException;
}
//交换k与row行
if (row != k)
{
for (j = 0; j < n; j++)
{
temp = a[row, j];
a[row, j] = a[k, j];
a[k, j] = temp;
temp = b[row, j];
b[row, j] = b[k, j];
b[k, j] = temp;
}
}
//首元化为1
for (j = k + 1; j < n; j++) a[k, j] /= a[k, k];
for (j = 0; j < n; j++) b[k, j] /= a[k, k];
a[k, k] = 1;
//k列化为0
//对a
for (j = k + 1; j < n; j++)
{
for (i = 0; i < k; i++) a[i, j] -= a[i, k] * a[k, j];
for (i = k + 1; i < n; i++) a[i, j] -= a[i, k] * a[k, j];
}
//对b
for (j = 0; j < n; j++)
{
for (i = 0; i < k; i++) b[i, j] -= a[i, k] * b[k, j];
for (i = k + 1; i < n; i++) b[i, j] -= a[i, k] * b[k, j];
}
for (i = 0; i < n; i++) a[i, k] = 0;
a[k, k] = 1;
}
return Mc;
}
///
/// 矩阵求逆(伴随矩阵法)
///
///
///
public static Matrix MatrixInvByCom(Matrix Ma)
{
double d = MatrixOperator.MatrixDet(Ma);
if (d == 0)
{
Exception myException = new Exception("没有逆矩阵");
throw myException;
}
Matrix Ax = MatrixOperator.MatrixCom(Ma);
Matrix An = MatrixOperator.MatrixSimpleMulti((1.0 / d), Ax);
return An;
}
///
/// 对应行列式的代数余子式矩阵
///
///
///
public static Matrix MatrixSpa(Matrix Ma, int ai, int aj)
{
int m = Ma.getM;
int n = Ma.getN;
if (m != n)
{
Exception myException = new Exception("数组维数不匹配");
throw myException;
}
int n2 = n - 1;
Matrix Mc = new Matrix(n2, n2);
double[,] a = Ma.Detail;
double[,] b = Mc.Detail;
//左上
for (int i = 0; i < ai; i++)
for (int j = 0; j < aj; j++)
{
b[i, j] = a[i, j];
}
//右下
for (int i = ai; i < n2; i++)
for (int j = aj; j < n2; j++)
{
b[i, j] = a[i + 1, j + 1];
}
//右上
for (int i = 0; i < ai; i++)
for (int j = aj; j < n2; j++)
{
b[i, j] = a[i, j + 1];
}
//左下
for (int i = ai; i < n2; i++)
for (int j = 0; j < aj; j++)
{
b[i, j] = a[i + 1, j];
}
//符号位
if ((ai + aj) % 2 != 0)
{
for (int i = 0; i < n2; i++)
b[i, 0] = -b[i, 0];
}
return Mc;
}
///
/// 矩阵的行列式
///
///
///
public static double MatrixDet(Matrix Ma)
{
int m = Ma.getM;
int n = Ma.getN;
if (m != n)
{
Exception myException = new Exception("数组维数不匹配");
throw myException;
}
double[,] a = Ma.Detail;
if (n == 1) return a[0, 0];
double D = 0;
for (int i = 0; i < n; i++)
{
D += a[1, i] * MatrixDet(MatrixSpa(Ma, 1, i));
}
return D;
}
///
/// 矩阵的伴随矩阵
///
///
///
public static Matrix MatrixCom(Matrix Ma)
{
int m = Ma.getM;
int n = Ma.getN;
Matrix Mc = new Matrix(m, n);
double[,] c = Mc.Detail;
double[,] a = Ma.Detail;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
c[i, j] = MatrixDet(MatrixSpa(Ma, j, i));
return Mc;
}
}
///下面是一个例子
private void Form1_Load(object sender, EventArgs e)
{
//Matrix A = new Matrix(3, 3, "A");
//int n = A.getN;
//int m = A.getM;
//double[,] a = A.Detail;
//a[0, 0] = 5;
//a[0, 1] = 1;
//a[0, 2] = 1;
//a[1, 0] = -6;
//a[1, 1] = 2;
//a[1, 2] = 0;
//a[2, 0] = -5;
//a[2, 1] = -5;
//a[2, 2] = 0;
//Matrix An = MatrixOperator.MatrixInvByCom(A);
//An.Name = "An";
//Matrix B = MatrixOperator.MatrixInv(A);
//B.Name = "B";
//MessageBox.Show(MatrixOperator.MatrixPrint(A));
//MessageBox.Show(MatrixOperator.MatrixPrint(B));
//MessageBox.Show(MatrixOperator.MatrixPrint(An));
//Matrix C = MatrixOperator.MatrixMulti(A, B);
//C.Name = "A*b";
//MessageBox.Show(MatrixOperator.MatrixPrint(C));
//Matrix D = MatrixOperator.MatrixMulti(A, An);
//D.Name = "A*an";
//MessageBox.Show(MatrixOperator.MatrixPrint(D));
//this.Close();
}
这些是我以前在网上找的,肯定能用,从哪找的我找不到了.