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.
| 253 | |
| 254 | |
| 255 | class Matrix(object): |
| 256 | """ |
| 257 | class: Matrix |
| 258 | This class represents a arbitrary matrix. |
| 259 | |
| 260 | Overview about the methods: |
| 261 | |
| 262 | __str__() : returns a string representation |
| 263 | operator * : implements the matrix vector multiplication |
| 264 | implements the matrix-scalar multiplication. |
| 265 | changeComponent(x,y,value) : changes the specified component. |
| 266 | component(x,y) : returns the specified component. |
| 267 | width() : returns the width of the matrix |
| 268 | height() : returns the height of the matrix |
| 269 | operator + : implements the matrix-addition. |
| 270 | operator - _ implements the matrix-subtraction |
| 271 | """ |
| 272 | |
| 273 | def __init__(self, matrix, w, h): |
| 274 | """ |
| 275 | simple constructor for initialzes |
| 276 | the matrix with components. |
| 277 | """ |
| 278 | self.__matrix = matrix |
| 279 | self.__width = w |
| 280 | self.__height = h |
| 281 | |
| 282 | def __str__(self): |
| 283 | """ |
| 284 | returns a string representation of this |
| 285 | matrix. |
| 286 | """ |
| 287 | ans = "" |
| 288 | for i in range(self.__height): |
| 289 | ans += "|" |
| 290 | for j in range(self.__width): |
| 291 | if j < self.__width - 1: |
| 292 | ans += str(self.__matrix[i][j]) + "," |
| 293 | else: |
| 294 | ans += str(self.__matrix[i][j]) + "|\n" |
| 295 | return ans |
| 296 | |
| 297 | def changeComponent(self, x, y, value): |
| 298 | """ |
| 299 | changes the x-y component of this matrix |
| 300 | """ |
| 301 | if x >= 0 and x < self.__height and y >= 0 and y < self.__width: |
| 302 | self.__matrix[x][y] = value |
| 303 | else: |
| 304 | raise Exception("changeComponent: indices out of bounds") |
| 305 | |
| 306 | def component(self, x, y): |
| 307 | """ |
| 308 | returns the specified (x,y) component |
| 309 | """ |
| 310 | if x >= 0 and x < self.__height and y >= 0 and y < self.__width: |
| 311 | return self.__matrix[x][y] |
| 312 | else: |
no outgoing calls