求矩阵的秩

本文介绍了一种用于计算矩阵秩并进行矩阵排名的算法。通过获取用户输入的m×n矩阵,程序实现了矩阵的秩计算和排序,展示了算法在矩阵处理领域的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>

/*  Matrix Ranker V1.2
 *
 *  This c program will get a matrix of mXn from user as input
 *  and will output the ranked version of the matrix.
 *
 *	Created by Eithan Shavit, Nov 2007                            */


void get_matrix (double **M,int m, int n);
void print_matrix (double **M,int m, int n);
int make_lead (double **M,int Srow,int Scol,int m,int n);
void swap_rows (double **M,int x,int y);
void zero_col (double **M,int LeadRow,int Scol,int m,int n);


// gets matrix values from user
void get_matrix (double **M,int m, int n)
{
	int i,j;
	for (i=0;i<m;i++)
	{
		printf ("Enter Values for row no. %d (divided by enter):\n",i+1);
		for (j=0;j<n;j++) scanf ("%lf",&M[i][j]);
	}
}


//


const double eps=0.00001;

// prints matrix of mXn
void print_matrix (double **M,int m, int n)
{
	int i,j,temp;
	for (i=0;i<m;i++) //go over columns
	{
		for (j=0;j<n;j++)  //go over rows
		{
			temp = (int) M[i][j];
			if (((M[i][j]-temp)<eps) && ((M[i][j]-temp)>-eps)) printf 

("%8d",temp); //zeros workaround
			else printf ("%8.2lf",M[i][j]);
		}
		printf ("\n");
	}
}

// makes Srow to be a lead row. if not suitable because of zeros,
// row will be swapped with another.
// returns 0 if starting column is all 0, 1 o.w.
int make_lead (double **M,int Srow,int Scol,int m,int n)
{
	int initial,flag=0;
	double alpha=0;
	initial = Srow;
	while ( (flag==0) && (Srow<=(m-1)))
	{
		if (M[Srow][Scol]<-eps || M[Srow][Scol]>eps) // if cell not zero (using zero 

div workaround)
		{
			// divide row by alpha
			alpha=M[Srow][Scol];
			for (Scol;Scol<=(n-1);Scol++)
			{
				M[Srow][Scol] = (M[Srow][Scol] / alpha);
			}
			swap_rows (M,Srow,initial); //if there was row starting with zeros, it 

will be swapped here eventually
			flag=1;

		}
		Srow++;
	}
	return flag;
}

// swaps 2 rows of the matrix
void swap_rows (double **M,int x,int y)
{
	double *temp;
	temp=M[x];
	M[x]=M[y];
	M[y]=temp;
}



// adds and subtracts values from nearby rows
// in order to create a zero column under lead row.
void zero_col (double **M,int LeadRow,int Scol,int m,int n)
{
	int i,j;
	double alpha=0;
	for (i=0;i<=(m-1);i++)
	{
		if (i==LeadRow) continue;
		alpha = (0 - M[i][Scol]);
		for (j=Scol;j<=(n-1);j++)
		{
			M[i][j]= (M[i][j] + (alpha * M[LeadRow][j]));
		}
	}

}

// 判断第x行是否全部是0

int dcmp(double x)
{
    return (x>eps)-(x<-eps);
}


bool  allZero(double **M,int n,int m,int x)
{
    for(int i=0;i<n;i++)
    {
        if(dcmp(M[x][i])!=0)
        {
              return 0;
        }
    }

    return 1;
}

int  rank(double **M,int m,int n)
{
    int ans=0;
    for(int i=0;i<m;i++)
    {
      if(!allZero(M,m,n,i))
       ans++;
    }

    return ans;
}

int main()
{
	int i,j,n,m,no_zero_col;


	printf ("<< Matrix Ranker >>\n");
	printf ("Enter Matrix's dimensions (m,n), divided by enter:\n");
	scanf("%d\n%d",&m,&n);

	double **M=new double *[m];

	for (i=0;i<m;i++)
	{
       M[i]=new double[n];
	}

	get_matrix (M,m,n); //get matrix from user
	printf ("\nMatrix is :\n");
	print_matrix(M,m,n); //print matrix

	// initiate indexs
	i=0;
	j=0;

	// fire algorithm
	while ((j<=(n-1)) && (i<=(m-1)))
	{
		no_zero_col=0;
		while ((no_zero_col==0) && (j<=(n-1)))
		{
			no_zero_col=make_lead (M,i,j,m,n);
			if (no_zero_col==0) j++;
		}
		if (no_zero_col==0) break;
		zero_col (M,i,j,m,n);
		j++;
		i++;
	}
	printf ("\nRanked Matrix is :\n");
	print_matrix (M,m,n);


    printf("Rank is %d",rank(M,m,n));

	return 0;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值