矩阵的一些操作

 /***********************typedef.h***************/
#define TRUE               1
#define FALSE              0
#define OK                 1
#define ERROR              0
#define INFEASIVLE        -1
#define OVERFLOW          -2
#define LIST_INIT_SIZE   100
#define LISTINCREMENT     10
#define EQUAL              1
#define STACK_INIT_SIZE  100
#define STACKINCREMENT    10
#define MAX              100
#define MAXSIZE          100

/*********************triplet.h*********************/
#include <stdio.h>
#include <malloc.h>
#include <list.h>
#include <typedef.h>

typedef int *Triplet;

typedef struct Triple{
  int i,j;
  ElemType e;
}Triple;

typedef struct RLSMatrix{
  Triple data[MAXSIZE+1];
  int rpos[80];
  int mu,nu,tu;
}RLSMatrix;

 

Status InitTriplet();
Status DestroyTriplet();
Status Get();
Status Put();
Status IsAscendin();
Status IsDescending();
Status Max();
Status Min();

void PrintTriplet();     /*Not In general use,Only for Multiplication.C */
Status MultSMatrix();    /*Not In general use,Only for Multiplication.C */
Status ScanMatrix();     /*Not In general use,Only for Multiplication.C */

                           /*Not In general use,Only for TSMatrix.C */
Status FastTransposeSMatrix();

Status InitTriplet(Triplet *T, ElemType v1,ElemType v2,ElemType v3){
  (*T)=(ElemType *)malloc(3 * sizeof(ElemType));
  if(!T)
    exit(OVERFLOW);
  (*T)[0]=v1; (*T)[1]=v2; (*T)[2]=v3;
  return OK;
}

Status DestroyTriplet(Triplet T){
   free(T);
   T=NULL;
   return OK;
}

Status Get (Triplet T,int i,ElemType *e){
  if(i<1||i>3)
     return ERROR;
  *e=T[i-1];
  return OK;
}

 

Status Put (Triplet T,int i,ElemType *e){
   if(i<1||i>3)return ERROR;
   T[i-1]=*e;
   return OK;
}

Status IsAscending(Triplet T){
  return(T[0]<=T[1])&&(T[1]<=T[2]);
}

Status IsDescending(Triplet T){
   return(T[0]>=T[1])&&(T[1]>=T[2]);
}

Status Max(Triplet T,ElemType *e){
   e[0]=(T[0]>=T[1])?((T[0]>=T[2])?T[0]:T[2]):((T[1]>=T[2])?T[1]:T[2]);
   return OK;
}

Status Min(Triplet T,ElemType *e){
   e[0]=(T[0]<=T[1])?((T[0]<=T[2])?T[0]:T[2]):((T[1]<=T[2])?T[1]:T[2]);
   return OK;
}

void PrintTriplet(RLSMatrix M)
{
  int r,c,n;
  for(r=1,n=1;r<=M.mu;r++){
    for(c=1;c<=M.nu;c++){
      if(r==M.data[n].i&&c==M.data[n].j){
 printf("%-6d",M.data[n].e);
 n++;
      }
      else printf("%-6d",0);
    }
    printf("/n");
  }
  puts("/n");
}

Status MultSMatrix(RLSMatrix M,RLSMatrix N,RLSMatrix *Q)
{
  int arow,tp,brow,p,t,q,ccol,ctemp[100];

  if(M.nu!=N.mu)
  {
    printf("The col of first Matrix is unequal the row of second Matrix!! ");
    return ERROR;
  }

  Q->mu=M.mu;Q->nu=N.nu;Q->tu=0;

  if(M.tu*N.tu!=0){
    for(arow=1;arow<=M.mu;++arow)
    {
      for(p=0;p<=N.nu;p++)
 ctemp[p]=0;

      Q->rpos[arow]=Q->tu+1;

      if(arow<M.mu)
 tp=M.rpos[arow+1];
      else
 tp=M.tu+1;

      for(p=M.rpos[arow];p<tp;p++)
      {
 brow=M.data[p].j;
 if(brow<N.mu)
   t=N.rpos[brow+1];
 else
   t=N.tu+1;
 for(q=N.rpos[brow];q<t;++q)
 {
   ccol=N.data[q].j;
   ctemp[ccol]+=M.data[p].e*N.data[q].e;
 }

      }

      for(ccol=1;ccol<=Q->nu;++ccol)
 if(ctemp[ccol]){
   if(++Q->tu>MAXSIZE) return ERROR;
   Q->data[Q->tu].i=arow;
   Q->data[Q->tu].j=ccol;
   Q->data[Q->tu].e=ctemp[ccol];
 }
      }
  }
  return OK;
}

 

Status ScanMatrix(RLSMatrix *M)
{
  int i,j,k,sum[80];

  printf("/n/nplease input the data size('mu' is sign if Matrix's row;'nu' is the sign of Matrix's col,  and 'e' or 'tu' is the length of Matrix):/nmu=  nu=  tu=/n");
  scanf("%d %d %d",&M->mu,&M->nu,&M->tu);

  if(M->tu>(M->mu*M->nu))
    printf("The third number which you had inputed has error! Too big!!!/n");
  else
  {
    printf("row= col= date= /n");
    for(i=1;i<=M->tu;i++)
    {
      M->data[0].i=0;
      M->data[0].j=0;
      M->data[0].e=0;

      scanf("%d %d %d",&M->data[i].i,&M->data[i].j,&M->data[i].e);
      if((M->data[i].i>M->mu)||
  (M->data[i].j>M->nu)||
  (M->data[i].i<M->data[i-1].i))
 printf("The date you had inputed has error!!!/n");
    }

    for(i=1;i<=M->mu;i++)
      sum[i]=0;

    for(i=1;i<=M->tu;++i)
      ++sum[M->data[i].i];

    M->rpos[1]=1;

    for(i=2;i<=M->mu;++i)
      M->rpos[i]=M->rpos[i-1]+sum[i-1];
  }
}


Status FastTransposeSMatrix(RLSMatrix M,RLSMatrix *T)
{
  int col,t,p,q,num[100],cpot[100];
  T->mu=M.nu;T->nu=M.mu;T->tu=M.tu;
  if(T->tu){
    for(col=1;col<=M.nu;++col) num[col]=0;
    for(t=1;t<=M.tu;++t) ++num[M.data[t].j];
    cpot[1]=1;
    for(col=2;col<=M.nu;++col) cpot[col]=cpot[col-1]+num[col-1];
    for(p=1;p<=M.tu;++p){
      col=M.data[p].j;
      q=cpot[col];
      T->data[q].i=M.data[p].j;
      T->data[q].j=M.data[p].i;
      T->data[q].e=M.data[p].e;
      ++cpot[col];
    }
  }
  return OK;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值