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 47: WnMatrix__new()
- Line 56: WnMatrix__assignElement()
- Line 70: WnMatrix__removeElement()
- Line 92: WnMatrix__getElement()
- Line 102: WnMatrix__getNumberOfElements()
- Line 109: WnMatrix__scaleMatrix()
- Line 143: WnMatrix__clear()
- Line 171: 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 66: WnMatrix__new()
- Line 75: WnMatrix__assignElement()
- Line 88: WnMatrix__writeMatrixToAsciiFile()
- Line 97: 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 70: WnMatrix__new()
- Line 79: WnMatrix__assignElement()
- Line 91: WnMatrix__getCsr()
- Line 107: WnMatrix__Csr__getRowPointerVector()
- Line 117: WnMatrix__getNumberOfElements()
- Line 125: WnMatrix__Csr__getColumnVector()
- Line 126: WnMatrix__Csr__getValueVector()
- Line 137: WnMatrix__Csr__free()
- Line 143: WnMatrix__getCoo()
- Line 158: WnMatrix__Coo__getRowVector()
- Line 159: WnMatrix__Coo__getColumnVector()
- Line 160: WnMatrix__Coo__getValueVector()
- Line 171: WnMatrix__Coo__free()
- Line 172: 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 67: WnMatrix__new()
- Line 76: WnMatrix__assignElement()
- Line 88: WnMatrix__getYale()
- Line 90: WnMatrix__Yale__getPointerVector()
- Line 91: WnMatrix__Yale__getValueVector()
- Line 138: WnMatrix__getElement()
- Line 150: WnMatrix__Yale__free()
- Line 151: WnMatrix__free()
To run example4, type the following on the command line:
./example4 ex4_output.txt
The result is ex4_output.txt
Example 5: Insert a smaller matrix into a larger one and extract a smaller matrix from a larger one.
wn_matrix routines demonstrated in example5.c are:
- Line 70: WnMatrix__new()
- Line 85: WnMatrix__assignElement()
- Line 98: WnMatrix__insertMatrix()
- Line 106: WnMatrix__getGslMatrix()
- Line 125: WnMatrix__getElement()
- Line 160: WnMatrix__free()
- Line 168: WnMatrix__extractMatrix()
- Line 179: WnMatrix__getNumberOfRows()
- Line 184: WnMatrix__getNumberOfColumns()
To run example5, type the following on the command line:
./example5 ex5_output.txt
The result is ex5_output.txt
Example 6: Convert ascii coordinate matrix data into XML data and write to a file.
wn_matrix routines demonstrated in example6.c are:
- Line 103: WnMatrix__new()
- Line 118: WnMatrix__assignElement()
- Line 131: WnMatrix__getCoo()
- Line 137: WnMatrix__free()
- Line 144: WnMatrix__Coo__writeToXmlFile()
- Line 152: WnMatrix__Coo__free()
To run example6, type the following on the command line:
./example6 ../../data_pub/matrix.txt ex6_output.xml
The result is ex6_output.xml
Output the matrix element values in format other than default.
./example6 ../../data_pub/matrix.txt ex6_output_with_formatting.xml %.12e
The result is ex6_output_with_formatting.xml
Example 7: Print out the data from a matrix in XML format.
wn_matrix routines demonstrated in example7.c are:
- Line 64: WnMatrix__is_valid_input_xml()
- Line 74: WnMatrix__new_from_xml()
- Line 83: WnMatrix__getCoo()
- Line 92: WnMatrix__getNumberOfElements()
- Line 97: WnMatrix__Coo__getRowVector()
- Line 98: WnMatrix__Coo__getColumnVector()
- Line 99: WnMatrix__Coo__getValueVector()
- Line 108: WnMatrix__Coo__free()
- Line 110: WnMatrix__free()
To run example7, type the following on the command line:
./example7 ../../data_pub/matrix.xml > ex7_output.txt
The result is ex7_output.txt
Use an XPath expression to select out particular rows, columns, or values:
./example7 ../../data_pub/matrix.xml "[row < 3 or value > 0]" > ex7_output_xpath.txt
The result is ex7_output_xpath.txt
Example 8: Print out the non-zero elements of a row or column of a matrix.
wn_matrix routines demonstrated in example8.c are:
- Line 69: WnMatrix__new_from_xml()
- Line 87: WnMatrix__getRow()
- Line 95: WnMatrix__getColumn()
- Line 108: WnMatrix__Line__getNonZeroIndices()
- Line 109: WnMatrix__Line__getNonZeroElements()
- Line 111: WnMatrix__Line__getNumberOfElements()
- Line 123: WnMatrix__Line__free()
- Line 124: WnMatrix__free()
To run example8, type the following on the command line:
./example8 ../../data_pub/matrix.xml row 1 > ex8_output.txt
The result is ex8_output.txt
Try other rows or columns. For example, try
./example8 ../../data_pub/matrix.xml column 2 > ex8_output_column.txt
The result is ex8_output_column.txt
Example 9: Insert and remove rows and columns.
wn_matrix routines demonstrated in example9.c are:
- Line 67: WnMatrix__new_from_xml()
- Line 80: WnMatrix__removeRow()
- Line 93: WnMatrix__getNumberOfRows()
- Line 102: WnMatrix__insertColumn()
- Line 115: WnMatrix__free()
- Line 136: WnMatrix__getNumberOfColumns()
- Line 144: WnMatrix__getElement()
To run example9, type the following on the command line:
./example9 ../../data_pub/matrix.xml > ex9_output.txt
The result is ex9_output.txt
Example 10: Get the transfer matrix form of a matrix.
wn_matrix routines demonstrated in example10.c are:
- Line 64: WnMatrix__new_from_xml()
- Line 79: WnMatrix__getTransferMatrix()
- Line 89: WnMatrix__getNumberOfRows()
- Line 93: WnMatrix__getNumberOfColumns()
- Line 102: WnMatrix__getElement()
- Line 115: WnMatrix__free()
To run example10, type the following on the command line:
./example10 ../../data_pub/matrix.xml ex10_output.txt
The result is ex10_output.txt
Example 11: Get a copy of a matrix and the transpose of a matrix.
wn_matrix routines demonstrated in example11.c are:
- Line 65: WnMatrix__new_from_xml()
- Line 80: WnMatrix__getCopy()
- Line 86: WnMatrix__getTranspose()
- Line 96: WnMatrix__getNumberOfRows()
- Line 99: WnMatrix__getNumberOfColumns()
- Line 108: WnMatrix__getElement()
- Line 123: WnMatrix__free()
To run example11, type the following on the command line:
./example11 ../../data_pub/matrix.xml ex11_output.txt
The result is ex11_output.txt
Example 12: Convert ascii vector data into XML data and write to a file.
wn_matrix routines demonstrated in example12.c are:
- Line 117: WnMatrix__write_gsl_vector_to_xml_file()
To run example12, type the following on the command line:
./example12 ../../data_pub/vector.txt ex12_output.xml
The result is ex12_output.xml
To output the vector data in a format other than the default, enter the format code as well:
./example12 ../../data_pub/vector.txt ex12_output_with_formatting.xml %.14e
The result is ex12_output_with_formatting.xml
Example 13: Print out the data from a vector in XML format.
wn_matrix routines demonstrated in example13.c are:
- Line 62: WnMatrix__is_valid_vector_input_xml()
- Line 72: WnMatrix__new_gsl_vector_from_xml()
To run example13, type the following on the command line:
./example13 ../../data_pub/vector.xml > ex13_output.txt
The result is ex13_output.txt
Use an XPath expression to select out particular elements (here only the elements larger than zero):
./example13 ../../data_pub/vector.xml "[. > 0]" > ex13_output_xpath.txt
The result is ex13_output_xpath.txt
Example 14: 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 example14.c are:
- Line 68: WnMatrix__is_valid_input_xml()
- Line 77: WnMatrix__new_from_xml()
- Line 83: WnMatrix__getCsr()
- Line 89: WnMatrix__free()
- Line 96: WnMatrix__Csr__writeToXmlFile()
- Line 104: WnMatrix__Csr__free()
To run example14, type the following on the command line:
./example14 ../../data_pub/matrix.xml ex14_csr.xml
The result is ex14_csr.xml
To output the matrix element values with a format other than the default, input the desired format code:
./example14 ../../data_pub/matrix.xml ex14_csr_with_formatting.xml %.12e
The result is ex14_csr_with_formatting.xml
Example 15: 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 example15.c are:
- Line 68: WnMatrix__is_valid_input_xml()
- Line 77: WnMatrix__new_from_xml()
- Line 83: WnMatrix__getYale()
- Line 89: WnMatrix__free()
- Line 96: WnMatrix__Yale__writeToXmlFile()
- Line 104: WnMatrix__Yale__free()
To run example15, type the following on the command line:
./example15 ../../data_pub/matrix.xml ex15_yale.xml
The result is ex15_yale.xml
To output the matrix element values with a format other than the default, input the desired format code:
./example15 ../../data_pub/matrix.xml ex15_yale_with_formatting.xml %.10f
The result is ex15_yale_with_formatting.xml
Example 16: Multiply a matrix by a vector and a transpose matrix by a vector.
wn_matrix routines demonstrated in example16.c are:
- Line 63: WnMatrix__is_valid_input_xml()
- Line 72: WnMatrix__new_from_xml()
- Line 78: WnMatrix__is_valid_vector_input_xml()
- Line 87: WnMatrix__new_gsl_vector_from_xml()
- Line 96: WnMatrix__computeMatrixTimesVector()
- Line 110: WnMatrix__getNumberOfRows()
- Line 116: WnMatrix__getNumberOfColumns()
- Line 127: WnMatrix__getElement()
- Line 164: WnMatrix__computeTransposeMatrixTimesVector()
- Line 225: WnMatrix__free()
To run example16, type the following on the command line:
./example16 ../../data_pub/matrix.xml ../../data_pub/vector.xml > ex16_output.txt
The result is ex16_output.txt
Example 17: Solve a matrix equation.
wn_matrix routines demonstrated in example17.c are:
- Line 61: WnMatrix__is_valid_input_xml()
- Line 70: WnMatrix__new_from_xml()
- Line 76: WnMatrix__is_valid_vector_input_xml()
- Line 85: WnMatrix__new_gsl_vector_from_xml()
- Line 91: WnMatrix__solve()
- Line 102: WnMatrix__getNumberOfRows()
- Line 108: WnMatrix__getNumberOfColumns()
- Line 118: WnMatrix__getElement()
- Line 138: WnMatrix__computeMatrixTimesVector()
- Line 156: WnMatrix__free()
To run example17, type the following on the command line:
./example17 ../../data_pub/matrix.xml ../../data_pub/vector.xml > ex17_output.txt
The result is ex17_output.txt
Example 18: Convert to arrow matrix form.
wn_matrix routines demonstrated in example18.c are:
- Line 64: WnMatrix__is_valid_input_xml()
- Line 73: WnMatrix__new_from_xml()
- Line 79: WnMatrix__getArrow()
- Line 85: WnMatrix__free()
- Line 91: WnMatrix__Arrow__getNumberOfRows()
- Line 92: WnMatrix__Arrow__getBandWidth()
- Line 93: WnMatrix__Arrow__getWingWidth()
- Line 112: WnMatrix__Arrow__free()
To run example18, type the following on the command line:
./example18 ../../data_pub/matrix2.xml 3 > ex18_output.txt
The result is ex18_output.txt
Example 19: Solve a matrix equation with the arrow solver.
wn_matrix routines demonstrated in example19.c are:
- Line 63: WnMatrix__is_valid_input_xml()
- Line 72: WnMatrix__new_from_xml()
- Line 78: WnMatrix__is_valid_vector_input_xml()
- Line 87: WnMatrix__new_gsl_vector_from_xml()
- Line 95: WnMatrix__get_gsl_vector_size()
- Line 102: WnMatrix__getArrow()
- Line 108: WnMatrix__Arrow__solve()
- Line 115: WnMatrix__Arrow__free()
- Line 130: WnMatrix__free()
- Line 151: WnMatrix__getNumberOfRows()
- Line 164: WnMatrix__computeMatrixTimesVector()
To run example19, type the following on the command line:
./example19 ../../data_pub/matrix2.xml ../../data_pub/vector2.xml > ex19_output.txt
The result is ex19_output.txt