Advertisement
Guest User

Untitled

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