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