代数余子式 C# 伴随矩阵
我想用C#编写一个矩阵类,其中的一个函数是求矩阵的伴随矩阵,我是想利用代数余子式来求伴随矩阵,可是如何利用程序来实现呢?请高手帮忙!...
我想用C#编写一个矩阵类,其中的一个函数是求矩阵的伴随矩阵,我是想利用代数余子式来求伴随矩阵,可是如何利用程序来实现呢?请高手帮忙!
展开
2个回答
展开全部
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()
{ }
/// <summary>
/// 矩阵加法
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵减法
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵打印
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵乘法
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵数乘
/// </summary>
/// <param name="k"></param>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵转置
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵求逆(高斯法)
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵求逆(伴随矩阵法)
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 对应行列式的代数余子式矩阵
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵的行列式
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵的伴随矩阵
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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();
}
这些是我以前在网上找的,肯定能用,从哪找的我找不到了。
{
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()
{ }
/// <summary>
/// 矩阵加法
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵减法
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵打印
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵乘法
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵数乘
/// </summary>
/// <param name="k"></param>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵转置
/// </summary>
/// <param name="Ma"></param>
/// <param name="Mb"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵求逆(高斯法)
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵求逆(伴随矩阵法)
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 对应行列式的代数余子式矩阵
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵的行列式
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 矩阵的伴随矩阵
/// </summary>
/// <param name="Ma"></param>
/// <returns></returns>
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();
}
这些是我以前在网上找的,肯定能用,从哪找的我找不到了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C#的没有,C的以前做过有个现成的,算法思想是一样的。可以参考参考
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
void input(float *matrix[],int m,int n);
void output(float *matrix[],int m,int n);
void InputExample();
void Adjoint(float *matrix1[],float *matrix2[],int n);
float AlCo(float* matrix[],int jie,int row,int column);
float determinant(float *matrix[],int n);
float Cofactor(float* matrix[],int jie,int row,int column);
int main()
{
float *matrix1[MAX],*matrix2[MAX];
int n;
printf("输入行列式的阶数:");
scanf("%d",&n);
InputExample();
printf("开始输入矩阵:\n");
input(matrix1,n,n);
Adjoint(matrix1,matrix2,n);
printf("它的伴随矩阵是:\n");
output(matrix2,n,n);
return 0;
}
void input(float *matrix[],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
matrix[i]=(float *)malloc(n*sizeof(float));
printf("Please input the matrix:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",matrix[i]+j);
}
void output(float *matrix[],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%f\t",*(matrix[i]+j));
printf("\n");
}
}
void InputExample()
{
printf("数据间用空格间隔,输完一行按回车。例如:\n");
printf("12 26 48\n");
printf("-1 24 10\n");
printf("2 -6 14\n");
}
void Adjoint(float *matrix1[],float *matrix2[],int n)
{
int i,j;
for(i=0;i<n;i++)
matrix2[i]=(float *)malloc(n*sizeof(float));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
*(matrix2[j]+i)=AlCo(matrix1,n,i,j);
}
float Determinant(float* matrix[],int n)
{
float result=0,temp;
int i;
if(n==1)
result=(*matrix[0]);
else
{
for(i=0;i<n;i++)
{
temp=AlCo(matrix,n,n-1,i);
result+=(*(matrix[n-1]+i))*temp;
}
}
return result;
}
float AlCo(float* matrix[],int jie,int row,int column)
{
float result;
if((row+column)%2==0)
result=Cofactor(matrix,jie,row,column);
else result=(-1)*Cofactor(matrix,jie,row,column);
return result;
}
float Cofactor(float* matrix[],int jie,int row,int column)
{
float result;
int i,j;
float* smallmatr[MAX-1];
for(i=0;i<jie-1;i++)
smallmatr[i]=(float*)malloc(sizeof(float)*(jie-1));
for(i=0;i<row;i++)
for(j=0;j<column;j++)
*(smallmatr[i]+j)=*(matrix[i]+j);
for(i=row;i<jie-1;i++)
for(j=0;j<column;j++)
*(smallmatr[i]+j)=*(matrix[i+1]+j);
for(i=0;i<row;i++)
for(j=column;j<jie-1;j++)
*(smallmatr[i]+j)=*(matrix[i]+j+1);
for(i=row;i<jie-1;i++)
for(j=column;j<jie-1;j++)
*(smallmatr[i]+j)=*(matrix[i+1]+j+1);
result=Determinant(smallmatr,jie-1);
return result;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
void input(float *matrix[],int m,int n);
void output(float *matrix[],int m,int n);
void InputExample();
void Adjoint(float *matrix1[],float *matrix2[],int n);
float AlCo(float* matrix[],int jie,int row,int column);
float determinant(float *matrix[],int n);
float Cofactor(float* matrix[],int jie,int row,int column);
int main()
{
float *matrix1[MAX],*matrix2[MAX];
int n;
printf("输入行列式的阶数:");
scanf("%d",&n);
InputExample();
printf("开始输入矩阵:\n");
input(matrix1,n,n);
Adjoint(matrix1,matrix2,n);
printf("它的伴随矩阵是:\n");
output(matrix2,n,n);
return 0;
}
void input(float *matrix[],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
matrix[i]=(float *)malloc(n*sizeof(float));
printf("Please input the matrix:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",matrix[i]+j);
}
void output(float *matrix[],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%f\t",*(matrix[i]+j));
printf("\n");
}
}
void InputExample()
{
printf("数据间用空格间隔,输完一行按回车。例如:\n");
printf("12 26 48\n");
printf("-1 24 10\n");
printf("2 -6 14\n");
}
void Adjoint(float *matrix1[],float *matrix2[],int n)
{
int i,j;
for(i=0;i<n;i++)
matrix2[i]=(float *)malloc(n*sizeof(float));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
*(matrix2[j]+i)=AlCo(matrix1,n,i,j);
}
float Determinant(float* matrix[],int n)
{
float result=0,temp;
int i;
if(n==1)
result=(*matrix[0]);
else
{
for(i=0;i<n;i++)
{
temp=AlCo(matrix,n,n-1,i);
result+=(*(matrix[n-1]+i))*temp;
}
}
return result;
}
float AlCo(float* matrix[],int jie,int row,int column)
{
float result;
if((row+column)%2==0)
result=Cofactor(matrix,jie,row,column);
else result=(-1)*Cofactor(matrix,jie,row,column);
return result;
}
float Cofactor(float* matrix[],int jie,int row,int column)
{
float result;
int i,j;
float* smallmatr[MAX-1];
for(i=0;i<jie-1;i++)
smallmatr[i]=(float*)malloc(sizeof(float)*(jie-1));
for(i=0;i<row;i++)
for(j=0;j<column;j++)
*(smallmatr[i]+j)=*(matrix[i]+j);
for(i=row;i<jie-1;i++)
for(j=0;j<column;j++)
*(smallmatr[i]+j)=*(matrix[i+1]+j);
for(i=0;i<row;i++)
for(j=column;j<jie-1;j++)
*(smallmatr[i]+j)=*(matrix[i]+j+1);
for(i=row;i<jie-1;i++)
for(j=column;j<jie-1;j++)
*(smallmatr[i]+j)=*(matrix[i+1]+j+1);
result=Determinant(smallmatr,jie-1);
return result;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询