Skip to main content

A simple handler for matrix objects.

Project description

# MatrixPy

_*A simple handler for matrix objects in Python 3.*_



## Contents:



* [Installation](https://github.com/shaybrynes/MatrixPy#installation)

* [License](https://github.com/shaybrynes/MatrixPy#license)

* [Usage](https://github.com/shaybrynes/MatrixPy#usage)

* [Printing the Matrices](https://github.com/shaybrynes/MatrixPy#printing-the-matrices)

* [Generate a Matrix](https://github.com/shaybrynes/MatrixPy#generate-a-matrix)

* [Round elements of a Matrix](https://github.com/shaybrynes/MatrixPy#round-elements-of-a-matrix)

* [Adding, Subtracting and Multiplying Matrices](https://github.com/shaybrynes/MatrixPy#adding-subtracting-and-multiplying-matrices)

* [Finding the Power of a Matrix](https://github.com/shaybrynes/MatrixPy#finding-the-power-of-a-matrix)

* [Finding the Transpose of a Matrix](https://github.com/shaybrynes/MatrixPy#finding-the-transpose-of-a-matrix)

* [Finding the Determinant of Matrices](https://github.com/shaybrynes/MatrixPy#finding-the-determinant-of-matrices)

* [Finding the Inverse of Matrices](https://github.com/shaybrynes/MatrixPy#finding-the-inverse-of-matrices)

* [Finding the solutions of a system of equations](https://github.com/shaybrynes/MatrixPy#finding-the-solutions-of-a-system-of-equations)

* [Support](https://github.com/shaybrynes/MatrixPy#support)

* [Future Additions](https://github.com/shaybrynes/MatrixPy#future-additions)



## License:



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



[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)



## 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



```DOS .bat

pip install MatrixPy

```



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



```python

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.

```python

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](http://latex.codecogs.com/gif.latex?%5Cbegin%7Bpmatrix%7D%201%20%26%200%20%26%200%20%5C%5C%200%20%26%201%20%26%200%20%5C%5C%200%20%26%200%20%26%201%5C%5C%20%5Cend%7Bpmatrix%7D)



Say, instead you wanted a 3x5 matrix.

```python

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](http://latex.codecogs.com/gif.latex?%5Cbegin%7Bpmatrix%7D%202%20%26%200%20%26%20-3%20%26%204%20%26%205%5C%5C%207%20%26%202%20%26%20-1%20%26%20-4%20%26%200%5C%5C%20-9%20%26%204%20%26%205%20%26%203%20%26%206%5C%5C%20%5Cend%7Bpmatrix%7D)



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;



```python

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.

```python

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:

```python

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:

```python

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.

```python

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.

```python

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;

```python

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;

```python

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.



```python

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:



```python

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](https://en.wikipedia.org/wiki/Gaussian_elimination#Computing_determinants),

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.

```python

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](https://en.wikipedia.org/wiki/Gaussian_elimination#Finding_the_inverse_of_a_matrix),

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,

```python

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](https://en.wikipedia.org/wiki/System_of_linear_equations#Row_reduction). 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](https://latex.codecogs.com/gif.latex?%5Cleft%5C%7B%5Cbegin%7Bmatrix%7D%20x%20+%203y%20+%204z%20%3D%20-10%5C%5C%203x%20-%204y%20+%202z%20%3D%204%5C%5C%20-x%20+%20y%20-%202z%20%3D%206%20%5Cend%7Bmatrix%7D%5Cright)



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

```python

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](https://github.com/shaybrynes/MatrixPy/issues).

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:



- [x] Ability to calculate the inverse and determinant.

- [x] Calculation of transpose.

- [x] Installation via PIP.

- [ ] (Much later) eigenvalues.


Keywords: maths matrix matrices
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: License :: OSI Approved :: Apache 2.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Description-Content-Type: text/markdown

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

MatrixPy-1.2.3b3.tar.gz (11.7 kB view hashes)

Uploaded Source

Built Distribution

MatrixPy-1.2.3b3-py3-none-any.whl (21.2 kB view hashes)

Uploaded Python 3

Supported by

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