[請益] 矩陣相乘但跑不出結果?????

看板PHP作者 (Hh)時間12年前 (2013/08/28 15:56), 編輯推噓3(3012)
留言15則, 6人參與, 最新討論串1/2 (看更多)
$file=fopen("USA-15_Restart_P.txt","r"); $line_num=0; while($line=fgets($file,10)) { $digit=preg_split("/[ ]/",$line); $Restart_p[0][$line_num]=$digit[0]; echo $Restart_p[0][$line_num]."<br>"; $line_num++; } fclose($file); $file=fopen("USA-15 network.txt","r"); $line_num=0; while($line=fgets($file,10)) { $number[$line_num]=preg_split("/[ ]/",$line); /* if($number[$line_num][0]=="link") { $linkout_node[]=intval($number[$line_num][1]); continue; } */ $digit_size[]=intval($number[$line_num][0]); $digit_size[]=intval($number[$line_num][1]); $nei[intval($number[$line_num][0])][intval($number[$line_num][1])]=1; $nei[intval($number[$line_num][1])][intval($number[$line_num][0])]=1; $line_num++; } fclose($file); //echo "count = ".count($digit_size)."<br>"; $maximum=0; $minimum=1000; for($i=0;$i<count($digit_size);$i++) { if($maximum<$digit_size[$i]) { $maximum=$digit_size[$i]; } if($minimum>$digit_size[$i]) { $minimum=$digit_size[$i]; } } //echo "maximum = ".$maximum."<br>"; //echo "minimum = ".$minimum."<br>"; //echo for($i=$minimum;$i<=$maximum;$i++) { for($j=$minimum;$j<=$maximum;$j++) { if($nei[$i][$j]==NULL) { $nei[$i][$j]=0; } //echo /*$i."|".$j."->".*/$nei[$i][$j]." ; "; $sum_i[$j]+=$nei[$i][$j]; } //echo "<br>"; } for($j=$minimum;$j<=$maximum;$j++) { //echo $sum_i[$j]." ; "; } $a=0; $b=0; for($i=$minimum;$i<=$maximum;$i++) { for($j=$minimum;$j<=$maximum;$j++) { if($nei[$i][$j]==NULL) { $nei[$i][$j]=0; } $adjacent_matrix[$a][$b]= $nei[$i][$j]/$sum_i[$j]; //echo /*$i."|".$j."->".*/$nei[$i][$j]/$sum_i[$j]." ; "; echo $adjacent_matrix[$a][$b]." ; "; $b++; } $a++; echo "<br>"; } //echo "a,b = ".matrix_print(matrix_operation($a, $b, '*' ))."<br>"; echo matrix_print(matrix_operation($adjacent_matrix, $Restart_p, '*' ))."<br>"; function _matrix_rows($matrix) { return count($matrix); } // A function to return the columns in a matrix - // Does not check for validity, it assumes the matrix is well formed. function _matrix_columns($matrix) { return count($matrix[0]); } function _matrix_well_formed($matrix) { // If this is not an array, it is badly formed, return false. if (!(is_array($matrix))) { return false; } else { // Count the number of rows. $rows = count($matrix); // Now loop through each row: for ($r = 0; $r < $rows; $r++) { // Make sure that this row is set, and an array. Checking to // see if it is set is ensuring that this is a 0 based // numerically indexed array. if (!(isset($matrix[$r]) && is_array($matrix[$r]))) { return false; } else { // If this is row 0, calculate the columns in it: if ($r == 0) { $cols = count($matrix[$r]); // Ensure that the number of columns is identical else exit } elseif (count($matrix[$r]) != $cols) { return false; } // Now, loop through all the columns for this row for ($c = 0; $c < $cols; $c++) { // Ensure this entry is set, and a number if (!(isset($matrix[$r][$c]) && is_numeric($matrix[$r][$c]))) { return false; } } } } } // Ok, if we actually made it this far, then we have not found // anything wrong with the matrix. return true; } function matrix_operation($a, $b, $operation) { // Verify both matrices are well formed $valid = false; if (_matrix_well_formed($a) && _matrix_well_formed( $b)) { // Make sure they have complementary numbers of rows and columns. // The number of rows in A should be the number of columns in B $rows = _matrix_rows($a); $columns = _matrix_columns($a); if (($columns == _matrix_rows( $b)) && ($rows == _matrix_columns( $b))) { // We have a valid setup for continuing $valid = true; } } // If invalid, return false if (!($valid)) { return false; } // Create a blank matrix the appropriate size, initialized to 0 $new = array_fill(0, $rows, array_fill(0, $rows, 0)); // For each row in a ... for ($r = 0; $r < $rows; $r++) { // For each column in b ... for ($c = 0; $c < $rows; $c++) { // Take each member of column b, with each member of row a // and add the results, storing this in the new table: // Loop over each column in A ... for ($ac = 0; $ac < $columns; $ac++) { // Evaluate the operation eval('$new[$r][$c] += $a[$r][$ac] '. $operation.' $b[$ac][$c];'); } } } // Return the finished matrix: return $new; } function matrix_print($matrix) { // Verify it is well formed if (_matrix_well_formed($matrix)) { $rows = _matrix_rows($matrix); $columns = _matrix_columns($matrix); // Start the table echo '<table>'; // For each row in the matrix: for ($r = 0; $r < $rows; $r++) { // Begin the row: echo '<tr>'; // For each column in this row for ($c = 0; $c < $columns; $c++) { // Echo the element: echo "<td>".sprintf("%8.4f",$matrix[$r][$c])."</td>"; } // End the row. echo '</tr>'; } // End the table. //echo "</table>/n"; echo "</table><br>"; } else { // It wasn't well formed: return false; } } ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.126.132.4

08/28 17:40, , 1F
可以明白的表述一下......你的問題是什麼?
08/28 17:40, 1F

08/28 18:42, , 2F
我要做的是二維的矩陣相乘,兩個矩陣都ECHO 得出來,但是
08/28 18:42, 2F

08/28 18:43, , 3F
ECHO出來卻是空白的????為什麼
08/28 18:43, 3F

08/28 18:44, , 4F
帶入矩陣相乘的FUNCTION卻是空白的???為什麼
08/28 18:44, 4F

08/28 18:48, , 5F
我可以推「因為愛」嗎? [逃]
08/28 18:48, 5F

08/28 23:12, , 6F
除了單元測試,我想不到其他你能做的事了。
08/28 23:12, 6F

08/29 08:31, , 7F
為何要用eval @@
08/29 08:31, 7F

08/30 12:51, , 8F
function是我在網路上找到的code,用他網路上提共的範例是
08/30 12:51, 8F

08/30 12:53, , 9F
echo得出結果的,還是有其他矩陣相乘的code可以借我參考呢
08/30 12:53, 9F

08/31 07:10, , 10F
這大概得自己debug,或是付錢找人debug了..
08/31 07:10, 10F

09/22 19:19, , 11F
既然還沒相乘都可以正常,那問題應該就出在相乘的地方,
09/22 19:19, 11F

09/22 19:19, , 12F
先看看相乘的地方能否正常取得兩個矩陣的值吧,其次就是
09/22 19:19, 12F

09/22 19:19, , 13F
那個eval到底有沒有做他該做的事
09/22 19:19, 13F

09/22 19:23, , 14F
不過說真的,你把所有程式貼出來,又都是範例的內容,
09/22 19:23, 14F

09/22 19:23, , 15F
裡面又一堆註解,應該沒多少人會理你
09/22 19:23, 15F
文章代碼(AID): #1I7Qr0Wu (PHP)
文章代碼(AID): #1I7Qr0Wu (PHP)