CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   Elemtary matrix to CSR global matrix (http://www.cfd-online.com/Forums/main/5184-elemtary-matrix-csr-global-matrix.html)

xueying September 19, 2002 12:42

Elemtary matrix to CSR global matrix
 
How can I assemble elemtary matrix to global matrix in CSR format? If anybody has this subroutine, can you share it with me?

Wen Long September 22, 2002 14:50

Re: Elemtary matrix to CSR global matrix
 
Here is my C progam, can you try it?

//--------------

/*Using Compressed Row Storage method to represente a sparce matrix to do A*Vin=Vout (matrix vector multiplying) */

#include <stdio.h> #include <stdlib.h>

/*#define N 100; */ /*The maximum size of the matrix is N*N */

void main(void) { float E[10000];

long int C[10000],R[101]; /* E[]----Element value vector

C[]----Column index vector

R[]----Row position vector */

float Vin[100], Vout[100]; /* Vin[N]---Input vector */

/* Vout[N]---Output vector, Vout[]=A*Vin[] */

long int n; /*actual matrix size */ long int num; long int i; void multiply(float*,long int *, long int *, long int ,float *,float * );

Again:

printf("Input size of matrix A, n=:\n"); scanf("%d",&n); if(n>100) {printf("n must < 100!, try again\n"); goto Again; }

printf("Input total number of non-zero elements in A\n"); scanf("%d",&num);

for(i=0;i<num; i++) /* input E[] and C[] */

{

printf("E[%d]=\n",i);

scanf("%f",&E[i]);

printf("C[%d]=\n",i);

scanf("%d",&C[i]);

}

for(i=0; i<=n; i++) /*input R[] */

{

printf("R[%d]=\n",i);

scanf("%d",&R[i]);

}

for(i=0; i<n; i++) /*input Vin[] */

{

printf("Vin[%d]=\n",i);

scanf("%f",&Vin[i]);

}

multiply(E,C,R,n,Vin,Vout); /*do the calculation */

printf("Vout[%d=",n); /* print Vout[n] */ for(i=0; i<n; i++)

{

printf("%10f\n",Vout[i]);

}

}

void multiply(float *pE,

long int *pC,

long int *pR,

long int n,

float *pVin,

float *pVout)

{

/* pE----pointer to array E */

/* pC----pointer to array C */

/* pR----pointer to array R */

/* pVin---pointer to array Vin */

/* pVout---pointer to array Vout */

int i,j;

for(i=0; i<n; i++) /* row i of A */

{

pVout[i]=0;

for(j=pR[i];j<pR[i+1];j++) /*j is the range of index of row i in E[] */

{

pVout[i]=pVout[i]+pE[j]*pVin[pC[j]]; /* sum up */

}

}

}


Wen Long September 24, 2002 09:44

Re: Elemtary matrix to CSR global matrix
 
Sorry, some chars missing when I was copying it (seems funny).

Here is the right one:

/*Using Compressed Row Storage method to represente a sparce matrix to do A*Vin (matrix vector multiplying) */

#include <stdio.h> #include <stdlib.h>

/*#define N 100; */ /*The maximum size of the matrix is N*N */

void main(void) { float E[10000];

long int C[10000],R[101]; /* E[]----Element value vector

C[]----Column index vector

R[]----Row position vector */

float Vin[100], Vout[100]; /* Vin[N]---Input vector */

/* Vout[N]---Output vector, Vout[]=A*Vin[] */

long int n; /*actual matrix size */ long int num; long int i; void multiply(float*,long int *, long int *, long int ,float *,float * );

Again:

printf("Input size of matrix A, n=:\n"); scanf("%d",&n); if(n>100) {printf("n must < 100!, try again\n"); goto Again; }

printf("Input total number of non-zero elements in A\n"); scanf("%d",&num);

for(i=0; i < num ; i++)

/* input E[] and C[] */

{

printf("E[%d]=\n",i);

scanf("%f",&E[i]);

printf("C[%d]=\n",i);

scanf("%d",&C[i]);

}

for(i=0; i < = n; i++) /*input R[] */

{

printf("R[%d]=\n",i);

scanf("%d",&R[i]);

}

for(i=0; i < n; i++)

/*input Vin[] */

{

printf("Vin[%d]=\n",i);

scanf("%f",&Vin[i]);

}

multiply(E,C,R,n,Vin,Vout); /*do the calculation */

printf("Vout[%d=",n); /* print Vout[n] */

for(i=0 ;

i < n ;

i++)

{

printf("%10f\n",Vout[i]);

}

}

void multiply(float *pE,

long int *pC,

long int *pR,

long int n,

float *pVin,

float *pVout)

{

/* pE----pointer to array E */

/* pC----pointer to array C */

/* pR----pointer to array R */

/* pVin---pointer to array Vin */

/* pVout---pointer to array Vout */

int i,j;

for(i=0;

i < n;

i++) /* row i of A */

{

pVout[i]=0;

for(j=pR[i];j<pR[i+1];j++) /*j is the range of index of row i in E[] */

{

pVout[i]=pVout[i]+pE[j]*pVin[pC[j]]; /* sum up */

}

}

}


All times are GMT -4. The time now is 23:07.