/* poly2.c 計算機数学 B プログラム 2 2 変数多項式の和と積 */ #include /* 高々 7 次の多項式の係数を 0 次から辞書式順序で並べる */ /*static int data1[8][8] = {{1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}; static int data2[8][8] = {{1,0,0,0,0,0,0,0}, {2,0,0,0,0,0,0,0}, {3,0,0,1,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}};*/ static int data1[8][8] = {{1,2,3,4,0,0,0,0}, {1,2,3,4,0,0,0,0}, {1,2,3,4,0,0,0,0}, {1,2,3,4,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}; static int data2[8][8] = {{1,2,3,4,0,0,0,0}, {1,2,3,4,0,0,0,0}, {1,2,3,4,0,0,0,0}, {1,2,3,4,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}; /* 多項式の構造体を定義する */ struct polyn2 { char var[2]; // 変数の文字 int deg[2]; // 次数，ただし deg 0 = (-1, -1) とする． int coef[8][8]; // coef[i][j] が (i, j) 次の係数 }; /* 関数の宣言 */ void xypoly(struct polyn2 *pf); void addpoly2(struct polyn2 g, struct polyn2 h, struct polyn2 *pf); void multpoly2(struct polyn2 g, struct polyn2 h, struct polyn2 *pf); int main () { int i, j; struct polyn2 f, g, h; for (i=0; i<8; i++) { for (j=0; j<8; j++) { g.coef[i][j] = data1[i][j]; h.coef[i][j] = data2[i][j]; } } xypoly(&g); xypoly(&h); addpoly2(g,h,&f); for (i=0; i<8; i++) { for (j=0; j<8; j++) { printf("%d ",f.coef[i][j]); } printf("\n"); } printf("\n\n"); multpoly2(g,h,&f); for (i=0; i<8; i++) { for (j=0; j<8; j++) { printf("%d ",f.coef[i][j]); } printf("\n"); } printf("\n"); return 0; } /* 変数を x とし，次数を調べて記入する */ void xypoly(struct polyn2 *pf) { int i, j, d1=-1, d2=-1; pf->var[0] = 'x'; pf->var[1] = 'y'; for (i=0; i<8; i++) { for (j=0; j<8; j++) { if (pf->coef[i][j]!=0) { if (i > d1) d1 = i; if (j > d2) d2 = j; } } } pf->deg[0] = d1; pf->deg[1] = d2; return; } /* 多項式 g, h の和を計算し，ポインタ pf を使い結果を f に入れる */ void addpoly2(struct polyn2 g, struct polyn2 h, struct polyn2 *pf) { int i, j; if (g.var[0] != h.var[0] || g.var[1] != h.var[1]) { printf("error : polynomial variables are not same"); return; } for (i=0; i<8; i++) { for (j=0; j<8; j++) { pf->coef[i][j] = g.coef[i][j] + h.coef[i][j]; } } xypoly(pf); return; } /* 多項式 g, h の積を計算し，ポインタ pf を使い結果を f に入れる */ void multpoly2(struct polyn2 g, struct polyn2 h, struct polyn2 *pf) { int i, j, k1, k2, d1, d2, a1, a2, b1, b2; if (g.var[0] != h.var[0] || g.var[1] != h.var[1]) { printf("error : polynomial variables are not same"); return; } if ((d1 = g.deg[0] + h.deg[0]) > 7 || (d2 = g.deg[1] + h.deg[1]) > 7) { printf("error : degrees are too large"); return; } for (i=0; i<8; i++) { for (j=0; j<8; j++) { pf->coef[i][j]=0; } } for (k1=0; k1<=d1; k1++) { a1 = 0; if (k1 > g.deg[0]) a1 = k1 - g.deg[0]; b1 = k1; if (k1 > h.deg[0]) b1 = h.deg[0]; for (k2=0; k2<=d2; k2++) { a2 = 0; if (k2 > g.deg[1]) a2 = k2 - g.deg[1]; b2 = k2; if (k2 > h.deg[1]) b2 = h.deg[1]; for (i=a1; i<=b1; i++) { for (j=a2; j<=b2; j++) { pf->coef[k1][k2] += g.coef[k1-i][k2-j] *h.coef[i][j]; } } } } pf->deg[0] = d1; pf->deg[1] = d2; pf->var[0] = 'x'; pf->var[1] = 'y'; return; }