Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class gausMethod{
- protected $columns ; // koloni, ravni na broq na neizvestni
- protected $rows ; // redove, ravni na broi uravneniq
- protected $aDeterminanta; // matricata
- protected $values = array();
- /*
- rows = broi uravneniq
- columns = broi neizvestni
- na4in na upotreba: Pyrvite 2 sa redove neizcestni, sled tova se izbroqvat stoinostite pred neizvestnite. red po red.
- */
- public function __construct($rows,$columns){
- $temp = func_get_args();
- $this->rows = $temp[0]; //broi uravneniq
- array_shift(&$temp);
- $this->columns = $temp[0]; //broi neizvestni
- array_shift(&$temp);
- $v = 0;
- for ($i = 0; $i < $this->rows ; $i++) {
- for ($j = 0; $j < $this->columns ; $j++) {
- $aDeterminanta[$i][$j] = $temp[$v];
- $v++;
- }
- }
- $this->convertToTriangularType();
- $this->calculateMatrix();
- }
- public function getMat(){
- return $this->aDeterminanta;
- }
- public function convertToTriangularType(){
- for ($j = 0;$j < $this->columns ;$j++){
- $temp = $this->aDeterminanta[$j][$j];
- for ($i=$j + 1;$i < $this->rows ;$i++){
- $temp2 = $this->aDeterminanta[$i][$j] / -$temp;
- $mRow = $this->muliplyRow($i,$temp2);
- $aRow = $this->addRow($mRow,$i);
- $this->updateRow($i,$aRow);
- }
- }
- for ($j = $this->columns - 1;$j > -1 ;$j--){
- $temp = $this->aDeterminanta[$j][$j];
- for ($i=$j - 1;$i > -1;$i--){
- $temp2 = $this->aDeterminanta[$i][$j] / -$temp;
- $mRow = $this->muliplyRow($i,$temp2);
- $aRow = $this->addRow($mRow,$i);
- $this->updateRow($i,$aRow);
- }
- }
- }
- //iz4islqvane na matricata
- public function calculateMatrix(){
- for ($i=0;$i<$this->rows;$i++){
- $temp = $this->aDeterminanta[$i][$this->columns];
- echo "Neizvestna $i e $temp";
- }
- }
- /*@Function@ multiplyRow @@
- *
- * Action:
- * Multiply's a row by a number
- * Return:
- * Row multiplied by number
- * Arguments:
- * $rowToMultiply: Koi red da se umnoji
- * $amountToMultiplyBy: S kolko da go umnoji
- *
- * @@End@@*/
- private function muliplyRow($rowToMultiply,$amountToMultiplyBy){
- $temp = $this->aDeterminanta[$rowToMultiply];
- for ($i = 0; $i < $this->columns; $i++) {
- $temp[$i] = $temp[$i] * $amountToMultiplyBy;
- }
- return $temp;
- }
- private function addRow($rowToAdd,$rowToAddTo){
- $temp = $this->aDeterminanta[$rowToAddTo];
- for ($i = 0;$i <$this->columns;$i++){
- $temp[$i] = $temp[$i] + $rowToAdd[$i];
- }
- return $temp;
- }
- private function updateRow($rowToUpdate,$newRow){
- $this->aDeterminanta[$rowToUpdate] = $newRow;
- }
- private function switchRows($nRowToSwith,$nRowToSwitchWith){
- if($nRowToSwith > $this->rows || $nRowToSwitchWith > $this->rows){
- Throw new Exception('Invalid aruments in switchRows function');
- }
- $temp = $this->aDeterminanta[$nRowToSwitchWith];
- $this->aDeterminanta[$nRowToSwitchWith] = $this->aDeterminanta[$nRowToSwith];
- $this->aDeterminanta[$nRowToSwith] = $temp;
- }
- private function AnalyzeFirstRow(){ //Analizirane na 1-iq stylb v tyrsene na 1-ca, razmnqne na redove.
- if ($this->aDeterminanta[0][0] == 1) { //ako pyrvoto 4islo e 1, spira funkciqta
- return;
- }
- for ($i = 1; $i < $this->rows; $i++) {
- if ($this->aDeterminanta[i][0] == 1) {
- $this->switchRows($this->aDeterminanta[i],$this->aDeterminanta[0]);
- break;
- }
- }
- }
- }
- $a = new gausMethod(1,1,1,1,1,1);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement