[請益] 矩陣相乘但跑不出結果?????
$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
08/28 18:42, 2F
→
08/28 18:43, , 3F
08/28 18:43, 3F
→
08/28 18:44, , 4F
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
08/29 08:31, 7F
→
08/30 12:51, , 8F
08/30 12:51, 8F
→
08/30 12:53, , 9F
08/30 12:53, 9F
推
08/31 07:10, , 10F
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
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
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
3
15
PHP 近期熱門文章
PTT數位生活區 即時熱門文章