MatrixPy

A simple handler for matrix objects.


Keywords
matrices, python, python3
License
Apache-2.0
Install
pip install MatrixPy==1.2.3b3

Documentation

MatrixPy

A simple handler for matrix objects in Python 3.

Contents:

License:

This project uses a license, the license is automatically included in the project files.

License

Installation:

The installation can be completed one of two ways, either clone the MatrixPy repository and place the folder in your project directory. Or run the following 'pip' command

pip install MatrixPy

Then add the following to the top of your code to to use the module.

from MatrixPy.matrix import Matrix

You are now ready to start using matrix objects in your project.

Usage:

To instantiate a matrix object in MatrixPy use the Matrix() method.

identity = ((1, 0, 0), (0, 1, 0), (0, 0, 1)) # The 3x3 identity matrix
a_matrix = Matrix(identity)

It is important to note that the matrix inputted must be of the type tuple, this important for immutability. The above code specifically makes the value of the matrix object the below matrix,

Identity

Say, instead you wanted a 3x5 matrix.

three_by_five = ((2, 0, -3, 4, 5), (7, 2, -1, -4, 0), (-9, 4, 5, 3, 6))
a_matrix = Matrix(three_by_five)

This would produce a matrix object equivalent to the matrix below,

ThreeByFive

In fact when using this object the only limit to the size of the matrices available is the memory available to python.

Once the Matrix Object has been instantiated the data used to make the object can be retrieved. To retrieve the data use any of the following commands;

a_tuple = a_matrix.matrix # Returns the tuple that stores all the matrix elements.
a_tuple = a_matrix.get_tuple() # Also returns the tuple.
a_list = a_matrix.get_list() # Returns a list that contains the same information as the tuple.

Printing the Matrices:

MatrixPy can handle the printing of your matrices to the screen, it can be achieved using this method.

identity_tuple = ((1, 0, 0), (0, 1, 0), (0, 0, 1))
identity_matrix = Matrix(identity_tuple)
identity_matrix.print()

This prints the matrix to the console.

Generate a Matrix:

This module also allows the generation of matrices of any size. The method to call is:

a_matrix = Matrix.generate_random(m, n, minimum, maximum, integers=True, decimal_places=None) 
# The last two arguments are non-essential.

This will generate a matrix of size 'm' x 'n' with elements that range in value from 'minimum' to 'maximum' these values must all be integers, however a future goal is to allow floating point values for the minimum and maximum value. 'integers' and 'decimal_places' are optional arguments that allow the generation of floating point values to fill the elements on th matrix. 'integers' set to false produces floating point values and 'decimal_places' sets the number of decimal places each of the elements should be rounded to.

As of 1.3.0b1 you can also generate identity matrices of any size. The method is similar:

a_identity = Matrix.generate_identity(m)

This will generate an identity matrix of size 'm' x 'n'.

Round elements of a Matrix:

MatrixPy also allows you to round every element of the matrix to a specified number of decimal places.

a_tuple = ((2.543, 3.55), (9.11034, 3.14159))
a_matrix = Matrix(a_tuple)

a_matrix.round(2, normalize=False)
# 'normalize' is a non-essential parameter that is true removes trailing zeroes if set to true.
a_matrix.print()

This produces a matrix whose elements are all rounded to 2 decimal places. These elements have kept their trailing zeroes because normalize is set to 'False'.

Adding, Subtracting and Multiplying Matrices:

MatrixPy handles the addition of two Matrices. It has two methods, that ultimately produce the same result.

a_matrix = ((1, 1, 1), (1, 1, 1), (1, 1, 1))
identity = ((1, 0, 0), (0, 1, 0), (0, 0, 1))

b_matrix = Matrix.add(a_matrix, identity) # Adds the two matrices, puts answer in new Matrix object.
c_matrix = a_matrix + b_matrix # Adds the two matrices, sets c_matrix to the result.
a_matrix += b_matrix # Adds the two matrices, uses Python's 'Dunder methods'.

a_matrix.print()
b_matrix.print()
c_matrix.print()

This section of code will output three matrices. Which in this case will all have the same value.

Subtraction of the matrices works in the same way, but the call to be made is as follows;

b_matrix = Matrix.subtract(a_matrix, identity) # Subtract the two matrices, puts answer in new Matrix object.
c_matrix = a_matrix - b_matrix # Subtract the two matrices, sets c_matrix to the result.
a_matrix -= b_matrix # Subtracts the two matrices, uses Python's 'Dunder methods'.

Similarily for multiplying matrices;

b_matrix = Matrix.multiply(a_matrix, identity) # Multiplies the two matrices, puts answer in new Matrix object.
c_matrix = a_matrix * b_matrix # Multiplies the two matrices, sets c_matrix to the result.
a_matrix *= b_matrix # Multiplies the two matrices, uses Python's 'Dunder methods'.

However, it should be noted that the normal rules for multiplying matrices applies. The rows in 'a' needs to match the number of columns in matrix 'b'.

Finding the Power of a Matrix:

You can also use MatrixPy to calculate the exponents of Matrices. Say, for instance, you wanted to raise the matrix to the power of 10 you can use this method to find the result of this.

a_tuple = ((2, 1, -1), (4, 1, 7), (8, -1, 3))
a_matrix = Matrix(a_tuple)

b_matrix = Matrix.power(a_matrix, 10) # Finds the 10th power of the matrix, uses traditional method calling
c_matrix = a_matrix ** 10 # Finds the 10th power of the matrix, uses 'Dunder methods'.

Finding the Transpose of a Matrix:

MatrixPy also allows the user to calculate the transpose of a given Matrix. The transpose is a flipping of the matrix across its diagonal axis from the upper-left most corner. The transpose is found as follows:

a_tuple = ((2, 1, -1), (4, 1, 7), (8, -1, 3))
a_matrix = Matrix(a_tuple)

b_matrix = Matrix.transpose(a_matrix) # Transposes the matrix, uses traditional method calling.
c_matrix = a_matrix ** "T" # Transposes the matrix, uses Python's 'Dunder methods'.

Both of these methods produce the same result.

Finding the Determinant of Matrices:

In MatrixPy determinants are found using the algorithm found here, on wikipedia. This method can be applied to an matrix of any size, hence the determinant of any 'm' x 'm' matrix can be found using MatrixPy.

The determinant in MatrixPy is calculated using either of the methods below, it returns a decimal object.

a_tuple = ((2, 1, -1), (4, 1, 7), (8, -1, 3))
a_matrix = Matrix(a_tuple)

determinant_1 = Matrix.determinant(a_matrix) # Finds the matrix's determinant, uses traditional method calling.
determinant_2 = abs(a_matrix) # Finds the matrix's determinant, uses Python's 'Dunder methods'.
print(determinant_1)
print(determinant_2)

Finding the Inverse of Matrices:

The algorithm for finding inverses is similar to the algorithm for finding the determinant, it can be found here, again on wikipedia. As with the determinant this can be applied to a matrix of any size.

The inverse in MatrixPy is calculated using the method below,

a_tuple = ((2, 1, -1), (4, 1, 7), (8, -1, 3))
a_matrix = Matrix(a_tuple)

b_matrix = Matrix.inverse(a_matrix) # Inverses the matrix, uses traditional method calling.
c_matrix = a_matrix ** -1 # Inverses the matrix, uses Python's 'Dunder methods'.

a_matrix.print()
b_matrix.print()

The two print statements will return the same value.

Finding the solutions of a system of equations:

This project now also supports the calculation for solutions for a system of simultaneous equations. The algorithm is also based on row reduction and its method can be seen in action here. This can be used to find the solutions of any number of variables.

The system of equations shown below has 3 equations for 3 variables. An equation is required for each variable for a full solution to be found. In short, the tuple of equation coefficients must be square.

system_eqs

The python code and method calls to find the solutions to this system of equations are as follows,

a_tuple = ((1, 3, 4), (3, -4, 2), (-1, 1, -2)) # Matrix of the co-efficients of the system of equations.
b_tuple = ((-10,), (4,), (6,)) # Matrix of the solutions to each of the equations.

a_matrix = Matrix(a_tuple)
b_matrix = Matrix(b_tuple)

c = Matrix.solve_system(a_matrix, b_matrix)
c.print()

This prints the solutions to the system of equations. It is important that the all the coefficients for a particular variable are in the same column. The output matrix has the solution for that variable in the same column as you placed it in the original matrix. For the example given the solution for 'x' will be located in the first column, 'y' in the second and 'z' in the third.

Support:

The best way to show me that there is a problem with this project is to submit an issue report here. Make sure to give as much detail as possible, fully submitting all errors and how you achieved this error. If I can not replicate an issue I will assume that it is down to user error (which I will also provide support for).

If you are consistently having issues setting up and using the project, do not hesitate to send me a private message.

Future Additions:

  • Ability to calculate the inverse and determinant.
  • Calculation of transpose.
  • Installation via PIP.
  • (Much later) eigenvalues.