Skip to main content

A package for Python that lets you create and perform various operations on Matrices sucjh as finding the adjoint, inverse, determinant of a matrix, etc..

Project description

Features

  • Addition, Multiplication, Division, Subraction operations supported between matrices and between a matrix and a int / float

  • Calculates:

    • Determinant

    • Inverse

    • Cofactor of a given element in the Matrix

    • Adjoint

Getting started

Creating a Matrix

To create a matrix, specify the order of the Matrix (mxn) where the first argument (m) is the number of rows in the matrix and the second argument (n) is the number of columns

We can use a nested list to represent a Matrix during initialization of an object In a nested list, the length of the outer list would be ‘m’ and the number of elements the inner lists have would be ‘n’

from matrix import Matrix

matrix_list = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

matrix1 = Matrix(3, 3, matrix_list)

print(matrix1)
#Prints:
# [ 1, 2, 3
#   4, 5, 6
#   7, 8, 9 ]

Operations on Matrices

Addition and Subraction

We can add and subract matrices extremely easily:

matrix_list2 = [
    [0, 1, 3],
    [5, 2, 7],
    [7, 1, 9]
]
matrix2 = Matrix(3, 3, matrix_list2)
matrix3 = matrix1 + matrix2
print(matrix3)
#Prints:
# [ 1, 3, 6
#   9, 7, 13
#   14, 9, 18 ]

Adding an int / float to a matrix will perform the operation on all elements of the matrix and return a new matrix

matrix4 = matrix1 + 5
print(matrix4)
#Prints:
# [ 6, 7, 8
#   9, 10, 11
#   12, 13, 14 ]
# Same way,
print(matrix4 - matrix1)
# [ 5, 5, 5
#   5, 5, 5
#   5, 5, 5 ]
print(matrix1 - 3)
#Prints:
# [ -2, -1, 0
#   1, 2, 3
#   4, 5, 6 ]

Multiplication and Division

Matrix multiplication can only be implemented if the number of columns in the first matrix is equal to the number of rows in the other matrix. Basically: A m x n Matrix can only be multiplied with a n x l Matrix .

The order of the resultant Matrix will be m x l

Example:

# m x n * n x l : Gives m x l
# 2 x 3 * 3 x 2 : Gives 2 x 2
# 2 x 3 * 4 x 2 : Cannot mutliply

Internally, division is calculated by multiplying a matrix and the inverse of the other matrix therefore the same condition applies for division

print(matrix1 * 5)
# [ 5, 10, 15
#   20, 25, 30
#   35, 40, 45 ]
print(matrix1 * matrix2)
# [ 31, 8, 44
#   67, 20, 101
#   103, 32, 44 ]

Comparing matrices

Matrix == Matrix | 0 -> bool

Matrices can be compared for equality to another matrix Zero is used as an alias for a zero matrix

Functionalities

mathmatrix provides many functionalities for matrices out of the box:

Let’s create a sample matrix matrix to perform the operations on

from mathmatrix import Matrix

matrix = Matrix(3,3,[[1,2,3],[4,5,6],[7,8,9]])

Transposing a matrix

Matrix.transpose() -> Matrix

After creating a matrix, you can transpose a Matrix using the transpose() method of Matrix

print(matrix.transpose())
# [ 1, 4, 7
#   2, 5, 8
#   3, 6, 7 ]

Adjoint of a Matrix

Matrix.adjoint() -> Matrix

Adjoint of a matrix is calculated as the transpose of cofactor matrix of a Matrix It can be calculated using the adjoint() method

print(matrix.adjoint())
# [ -3, 6, -3
#   6, -12, 6
#  -3, 6, -3 ]

Determinant of a Matrix

Matrix.determinant() -> int | float

print(matrix.determinant())
# 0

Inverse of a Matrix

Matrix.inverse() -> Matrix

Inverse of a matrix only exists for non-singular matrices ( Determinant of the Matrix should not be zero )

print(matrix.determinant())
# 0
# Since determinant is zero, if we try to calculate Inverse it will throw the error:
# ZeroDivisionError: Determinant of Matrix is zero, inverse of the matrix does not exist

Cofactor of an element

Matrix.cofactor(m:int, n:int) -> int | float

Specify the position of the desired element in row number (m) and column number (n) to calculate it’s corresponding cofactor

Chaining functions

Since functions return a new Matrix, you can chain many functions to get the desired output For example:

matrix.transpose().adjoint().determinant()
(matrix.determinant() * matrix.adjoint()).transpose()

are all completely valid

Additional Functions

Generating a zero matrix

gen_zero_matrix(m:int, n:int) -> Matrix

You can use the gen_zero_matrix function to create a zero matrix of a given order For example,

from mathmatrix import gen_zero_matrix, Matrix
zero3 = gen_zero_matrix(3,3)
print(zero3)
# [ 0, 0, 0
#   0, 0, 0
#   0, 0, 0 ]
print(zero3 == 0)
# True

Generating an identity matrix

gen_zero_matrix(m:int, n:int) -> Matrix

You can use the gen_zero_matrix function to create a zero matrix of a given order For example,

from mathmatrix import gen_zero_matrix, Matrix
zero3 = gen_zero_matrix(3,3)
print(zero3)
# [ 0, 0, 0
#   0, 0, 0
#   0, 0, 0 ]
print(zero3 == 0)
# True

Note: For any Matrix matrix,

print(matrix * matrix.inverse() == gen_identity_matrix(matrix.m, matrix.n))
# Always true (Inverse cannot be calculated for singular matrices so error is thrown in that case)
print((matrix - matrix) == gen_zero_matrix(matrix.m,matrix.n))
# Always true

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mathmatrix-0.5.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

mathmatrix-0.5-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file mathmatrix-0.5.tar.gz.

File metadata

  • Download URL: mathmatrix-0.5.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for mathmatrix-0.5.tar.gz
Algorithm Hash digest
SHA256 0ff619da04f745cd8059e1028f2e1b19c9122a023c41fc6eb0bac0699e54a796
MD5 fa7dbc74fc447b146a55dffde2a8a0fd
BLAKE2b-256 e9893bd97980f8bdcf8803243a21a46d41559d9670e67e3dc1b0b515d014be08

See more details on using hashes here.

File details

Details for the file mathmatrix-0.5-py3-none-any.whl.

File metadata

  • Download URL: mathmatrix-0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for mathmatrix-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d0ba8f7397b48c6b7d25898f93acfed8c14d0501a459462d2881c848a4c9eef4
MD5 c7295fd406bbc59c0c290daf56ac2e8e
BLAKE2b-256 22953fdb8cd069f1683a9a09796ca1df8091330c3f495e2a678463acb8c49b41

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page