[BACK]Return to matrix.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Kan

Annotation of OpenXM/src/kan96xx/Kan/matrix.c, Revision 1.4

1.4     ! ohara       1: /* $OpenXM: OpenXM/src/kan96xx/Kan/matrix.c,v 1.3 2001/05/04 01:06:24 takayama Exp $ */
1.1       maekawa     2: /* matrix.c */
                      3: #include <stdio.h>
1.4     ! ohara       4: #include <stdlib.h>
1.1       maekawa     5: #include "datatype.h"
                      6: #include "stackm.h"
                      7: #include "extern.h"
                      8: #include "extern2.h"
                      9:
                     10: #include "matrix.h"   /* ind() and ind2() are defined. */
1.3       takayama   11: /* You must use n and n2. */
1.1       maekawa    12:
                     13: struct arrayOfPOLY *aaAdd(aa,bb)
1.3       takayama   14:      struct arrayOfPOLY *aa,*bb;
                     15:      /* this function has not been tested yet. */
1.1       maekawa    16: {
                     17:   int i;
                     18:   POLY *r;
                     19:   POLY *a;
                     20:   POLY *b;
                     21:   int size;
                     22:   struct arrayOfPOLY *ra;
                     23:   size = aa->n;
                     24:   a = aa->array; b = bb->array;
                     25:   r = (POLY *)sGC_malloc(sizeof(POLY)*size);
                     26:   if (r == (POLY *)NULL) errorMatrix("aaAdd(): no more memory");
                     27:   for (i=0; i<size; i++) {
                     28:     r[i] = ppAdd(a[i],b[i]);
                     29:   }
                     30:   ra = (struct arrayOfPOLY *)sGC_malloc(sizeof(struct arrayOfPOLY));
                     31:   if (ra == (struct arrayOfPOLY *)NULL) errorMatrix("No more memory.");
                     32:   ra->n = size; ra->array = r;
                     33:   return(ra);
                     34: }
                     35:
                     36: struct matrixOfPOLY *aaMult(aa,bb)
1.3       takayama   37:      struct matrixOfPOLY *aa,*bb;
1.1       maekawa    38: {
                     39:   POLY *a;
                     40:   POLY *b;
                     41:   int m,n,m2,n2; /* c.f. matrix.h */
                     42:   /*  (m,n) * (m2,n2) */
                     43:   int i,j,k;
                     44:   POLY *r;
                     45:   POLY tmp;
                     46:   struct matrixOfPOLY *rmat;
                     47:
                     48:   m = aa->m; n = aa->n;
                     49:   m2 = bb->m; n2 = bb->n;
                     50:   a = aa->mat; b = bb->mat;
                     51:   r = (POLY *)sGC_malloc(sizeof(POLY)*m*n2);
                     52:   if (r == (POLY *)NULL) errorMatrix("aaMult(): no more memory");
                     53:   /* we do not check n == m2. */
                     54:   for (i=0; i<m; i++) {
                     55:     for (j=0; j<n2; j++) {
                     56:       tmp = ZERO;
                     57:       for (k=0; k<n; k++) {
1.3       takayama   58:         tmp = ppAddv(tmp, ppMult( a[ind(i,k)], b[ind2(k,j)]));
1.1       maekawa    59:       }
                     60:       r[ind2(i,j)] = tmp;
                     61:     }
                     62:   }
                     63:   rmat = (struct matrixOfPOLY *)sGC_malloc(sizeof(struct matrixOfPOLY));
                     64:   if (rmat == (struct matrixOfPOLY *)NULL) errorMatrix("No more memory.");
                     65:   rmat->m = m; rmat->n = n2;
                     66:   rmat->mat = r;
                     67:   return(rmat);
                     68: }
                     69:
                     70:
                     71: /****************  error handler ************************/
                     72: void errorMatrix(str)
1.3       takayama   73:      char *str;
1.1       maekawa    74: {
                     75:   fprintf(stderr,"matrix.c: %s\n",str);
                     76:   exit(10);
                     77: }
                     78:
                     79: /********************************************************/
                     80:
                     81: struct arrayOfPOLY *newArrayOfPOLY(size)
1.3       takayama   82:      int size;
                     83:      /* if size<=0, it returns [0,null]. */
1.1       maekawa    84: {
                     85:   struct arrayOfPOLY *ap;
                     86:   ap = (struct arrayOfPOLY *)sGC_malloc(sizeof(struct arrayOfPOLY));
                     87:   if (ap == (struct arrayOfPOLY *)NULL) errorMatrix("No more memory.");
                     88:   if (size <=0) {
                     89:     ap->n = 0; ap->array = (POLY *)NULL;
                     90:     return(ap);
                     91:   }
                     92:   ap->n = size;
                     93:   ap->array = (POLY *)sGC_malloc(size*sizeof(POLY));
                     94:   if (ap->array == (POLY *)NULL) errorMatrix("No more memory.");
                     95:   return(ap);
                     96: }
                     97:
                     98: struct matrixOfPOLY *newMatrixOfPOLY(m,n)
1.3       takayama   99:      int m,n;
                    100:      /* if size<=0, it returns [0,null]. */
1.1       maekawa   101: {
                    102:   struct matrixOfPOLY *ap;
                    103:   ap = (struct matrixOfPOLY *)sGC_malloc(sizeof(struct matrixOfPOLY));
                    104:   if (ap == (struct matrixOfPOLY *)NULL) errorMatrix("No more memory.");
                    105:   if ((m <=0) || (n <= 0)) {
                    106:     ap->m = ap->n = 0; ap->mat = (POLY *)NULL;
                    107:     return(ap);
                    108:   }
                    109:   ap->m = m; ap->n = n;
                    110:   ap->mat = (POLY *)sGC_malloc(m*n*sizeof(POLY));
                    111:   if (ap->mat == (POLY *)NULL) errorMatrix("No more memory.");
                    112:   return(ap);
                    113: }
                    114:
                    115:
                    116: struct arrayOfPOLY *carrayToArrayOfPOLY(a,size)
1.3       takayama  117:      POLY a[];
                    118:      int size;
                    119:      /* a[] is read only. */
1.1       maekawa   120: {
                    121:   struct arrayOfPOLY *ans;
                    122:   int i;
                    123:   ans = newArrayOfPOLY(size);
                    124:   for (i=0; i<size; i++) {
                    125:     ans->array[i] = a[i];
                    126:   }
                    127:   return(ans);
                    128: }
                    129:
                    130:
                    131:
                    132:
                    133:
                    134:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>