int main (void) { struct poly *p0 = polySetCoefficient (polySetCoefficient (polySetCoefficient ( polyCreate() , 0, 4.0), 1, -1.0), 10, 2.0); struct poly *p1 = polyCopy (p0); struct poly *p2, *p3, *p4; printf ("%g\n", polyGetCoefficient (p0, 10)); printf ("%g\n", polyGetCoefficient (p0, 100)); printf ("%d\n", polyDegree (p0)); polyPrint (p0); polyPrint (p1); polySetCoefficient (p1, 2, 1.0/2.0); polyPrint (p1); p2 = polyAdd (p0, p1); polyPrint (p2); p3 = polyMultiply (p0, p1); polyPrint (p3); p4 = polyPrime (p0); polyPrint (p4); printf ("%g\n", polyEval (p0, 0.0)); printf ("%g\n", polyEval (p0, 1.0)); printf ("%g\n", polyEval (p0, 2.0)); p0 = polyDelete (p0); p1 = polyDelete (p1); p2 = polyDelete (p2); p3 = polyDelete (p3); p4 = polyDelete (p4); return 0; }
struct poly *polyCopy (struct poly *p) { POLY n; int size, i = 0; if (p == NULL) return polyCreate(); size = sizeof(double) * (p->degree + 1); n = polyCreate(); n->degree = p->degree; n->coef = realloc(n->coef, size); //memcpy(n->coef, p->coef, size); for (; i < p->degree + 1; i++) n->coef[i] = p->coef[i]; return n; }
struct poly *polyMultiply (struct poly *p0, struct poly *p1) { POLY n; int i = 0, j; if (p0 == NULL || p1 == NULL) return NULL; n = polySetCoefficient(polyCreate(), p0->degree + p1->degree, 0); for (; i < p0->degree + 1; i++) for (j = 0; j < p1->degree + 1; j++) n->coef[i + j] += polyGetCoefficient(p0, i) * polyGetCoefficient(p1, j); return n; }
struct poly *polyAdd (struct poly *p0, struct poly *p1) { POLY n; int i = 0; if (p0 == NULL && p1 == NULL) return NULL; else if (p0 == NULL) return polyCopy(p1); else if (p1 == NULL) return polyCopy(p0); n = polySetCoefficient(polyCreate(), MAX(p0->degree, p1->degree), 0); for (; i < n->degree + 1; i++) n->coef[i] = polyGetCoefficient(p0, i) + polyGetCoefficient(p1, i); return n; }