MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / Matrix

Class Matrix

matrix/matrix_class.py:6–360  ·  view source on GitHub ↗

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 =

Source from the content-addressed store, hash-verified

4
5
6class Matrix:
7 """
8 Matrix object generated from a 2D array where each element is an array representing
9 a row.
10 Rows can contain type int or float.
11 Common operations and information available.
12 >>> rows = [
13 ... [1, 2, 3],
14 ... [4, 5, 6],
15 ... [7, 8, 9]
16 ... ]
17 >>> matrix = Matrix(rows)
18 >>> print(matrix)
19 [[1. 2. 3.]
20 [4. 5. 6.]
21 [7. 8. 9.]]
22
23 Matrix rows and columns are available as 2D arrays
24 >>> matrix.rows
25 [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
26 >>> matrix.columns()
27 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
28
29 Order is returned as a tuple
30 >>> matrix.order
31 (3, 3)
32
33 Squareness and invertability are represented as bool
34 >>> matrix.is_square
35 True
36 >>> matrix.is_invertable()
37 False
38
39 Identity, Minors, Cofactors and Adjugate are returned as Matrices. Inverse can be
40 a Matrix or Nonetype
41 >>> print(matrix.identity())
42 [[1. 0. 0.]
43 [0. 1. 0.]
44 [0. 0. 1.]]
45 >>> print(matrix.minors())
46 [[-3. -6. -3.]
47 [-6. -12. -6.]
48 [-3. -6. -3.]]
49 >>> print(matrix.cofactors())
50 [[-3. 6. -3.]
51 [6. -12. 6.]
52 [-3. 6. -3.]]
53 >>> # won't be apparent due to the nature of the cofactor matrix
54 >>> print(matrix.adjugate())
55 [[-3. 6. -3.]
56 [6. -12. 6.]
57 [-3. 6. -3.]]
58 >>> matrix.inverse()
59 Traceback (most recent call last):
60 ...
61 TypeError: Only matrices with a non-zero determinant have an inverse
62
63 Determinant is an int, float, or Nonetype

Callers 8

identityMethod · 0.70
get_minorMethod · 0.70
minorsMethod · 0.70
cofactorsMethod · 0.70
adjugateMethod · 0.70
__add__Method · 0.70
__sub__Method · 0.70
__mul__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected