Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Core;
- class Gauss
- {
- /**
- * @var array
- */
- protected $matrix;
- /**
- * @var integer
- */
- protected $matrixSize;
- /**
- * Gauss constructor.
- */
- public function __construct()
- {
- $this->matrix = include(ROOT . "/etc/matrix.php");
- $this
- ->setMatrixSize()
- ->printMatrix();
- }
- /**
- * @return array
- */
- public function resolve(): array
- {
- $tmp = 0.0;
- $xx = [];
- $k = 0;
- $i = 0;
- $j = 0;
- /**
- * Прямой ход (верхнетреугольный вид)
- */
- for ($i = 0; $i < $this->matrixSize; $i++) {
- $tmp = $this->matrix[$i][$i];
- for ($j = $this->matrixSize; $j >= $i; $j--) {
- $this->matrix[$i][$j] /= $tmp;
- }
- for ($j = $i + 1; $j < $this->matrixSize; $j++) {
- $tmp = $this->matrix[$j][$i];
- for ($k = $this->matrixSize; $k >= $i; $k--) {
- $this->matrix[$j][$k] -= $tmp * $this->matrix[$i][$k];
- }
- }
- }
- /**
- * Обратный ход
- */
- $xx[$this->matrixSize - 1] = $this->matrix[$this->matrixSize - 1][$this->matrixSize];
- for ($i = $this->matrixSize - 2; $i >= 0; $i--) {
- $xx[$i] = $this->matrix[$i][$this->matrixSize];
- for ($j = $i + 1; $j < $this->matrixSize; $j++) {
- $xx[$i] -= $this->matrix[$i][$j] * $xx[$j];
- }
- }
- return $xx;
- }
- /**
- * @return Gauss
- */
- protected function printMatrix(): Gauss
- {
- echo "Your matrix" . PHP_EOL;
- foreach ($this->matrix as $element) {
- $string = implode("\t", $element);
- echo "$string \r\n";
- }
- return $this;
- }
- /**
- * @return Gauss
- */
- protected function setMatrixSize(): Gauss
- {
- $this->matrixSize = count($this->matrix);
- return $this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement