From 3b37a00a4be251f87e543d269489cb7a989425d5 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 28 Apr 2001 21:40:25 +0000 Subject: Hop, gros bordel, plein de fichiers ajoutes et supprimes :) --- lib/polynom.c | 277 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 lib/polynom.c (limited to 'lib/polynom.c') diff --git a/lib/polynom.c b/lib/polynom.c new file mode 100644 index 0000000..f1c19a2 --- /dev/null +++ b/lib/polynom.c @@ -0,0 +1,277 @@ +/* + * + * Operations sur les polynomes + * + */ + +#include "polynom.h" +#include "scalaires.h" +#include "exceptions.h" +#include "main.h" +#include +#include +#include +#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define _(x) x +#endif + +/* FIXME: manque div et mod et poly_to_string */ + +polynome ply_constr(rationnel coef, int degre) +{ /* constructeur monome */ + polynome temp; + + if (!coef.num) + return NULL; + + temp = (monome *) Emalloc(sizeof(monome)); + + temp->coef = coef; + temp->degre = degre; + temp->suiv = NULL; + + return temp; +} + +polynome ply_vide(void) +{ /* cree un polynome */ + + return NULL; +} + +void ply_destruct(polynome poly) +{ /* destructeur */ + if (poly) { + ply_destruct(poly->suiv); + free(poly); + } +} + + +polynome ply_copy(polynome poly) +{ /* recopie */ + polynome result = NULL, temp = NULL, t; + + while (poly) { + t = ply_constr(poly->coef, poly->degre); + if (result) { + temp->suiv = t; + temp = t; + } else { + result = t; + temp = t; + } + poly = poly->suiv; + } + return result; +} + + + + +polynome ply_addition(polynome poly1, polynome poly2) +{ /* addition de deux polynomes */ + polynome resultat = NULL, temp = NULL, t; + rationnel newrat; + int degre; + + while (poly1 && poly2) { + if (poly1->degre > poly2->degre) { + t = ply_constr(poly1->coef, poly1->degre); + poly1 = poly1->suiv; + } else if (poly1->degre < poly2->degre) { + t = ply_constr(poly2->coef, poly2->degre); + poly2 = poly2->suiv; + } else { + newrat = rat_addition(poly1->coef, poly2->coef); + degre = poly1->degre; + t = ply_constr(newrat, degre); + poly1 = poly1->suiv; + poly2 = poly2->suiv; + } + if (t) { + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + } + } + + while (poly1) { + t = ply_constr(poly1->coef, poly1->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + poly1 = poly1->suiv; + } + + while (poly2) { + t = ply_constr(poly2->coef, poly2->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + poly2 = poly2->suiv; + } + + return resultat; +} + +polynome ply_soustraction(polynome poly1, polynome poly2) +{ /* soustraction de deux polynomes */ + + polynome resultat = NULL, temp = NULL, t; + rationnel newrat; + int degre; + + while (poly1 && poly2) { + if (poly1->degre > poly2->degre) { + t = ply_constr(poly1->coef, poly1->degre); + poly1 = poly1->suiv; + } else if (poly1->degre < poly2->degre) { + t = ply_constr(rat_moinsunaire(poly2->coef), poly2->degre); + poly2 = poly2->suiv; + } else { + newrat = rat_soustraction(poly1->coef, poly2->coef); + degre = poly1->degre; + t = ply_constr(newrat, degre); + poly1 = poly1->suiv; + poly2 = poly2->suiv; + } + if (t) { + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + } + } + + while (poly1) { + t = ply_constr(poly1->coef, poly1->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + poly1 = poly1->suiv; + } + + while (poly2) { + t = ply_constr(rat_moinsunaire(poly2->coef), poly2->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + poly2 = poly2->suiv; + } + + return resultat; +} + + +polynome ply_multiplication(polynome poly1, polynome poly2) +{ /* multiplication de deux polynomes */ + polynome temp = NULL, t, resultat = NULL; + + while (poly1) { + while (poly2) { + t = + ply_constr(rat_multiplication(poly1->coef, poly2->coef), + poly1->degre + poly2->degre); + if (t) { + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + } + poly2 = poly2->suiv; + } + poly1 = poly1->suiv; + } + + return resultat; +} + +polynome ply_division(polynome poly1, polynome poly2) +{ /* division de deux polynomes */ + polynome result=NULL; + + return result; + + +} + +polynome ply_modulo(polynome poly1, polynome poly2) +{ /* reste de la division de deux polynomes */ + polynome result=NULL; + + return result; +} + +polynome ply_exposant(polynome poly, unsigned int exp) +{ /* exponentiation d'un polynome */ + int i; + polynome result, temp; + + if (poly) { + result = ply_constr(rat_constr(1, 1), 0); + for (i = 0; i < exp; i++) { + temp = ply_multiplication(result, poly); + ply_destruct(result); + result = temp; + } + } else { + result = NULL; + } + return result; + +} + +double ply_valuation(polynome poly, double point) +{ /* valuation d'un polynome en un point */ + double result = 0; + + while (poly) { + result += rat_to_double(poly->coef) * pow(point, (double) (poly->degre)); + } + return result; +} + +char *ply_affichage(polynome poly) +{ /* routine d'affichage d'un polynome */ + char buf[BUFSIZ], temp[BUFSIZ]; /* FIXME: pas glop comme routine, malloquer tout ca ? */ + + while (poly) { + if (poly->degre != 0) { + sprintf(temp, "%+f*%c^%u", rat_to_double(poly->coef), mute, poly->degre); + } else { + sprintf(temp, "%+f", rat_to_double(poly->coef)); + } + strcat(buf, temp); /* FIXME: gerer le depassement de buf si po malloc */ + } + return Estrdup(buf); +} -- cgit v1.2.3