Pymatrix module in python

Pymatrix is a lightweight matrix library which supports basic linear algebra operations. The elements in matrix should be numeric type to support basic algebra operations – int, float, rational, complex.

Instantiating Matrix

  1. Using Matrix constructor
    Matrix can be initialised using constructor of Matrix class in pymatrix library.

    Syntax: Matrix(rows, cols, fill=val(optional))

    Parameters:
    rows – specify number of rows
    cols – specify number of columns
    fill – initialise all the elements with this value. It is an optional argument, the default fill is 0.

    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import pymatrix
      
      
    m = pymatrix.Matrix(2, 3, fill = '2')
    print(m)

    chevron_right

    
    

    Output:

    2 2 2
    2 2 2
  2. Using list of lists
    We can convert a list of lists into a matrix using the from_list() method where each list is treated as a row. Example-

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import pymatrix
      
      
    list = [[1, 2, 3], [6, 4, 7], [3, 9, 1]]
    m = pymatrix.Matrix.from_list(list)
    print(m)

    chevron_right

    
    

    Output:

    1 2 3
    6 4 7
    3 9 1
  3. Using string
    We can convert a string into matrix object using from_string() method. The string is in triple qoutes and each line is treated as a row. Example

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import pymatrix
      
      
    string = '''1 2 3
           6 4 7
           3 9 1'''
      
    m = pymatrix.Matrix.from_string(string)
    print(m)

    chevron_right

    
    

    Output:

    1 2 3
    6 4 7
    3 9 1
  4. Note: matrix() function is a shortcut for above 2nd and 3rd methods, we don’t need to specify from_list() or from_string() method with matrix() function. Example –

    m = pymatrix.matrix([[1, 2, 3]])
    m = pymatrix.matrix('''1 2 3
    4 5 6''')

Matrix methods

Following are some of the methods provided in pymatrix library :

  • identity(n) – Creating an identity matrix of given size. This returns an object of Matrix class.
  • is_square() – Checks whether given matrix is a square matrix or not. This returns a boolean value.
  • is_invertible() – Checks whether given matrix is invertible or not. This returns a boolean value.
  • inv() – Returns the inverse matrix if it exists, otherwise raises MatrixError exception.
  • det() – Returns the determinant matrix if square matrix, otherwise raises MatrixError(‘non-square matrix does not have determinant’) exception.
  • rank() – Returns the rank of matrix, rank is of integer type.
  • trans() – Returns the transpose of the matrix.
  • adjoint() – Returns the adjoint matrix.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program for Matrix methods
from pymatrix import Matrix
  
  
# identity matrix of size 2
identity_matrix = Matrix.identity(2)
print('\nIdentity matrix :')
print(identity_matrix)
  
m = Matrix.from_list([[1,2,1],[2,1,1],[1,1,1]])
  
print('\nIs a square matrix :')
print(m.is_square())
  
print('\nIs an invertible matrix :')
print(m.is_invertible())
  
print('\nInverse :')
print(m.inv())
  
print('\nDeterminant :')
print(m.det())
  
print('\nRank :')
print(m.rank())
  
print('\nTranspose :')
print(m.trans())
  
print('\nAdjoint :')
print(m.adjoint())

chevron_right


Output:

Identity matrix :
1 0
0 1

Is a square matrix :
True

Is an invertible matrix :
True

Inverse :
 0.0  1.0 -1.0
 1.0  0.0 -1.0
-1.0 -1.0  3.0

Determinant :
-1.0

Rank :
3

Transpose :
1 2 1
2 1 1
1 1 1

Adjoint :
 0.0 -1.0  1.0
-1.0  0.0  1.0
 1.0  1.0 -3.0

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.