summaryrefslogtreecommitdiff
path: root/polynom.c
diff options
context:
space:
mode:
Diffstat (limited to 'polynom.c')
-rw-r--r--polynom.c276
1 files changed, 276 insertions, 0 deletions
diff --git a/polynom.c b/polynom.c
new file mode 100644
index 0000000..33834ef
--- /dev/null
+++ b/polynom.c
@@ -0,0 +1,276 @@
+/*
+ *
+ * Operations sur les polynomes
+ *
+ */
+
+#include "polynom.h"
+#include "scalaires.h"
+#include "exceptions.h"
+#include "main.h"
+#include <stdio.h>
+#include <math.h>
+#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, 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, 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, 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, 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;
+
+ return result;
+
+
+}
+
+polynome ply_modulo(polynome poly1, polynome poly2)
+{ /* reste de la division de deux polynomes */
+ polynome result;
+
+ 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[512], temp[32]; /* FIXME: pas glop comme routine, malloquer tout ca ? */
+ char debut = 1;
+
+ 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);
+ }
+}