Les Snippets

Connexion

INTERPOLATION POLYNOMIALE

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 07/02/2008 00:28:34 et initié par us_30 [Liste]
Date de mise à jour : 07/02/2008 23:55:57
Vue : 3450
Catégorie(s) : Maths
Langages dispo pour ce code :
- VB6, VBA
- ObjectiveCaml
- PHP 5
- Voir tous les langages pour ce code snippet



Langage : PHP 5
Date ajout : 16/04/2008
Posté par Eregon [Liste]
<?php
//Structure pour les points du graph
class Point
{
    public $x;
    public $y;
    function __construct($x, $y)
    {
        $this->x = $x;
        $this->y = $y;
    }
}
//Affichage des polynomes
class Number
{
    public static function show_sign($n)
    {
        return ($n >= 0) ? '+ ' : '- ';
    }
    public static function show($n, $var = '', $pow = 1, $first_element = false)
    {
        if($pow > 1)
            $var .= '<sup>'.$pow.'</sup>';
        elseif($pow == 0)
            $var = '';
        $var .= ' ';
        if($n == 0)
            return '';
        elseif($pow != 0 && ($n == 1 || $n == -1))
            $s = self::show_sign($n);
        else//if($n != 0)
            $s = self::show_sign($n).abs($n);
        if($first_element && $n >= 0)
            return ($n==1) ? $var : $n.$var;
        return $s.$var;
    }
}
//Structure et fonctions des polynomes
class Polynom implements ArrayAccess
{
    private $coef = Array();//coeficients
    private $degree;
    function __construct($coeficients)
    {
        if(!is_array($coeficients))
            $coeficients = func_get_args();
        $this->degree = count($coeficients)-1;
        for($d=0; $d<=$this->degree; $d++)
            $this->coef[$d] = $coeficients[$this->degree-$d];
    }
    function __toString()
    {
        $return = Number::show($this[$this->degree], 'x', $this->degree, true);
        for($d=$this->degree-1; $d>=0; $d--)
            $return .= Number::show($this[$d], 'x', $d);
        return $return;
    }
    //ArrayAccess
    public function offsetGet($offset)
        { return $this->coef[$offset]; }
    public function offsetSet($offset, $value)
        { $this->coef[$offset] = $value; }
    public function offsetUnset($offset) {}
    public function offsetExists($offset)
        { return array_key_exists($offset, $this->coef); }
    public function add(Polynom $p)
    {
        $max = ($this->degree > $p->degree) ? $this : $p;
        $min = ($this->degree > $p->degree) ? $p : $this;
        for($d=0; $d<=$min->degree; $d++)
            $max[$d] += $min[$d];
        return $max;
    }
    public function product(Polynom $p)
    {
        $result = new Polynom(array_fill(0, $this->degree+$p->degree+1, 0));
        for($d=0; $d<=$this->degree; $d++)
        {
            for($e=0; $e<=$p->degree; $e++)
                $result[$d+$e] += $this[$d]*$p[$e];
        }
        return $result;
    }
}
class Interpolation
{
    public static function Lagrange($p)
    {//Interpolation polynomiale de Lagrange(Array(Point) $points)
        $n = count($p);
        $result = new Polynom(0);
        for($k=0; $k<$n; $k++)
        {
            $t = new Polynom($p[$k]->y);
            for($i=0; $i<$n; $i++)
            {
                if($i != $k)
                {
                    $poly[$k][$i] = new Polynom(
                    1/($p[$k]->x - $p[$i]->x) ,
                    -$p[$i]->x/($p[$k]->x-$p[$i]->x) );
                    $t = $t->product($poly[$k][$i]);
                }
            }
            $result = $result->add($t);
        }
        return $result;
    }
}
//TEST\\
//Points : (−9, 5), (−4, 2), (−1, −2), (7, 9)
$a = new Point(-9, 5);
$b = new Point(-4, 2);
$c = new Point(-1, -2);
$d = new Point(7, 9);
echo $polynom = Interpolation::Lagrange(Array($a, $b, $c, $d));
?>

Remarque :
Requiert la classe ArrayAccess de la SPL (utilisée pour plus de simplicité avec les polynômes)

Snippets en rapport avec : Points, Interpolation, Polynomiale



Codes sources en rapport avec : Points, Interpolation, Polynomiale

{Flash} DÉPLACEMENT OBJET
Ici se trouve le code pour déplacer un objet sur la scène, sans interpolations. Vous trouverez un c...

{C / C++ / C++.NET} APPLICATION MATHÉMATIQUE EN C++ ET QT
Cette application mathématiques a été réalisée avec le langage C++ et la QT 2.3.0 . Elle traite : ...

{Delphi} RÉGRESSION POLYNOMIALE
Calcul le meilleurs polynome approximant n points expérimentaux....

{Visual Basic, VB6, VB.NET, VB 2005} ALGORITHME DE PATHFINDING ASTAR
Cet algorithme célèbre est un algorithme de type PathFinding. Il recherche le chemin le plus rap...

{Visual Basic, VB6, VB.NET, VB 2005} GRAPHIQUE EN TEMPS RÉEL (AVEC LISSAGE DES COURBES ET ANTIALIAS)
Bonjour à tous, voilà je cherchais un controle qui me permettrait d'afficher un graphique en temp...

{Visual Basic, VB6, VB.NET, VB 2005} INTÉGRER DES SCORES À VOS JEUX
Très simplement, ce Module sert à écrire dans le registre 5 joueurs et leurs scores. Avec modificat...

{Flash} UTILISATION DES INTERPOLATIONS DE FORME ET DES REPÈRES
C’est juste un exemple de ce que l’on peut produire avec les interpolations de forme Le fichier sou...

{Javascript / DHTML} S'ENTRAINER EN MATH GRACE A DES EXERCICES
Ce code se compose de 4 pages dont la page du sommaire est Math.htm, il permet de s'entrainer en mat...

{Flash} ANIMATION CERVEAU AVEC ECLAIRS ALEATOIRE
une petite animation d'un cerveau qui sert a rien mais c'est joli par interpolation de mouvement et ...

{Javascript / DHTML} SYSTÈME POUR COMPTER VOS POINTS LORS DE VOS PARTIE DE FLÉCHETTE
Bonjour à tous ! Ce script fait de principalement de javascript et un peu de PHP est conçu pour s...