Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Core;
  4.  
  5. class Gauss
  6. {
  7.  
  8.     /**
  9.      * @var array
  10.      */
  11.     protected $matrix;
  12.  
  13.     /**
  14.      * @var integer
  15.      */
  16.     protected $matrixSize;
  17.  
  18.     /**
  19.      * Gauss constructor.
  20.      */
  21.     public function __construct()
  22.     {
  23.         $this->matrix = include(ROOT . "/etc/matrix.php");
  24.         $this
  25.             ->setMatrixSize()
  26.             ->printMatrix();
  27.     }
  28.  
  29.     /**
  30.      * @return array
  31.      */
  32.     public function resolve(): array
  33.     {
  34.         $tmp = 0.0;
  35.         $xx = [];
  36.         $k = 0;
  37.         $i = 0;
  38.         $j = 0;
  39.  
  40.         /**
  41.          * Прямой ход (верхнетреугольный вид)
  42.          */
  43.         for ($i = 0; $i < $this->matrixSize; $i++) {
  44.             $tmp = $this->matrix[$i][$i];
  45.             for ($j = $this->matrixSize; $j >= $i; $j--) {
  46.                 $this->matrix[$i][$j] /= $tmp;
  47.             }
  48.  
  49.             for ($j = $i + 1; $j < $this->matrixSize; $j++) {
  50.                 $tmp = $this->matrix[$j][$i];
  51.                 for ($k = $this->matrixSize; $k >= $i; $k--) {
  52.                     $this->matrix[$j][$k] -= $tmp * $this->matrix[$i][$k];
  53.                 }
  54.             }
  55.         }
  56.  
  57.         /**
  58.          * Обратный ход
  59.          */
  60.         $xx[$this->matrixSize - 1] = $this->matrix[$this->matrixSize - 1][$this->matrixSize];
  61.         for ($i = $this->matrixSize - 2; $i >= 0; $i--) {
  62.             $xx[$i] = $this->matrix[$i][$this->matrixSize];
  63.             for ($j = $i + 1; $j < $this->matrixSize; $j++) {
  64.                 $xx[$i] -= $this->matrix[$i][$j] * $xx[$j];
  65.             }
  66.         }
  67.         return $xx;
  68.     }
  69.  
  70.     /**
  71.      * @return Gauss
  72.      */
  73.     protected function printMatrix(): Gauss
  74.     {
  75.         echo "Your matrix" . PHP_EOL;
  76.         foreach ($this->matrix as $element) {
  77.             $string = implode("\t", $element);
  78.             echo "$string \r\n";
  79.         }
  80.         return $this;
  81.     }
  82.  
  83.     /**
  84.      * @return Gauss
  85.      */
  86.     protected function setMatrixSize(): Gauss
  87.     {
  88.         $this->matrixSize = count($this->matrix);
  89.         return $this;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement