text2-15.htm
来自「浙江大学计算机学院数据结构课程的教学课件」· HTM 代码 · 共 95 行
HTM
95 行
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#3399FF" alink="#FF0066">
<div id="Layer1" style="position:absolute; width:711px; height:21px; z-index:1; top: 10px; background-color: #CCCCCC; layer-background-color: #CCCCCC; border: 1px none #000000; left: 26px"><b>|</b><font face="宋体" size="2"><a href="../text1/text1-0.htm">第一章</a></font><b>|</b><font face="宋体" size="2">第二章</font><b>|</b><font face="宋体" size="2"><a href="../text3/text3-0.htm">第三章</a></font><b>|</b><font face="宋体" size="2"><a href="../text4/text4-0.htm">第四章</a></font><b>|</b><font face="宋体" size="2"><a href="../text5/text5-0.htm">第五章</a></font><b>|</b><font face="宋体" size="2"><a href="../text6/text6-0.htm">第六章</a></font><b>|</b><font face="宋体" size="2"><a href="../text7/text7-0.htm">第七章</a></font><b>|</b><font face="宋体" size="2"><a href="../text8/text8-0.htm">第八章</a></font><b>|</b><font face="宋体" size="2"><a href="../text9/text9-0.htm">第九章</a></font><b>|</b><font face="宋体" size="2"><a href="../text10/text10-0.htm">第十章</a></font><b>|</b><font size="2" face="宋体"><a href="../textA/textA-0.htm">算法分析</a><b><font color="#000000">|</font></b>
</font></div>
<pre align="left"><b><font face="Arial" size="4">
(2) <font size="5" color="#CC0033">Matrix Multiplication</font> </font></b></pre>
<table width="695" height="91">
<tr>
<td width="334"><b><font face="Arial" size="4"><img src="image/Sab.gif" width="121" height="39"></font></b></td>
<td width="345"><b><font face="Arial" size="4"><i>0 <= i < m, 0<= j < p</i></font></b></td>
</tr>
<tr>
<td width="334"><b><font face="Arial" size="4"><font color="#FF0033"><i>A(m*n)
·B(n*p) ~ D (m*p) </i></font></font></b></td>
<td width="345"><b><font face="Arial" size="4"> <img src="image/matrixab.gif" width="372" height="111"><font color="#FF0033"></font></font></b></td>
</tr>
</table>
<pre align="left"><b><font face="Arial" size="4"><font color="#FF0000" face="Arial, Helvetica, sans-serif">PROGRAM</font>
<font color="#0033CC">variables</font>:
<i><font color="#CC0099">row</font></i><font color="#CC0099"> </font> --- the row of A that we are currently multiplying
with the column in B
<i><font color="#CC0099">row_being</font></i> --- the position in a of the first element of the
current row
<i><font color="#CC0099">column</font></i> --- the column of B that we are currently multiplying
with a row in A
<i><font color="#CC0099">totald</font></i> --- the current number of elements in the product matrix D
<font color="#CC0099"><i>new_b </i></font>--- the spare matrix that is the transpose of B
<i><font color="#FF0033">a[totala + 1].row = rows_a
new_b [totalb + 1 ].row = cols_b</font></i>
void <font color="#FF0033">mmult</font> (term a[ ], term b [ ], term d[ ])
{ <font size="3"><i><font color="#CC0099"> /* multiply two sparse matrix*/</font></i></font>
int i, j, column, totalb = b[ 0].value, totald = 0;
int rows_a = a [0].row, cols_a = a[0].col, totala = a[0].value;
int cols_b = b[0].col;
int row_being = 1, row = a [1].row, sum = 0;
int new_b<font size="3">[MAX_TERMS]</font> [3];
if ( cols_a != b[ 0 ].row ) {
fprintf ( stderr, "Incompatible matrices \ n ");
exit(1);
}
<font color="#FF0033">fast_transpose</font>( b, new_b); <i><font color="#CC0099" size="3"> /* set boundary condition */</font></i>
a [totala +1 ].row = rows_a;
new_b [totalb + 1 ].row = cols_b;
new_b [totalb +1 ].col = 0;
for ( i =1; i <= totala; ) {
column = new_b [1].row;
for ( j =1; j <= totalb +1; ) { <i><font size="3" color="#CC0099"> /* multiply row of a by column of b */</font></i>
if (a[ i ].row != row ) {
<font color="#CC0033">storesum</font> ( d, &totald, row, column, &sum );
<font color="#CC0033"></font>i = row_ being;
<font color="#CC0033"></font>for ( ; new_b[ j ].row == column; j++)
<font color="#CC0033"></font> ;
<font color="#CC0033"></font>column = new_b[ j ].row;
}
else if (new_b[ j ].row != column ) {
storesum (d, &totald, row, column, &sum);
i = row_being.row;
column = new_b [ j ].row;
}
else switch (COMPARE ( a[ i ].col, new_b[ j ].col)) {
case -1: i ++ ; <font color="#CC0099" size="3">/* go to next term in a */</font>
break;
case 0 : <font color="#CC0099" size="3"> <i>/* add terms, go to next term in a and b */</i></font>
sum += ( a[ i++ ].value * new_b[ j++ ].value );
break;
case 1 : j++; <i><font color="#CC0099" size="3"> /* advance to next term in b */</font></i>
}
} / * end of for j <=totalb +1 */
for ( ; a[ i ].row == row; i++ )
;
row_being = i; row = a [ i ].row;
} <i><font color="#FF0099" size="3"> / * end of for i <= totala */</font></i>
d [ 0 ].row = row_a;
d [ 0 ].col = cols_b;
d [ 0 ].value = totald;
}
</font></b></pre>
<table width="731" cellspacing="0" cellpadding="0">
<tr>
<td width="327"> </td>
<td width="271"><a href="../index.htm"><img width="60" height="25" usemap="#MapMap4" border="0" src="../../images/home.gif"></a><a href="../index.htm"><map name="MapMap4"><area shape="rect" coords="42,-34,88,-15" href="text0.htm"><area shape="rect" coords="4,4,55,23" href="text2-index.htm"></map></a></td>
<td width="131"><font face="楷体_GB2312" size="2"><b><a href="text2-14.htm">上一页</a>
<a href="text2-16.htm">下一页</a> </b></font></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?