multipoly.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "common.h"
00029
00030 #include <stdio.h>
00031 #include <math.h>
00032 #include <signal.h>
00033
00034 #include "bu.h"
00035 #include "vmath.h"
00036 #include "bn.h"
00037
00038
00039 #define FAILSTR "failure in multipoly.c"
00040
00041
00042 #define Max(a, b) (((a) > (b)) ? (a) : (b))
00043 #define Min(a, b) (((a) > (b)) ? (a) : (b))
00044
00045
00046
00047
00048
00049
00050 struct bn_multipoly *
00051 bn_multipoly_new(int dgrs, int dgrt)
00052 {
00053 struct bn_multipoly *newmp = bu_malloc(sizeof(struct bn_multipoly), FAILSTR);
00054 int i, s, t;
00055
00056 newmp->cf = bu_malloc(dgrs * sizeof(double *), FAILSTR);
00057
00058 for (i = 0; i < dgrs; i++) {
00059 newmp->cf[i] = bu_malloc(dgrt * sizeof(double), FAILSTR);
00060 }
00061
00062 newmp->dgrs = dgrs;
00063 newmp->dgrt = dgrt;
00064 for (s = 0; s < dgrs; s++) {
00065 for (t = 0; t < dgrt; t++) {
00066 newmp->cf[s][t] = 0;
00067 }
00068 }
00069 return newmp;
00070 }
00071
00072
00073
00074
00075
00076
00077 struct bn_multipoly *
00078 bn_multipoly_grow(register struct bn_multipoly *P, int dgrs, int dgrt)
00079 {
00080 int i, j;
00081 if (dgrs > P->dgrs) {
00082 P->cf = bu_realloc(P->cf, dgrs * sizeof(double *), FAILSTR);
00083 for (i = P->dgrs; i < dgrs; i++) {
00084 P->cf[i] = bu_malloc(Max(P->dgrt, dgrt) * sizeof(double), FAILSTR);
00085 for (j = 0; j < Max(P->dgrt, dgrt); j++) {
00086 P->cf[i][j] = 0;
00087 }
00088 }
00089 }
00090 if (dgrt > P->dgrt) {
00091 for (i = 0; i < P->dgrt; i++) {
00092 P->cf[i] = bu_realloc(P->cf, dgrt * sizeof(double *), FAILSTR);
00093 for (j = P->dgrt; j < dgrt; j++) {
00094 P->cf[i][j] = 0;
00095 }
00096 }
00097 }
00098 return P;
00099 }
00100
00101
00102
00103
00104
00105
00106 struct bn_multipoly *
00107 bn_multipoly_set(register struct bn_multipoly *P, int s, int t, double val)
00108 {
00109 bn_multipoly_grow(P, s + 1, t + 1);
00110 P->cf[s][t] = val;
00111 return P;
00112 }
00113
00114
00115
00116
00117
00118
00119 struct bn_multipoly *
00120 bn_multpoly_add(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
00121 {
00122 struct bn_multipoly *sum = bn_multipoly_new(Max(p1->dgrs, p2->dgrs), Max(p1->dgrt, p2->dgrs));
00123 int s, t;
00124 for (s = 0; s < sum->dgrs; s++) {
00125 for (t = 0; t < sum->dgrt; t++) {
00126 sum->cf[s][t] = p1->cf[s][t] + p2->cf[s][t];
00127 }
00128 }
00129 return sum;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 struct bn_multipoly *
00139 bn_multipoly_mul(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
00140 {
00141 struct bn_multipoly *product = bn_multipoly_new(p1->dgrs + p2->dgrs, p1->dgrt + p2->dgrt);
00142 int s1,s2,t1,t2;
00143 for (s1 = 0; s1 < p1->dgrs; s1++) {
00144 for (t1 = 0; t1 < p1->dgrt; t1++) {
00145 for (s2 = 0; s2 < p2->dgrs; s2++) {
00146 for (t2 = 0; t2 < p2->dgrt; p2++) {
00147 product->cf[s1 + s2][t1 + t2] = p1->cf[s1][t1] * p2->cf[s2][t2];
00148 }
00149 }
00150 }
00151 }
00152 return product;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164