[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.1.1.1

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

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