作者fly1102 (Hh)
站内PHP
标题[请益] 矩阵相乘但跑不出结果?????
时间Wed Aug 28 15:56:46 2013
$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
1F:→ tkdmaf:可以明白的表述一下......你的问题是什麽? 08/28 17:40
2F:→ fly1102:我要做的是二维的矩阵相乘,两个矩阵都ECHO 得出来,但是 08/28 18:42
3F:→ fly1102:ECHO出来却是空白的????为什麽 08/28 18:43
4F:→ fly1102:带入矩阵相乘的FUNCTION却是空白的???为什麽 08/28 18:44
5F:→ PsMonkey:我可以推「因为爱」吗? [逃] 08/28 18:48
6F:→ tkdmaf:除了单元测试,我想不到其他你能做的事了。 08/28 23:12
7F:→ tails32100:为何要用eval @@ 08/29 08:31
8F:→ fly1102:function是我在网路上找到的code,用他网路上提共的范例是 08/30 12:51
9F:→ fly1102:echo得出结果的,还是有其他矩阵相乘的code可以借我参考呢 08/30 12:53
10F:推 bobju:这大概得自己debug,或是付钱找人debug了.. 08/31 07:10
11F:推 cwlin0416:既然还没相乘都可以正常,那问题应该就出在相乘的地方, 09/22 19:19
12F:→ cwlin0416:先看看相乘的地方能否正常取得两个矩阵的值吧,其次就是 09/22 19:19
13F:→ cwlin0416:那个eval到底有没有做他该做的事 09/22 19:19
14F:推 cwlin0416:不过说真的,你把所有程式贴出来,又都是范例的内容, 09/22 19:23
15F:→ cwlin0416:里面又一堆注解,应该没多少人会理你 09/22 19:23