📄 mod2dense.html
字号:
<HTML><HEAD><TITLE> Dense Modulo-2 Matrix Routines </TITLE></HEAD><BODY><H1> Dense Modulo-2 Matrix Routines </H1><P>This module implements operations on matrices in which the elementsare all 0 or 1, with addition and multiplication being done modulo 2.The matrices are stored with consecutive bits of a column packed into32-bit words, and the procedures are implemented where possible usingbit operations on these words. <P>This is an appropriate representation when the matrices are dense(ie, 0s and 1s are about equally frequent). Matrices in which mostelements are 0s may be better handled with the <AHREF="mod2sparse.html">sparse modulo-2 matrix routines</A>. Matricescan be converted between these two formats using the <AHREF="mod2convert.html">module-2 matrix conversion routines</A>. <P>All procedures in this module display an error message on standarderror and terminate the program if passed an invalid argument(indicative of a programming error), or if memory cannot be allocated.Errors from invalid contents of a file result in an error code beingreturned to the caller, with no message being printed by this module.<H2>Representation of dense matrices</H2><P>This module represents a matrix by a pointer to a structure of type<TT>mod2dense</TT>. This structure records the number of rows andcolumns in the matrix, and contains an array of pointers to where thebits making up each column are stored. These bits are packed 32 perword. When possible, bits in a column are manipulated 32 bits at atime, which operations such as adding one column to another muchfaster than the corresponding operations on rows. The pointerstructure also allows the columns of a matrix to easily be rearranged,which may be necessary when doing matrix inversion.<P><B>Header files required</B>:<TT>mod2dense.h</TT><A NAME="dimension-sec"><P><HR><CENTER><BIG>Dimension Macros</BIG></CENTER></A><HR>The following macros take a pointer to a mod2dense structure as theirargument, and return the number of rows or the number of columns inthe matrix pointed to, which will have been fixed when the matrix wascreated with <A HREF="#allocate">mod2dense_allocate</A>:<BLOCKQUOTE><PRE>mod2dense_rows(m) /* Returns the number of rows in m */mod2dense_cols(m) /* Returns the number of columns in m */</PRE></BLOCKQUOTE><A NAME="alloc-sec"><P><HR><CENTER><BIG>Allocating and Freeing Dense Modulo-2 Matrices</BIG></CENTER></A><A NAME="allocate"><HR><B>mod2dense_allocate</B>: Allocate space for a dense module-2 matrix.</A><BLOCKQUOTE><PRE>mod2dense *mod2dense_allocate ( int n_rows, /* Number of rows in matrix */ int n_cols /* Number of columns in matrix */)</PRE></BLOCKQUOTE>Allocates space for a matrix with the given number of rows andcolumns, and returns a pointer to it. If there is not enough memoryavailable, a message is displayed on standard error and the program isterminated. The matrix should be freed with <AHREF="#free"><TT>mod2dense_free</TT></A> once it is no longer in use.<P><A NAME="free"><HR><B>mod2dense_free</B>: Free the space occupied by a dense module-2 matrix.</A><BLOCKQUOTE><PRE>void mod2dense_free ( mod2dense *m /* Pointer to matrix to free */)</PRE></BLOCKQUOTE>Frees the space occupied by the matrix for re-use. The pointer passedshould no longer be used.<A NAME="copy-clear-sec"><P><HR><CENTER><BIG>Copying and Clearing Dense Modulo-2 Matrices</BIG></CENTER></A><A NAME="clear"><HR><B>mod2dense_clear</B>: Set all elements of a matrix to zero.</A><BLOCKQUOTE><PRE>void mod2dense_clear( mod2dense *m /* Pointer to matrix to clear */)</PRE></BLOCKQUOTE>Sets all of the elements of the matrix passed to 0.<P><A NAME="copy"><HR><B>mod2dense_copy</B>: Copy the contents of one matrix to another.</A><BLOCKQUOTE><PRE>void mod2dense_copy( mod2dense *m /* Pointer to matrix to copy from */ mod2dense *r /* Pointer to matrix to receive data */)</PRE></BLOCKQUOTE>Copies the contents of the first matrix passed, <B>m</B>, to thesecond matrix passed, <B>r</B>, which must already have beenallocated, and must have at least as many rows and columns as thefirst. If <B>r</B> is larger than <B>m</B>, its elements that haverow or column indexes greater than the dimension of <B>m</B> are setto zeros.<P><A NAME="copyrows"><HR><B>mod2dense_copyrows</B>: Copy selected rows from one matrix to another.</A><BLOCKQUOTE><PRE>void mod2dense_copyrows( mod2dense *m, /* Pointer to matrix to copy columns from */ mod2dense *r, /* Pointer to matrix in which to store data */ int *rows /* Indexes of rows, numbered from 0 */)</PRE></BLOCKQUOTE>Copies selected rows of the first matrix, <B>m</B>, to the secondmatrix, <B>r</B>, which must already have been allocated, and whichmust have at least as many columns as <B>m</B>. The indexes of therows to copy are given in order as an array of length the same asthe number of rows in <B>r</B>; duplicates are allowed. Rowindexes start at 0. These rows are copied to <B>r</B>, with therow indexed by the first entry in <B>rows</B> going to the firstrow of <B>r</B>, and so forth. If <B>r</B> has more columns than<B>m</B>, the extra entries in each row are set to zeros.<P><A NAME="copycols"><HR><B>mod2dense_copycols</B>: Copy selected columns from one matrix to another.</A><BLOCKQUOTE><PRE>void mod2dense_copycols( mod2dense *m, /* Pointer to matrix to copy columns from */ mod2dense *r, /* Pointer to matrix in which to store data */ int *cols /* Indexes of columns, numbered from 0 */)</PRE></BLOCKQUOTE>Copies selected columns of the first matrix, <B>m</B>, to the secondmatrix, <B>r</B>, which must already have been allocated, and whichmust have at least as many rows as <B>m</B>. The indexes of thecolumns to copy are given in order as an array of length the same asthe number of columns in <B>r</B>; duplicates are allowed. Columnindexes start at 0. These columns are copied to <B>r</B>, with thecolumn indexed by the first entry in <B>cols</B> going to the firstcolumn of <B>r</B>, and so forth. If <B>r</B> has more rows than<B>m</B>, the extra entries in each column are set to zeros.<A NAME="input-output-sec"><P><HR><CENTER><BIG>Input and Output of Dense Modulo-2 Matrices</BIG></CENTER></A><A NAME="print"><HR><B>mod2dense_print</B>: Print a dense modulo-2 matrix in human-readable form.</A><BLOCKQUOTE><PRE>void mod2dense_print( FILE *f, /* File to print to */ mod2dense *m /* Pointer to matrix to print */)</PRE></BLOCKQUOTE>The matrix is printed on standard output as "0" and "1" characters,each preceded by a space, with one line of "0"s and "1"s for each rowof the matrix.<P><A NAME="write"><HR><B>mod2dense_write</B>: Write a dense modulo-2 matrix to a file in machine-readable format.</A><BLOCKQUOTE><PRE>int mod2dense_write( FILE *f, /* File to write data to */ mod2dense *m /* Pointer to matrix write out */)</PRE></BLOCKQUOTE>Writes a machine-readable representation the dense matrix <B>m</B> tothe file <B>f</B>. The file should have been opened in binary mode(with a "b" in the mode passed to fopen). The contents written willnot be text, and will not be human-readable. Other binary data mayprecede or follow the data for the matrix written. <P>The data written to the file consists of the number of rows and thenumber of columns, followed by the bits in each column, packed into32-bit words. The data should be readable by <AHREF="#read"><TT>mod2dense_read</TT></A> even on a machine with adifferent byte-ordering.<P>The value returned by <TT>mod2dense_write</TT> is one if theoperation was successful, zero if an error of some sort occurred.<P><A NAME="read"><HR><B>mod2dense_read</B>: Read a dense modulo-2 matrix from a file.</A><BLOCKQUOTE><PRE>mod2dense *mod2dense_read( FILE *f, /* File to read data from */)</PRE></BLOCKQUOTE>Reads a dense modulo-2 matrix from the file <B>f</B>. This fileshould have been opened in binary mode (with a "b" in the mode passedto fopen). The contents of the file at the point when<TT>mod2dense_read</TT> is called should have been written by <AHREF="#write"><TT>mod2dense_write</TT></A>. Other binary data mayprecede or follow this data.<P>The value returned is a pointer to the matrix read, for which spacewill have been allocated by <TT>mod2dense_read</TT>, or zero if anerror occurred (either an error reading the file, or data not in theright format).<A NAME="elementary-sec"><P><HR><CENTER><BIG>Elementary Operations on Dense Modulo-2 Matrices</BIG></CENTER>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -