Commit 694e75fce0d2c9dcbe993ccf2c8e03cfa7022a52
1 parent
b1125e94
Add matrix handling howto.
Showing
1 changed file
with
185 additions
and
0 deletions
examples/python/matrix_howto.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# Scope | |
| 4 | +# | |
| 5 | +# This script provides an example for matrix handling with GammaLib | |
| 6 | +# | |
| 7 | +# Usage | |
| 8 | +# ./matrix_howto.py | |
| 9 | +# | |
| 10 | +# ------------------------------------------------------------------------- | |
| 11 | +# | |
| 12 | +# Copyright (C) 2013 Juergen Knoedlseder | |
| 13 | +# | |
| 14 | +# This program is free software: you can redistribute it and/or modify | |
| 15 | +# it under the terms of the GNU General Public License as published by | |
| 16 | +# the Free Software Foundation, either version 3 of the License, or | |
| 17 | +# (at your option) any later version. | |
| 18 | +# | |
| 19 | +# This program is distributed in the hope that it will be useful, | |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 22 | +# GNU General Public License for more details. | |
| 23 | +# | |
| 24 | +# You should have received a copy of the GNU General Public License | |
| 25 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 26 | +# | |
| 27 | +# ========================================================================== | |
| 28 | +import gammalib | |
| 29 | + | |
| 30 | + | |
| 31 | +# =========================== # | |
| 32 | +# Illustrates matrix handling # | |
| 33 | +# =========================== # | |
| 34 | +def handle_matrix(): | |
| 35 | + """ | |
| 36 | + Illustrates the handling of matrix. Although we use in this example the | |
| 37 | + GMatrix class, the same operations can be performed on a sparse matrix | |
| 38 | + GMatrixSparse or a symmetric matrix GMatrixSymmetric. | |
| 39 | + """ | |
| 40 | + # Create matrix with 4 rows and 5 columns | |
| 41 | + matrix = gammalib.GMatrix(4,5) | |
| 42 | + | |
| 43 | + # Get matrix attributes | |
| 44 | + number_of_rows = matrix.rows() # Will be 4 | |
| 45 | + number_of_columns = matrix.columns() # Will be 5 | |
| 46 | + number_of_elements = matrix.size() # Will be 20=4*5 | |
| 47 | + matrix_fill = matrix.fill() # Will be 0 | |
| 48 | + matrix_minimum = matrix.min() # Will be 0 | |
| 49 | + matrix_maximum = matrix.max() # Will be 0 | |
| 50 | + matrix_sum = matrix.sum() # Will be 0 | |
| 51 | + | |
| 52 | + # Dump some information | |
| 53 | + print("Attributes of an empty GMatrix:") | |
| 54 | + print(gammalib.parformat("Matrix rows")+str(number_of_rows)) | |
| 55 | + print(gammalib.parformat("Matrix columns")+str(number_of_columns)) | |
| 56 | + print(gammalib.parformat("Matrix elements")+str(number_of_elements)) | |
| 57 | + print(gammalib.parformat("Matrix fill")+str(matrix_fill)) | |
| 58 | + print(gammalib.parformat("Smallest matrix element")+str(matrix_minimum)) | |
| 59 | + print(gammalib.parformat("Largest matrix element")+str(matrix_maximum)) | |
| 60 | + print(gammalib.parformat("Sum of all element")+str(matrix_sum)) | |
| 61 | + | |
| 62 | + # Set all matrix elements to 2.0 | |
| 63 | + matrix.set(2.0) | |
| 64 | + | |
| 65 | + # Now set all matrix elements to specific values | |
| 66 | + for row in range(matrix.rows()): | |
| 67 | + for column in range(matrix.columns()): | |
| 68 | + matrix[row,column] = row + column*matrix.rows() + 1.5 | |
| 69 | + | |
| 70 | + # Get matrix attributes | |
| 71 | + matrix_fill = matrix.fill() # Will be 1 | |
| 72 | + matrix_minimum = matrix.min() # Will be 1.5 | |
| 73 | + matrix_maximum = matrix.max() # Will be 20.5 | |
| 74 | + matrix_sum = matrix.sum() # Will be 220.0 | |
| 75 | + | |
| 76 | + # Dump some information | |
| 77 | + print("Attributes of a filled GMatrix:") | |
| 78 | + print(gammalib.parformat("Matrix fill")+str(matrix_fill)) | |
| 79 | + print(gammalib.parformat("Smallest matrix element")+str(matrix_minimum)) | |
| 80 | + print(gammalib.parformat("Largest matrix element")+str(matrix_maximum)) | |
| 81 | + print(gammalib.parformat("Sum of all element")+str(matrix_sum)) | |
| 82 | + | |
| 83 | + # Perform some matrix operations | |
| 84 | + other_matrix = matrix.copy() # Use copy() to create a deep copy | |
| 85 | + sum_matrix = matrix + other_matrix # Add two matrices | |
| 86 | + zero_matrix = matrix - other_matrix # Subtract two matrices | |
| 87 | + other_matrix += matrix # Add-on matrix | |
| 88 | + other_matrix -= matrix # Subtract off matrix | |
| 89 | + other_matrix *= 2.0 # Multiply elements by 2 | |
| 90 | + other_matrix /= 2.0 # Divide elements by 2 | |
| 91 | + neg_matrix = -matrix # Negate matrix | |
| 92 | + abs_matrix = gammalib.abs(neg_matrix) # Absolute value of elements | |
| 93 | + | |
| 94 | + # And now some more tricky operations | |
| 95 | + transposed = gammalib.transpose(matrix) # First transpose matrix | |
| 96 | + mult_matrix = matrix * transposed # Now we can multiply it | |
| 97 | + other_matrix *= transposed # Multiply on matrix | |
| 98 | + #inverted = gammalib.invert(matrix) # NOT YET IMPLEMENTED | |
| 99 | + vector = gammalib.GVector(5) # Allocate vector | |
| 100 | + multiplied = matrix * vector # Vector multiplication | |
| 101 | + | |
| 102 | + # Compare matrices | |
| 103 | + if matrix == abs_matrix: | |
| 104 | + print("Yes, got it!") | |
| 105 | + if matrix != transposed: | |
| 106 | + print("Got it again!") | |
| 107 | + | |
| 108 | + # Copy columns one-by-one from one matrix into another | |
| 109 | + destination = gammalib.GMatrix(4,5) | |
| 110 | + for column in range(matrix.columns()): | |
| 111 | + vector = matrix.column(column) # Extract column | |
| 112 | + destination.column(column, vector) # Set column | |
| 113 | + | |
| 114 | + # Add columns one-by-one from one matrix into another | |
| 115 | + destination = matrix.copy() | |
| 116 | + for column in range(matrix.columns()): | |
| 117 | + vector = matrix.column(column) # Extract column | |
| 118 | + destination.add_to_column(column, vector) # Add column | |
| 119 | + | |
| 120 | + # Copy rows one-by-one from one matrix into another | |
| 121 | + destination = gammalib.GMatrix(4,5) | |
| 122 | + for row in range(matrix.rows()): | |
| 123 | + vector = matrix.row(row) # Extract row | |
| 124 | + destination.row(row, vector) # Set row | |
| 125 | + | |
| 126 | + # Add rows one-by-one from one matrix into another | |
| 127 | + destination = matrix.copy() | |
| 128 | + for row in range(matrix.rows()): | |
| 129 | + vector = matrix.row(row) # Extract row | |
| 130 | + destination.add_to_row(row, vector) # Add row | |
| 131 | + | |
| 132 | + # Return | |
| 133 | + return | |
| 134 | + | |
| 135 | + | |
| 136 | +# ======================== # | |
| 137 | +# Illustrates matrix usage # | |
| 138 | +# ======================== # | |
| 139 | +def matrix_usage(): | |
| 140 | + """ | |
| 141 | + Illustrates the usage of the different matrix storage classes. | |
| 142 | + """ | |
| 143 | + # Create matrices with 4 rows and 4 columns | |
| 144 | + matrix_generic = gammalib.GMatrix(4,4) | |
| 145 | + matrix_sparse = gammalib.GMatrixSparse(4,4) | |
| 146 | + matrix_symmetric = gammalib.GMatrixSymmetric(4,4) | |
| 147 | + | |
| 148 | + # Now set all matrix elements to specific values | |
| 149 | + for row in range(matrix_generic.rows()): | |
| 150 | + for column in range(matrix_generic.columns()): | |
| 151 | + matrix_generic[row,column] = row * column | |
| 152 | + matrix_sparse[row,column] = row * column | |
| 153 | + matrix_symmetric[row,column] = row * column | |
| 154 | + | |
| 155 | + # Convert from one storage class to another | |
| 156 | + matrix = gammalib.GMatrix(matrix_sparse) | |
| 157 | + matrix = gammalib.GMatrix(matrix_symmetric) | |
| 158 | + matrix = gammalib.GMatrixSparse(matrix_generic) | |
| 159 | + matrix = gammalib.GMatrixSparse(matrix_symmetric) | |
| 160 | + matrix = gammalib.GMatrixSymmetric(matrix_generic) | |
| 161 | + matrix = gammalib.GMatrixSymmetric(matrix_sparse) | |
| 162 | + | |
| 163 | + # Return | |
| 164 | + return | |
| 165 | + | |
| 166 | + | |
| 167 | +# ======================== # | |
| 168 | +# Main routine entry point # | |
| 169 | +# ======================== # | |
| 170 | +if __name__ == '__main__': | |
| 171 | + """ | |
| 172 | + Example script for matrix handling. | |
| 173 | + """ | |
| 174 | + # Dump header | |
| 175 | + print("") | |
| 176 | + print("*******************************") | |
| 177 | + print("* Example for matrix handling *") | |
| 178 | + print("*******************************") | |
| 179 | + | |
| 180 | + # Illustrate matrix handling | |
| 181 | + handle_matrix() | |
| 182 | + | |
| 183 | + # Illustrate usage of different matrix classes | |
| 184 | + matrix_usage() | |
| 185 | + | |
| 0 | 186 | \ No newline at end of file | ... | ... |