Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1.  
  2. <?php
  3. class gausMethod{
  4.    
  5.     protected $columns ; // koloni, ravni na broq na neizvestni
  6.     protected $rows ;  // redove, ravni na broi uravneniq
  7.     protected $aDeterminanta; // matricata
  8.     protected $values = array();
  9. /*
  10. rows = broi uravneniq
  11. columns = broi neizvestni
  12.  na4in na upotreba: Pyrvite 2 sa redove neizcestni, sled tova se izbroqvat stoinostite pred neizvestnite. red po red.
  13. */
  14.     public function __construct($rows,$columns){
  15.         $temp = func_get_args();
  16.         $this->rows = $temp[0]; //broi uravneniq
  17.         array_shift(&$temp);
  18.         $this->columns = $temp[0]; //broi neizvestni
  19.         array_shift(&$temp);
  20.         $v = 0;
  21.         for ($i = 0; $i < $this->rows ; $i++) {
  22.             for ($j = 0; $j < $this->columns ; $j++) {
  23.                 $aDeterminanta[$i][$j] = $temp[$v];
  24.                 $v++;
  25.             }
  26.         }
  27.         $this->convertToTriangularType();
  28.         $this->calculateMatrix();
  29.     }
  30.    
  31.     public function getMat(){
  32.         return $this->aDeterminanta;
  33.     }
  34.    
  35.  
  36.    
  37.     public function convertToTriangularType(){
  38.         for ($j = 0;$j < $this->columns ;$j++){
  39.             $temp = $this->aDeterminanta[$j][$j];
  40.             for ($i=$j + 1;$i < $this->rows ;$i++){
  41.                 $temp2 = $this->aDeterminanta[$i][$j] / -$temp;
  42.                 $mRow = $this->muliplyRow($i,$temp2);
  43.                 $aRow = $this->addRow($mRow,$i);
  44.                 $this->updateRow($i,$aRow);
  45.             }
  46.         }
  47.         for ($j = $this->columns - 1;$j > -1 ;$j--){
  48.             $temp = $this->aDeterminanta[$j][$j];
  49.             for ($i=$j - 1;$i > -1;$i--){
  50.                 $temp2 = $this->aDeterminanta[$i][$j] / -$temp;
  51.                 $mRow = $this->muliplyRow($i,$temp2);
  52.                 $aRow = $this->addRow($mRow,$i);
  53.                 $this->updateRow($i,$aRow);
  54.             }
  55.         }
  56.     }
  57.    
  58.     //iz4islqvane na matricata
  59.     public function calculateMatrix(){
  60.         for ($i=0;$i<$this->rows;$i++){
  61.             $temp = $this->aDeterminanta[$i][$this->columns];
  62.             echo "Neizvestna $i e $temp";
  63.         }
  64.     }
  65.    
  66.    
  67.     /*@Function@ multiplyRow @@
  68.      *
  69.      * Action:
  70.      *  Multiply's a row by a number
  71.      * Return:
  72.      *  Row multiplied by number
  73.      * Arguments:
  74.      *  $rowToMultiply: Koi red da se umnoji
  75.      *  $amountToMultiplyBy: S kolko da go umnoji
  76.      *
  77.      * @@End@@*/
  78.     private function muliplyRow($rowToMultiply,$amountToMultiplyBy){
  79.         $temp = $this->aDeterminanta[$rowToMultiply];
  80.         for ($i = 0; $i < $this->columns; $i++) {
  81.             $temp[$i] = $temp[$i] * $amountToMultiplyBy;
  82.         }
  83.         return $temp;
  84.     }
  85.    
  86.     private function addRow($rowToAdd,$rowToAddTo){
  87.         $temp = $this->aDeterminanta[$rowToAddTo];
  88.         for ($i = 0;$i <$this->columns;$i++){
  89.             $temp[$i] = $temp[$i] + $rowToAdd[$i];
  90.         }
  91.         return $temp;
  92.     }
  93.    
  94.     private function updateRow($rowToUpdate,$newRow){
  95.         $this->aDeterminanta[$rowToUpdate] = $newRow;
  96.     }
  97.    
  98.    
  99.     private function switchRows($nRowToSwith,$nRowToSwitchWith){
  100.         if($nRowToSwith > $this->rows || $nRowToSwitchWith > $this->rows){
  101.             Throw new Exception('Invalid aruments in switchRows function');
  102.         }
  103.         $temp = $this->aDeterminanta[$nRowToSwitchWith];
  104.         $this->aDeterminanta[$nRowToSwitchWith] = $this->aDeterminanta[$nRowToSwith];
  105.         $this->aDeterminanta[$nRowToSwith] = $temp;
  106.     }
  107.    
  108.     private function AnalyzeFirstRow(){ //Analizirane na 1-iq stylb v tyrsene na 1-ca, razmnqne na redove.
  109.         if ($this->aDeterminanta[0][0] == 1) { //ako pyrvoto 4islo e 1, spira funkciqta
  110.             return;
  111.         }
  112.         for ($i = 1; $i < $this->rows; $i++) {
  113.             if ($this->aDeterminanta[i][0] == 1) {
  114.                 $this->switchRows($this->aDeterminanta[i],$this->aDeterminanta[0]);
  115.                 break;
  116.             }
  117.         }
  118.     }
  119.    
  120.    
  121. }
  122.  
  123.  
  124. $matrix = new gausMethod(4,4,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement