稀疏矩阵的存储与快速转置

发布时间:2016-12-10 14:51:19 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"稀疏矩阵的存储与快速转置",主要涉及到稀疏矩阵的存储与快速转置方面的内容,对于稀疏矩阵的存储与快速转置感兴趣的同学可以参考一下。

点击(此处)折叠或打开#include <stdio.h>#include <stdlib.h>typedef struct {    int row;    int col;    int e;}Triplet;typedef struct {    Triplet data[10];    int rows;    int cols;    int nums;}Table;void Create (Table *t,int *Matrix,int m,int n){    int i,j;    /*    for (i=0;i<3;i++)    {    for (j=0;j<4;j++)            printf ("%d ",*(Matrix+i*n+j));        printf ("\n");    }    getch ();    */    t->rows=m;    t->cols=n;    t->nums=0;    for (i=0;i<m;i++)        for (j=0;j<n;j++)            if (*(Matrix+i*n+j)!=0)            {                t->nums++;                t->data[t->nums].row=i+1;                t->data[t->nums].col=j+1;                t->data[t->nums].e=*(Matrix+i*n+j);            }}void Trans (Table M,Table *T){    int col,t,p,q;    int num[10];    int cpot[10];    T->rows=M.cols;    T->cols=M.rows;    T->nums=M.nums;    if (T->nums)    {        for (col=1;col<=M.cols;++col)            num[col]=0;        for (t=1;t<=M.nums;++t)            ++num[M.data[t].col];        cpot[1]=1;        for (col=2;col<=M.cols;++col)            cpot[col]=cpot[col-1]+num[col-1];        for (p=1;p<=M.nums;++p)        {            col=M.data[p].col;            q=cpot[col];            T->data[q].row=M.data[p].col;            T->data[q].col=M.data[p].row;            T->data[q].e=M.data[p].e;            ++cpot[col];        }    }}void ToMatrix(Table t,int *Matrix,int m,int n){    int i,j;    for (i=0;i<m;i++)        for (j=0;j<n;j++)            *(Matrix+i*n+j)=0;    for (i=1;i<=t.nums;i++)    {        *(Matrix+(t.data[i].row-1)*n+(t.data[i].col-1))=t.data[i].e;    }}int main (){    int i,j;    Table A,TA;    int MatrixA[3][4] =        {{3,0,0,5},         {0,-1,0,0},         {0,2,0,0}};    int MatrixB[4][3];    Create (&A,MatrixA,3,4);    Trans (A, &TA);    ToMatrix (TA,MatrixB,4,3);    for (i=0;i<4;i++)    {        for (j=0;j<3;j++)            printf ("%d ",MatrixB[i][j]);        printf ("\n");    }} 点击(此处)折叠或打开#include <stdio.h>#include <stdlib.h>typedef struct {    int row;    int col;    int e;}Triplet;typedef struct {    Triplet data[10];    int rows;    int cols;    int nums;}Table;void Create (Table *t,int *Matrix,int m,int n){    int i,j;    /*    for (i=0;i<3;i++)    {    for (j=0;j<4;j++)            printf ("%d ",*(Matrix+i*n+j));        printf ("\n");    }    getch ();    */    t->rows=m;    t->cols=n;    t->nums=0;    for (i=0;i<m;i++)        for (j=0;j<n;j++)            if (*(Matrix+i*n+j)!=0)            {                t->nums++;                t->data[t->nums].row=i+1;                t->data[t->nums].col=j+1;                t->data[t->nums].e=*(Matrix+i*n+j);            }}void Trans (Table M,Table *T){    int col,t,p,q;    int num[10];    int cpot[10];    T->rows=M.cols;    T->cols=M.rows;    T->nums=M.nums;    if (T->nums)    {        for (col=1;col<=M.cols;++col)            num[col]=0;        for (t=1;t<=M.nums;++t)            ++num[M.data[t].col];        cpot[1]=1;        for (col=2;col<=M.cols;++col)            cpot[col]=cpot[col-1]+num[col-1];        for (p=1;p<=M.nums;++p)        {            col=M.data[p].col;            q=cpot[col];            T->data[q].row=M.data[p].col;            T->data[q].col=M.data[p].row;            T->data[q].e=M.data[p].e;            ++cpot[col];        }    }}void MultSMatrix (Table M,Table N,Table *Q){    int arow,brow,tp,p,q,t,ccol,row,i;    int ctemp[10];    int rpos[10];    int num[10];    int Mrpos[10];    int Nrpos[10];    if (M.cols!=N.rows)        exit (1);    Q->rows=M.rows;    Q->cols=N.cols;    Q->nums=0;    for (row=1;row<=M.rows;++row)         num[row]=0;    for (t=1;t<=M.nums;++t)        ++num[M.data[t].row];    Mrpos[1]=1;    for (row=2;row<=M.rows;++row)        Mrpos[row]=Mrpos[row-1]+num[row-1];    for (row=1;row<=N.rows;++row)         num[row]=0;    for (t=1;t<=N.nums;++t)        ++num[N.data[t].row];    Nrpos[1]=1;    for (row=2;row<=N.rows;++row)        Nrpos[row]=Nrpos[row-1]+num[row-1];    if (M.nums*N.nums!=0)    {        for (arow=1;arow<=M.rows;++arow)        {            for (i=0;i<10;i++)                ctemp[i]=0;            rpos[arow]=Q->nums+1;            if (arow<M.rows)                tp=Mrpos[arow+1];            else                tp=M.nums+1;            for (p=Mrpos[arow];p<tp;++p)            {                brow=M.data[p].col;                if (brow<N.rows)                    t=Nrpos[brow+1];                else                    t=N.nums+1;                for (q=Nrpos[brow];q<t;++q)                {                    ccol=N.data[q].col;                    ctemp[ccol]=ctemp[ccol]+M.data[p].e*N.data[q].e;                }            }            for (ccol=1;ccol<=Q->cols;++ccol)                if (ctemp[ccol])                {                    Q->nums++;                    Q->data[Q->nums].row=arow;                    Q->data[Q->nums].col=ccol;                    Q->data[Q->nums].e=ctemp[ccol];                }        }    }}void ToMatrix(Table t,int *Matrix,int m,int n){    int i,j;    for (i=0;i<m;i++)        for (j=0;j<n;j++)            *(Matrix+i*n+j)=0;    for (i=1;i<=t.nums;i++)    {        *(Matrix+(t.data[i].row-1)*n+(t.data[i].col-1))=t.data[i].e;    }}int main (){    int i,j;    Table A,B,AB;    int MatrixA[3][2] =        {{2,3},         {0,4},         {1,2}};    int MatrixB[2][3] =        {{2,5,1},         {0,2,4}};    int MatrixAB[3][3];    Create (&A,MatrixA,3,2);    Create (&B,MatrixB,2,3);    MultSMatrix (A,B,&AB);    ToMatrix (AB,MatrixAB,3,3);    for (i=0;i<3;i++)    {        for (j=0;j<3;j++)            printf ("%d ",MatrixAB[i][j]);        printf ("\n");    }    getchar ();} 管理员在2009年8月13日编辑了该文章文章。 --> --> 阅读(671) | 评论(0) | 转发(1) | 0 上一篇:迷宫问题 下一篇:二叉树 相关热门文章 建筑工程管理软件信息管理的目... 块设备驱动程序设计 !优惠HP8561E HP8563E频谱分析... !中秋热卖AgilentE4421B信号源... ipcam的几个概念 test123 编写安全代码——小心有符号数... 使用openssl api进行加密解密... 一段自己打印自己的c程序... sql relay的c++接口 怎么样找出BIND中查询并发量多... 可有人在实际的openstack生产... 如下makefile如何编写 sqlldr 参数配置 讨论一下各位所管理的mysql生... 热门推荐 --> 给主人留下些什么吧!~~ 评论热议

上一篇:迷宫问题
下一篇:背包资料

相关文章

相关评论