Пример #1
0
 public static function ex4()
 {
     header('Content-type: application/json; charset=utf-8');
     //input matricea A , A init
     $A = self::getMatrixFromString($_POST['matrice']);
     $n = $_POST['n'];
     $epsilon = $_POST['epsilon'];
     $arrayS = self::getArrayFromString($_POST['arr']);
     $b = self::getB($A, $arrayS, $n, $epsilon);
     $QR = self::HouseholderDecomposition($A, $epsilon, $n, $b);
     $x = self::getX($QR["r"], $n, $QR["b"]);
     $matrix = new Matrix($n, $n);
     $matrix->fromArray($A);
     $QRlibf = array();
     $QRlib = new QRGivens($matrix);
     $QRlibf['Qlib'] = $QRlib->getQ()->asArray();
     $QRlibf['rlib'] = $QRlib->getR()->asArray();
     $xlib = self::getX($QR["r"], $n, $QR["b"]);
     $vector1 = array();
     $ainit = Util::getInit($A);
     for ($i = 0; $i < $n; $i++) {
         $vector1[$i] = $ainit[$i] * $x[$i] - $b[$i];
     }
     $norm1 = Util::getNorm($vector1, $n);
     $vector2 = array();
     for ($i = 0; $i < $n; $i++) {
         $vector2[$i] = $ainit[$i] * $xlib[$i] - $b[$i];
     }
     $norm2 = Util::getNorm($vector2, $n);
     $vector3 = array();
     for ($i = 0; $i < $n; $i++) {
         $vector3[$i] = $x[$i] - $arrayS[$i];
     }
     $norm3 = Util::getNorm($vector3, $n) / Util::getNorm($arrayS, $n);
     $vector4 = array();
     for ($i = 0; $i < $n; $i++) {
         $vector4[$i] = $xlib[$i] - $arrayS[$i];
     }
     $norm4 = Util::getNorm($vector4, $n) / Util::getNorm($arrayS, $n);
     echo json_encode(array("A" => Util::getStringFromMatrix($A), "epsilon" => $epsilon, "Q" => self::getStringFromMatrix($QR['Q']), "r" => self::getStringFromMatrix($QR['r']), "Qlib" => self::getStringFromMatrix($QRlibf['Qlib']), "rlib" => self::getStringFromMatrix($QRlibf['rlib']), "x" => Util::getStringFromArray($x), "xlib" => Util::getStringFromArray($xlib), "norm1" => $norm1, "norm2" => $norm2, "norm3" => $norm3, "norm4" => $norm4));
 }
Пример #2
0
 public function testMultiply()
 {
     $matrix = Matrix::fromArray([[1, 2, 3], [4, 5, 6]])->multiply(Matrix::fromArray([[7, 8], [9, 10], [11, 12]]));
     $this->assertInstanceOf(Matrix::class, $matrix);
     $this->assertSame([[58.0, 64.0], [139.0, 154.0]], $matrix->toArray());
 }