Пример #1
0
 public static function ex3()
 {
     header('Content-Type: application/json');
     // Load files and parse them
     $a = new SparseMatrix();
     $b = new SparseMatrix();
     $a->parseFile(self::A);
     $b->parseFile(self::B);
     $x = array();
     $n = $a->Count();
     for ($i = 0; $i <= $n; $i++) {
         $x[$i - 1] = $i;
     }
     $res_aorix = self::multiplyMatrixWithVector($a->Matrix(), $x);
     print_r($res_aorix);
     //TODO: response json
     echo json_encode(array("axb" => json_encode($res_aorix)));
 }
Пример #2
0
 /**
  * ex1 method
  * @param void
  * @return void
  */
 public static function ex1()
 {
     header('Content-Type: application/json');
     // Load files and parse them
     define("TEST", 0);
     $p = 7;
     $kmax = 10000;
     $epsilon = pow(10, -$p);
     $epsilon2 = pow(10, 8);
     $a1 = new SparseMatrix();
     $a2 = new SparseMatrix();
     $a3 = new SparseMatrix();
     $a4 = new SparseMatrix();
     $m = array();
     $b = array();
     $n = array();
     $diagonal = array();
     $L = array();
     $U = array();
     $B = array();
     $C = array();
     $a1->parseFile(self::RareM1);
     if (!TEST) {
         $a2->parseFile(self::RareM2);
         $a3->parseFile(self::RareM3);
         $a4->parseFile(self::RareM4);
     }
     // Initialization
     $m[0] = $a1->Matrix();
     if (!TEST) {
         $m[1] = $a2->Matrix();
         $m[2] = $a3->Matrix();
         $m[3] = $a4->Matrix();
     }
     $b[0] = $a1->Vector();
     if (!TEST) {
         $b[1] = $a2->Vector();
         $b[2] = $a3->Vector();
         $b[3] = $a4->Vector();
     }
     $n[0] = count($a1);
     if (!TEST) {
         $n[1] = count($a2);
         $n[2] = count($a3);
         $n[3] = count($a4);
     }
     $xc = array();
     $nlen = count($n);
     //Check elements on the diagonal and build the diagonal matrix at the same time
     for ($q = 0; $q < $nlen; $q++) {
         for ($i = 0; $i < $n[$q]; $i++) {
             $el = $m[$q][$i]->FindCol($i);
             if (!$el) {
                 echo "Error, diagonal element null on the matrix " . $i;
                 die;
             } else {
                 $diagonal[$q][$i] = new SinglyList();
                 $diagonal[$q][$i]->Append($el->Value(), $i);
             }
         }
     }
     $output = "";
     // build upper and lower matrix
     for ($q = 0; $q < $nlen; $q++) {
         for ($i = 0; $i < $n[$q]; $i++) {
             $crwA = $m[$q][$i]->Tail();
             $U[$q][$i] = new SinglyList();
             $L[$q][$i] = new SinglyList();
             while ($crwA !== null) {
                 $j = $crwA->Column();
                 $output .= $j . '-' . $i . '|';
                 if ($j < $i) {
                     $U[$q][$i]->Append($crwA->Value(), $j);
                 } elseif ($j > $i) {
                     $L[$q][$i]->Append($crwA->Value(), $j);
                 }
                 $crwA = $crwA->Next();
             }
         }
     }
     $resp = array();
     // build b & c matrixes
     for ($q = 0; $q < $nlen; $q++) {
         $B[$q] = self::plusMatrix($L[$q], self::multiplyMatrixWithScalar($diagonal[$q], round(5 / 6, $p)));
         $C[$q] = self::substractMatrixes(self::multiplyMatrixWithScalar($diagonal[$q], -round(1 / 6, $p)), $U[$q]);
     }
     unset($L);
     unset($U);
     unset($d);
     for ($q = 0; $q < $nlen; $q++) {
         for ($i = 0; $i < $n[$q]; $i++) {
             if (TEST) {
                 $xc[$q][0][$i] = $i + 1;
             } else {
                 $xc[$q][0][$i] = 0;
             }
         }
         $k = 0;
         do {
             $xc[$q][$k + 1] = array();
             for ($i = 0; $i < $n[$q]; $i++) {
                 $sub = self::computeBSubstraction($m[$q], $i, $xc[$q][$k + 1], $xc[$q][$k]);
                 // cu plus primul x1 da ca in exemplu
                 if (TEST) {
                     $xc[$q][$k + 1][$i] = -0.2 * $xc[$q][$k][$i] + 1.2 * round(($b[$q][$i] + $sub) / $m[$q][$i]->FindCol($i)->Value(), $p);
                 } else {
                     $xc[$q][$k + 1][$i] = -0.2 * $xc[$q][$k][$i] + 1.2 * round(($b[$q][$i] - $sub) / $m[$q][$i]->FindCol($i)->Value(), $p);
                 }
             }
             if (TEST) {
                 print_r($xc);
             }
             $dx = self::getStandardNorm(self::subtractVectors($xc[$q][$k + 1], $xc[$q][$k], $n[$q], $p), $n[$q]);
             $k++;
         } while ($dx >= $epsilon && $k <= $kmax && $dx <= $epsilon2);
         if ($dx < $epsilon) {
             $resp[$q] = $xc[$q][$k - 1];
         } else {
             $resp[$q] = "divergenta";
         }
     }
     $norm = array();
     for ($q = 0; $q < $nlen; $q++) {
         @($norm[$q] = self::subtractVectors(self::multiplyMatrixWithVector($m[$q], $resp[$q]), $b[$q], $n[$q], $p));
     }
     echo json_encode(array("resp" => $resp, "norm" => $norm));
     die;
 }