0

PHPで行列を解きたい。たとえば、次の 3 つの点があるとし(x0, y0), (x1, y1) and (x2, y2)ます。n 点が与えられたら、 を解きたい。この時点で私が持っているのはこれです:p[0], p[1] and p[2]y = p[2]*x^2 + p[1]*x^1 + p[0]*x^0y = p[n] * x^n + p[n-1] * x^(n-1) + ... + p[0] * x^0

<?php

$system = new EQ();
$system->add(1, 2);
$system->add(4, 5);
$system->solvePn(0);

class EQ {

    private $points = array();

    public function add($x, $y) {
        $this->points[] = array($x, $y);
    }

    public function solvePn($n) {
        // Solve p[n]
        // So eliminate p[m], p[m-1], ..., p[n+1], p[n-1], ..., p[1], p[0]
        $m = count($this->points);
        $a = $m;
        // Eliminate p[a]
        if ($a != $n) {

        }
        $a--;
    }

}
?>

しかし、今、私は次に何をすべきかわかりません。

4

1 に答える 1

0

ダビンとローマンに感謝します。Lagrange を使用しましたが、現在は正常に動作しています。たとえば、(1,1)、(2,3)、(3,27) の 3 つの点がある場合、クラスはラグランジュを使用して多項式近似を計算します。$system->f(4) を呼び出して、この多項式の x=4 の y 値を計算できます。

<?php

$system = new EQ();
$system->add(1, 1);
$system->add(2, 3);
$system->add(3, 27);
echo $system->f(4);

class EQ {

    private $points = array();
    private $formula = NULL;

    public function add($x, $y) {
        $this->points[] = array($x, $y);
    }

    public function lz($z) {
        $point = $this->points[$z];
        // Get the x and y value of this point
        $x = $point[0];
        $y = $point[1];
        // Now get all points except these and build the formula
        $index = 0;
        $above = '';
        $below = 1;
        foreach ($this->points as $point) {
            if ($index != $z) {
                $xp = $point[0];
                $yp = $point[1];
                $above .= '(x-' . $xp . ')';
                $below *= ($x - $xp);
            }
            $index++;
        }
        $factor = $y / $below;
        $above = ungroup($above);
        foreach ($above as $degree=>$subfactor) {
            $above[$degree] = $subfactor * $factor;
        }
        return $above;
    }

    public function f($x) {
        if ($this->formula === NULL) $this->L();
        $formula = $this->formula;
        $val = 0;
        foreach ($formula as $degree=>$factor) {
            $subval = $factor * pow($x, $degree);
            $val += $subval;
        }
        return $val;
    }

    public function L() {
        $n = count($this->points);
        $formula = array();
        for ($z = 0; $z < $n; $z++) {
            $l = $this->lz($z);
            foreach ($l as $degree=>$factor) {
                $formula[$degree] += $factor;
            }
        }
        $this->formula = $formula;
        return $formula;
    }

}

// Transform a group-formula to a list with terms
// @example (x-1)*(x-2)
function ungroup($formula) {
    preg_match_all('/\(([^)]{1,})\)/', $formula, $matches);
    $groups = $matches[1];
    $factorTerms = getTerms(reset($groups));
    while (key($groups) < count($groups) - 1) {
        next($groups);
        $terms = getTerms(current($groups));
        $newTerms = array();
        foreach ($terms as $term) {
            foreach ($factorTerms as $factorTerm) {
                $degree = getDegree($term) + getDegree($factorTerm);
                $factor = getFactor($term) * getFactor($factorTerm);
                $newTerm = '';
                if ($factor != 1) $newTerm = ($factor == -1?'-':$factor);
                if ($degree != 0) $newTerm .= 'x' . ($degree == 1?'':'^' . $degree);
                if (strlen($newTerm) == 0) $newTerm = '0';
                $newTerms[] = $newTerm;
            }
        }
        $factorTerms = $newTerms;
    }
    $terms = array();
    foreach ($factorTerms as $term) {
        $degree = getDegree($term);
        $factor = getFactor($term);
        $terms[$degree] += $factor;
    }
    return $terms;
}

function getFactor($term) {
    if (strpos($term, 'x') !== false) {
        $pattern = '/([0-9\-]{1,})[\*]?x/';
        preg_match($pattern, $term, $matches);
        if (count($matches) == 2) {
            $n = $matches[1];
            if ($n === '-') return -1;
            return $n;
        }
        return 1;
    }
    return $term;
}

function getDegree($term) {
    if (strpos($term, 'x') !== false) {
        $pattern = '/x\^([0-9\-]{1,})/';
        preg_match($pattern, $term, $matches);
        if (count($matches) == 2) {
            return $matches[1];
        }
        return 1;
    }
    return 0;
}

function getTerms($group) {
    $group = str_replace('-', '+-', $group);
    $group = preg_replace('/\+{1,}/', '+', $group);
    $terms = explode('+', $group);
    return $terms;
}
?>
于 2011-03-29T14:00:48.860 に答える