Skip to main content

A simple matrix module for basic matrix mathematical operations

Project description

matrixObj

A small simple matrix module for basic mathematical matrix operations. Create and make us of Matrix objects in your python codes, perform operations like Matrix Addition, Subtration, Multiplication and Scalar Division and other operations like (transpose, co-factor, inverse, minor, determinant, adjoint and elementary operation).

Installation

pip install matrixObj

Project Demo

from matrixObj import Matrix

You can create a new Matrix instance with any of the python number objects (int, float, complex and Fraction) eg.

matA = Matrix([5, 3, -1], [3, 5, 9], [0, 5, 2])

matB = Matrix([3.0, 4.5], [2.6, 5.2])

cmplxMat = Matrix([1+2j, 2+1j], [4-4j, 1-2j])

print(matA)
print(matB)
print(cmplxMat)
Output:
| 5      3      -1|
| 3      5       9|
| 0      5       2|

|3.0    4.5|
|2.6    5.2|

|(1+2j) (2+1j)|
|(4-4j) (1-2j)|

with Fraction object, you will have to import it from the fractions module

from fractions import Fraction

fracMat = Matrix([Fraction(1,3), Fraction(2,5)], [Fraction(1,2), Fraction(3,27)])

print(fracMat)
output:

|1/3    2/5|
|1/2    1/9|

Matrix Arithmetic Operations

A = Matrix([1, -3, 3], [4, 0, 2])
B = Matrix([4, 6, 0], [3, 0, 5])
C = Matrix([3, -2], [0, 4], [-1, 5])
D = Matrix([1, -3, 3], [4, 0, 2])

sumAB = A + B
productAC = A * C

print(sumAB)
print(productAC)
print(A == D)           # Matrix Equality
Output:
|5      3       3|
|7      0       7|

| 0      1|
|10      2|

True

Square Matrix Operations

matrixA = Matrix([1, 0, 4], [5, 2, 1], [1, 1, 1])

print(matrixA.determinant())    # The determinant of the matrix

print(matrixA.minor())          # The minor of the matrix

print(matrixA.cofactor())       # The Cofactor of the matrix

print(matrixA.inverse(infraction=True)) # Inverse of the matrix returned as a matrix of Fraction object
Output:
13

|  1      4       3|
| -4     -3       1|
| -8    -19       2|

| 1     -4       3|
| 4     -3      -1|
|-8     19       2|

| 1/13   4/13   -8/13|
|-4/13  -3/13   19/13|
| 3/13  -1/13    2/13|

Elementary Operations

mat = Matrix([3, 0, 1], [0, 2, 3], [5, -3, -1])

# Elementary Row operation
print(mat.elementary_operation(2, 3))           # Interchange row 2 and 3

print(mat.elementary_operation(1, scalar=2))    # Multiply row 1 by 2

print(mat.elementary_operation(1, 3, scalar=2)) # Multiply row 3 by 2, add the result to row 1
Output:
| 3      0       1|
| 5     -3      -1|
| 0      2       3|

| 6      0       2|
| 0      2       3|
| 5     -3      -1|

|13     -6      -1|
| 0      2       3|
| 5     -3      -1|

For Elementary column Operation set argument row=False eg.

# Elementary Column operation

print(mat.elementary_operation(2, 3, row=False))           # Interchanges column 2 and 3
| 3      1       0|
| 0      3       2|
| 5     -1      -3|

Modifying the Matrix

After initialization of a Matrix object, you can change, insert new and expand the rows/columns in the matrix eg.

mat = Matrix([1, 0, 2], [3, 1, 0])

mat.setrow(1, [5, -3, 2])       # Changes row 1 to [5, -3, 2]
mat.insertrow(2, [3, -3, 1])    # Inserts [3, -3, 1] in the second row
mat.expandrow([2, 5, 5])        # Appends row [2, 5, 5] to the matrix
mat.expandcolumn([4, 5, -4, 0]) # Appends column [4, 5, -4, 0]

print(mat)      # With all the modifications we should now have a 4x4 matrix
Output:
| 5     -3       2       4|
| 3     -3       1       5|
| 3      1       0      -4|
| 2      5       5       0|

Modifying by Slicing

Get, set or delete part/slice of the matrix. eg.

# Getting part/slice of the matrix
matA = Matrix([2, 5, -4], [4, 5, 9], [-1, -8, 0], [7, 0, 5])

print(matA[1, 0])           # Gets first element in the second row 

print(matA[2])              # Gets the third row of the matrix

print(matA[:, -1])          # Gets the last column of the matrix 

print(matA[0:3])            # Gets the first, the second and the third row

print(matA[-1::-1, -1::-1]) # Gets the reverse of the matrix rows with each row also reversed

Output:
4

|-1    -8     0|

|-4     9     0     5|

| 2     5    -4|
| 4     5     9|
|-1    -8     0|

| 5     0     7|
| 0    -8    -1|
| 9     5     4|
|-4     5     2|
# Setting part/slice of the matrix
matA[1] = [-20, 40, 10]     # sets the second row
print(matA)

matA[:, 0] = [1, 2, 3, 4]   # sets the first column
print(matA)
Output:

|  2      5     -4|
|-20     40     10|
| -1     -8      0|
|  7      0      5|

|  1      5     -4|
|  2     40     10|
|  3     -8      0|
|  4      0      5|

All elements in a slice can be set to the same scalar value eg.

matA[-1] = 4        # Sets all the elements in the last row to 4
print(matA)

from fractions import Fraction
matA[:] = Fraction(1, 3)    # Sets all elements in the matrix to 1/3
print(matA)
Output:
| 2     5    -4|
| 4     5     9|
|-1    -8     0|
| 4     4     4|

|1/3    1/3    1/3|
|1/3    1/3    1/3|
|1/3    1/3    1/3|
|1/3    1/3    1/3|

Using the del() function to delete some part of the matrix, this can only delete complete row(s) or column...

matA = Matrix([2, 5, -4], [4, 5, 9], [-1, -8, 0], [7, 0, 5])
del(matA[1])            # Deletes the second row
print(matA)
Output:

| 2     5    -4|
|-1    -8     0|
| 7     0     5|
del(matA[:, 0])         # Deletes the first column
print(matA)
Output:

| 5    -4|
| 5     9|
|-8     0|
| 0     5|
del(matA[1:-1]) # Deletes the second row up to the last row
print(matA)
Output:

| 2     5    -4|
| 7     0     5|

NOTE: Using the methods (insertrow(), insertcolumn(), expandrow(), removerow() etc.) to modify matrix take indexes from 1 instead of slicing which takes indexes from 0

String Representation

The String Representation of the Matrix object (as can be seen from the examples above). All elements (four H_whitespaced from each other and rightly justified) in each row of the matrix are arranged in between two pipe charater "|" but guess what? You can change the this representation to any other desired representation with setrepr() method. eg. If your loyalty is to python (^_^) and you prefer to have your matrix represented as python list...

# Declear a function that takes exactly one argument and returns a str
# This argument will be the tuple of the matrix rows
# which will be supplied by the matrix class when invoking the function

def newrepr(matrix):
    return str(list(matrix))

Matrix.setrepr(newrepr)

mat = Matrix([4, 5, -1], [0, 1, 3], [5, 2, 7])
print(mat)
Output:
[[4, 5, -1], [0, 1, 3], [5, 2, 7]]

You can do str manipulation on the matrix parameter in the body of your defined function and simply return the desired str representantion for your matrix objects. To change back to the defaul representation, use resetrepr()

Other Methods of the Matrix class

  • tranpose(): returns the transpose of matrix
  • zero_matrix(): generates a zero matrix
  • ones_matrix(): generates a ones matrix
  • random_matrix(): generates a matrix with random numbers
  • generate_identity_matrix(): generates identity matrix

Boolean Methods

  • issquare()
  • symmetric()
  • skew()
  • invertible() etc.

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

matrixObj-1.1.1.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matrixObj-1.1.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file matrixObj-1.1.1.tar.gz.

File metadata

  • Download URL: matrixObj-1.1.1.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1

File hashes

Hashes for matrixObj-1.1.1.tar.gz
Algorithm Hash digest
SHA256 fac8063bde851516adeedb74c1d4d295d25e862c117038e153651370966c5ce0
MD5 53b1bbc7b01d049d831878033170b29c
BLAKE2b-256 24528d5962c7bf7f6af12d00d5fa24f76854bcdc33dc6e117ad3873bb78593e9

See more details on using hashes here.

File details

Details for the file matrixObj-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: matrixObj-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1

File hashes

Hashes for matrixObj-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c0866fa5da5d5170d4d0f5e87d19d861d19a59d28e2ab08d364c81390b0e6d83
MD5 02dbc8f6db38b88eb5a9d609944c2c0d
BLAKE2b-256 4cc2f77228839c00d79e6fa85e59b81e02034214237996e67940e0018ecc924e

See more details on using hashes here.

Supported by

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