#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;
}
求矩阵的秩
最新推荐文章于 2022-11-01 09:39:51 发布