Warning: This is no longer the latest available version of this module. Please see the releases page for the most recent version. The Webnucleo group strongly recommends the use of the latest version of any of its online modules.
This tutorial demonstrates how to use the example codes in the wn_matrix distribution src/examples directory. The sample input files used in the tutorial are included in the distribution release and may be located in the data_pub directory.
Example 1: Create a matrix, store elements, and scale them.
wn_matrix routines demonstrated in example1.c are:
- Line 56: WnMatrix__new()
- Line 65: WnMatrix__assignElement()
- Line 77: WnMatrix__removeElement()
- Line 100: WnMatrix__getElement()
- Line 109: WnMatrix__getNumberOfElements()
- Line 116: WnMatrix__scaleMatrix()
- Line 150: WnMatrix__clear()
- Line 174: WnMatrix__free()
To run example1, type the following on the command line:
./example1 > ex1_output.txt
The result is ex1_output.txt
Example 2: Print out a matrix to a file.
wn_matrix routines demonstrated in example2.c are:
- Line 76: WnMatrix__new()
- Line 85: WnMatrix__assignElement()
- Line 98: WnMatrix__writeMatrixToAsciiFile()
- Line 107: WnMatrix__free()
To run example2, type the following on the command line:
./example2 2.0 ex2_output.txt
The result is ex2_output.txt
Example 3: Convert a matrix to compressed row format.
wn_matrix routines demonstrated in example3.c are:
- Line 80: WnMatrix__new()
- Line 89: WnMatrix__assignElement()
- Line 101: WnMatrix__getCsr()
- Line 117: WnMatrix__Csr__getRowPointerVector()
- Line 127: WnMatrix__getNumberOfElements()
- Line 135: WnMatrix__Csr__getColumnVector()
- Line 136: WnMatrix__Csr__getValueVector()
- Line 147: WnMatrix__Csr__free()
- Line 153: WnMatrix__getCoo()
- Line 168: WnMatrix__Coo__getRowVector()
- Line 169: WnMatrix__Coo__getColumnVector()
- Line 170: WnMatrix__Coo__getValueVector()
- Line 181: WnMatrix__Coo__free()
- Line 182: WnMatrix__free()
To run example3, type the following on the command line:
./example3 ex3_output.txt
The result is ex3_output.txt
Example 4: Convert a matrix to Yale sparse format.
wn_matrix routines demonstrated in example4.c are:
- Line 77: WnMatrix__new()
- Line 86: WnMatrix__assignElement()
- Line 98: WnMatrix__getYale()
- Line 100: WnMatrix__Yale__getPointerVector()
- Line 101: WnMatrix__Yale__getValueVector()
- Line 114: WnMatrix__getNumberOfRows()
- Line 124: WnMatrix__getNumberOfElements()
- Line 151: WnMatrix__getElement()
- Line 163: WnMatrix__Yale__free()
- Line 164: WnMatrix__free()
To run example4, type the following on the command line:
./example4 ex4_output.txt
The result is ex4_output.txt
Example 5: Multiply a matrix by a vector and a transpose matrix by a vector.
wn_matrix routines demonstrated in example5.c are:
- Line 60: WnMatrix__new()
- Line 69: WnMatrix__assignElement()
- Line 99: WnMatrix__getMatrixTimesVector()
- Line 120: WnMatrix__getElement()
- Line 148: WnMatrix__getTransposeMatrixTimesVector()
- Line 196: WnMatrix__free()
To run example5, type the following on the command line:
./example5 > ex5_output.txt
The result is ex5_output.txt
Example 6: Insert a smaller matrix into a larger one and extract a smaller matrix from a larger one.
wn_matrix routines demonstrated in example6.c are:
- Line 80: WnMatrix__new()
- Line 95: WnMatrix__assignElement()
- Line 107: WnMatrix__insertMatrix()
- Line 115: WnMatrix__getDenseMatrix()
- Line 132: WnMatrix__getElement()
- Line 165: WnMatrix__free()
- Line 171: WnMatrix__extractMatrix()
- Line 182: WnMatrix__getNumberOfRows()
- Line 187: WnMatrix__getNumberOfColumns()
To run example6, type the following on the command line:
./example6 ex6_output.txt
The result is ex6_output.txt
Example 7: Print out the non-zero elements of a row or column of a matrix. from a larger one.
wn_matrix routines demonstrated in example7.c are:
- Line 107: WnMatrix__new()
- Line 116: WnMatrix__assignElement()
- Line 141: WnMatrix__getRow()
- Line 146: WnMatrix__getColumn()
- Line 156: WnMatrix__Line__getNonZeroIndices()
- Line 157: WnMatrix__Line__getNonZeroElements()
- Line 159: WnMatrix__Line__getNumberOfElements()
- Line 171: WnMatrix__Line__free()
- Line 172: WnMatrix__free()
To run example7, type the following on the command line:
./example7 ../../data_pub/coordinate_matrix_1_input.txt row 1 > ex7_output.txt
The result is ex7_output.txt
Try other rows or columns. For example, try
./example7 ../../data_pub/coordinate_matrix_1_input.txt column 2 > ex7_output_column.txt
The result is ex7_output_column.txt
Example 8: Get the transfer matrix form of a matrix.
wn_matrix routines demonstrated in example8.c are:
- Line 112: WnMatrix__new()
- Line 121: WnMatrix__assignElement()
- Line 134: WnMatrix__getTransferMatrix()
- Line 144: WnMatrix__getNumberOfRows()
- Line 147: WnMatrix__getNumberOfColumns()
- Line 154: WnMatrix__getElement()
- Line 167: WnMatrix__free()
To run example8, type the following on the command line:
./example8 ../../data_pub/coordinate_matrix_2_input.txt ex8_output.txt
The result is ex8_output.txt
Example 9: Get a copy of a matrix and the transpose of a matrix.
wn_matrix routines demonstrated in example9.c are:
- Line 113: WnMatrix__new()
- Line 122: WnMatrix__assignElement()
- Line 135: WnMatrix__getCopy()
- Line 141: WnMatrix__getTranspose()
- Line 151: WnMatrix__getNumberOfRows()
- Line 154: WnMatrix__getNumberOfColumns()
- Line 161: WnMatrix__getElement()
- Line 176: WnMatrix__free()
To run example9, type the following on the command line:
./example9 ../../data_pub/coordinate_matrix_2_input.txt ex9_output.txt
The result is ex9_output.txt
Example 10: Convert ascii coordinate matrix data into XML data and write to a file.
wn_matrix routines demonstrated in example10.c are:
- Line 103: WnMatrix__new()
- Line 112: WnMatrix__assignElement()
- Line 125: WnMatrix__getCoo()
- Line 131: WnMatrix__free()
- Line 137: WnMatrix__Coo__writeToXmlFile()
- Line 143: WnMatrix__Coo__free()
To run example10, type the following on the command line:
./example10 ../../data_pub/coordinate_matrix_2_input.txt ex10_output.xml
The result is ex10_output.xml
Example 11: Print out the data from a matrix in XML format.
wn_matrix routines demonstrated in example11.c are:
- Line 74: WnMatrix__is_valid_input_xml()
- Line 84: WnMatrix__new_from_xml()
- Line 93: WnMatrix__getCoo()
- Line 102: WnMatrix__getNumberOfElements()
- Line 107: WnMatrix__Coo__getRowVector()
- Line 108: WnMatrix__Coo__getColumnVector()
- Line 109: WnMatrix__Coo__getValueVector()
- Line 118: WnMatrix__Coo__free()
- Line 120: WnMatrix__free()
To run example11, type the following on the command line:
./example11 ../../data_pub/coordinate_matrix_2.xml > ex11_output.txt
The result is ex11_output.txt
Use an XPath expression to select out particular rows, columns, or values:
./example11 ../../data_pub/coordinate_matrix_2.xml "[row < 3 or value > 0]" > ex11_output_xpath.txt
The result is ex11_output_xpath.txt
Example 12: Read in coordinate matrix data from an XML file, convert to compressed sparse row format, and output to an XML file.
wn_matrix routines demonstrated in example12.c are:
- Line 74: WnMatrix__is_valid_input_xml()
- Line 83: WnMatrix__new_from_xml()
- Line 89: WnMatrix__getCsr()
- Line 95: WnMatrix__free()
- Line 101: WnMatrix__Csr__writeToXmlFile()
- Line 107: WnMatrix__Csr__free()
To run example12, type the following on the command line:
./example12 ../../data_pub/coordinate_matrix_2.xml ex12_csr.xml
The result is ex12_csr.xml
Example 13: Read in coordinate matrix data from an XML file, convert to Yale sparse format, and output to an XML file.
wn_matrix routines demonstrated in example13.c are:
- Line 74: WnMatrix__is_valid_input_xml()
- Line 83: WnMatrix__new_from_xml()
- Line 89: WnMatrix__getYale()
- Line 95: WnMatrix__free()
- Line 101: WnMatrix__Yale__writeToXmlFile()
- Line 107: WnMatrix__Yale__free()
To run example13, type the following on the command line:
./example13 ../../data_pub/coordinate_matrix_2.xml ex13_yale.xml
The result is ex13_yale.xml