class: Matrix This class represents a arbitrary matrix. Overview about the methods: __str__() : returns a string representation operator * : implements the matrix vector multiplication implements the matrix-scalar multiplication.
| 189 | |
| 190 | |
| 191 | class Matrix(object): |
| 192 | """ |
| 193 | class: Matrix |
| 194 | This class represents a arbitrary matrix. |
| 195 | |
| 196 | Overview about the methods: |
| 197 | |
| 198 | __str__() : returns a string representation |
| 199 | operator * : implements the matrix vector multiplication |
| 200 | implements the matrix-scalar multiplication. |
| 201 | changeComponent(x,y,value) : changes the specified component. |
| 202 | component(x,y) : returns the specified component. |
| 203 | width() : returns the width of the matrix |
| 204 | height() : returns the height of the matrix |
| 205 | operator + : implements the matrix-addition. |
| 206 | operator - _ implements the matrix-subtraction |
| 207 | """ |
| 208 | def __init__(self,matrix,w,h): |
| 209 | """ |
| 210 | simple constructor for initialzes |
| 211 | the matrix with components. |
| 212 | """ |
| 213 | self.__matrix = matrix |
| 214 | self.__width = w |
| 215 | self.__height = h |
| 216 | def __str__(self): |
| 217 | """ |
| 218 | returns a string representation of this |
| 219 | matrix. |
| 220 | """ |
| 221 | ans = "" |
| 222 | for i in range(self.__height): |
| 223 | ans += "|" |
| 224 | for j in range(self.__width): |
| 225 | if j < self.__width -1: |
| 226 | ans += str(self.__matrix[i][j]) + "," |
| 227 | else: |
| 228 | ans += str(self.__matrix[i][j]) + "|\n" |
| 229 | return ans |
| 230 | def changeComponent(self,x,y, value): |
| 231 | """ |
| 232 | changes the x-y component of this matrix |
| 233 | """ |
| 234 | if x >= 0 and x < self.__height and y >= 0 and y < self.__width: |
| 235 | self.__matrix[x][y] = value |
| 236 | else: |
| 237 | raise Exception ("changeComponent: indices out of bounds") |
| 238 | def component(self,x,y): |
| 239 | """ |
| 240 | returns the specified (x,y) component |
| 241 | """ |
| 242 | if x >= 0 and x < self.__height and y >= 0 and y < self.__width: |
| 243 | return self.__matrix[x][y] |
| 244 | else: |
| 245 | raise Exception ("changeComponent: indices out of bounds") |
| 246 | def width(self): |
| 247 | """ |
| 248 | getter for the width |
no outgoing calls