gistfile1.txt m=$_m;}public function p(){echo "输出矩阵\n";foreach($this-m as $row){echo implode(",",$row)."\n";}return $this;}public function swap($r1,$r2){echo "变换第{$r1}行和第{$r2}行\n";$temp=$this-m[$r1-1];$this-m[$r1-1]=$t
m=$_m; } public function p(){ echo "输出矩阵\n"; foreach($this->m as $row){ echo implode(",",$row)."\n"; } return $this; } public function swap($r1,$r2){ echo "变换第{$r1}行和第{$r2}行\n"; $temp=$this->m[$r1-1]; $this->m[$r1-1]=$this->m[$r2-1]; $this->m[$r2-1]=$temp; return $this; } public function t($str){ preg_match("/r(\d*)([+\-])(\d*)r(\d*)/i",$str,$out); $trans=$out[1]-1; $origin=$out[4]-1; $k=$out[3]?intval($out[3]):1; for($i=0;$im[$trans]);$i++){ switch($out[2]){ case "+": $this->m[$trans][$i]+=$k*$this->m[$origin][$i]; break; case "-": $this->m[$trans][$i]-=$k*$this->m[$origin][$i]; } } return $this; } public function k($row,$k){ echo "第{$row}行变为{$k}倍\n"; foreach($this->m[$row-1] as &$item){ $item *=$k; } return $this; } public function __destruct(){ $this->p(); } } /* $m=new Martrix([ [2,1,8,3,7], [2,-3,0,7,5], [3,-2,5,8,0], [1,0,3,2,0] ]); $m=new Martrix([ [1,2,1,-1], [3,6,-1,-3], [5,10,1,5], ]); */ $m=new Martrix([ [2,3,1,4], [1,-2,4,-5], [3,8,-2,13], [4,-1,9,-6], ]); $m ->swap(1,2) ->t("r2-2r1") ->t("r3-3r1") ->t("r4-4r1") ->t("r4-r2") ->t("r3-2r2") ->k(2,1/7) ->t("r1+2r2") //->k(3,-1) ;