A simple module for matrix multiplication
Install using git.
git clone https://github.com/Akshat-Tripathi/matrix-lib.git
This module allows you to:
- Create matrices
- Add and subtract matrices by each other
- Add and subtract matrices by numbers
- Multiply and divide matrices elementwise
- Multiply and divide matrices by numbers
- Perform dot product calculations on matrices
- Transpose matrices
- Apply functions to matrices
- Create matrices with random values
To create a matrix, the matrix class must be initialised by passing a 2d array.
a = matrix([[1, 2],
[3, 4],
[5, 6]])
b = matrix([[7, 8, 9],
[10, 11, 12]])
c = matrix([[1, 2],
[3, 4],
[5, 6]])
Two matrices may be added if they have the same shape.
a+c #This works
a+b #This doesn't work
Two matrices may be subtracted if they have the same shape.
a-c #This works
a-b #This doesn't work
Two matrices may be multiplied elementwise if they have the same shape.
a*c #This works
a*b #This doesn't work
Two matrices may be divided elementwise if they have the same shape.
a/c #This works
a/b #This doesn't work
A matrix may have all of its values changed by a number in the following way.
a+5
a-5
a*5
a/5
Two matrices can only have a dot product applied if the number of columns in the first matrix is equal to the number of rows in the second.
a.dot_product(b) #This does work
a.dot_product(c) #This doesn't work
A matrix is transposed when its rows and columns swap.
a.Transpose()
#This returns matrix([[1, 2, 3],
[4, 5, 6]])
This applies a function to every element of a matrix.
a.apply_function(lamda x: x**2) #This squares every element in a
A matrix's data can be seen by using .matrix
.
A matrix's shape is shown with .shape
a.matrix #This returns [[1, 2],
[3, 4],
[5, 6]]
a.shape # This returns [3, 2]