⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.html

📁 一个通用的数学库
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<HTML><!--  -- Copyright (c) 1998,1999  -- University of Notre Dame  --  -- Permission to use, copy, modify, distribute and sell this software  -- and its documentation for any purpose is hereby granted without fee,  -- provided that the above copyright notice appears in all copies and  -- that both that copyright notice and this permission notice appear  -- in supporting documentation.  The University of Notre Dame makes no  -- representations about the suitability of this software for any  -- purpose.  It is provided "as is" without express or implied warranty.  --  --><HEAD><TITLE>Description of Matrix</TITLE></HEAD><BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 	ALINK="#ff0000"> <FONT FACE=Helvetica><!--end header--><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 WIDTH=100%><TR><TD><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%><TR><TD Align=left valign=middle width=440 rowspan=2><FONT FACE=verdana,arial,helvetica SIZE=+3 COLOR=#110088><B>Matrix</B></FONT></TD><TD valign=middle Align=right width=128 rowspan=2><A HREF="http://lsc.nd.edu/research/mtl"><IMG BORDER=0 SRC="./glossy-mtl-logo.jpg" height=48 ALT="[MTL Home]"></A></TD><TD Align=center width=280 nowrap><Font face=verdana,arial,helvetica size=+3><b>Programmers Guide</b></font></TD></TR><TR BGCOLOR='#2020b0'><TD HEIGHT=20 VALIGN='MIDDLE' NOWRAP align=center STYLE="font-family:verdana,arial,helvetica;font-weight:bold;">&nbsp;&nbsp;<A STYLE="text-decoration:none;color:white;font-family:verdana,arial,helvetica;font-weight:bold;" HREF="./table_of_contents.html">Contents</A>&nbsp;|&nbsp;<A STYLE="text-decoration:none;color:white;font-family:verdana,arial,helvetica;font-weight:bold;" HREF="./mtl_index.html">Index</A>&nbsp;|&nbsp;&nbsp;<A STYLE="text-decoration:none;color:white;font-family:verdana,arial,helvetica;font-weight:bold;" HREF="http://lsc.nd.edu/research/mtl/search.html">Search</A>&nbsp;</TD></TR></TABLE></TD></TR><TR><TD><Table Border=0 CellPadding=0 CellSpacing=0 width=100% cols=2><TR><TD Align=left><BR><TABLE BORDER=0 CellSpacing=0 CellPadding=0><TR><TD><Img src = "containers.gif" Alt="" WIDTH = "194"  HEIGHT = "38" ></TD></TR></TABLE></TD><TD Align=right><BR><TABLE BORDER=0 CellSpacing=0 CellPadding=0><TR><TD><Img src = "concept.gif" Alt="" WIDTH = "194"  HEIGHT = "38" ></TD></TR></TABLE></TD></TR><TR><TD Align=left VAlign=top><b>Category</b>:containers</TD><TD Align=right VAlign=top><b>Component type</b>:concept</TD></TR></Table></TD></TR><TR><TD><IMG SRC="whitespace.gif" Alt="" WIDTH="32" HEIGHT="10" ALIGN="TOP"></TD></TR><TR BGCOLOR=#e0e0e0><TD VALIGN=BOTTOM><FONT FACE=arial,helvetica SIZE=+1><B>Description</B></FONT></TD></TR><TR><TD> <p>The central concept in the <A HREF="Matrix.html" >Matrix</A> Template Library is of course the <A HREF="Matrix.html" >Matrix</A>. An MTL <A HREF="Matrix.html" >Matrix</A> can be thought of as a Container of Containers, or a two-dimensional container. As an STL Container an MTL <A HREF="Matrix.html" >Matrix</A> has <tt>begin()</tt> and <tt>end()</tt> functions which return iterators for traversing over the 2D container.  These iterators dereferece to give a 1D container, which also has <tt>begin()</tt> and <tt>end()</tt> functions, since it to is an STL Container. The MTL implements a large variety of <A HREF="matrix.html" >matrix</A> formats, all with very different data representations and implementation details.  However, the same MTL <A HREF="Matrix.html" >Matrix</A> interface is provided for all of the matrices. This <A HREF="Matrix.html" >Matrix</A> interface is described here. <h4>1D and 2D Iterators</h4> The following is a simplified example of how the <A HREF="Matrix.html" >Matrix</A> iterators can be used to write a generic <A HREF="matrix.html" >matrix</A>-vector product. <p> <pre> template < class Matrix, class VecX, class VecY > void matvec_mult(const <A HREF="Matrix.html" >Matrix</A>& A, VecX x, VecY y) {   typename <A HREF="Matrix.html" >Matrix</A>::const_iterator i;   typename <A HREF="Matrix.html" >Matrix</A>::OneD::const_iterator j;   for (i = A.begin(); i != A.end(); i)     for (j = (*i).begin(); j != (*i).end(); j)       y[j.row()] += *j * x[j.column()]; } </pre> <p> There are two iterators used in the above algorithm, <tt>i</tt> and <tt>j</tt>. We refer to <tt>i</tt> as a 2D iterator since it iterates through the 2D container. We refer to <tt>j</tt> as the 1D iterator. <tt>*i</tt> gives a 1D Container, and the <tt>(*i).begin()</tt> and <tt>(*i).end()</tt> expressions define the iteration through the 1D part of the <A HREF="Matrix.html" >Matrix</A>, which could be a Row, Column, or Diagonal depending on the <A HREF="Matrix.html" >Matrix</A> type. <p>MTL matrices can also be sparse or <A HREF="dense.html" >dense</A>, so the traversal behaviour of the 1D iterator <tt>j</tt> varies accordingly. <tt>j</tt> only iterates over the non-zero elements in the sparse case. In addition the <tt>row()</tt> and <tt>column()</tt> functions on the 1D iterator return the row and column index corresponding to the position of the iterator. This hides the differences in indexing between a large class of sparse and <A HREF="dense.html" >dense</A> matrices. <p>Compare the MTL-style code to that of the BLAS-style and the SPARSKIT <A HREF="matrix.html" >matrix</A>-vector products. The BLAS and SPARSKIT algorithms include a lot of details that are specific to the <A HREF="matrix.html" >matrix</A> format being used. For instance, the sparse <A HREF="matrix.html" >matrix</A> algorithm must explicitly access the index and pointer arrays <tt>ia</tt> and <tt>ja</tt>. <p> <pre> // SPARSKIT-style sparse <A HREF="matrix.html" >matrix</A>-vector multiply void matvec_mult(double* a, int n, int* ia, int* ja,                  double* y, double* x) {   for (int i = 0; i < n; i)     for (int k = ia[i]; k < ia[i+1]; k)       y[i] +=  a[k] * x[ja[k]];   } } </pre> <p>The <A HREF="dense.html" >dense</A> <A HREF="matrix.html" >matrix</A> algorithm must use the leading <A HREF="dimension.html" >dimension</A> <tt>lda</tt> of the <A HREF="matrix.html" >matrix</A> to map to the appropriate place in memory. <p> <pre> // BLAS-style <A HREF="dense.html" >dense</A> <A HREF="matrix.html" >matrix</A>-vector multiply void matvec_mult(double* a, int m, int n, int lda,                   double* y, double* x) {   for (int i = 0; i < m; i)     for (int j = 0; j < n; j)       y[i] += a[i*lda+j] * x[j];   } } </pre> <p>The MTL-style algorithm has none of these format specific expressions because the implementation details are hidden under the MTL <A HREF="Matrix.html" >Matrix</A> interface. <p>If one uses a <A HREF="dense.html" >dense</A> MTL <A HREF="Matrix.html" >Matrix</A> with the generic algorithm, the resulting assembly code looks similar to the BLAS-style algorithm. If one uses a sparse MTL <A HREF="Matrix.html" >Matrix</A> with the generic algorithm, the resulting assembly code looks similar to the SPARSKIT-style algorithm. <h4>operator()(i,j)</h4> One may ask, what about the <tt>A(i,j)</tt> operator? MTL matrices do have an <tt>A(i,j)</tt> operator, but using it typically is not the most efficient or ``generic'' way to traverse and access the <A HREF="matrix.html" >matrix</A>, especially if the <A HREF="matrix.html" >matrix</A> is sparse. Furthermore, if the <A HREF="Matrix.html" >Matrix</A> is <A HREF="banded.html" >banded</A>, one would have to consider which indices of the <A HREF="Matrix.html" >Matrix</A> are valid to access. With the MTL <A HREF="Matrix.html" >Matrix</A> iterators, one does not have to worry about such considerations. Traversing with the MTL <A HREF="Matrix.html" >Matrix</A> iterators gives you access to all <A HREF="matrix.html" >matrix</A> elements that are stored in any given storage format.  <h4>operator[](i)</h4> This operator gives access to the OneD containers within a <A HREF="Matrix.html" >Matrix</A>. For instance, if you have a <A HREF="column_major.html" >column_major</A> <A HREF="matrix.html" >matrix</A> <tt>A</tt>, and wish to find the index of the maximum element in the first column, one would do the following: <pre> <A HREF="Matrix.html" >Matrix</A>::Column first_column = A[0]; max_elt_index = max_index(first_column); </pre> <h4>Accessing Rows vs. Columns</h4> Most <A HREF="matrix.html" >matrix</A> types only provide access to either rows or columns, but not both. However, if one has a <A HREF="dense.html" >dense</A> <A HREF="rectangle.html" >rectangle</A> <A HREF="matrix.html" >matrix</A>, then the <A HREF="matrix.html" >matrix</A> can be easily converted back and forth from <A HREF="row_major.html" >row_major</A> to <A HREF="column_major.html" >column_major</A> using the <tt>rows</tt> and <tt>columns</tt> helper functions. The following finds the maximum element in the first row of the <A HREF="matrix.html" >matrix</A>. <pre> max_elt_index = max_index(rows(A)[0]); </pre>   <h4>Submatrices</h4> The <tt>sub_matrix()</tt> function returns a new <A HREF="matrix.html" >matrix</A> object that is a view into a particular portion of the <A HREF="matrix.html" >matrix</A>. The indexing within the new <A HREF="matrix.html" >matrix</A> is reset so that the first element is at (0,0). Here is an example of creating a submatrix: <p> <pre>      [  1  2  3  4 ] A =  [  5  6  7  8 ]      [  9 10 11 12 ]      [ 13 14 15 16 ] A_00 = [  1  2 ]  A_01 = [ 3 4 ]        [  5  6 ]         [ 7 8 ] A_10 = [  9 10 ]  A_11 = [ 11 12 ]        [ 13 14 ]         [ 15 16 ] A_00 = A.sub_matrix(0,2,0,2); A_01 = A.sub_matrix(2,4,0,2); A_10 = A.sub_matrix(0,2,2,4); A_11 = A.sub_matrix(2,4,2,4); </pre> <p>If one wants to create submatrices for the whole <A HREF="matrix.html" >matrix</A>, as above, MTL provides a short cut in the partition fuction. The function returns a <A HREF="Matrix.html" >Matrix</A> of submatrices. The input is the row and column numbers that split up the matrices. The following code gives an example. <p> <pre> int splitrows[] = { 2 }; int splitcols[] = { 2 }; <A HREF="Matrix.html" >Matrix</A>::partitioned Ap(2,2); partition(array_to_vec(splitrows),            array_to_vec(splitcols), A, Ap); </pre> <p>Now <tt>Ap(0,0)</tt> is equivalent to <tt>A_00</tt>, <tt>Ap(0,1)</tt> is equivalent to <tt>A_01</tt>, etc. <h4><A HREF="Matrix.html" >Matrix</A> Type Selection</h4> To create an MTL <A HREF="Matrix.html" >Matrix</A>, one uses the <A HREF="matrix.html" >matrix</A> type constructor to <A HREF="choose.html" >choose</A> the particular storage format, element type, etc. This performs a shallow <A HREF="copy.html" >copy</A>, since MTL objects are really just reference counted handles to the actually data.</TD></TR><TR><TD><IMG SRC="whitespace.gif" Alt="" WIDTH="32" HEIGHT="10" ALIGN="TOP"></TD></TR><TR BGCOLOR=#e0e0e0><TD VALIGN=BOTTOM><FONT FACE=arial,helvetica SIZE=+1><B>Refinement of</B></FONT></TD></TR><TR><TD> Container</TD></TR><TR><TD><IMG SRC="whitespace.gif" Alt="" WIDTH="32" HEIGHT="10" ALIGN="TOP"></TD></TR><TR BGCOLOR=#e0e0e0><TD VALIGN=BOTTOM><FONT FACE=arial,helvetica SIZE=+1><B>Associated types</B></FONT></TD></TR><TR><TD><TABLE BORDER><TR><TH>Concept</TH><TH>Type name</TH><TH>Description</TH></TR>      <TR><TD VALIGN="TOP">    <TT>Tag </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::shape</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      See <A HREF="matrix_traits.html" >matrix_traits</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>Tag </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::orientation</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Either <A HREF="row_tag.html" >row_tag</A> or <A HREF="column_tag.html" >column_tag</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>Tag </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::sparsity</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Either <A HREF="dense_tag.html" >dense_tag</A> or <A HREF="sparse_tag.html" >sparse_tag</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>Tag </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::dimension</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Either <A HREF="oned_tag.html" >oned_tag</A> or <A HREF="twod_tag.html" >twod_tag</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT><A HREF="Matrix.html" >Matrix</A> </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::transpose_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Used by trans helper function      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT><A HREF="Matrix.html" >Matrix</A> </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::strided_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Used by rows and columns helper functions      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT><A HREF="Matrix.html" >Matrix</A> </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::scaled_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Used by scaled helper function      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT><A HREF="Vector.html" >Vector</A> </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::OneD</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      The type for a OneD slice of the <A HREF="Matrix.html" >Matrix</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT><A HREF="Vector.html" >Vector</A> </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::OneDRef</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      The type for a reference to a OneD slice of the <A HREF="Matrix.html" >Matrix</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT><A HREF="Matrix.html" >Matrix</A> </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::submatrix_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      The type for a submatrix of this <A HREF="Matrix.html" >Matrix</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT><A HREF="Matrix.html" >Matrix</A> </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::partition_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      The type for a partitioned version of this <A HREF="Matrix.html" >Matrix</A>      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>TrivialConcept </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::value_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      The element type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>TrivialConcept&#38; </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::reference</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Reference to the element type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>const TrivialConcept&#38; </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::const_reference</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Const reference to the element type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>TrivialConcept* </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::pointer</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Pointer to the element type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>BidirectionalIterator </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::iterator</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Iterator type, dereference gives a OneD      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>const BidirectionalIterator </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::const_iterator</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Const iterator type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>BidirectionalIterator </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::reverse_iterator</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Reverse iterator type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>const BidirectionalIterator </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::const_reverse_iterator</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Const reverse iterator type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>NonNegativeIntegralType </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::size_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Size type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>IntegralType </TT>    </TD>    <TD VALIGN="TOP">    <TT>X::difference_type</TT>    </TD>    <TD><FONT FACE=Times SIZE=3>      Difference type      </FONT></TD>	    </TR>        <TR><TD VALIGN="TOP">    <TT>size_type </TT>    </TD>    <TD VALIGN="TOP">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -