matrix.matrix_class¶
Classes¶
Matrix object generated from a 2D array where each element is an array representing |
Module Contents¶
- class matrix.matrix_class.Matrix(rows: list[list[int]])¶
Matrix object generated from a 2D array where each element is an array representing a row. Rows can contain type int or float. Common operations and information available. >>> rows = [ … [1, 2, 3], … [4, 5, 6], … [7, 8, 9] … ] >>> matrix = Matrix(rows) >>> print(matrix) [[1. 2. 3.]
[4. 5. 6.] [7. 8. 9.]]
Matrix rows and columns are available as 2D arrays >>> matrix.rows [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> matrix.columns() [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Order is returned as a tuple >>> matrix.order (3, 3)
Squareness and invertability are represented as bool >>> matrix.is_square True >>> matrix.is_invertable() False
Identity, Minors, Cofactors and Adjugate are returned as Matrices. Inverse can be a Matrix or Nonetype >>> print(matrix.identity()) [[1. 0. 0.]
[0. 1. 0.] [0. 0. 1.]]
>>> print(matrix.minors()) [[-3. -6. -3.] [-6. -12. -6.] [-3. -6. -3.]] >>> print(matrix.cofactors()) [[-3. 6. -3.] [6. -12. 6.] [-3. 6. -3.]] >>> # won't be apparent due to the nature of the cofactor matrix >>> print(matrix.adjugate()) [[-3. 6. -3.] [6. -12. 6.] [-3. 6. -3.]] >>> matrix.inverse() Traceback (most recent call last): ... TypeError: Only matrices with a non-zero determinant have an inverse
Determinant is an int, float, or Nonetype >>> matrix.determinant() 0
Negation, scalar multiplication, addition, subtraction, multiplication and exponentiation are available and all return a Matrix >>> print(-matrix) [[-1. -2. -3.]
[-4. -5. -6.] [-7. -8. -9.]]
>>> matrix2 = matrix * 3 >>> print(matrix2) [[3. 6. 9.] [12. 15. 18.] [21. 24. 27.]] >>> print(matrix + matrix2) [[4. 8. 12.] [16. 20. 24.] [28. 32. 36.]] >>> print(matrix - matrix2) [[-2. -4. -6.] [-8. -10. -12.] [-14. -16. -18.]] >>> print(matrix ** 3) [[468. 576. 684.] [1062. 1305. 1548.] [1656. 2034. 2412.]]
Matrices can also be modified >>> matrix.add_row([10, 11, 12]) >>> print(matrix) [[1. 2. 3.]
[4. 5. 6.] [7. 8. 9.] [10. 11. 12.]]
>>> matrix2.add_column([8, 16, 32]) >>> print(matrix2) [[3. 6. 9. 8.] [12. 15. 18. 16.] [21. 24. 27. 32.]] >>> print(matrix * matrix2) [[90. 108. 126. 136.] [198. 243. 288. 304.] [306. 378. 450. 472.] [414. 513. 612. 640.]]
- __eq__(other: object) bool ¶
- __ne__(other: object) bool ¶
- __repr__() str ¶
- __str__() str ¶
- add_column(column: list[int], position: int | None = None) None ¶
- add_row(row: list[int], position: int | None = None) None ¶
- columns() list[list[int]] ¶
- determinant() int ¶
- classmethod dot_product(row: list[int], column: list[int]) int ¶
- get_cofactor(row: int, column: int) int ¶
- get_minor(row: int, column: int) int ¶
- is_invertable() bool ¶
- property is_square: bool¶
- property num_columns: int¶
- property num_rows: int¶
- property order: tuple[int, int]¶