Skip to main content

What is matops?

matops is a lightweight pure Python package to do basic matrix operations like addition, multiplication etc. as well as to check some properties of matrices e.g. whether a matrix is scalar or not.

What is a matrix?

In mathematics, a matrix is a set of numbers arranged in rows and columns so as to form a rectangular array. The numbers are called the elements, or entries, of the matrix.

Installation:

You can use pip to install matops with the following command:

pip install matops

Examples:

Addition:

from matops import Matrix

m1 = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

m2 = Matrix(
    [
        [5, 6],
        [7, 8],
    ]
)

m3 = m1 + m2

print(m3)
[[6, 8], [10, 12]]

You can use prettify() method to print it in a more readable format.

m3.prettify()
[6, 8]
[10, 12]

Transpose:

from matops import Matrix

m = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

m.transpose().prettify()
[1, 3]
[2, 4]

The Matrix class

The Matrix class is at the heart of matops. You can import it as follows to use it in your code:

from matops import Matrix

Instantiation

m = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

Supported operations and methods:

Addition

from matops import Matrix

m1 = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

m2 = Matrix(
    [
        [5, 6],
        [7, 8],
    ]
)

m3 = m1 + m2

m3.prettify()
[6, 8]
[10, 12]

Subtraction

from matops import Matrix

m1 = Matrix(
    [
        [7, 9],
        [1, 5],
    ]
)

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

m3 = m1 - m2

m3.prettify()
[4, 8]
[1, 3]

Multiplication

Multiplication can be performed either with an int, float or another Matrix.

With an int or a float

When multiplying a Matrix with an int or a float, make sure that Matrix is on the left side of the * operator.

from matops import Matrix

m1 = Matrix(
    [
        [4, 0],
        [1, -9],
    ]
)

m2 = m1 * 2

m2.prettify()
[8, 0]
[2, -18]

With another Matrix

When multiplying a Matrix with another Matrix, make sure that the Matrix on the left side of the * operator has the same number of columns as the rows of the Matrix on the right side. If that's not the case, you might get an error.

from matops import Matrix

m1 = Matrix(
    [
        [3, 4, 2],
    ]
)
m2 = Matrix(
    [
        [13, 9, 7, 15],
        [8, 7, 4, 6],
        [6, 4, 0, 3],
    ]
)

m3 = m1 * m2

m3.prettify()
[83, 63, 37, 75]

Negative

Negative of a Matrix basically flips the sign of every element from + to - and vice versa. It is essentially the same as multiplying the Matrix by -1.

from matops import Matrix

m1 = Matrix(
    [
        [1, -2],
        [-3, 4],
    ]
)

m2 = -m1

m2.prettify()
[-1, 2]
[3, -4]

Transpose

The transpose of a matrix is an operator which flips a matrix over its diagonal. We essentially convert rows into columns (or columns into rows). The .transpose() method can be used to find the transpose of a Matrix.

from matops import Matrix

m = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

m.transpose().prettify()
[1, 3]
[2, 4]

Equality

We can check whether two Matrix are equal or not using == and != operators.

from matops import Matrix

m1 = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

m2 = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

m3 = Matrix(
    [
        [1, 0],
        [0, 1],
    ]
)

print(m1 == m2)
print(m1 == m3)
print(m1 != m3)
True
False
True

Row Matrix

If a matrix has only one row, it's called a row matrix. We can use .is_row_matrix() method to check whether a Matrix is a row matrix or not.

from matops import Matrix

m1 = Matrix(
    [
        [1, 2],
    ]
)

m2 = Matrix(
    [
        [1],
        [2],
    ]
)

print(m1.is_row_matrix())
print(m2.is_row_matrix())
True
False

Column Matrix

If a matrix has only one column, it's called a column matrix. We can use .is_column_matrix() method to check whether a Matrix is a column matrix or not.

from matops import Matrix

m1 = Matrix(
    [
        [1],
        [2],
    ]
)

m2 = Matrix(
    [
        [1, 2],
    ]
)

print(m1.is_column_matrix())
print(m2.is_column_matrix())
True
False

In this case, m1 is a column matrix while m2 is not.

Rectangular Matrix

If the number of rows in a matrix are not equal to the number of columns, that matrix is callled a rectangular matrix. We can use .is_rectangular_matrix() method to check whether a Matrix is a rectangular matrix or not.

from matops import Matrix

m = Matrix(
    [
        [1],
        [2],
    ]
)

print(m.is_rectangular_matrix())
True

In this case m is a rectangular matrix because the number of rows (2) are not equal to the number of columns (1).

Square Matrix

As opposed to a rectangular matrix, if the number of rows in a matrix are equal to the number of columns, that matrix is callled a square matrix. We can use .is_square_matrix() method to check whether a Matrix is a square matrix or not.

from matops import Matrix

m = Matrix(
    [
        [1, 2],
        [3, 4],
    ]
)

print(m.is_square_matrix())
True

In this case, m is a square matrix because the number of rows (2) are equal to the number of columns (2).

Zero Matrix

If all the elements of a matrix are zero, it is callled zero matrix. We can use .is_zero_matrix() method to check whether a Matrix is a zero matrix or not.

from matops import Matrix

m = Matrix(
    [
        [0, 0],
        [0, 0],
    ]
)

print(m.is_zero_matrix())
True

In this case, m is a zero matrix because all of its elements are equal to zero.

Symmetric Matrix

If the transpose of a matrix is equal to the original matrix itself, then that matrix is called a symmetric matrix. We can use .is_symmetric_matrix() method to check whether a Matrix is a symmetric matrix or not. Consider the following example:

from matops import Matrix

m = Matrix(
    [
        [3, 2],
        [2, 4],
    ]
)

m.transpose().prettify()
[3, 2]
[2, 4]

As you can see we are printing the transpose of the matrix m but it is again equal to m itself. So m is a symmetric matrix and we can verify that as follows:

from matops import Matrix

m = Matrix(
    [
        [3, 2],
        [2, 4],
    ]
)

print(m.is_symmetric_matrix())
True

Skew-symmetric Matrix

If the transpose of a matrix is equal to the negative of the original matrix, then that matrix is called a skew-symmetric matrix. We can use .is_skew_symmetric_matrix() method to check whether a Matrix is a skew-symmetric matrix or not. Consider the following example:

from matops import Matrix

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

m.transpose().prettify()
[0, -2, -3]
[2, 0, -1]
[3, 1, 0]

If you look closely, you'll see that the transpose of matrix m is actually negative of the matrix m. So m is a skew-symmetric matrix and we can verify that as follows:

from matops import Matrix

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

print(m.is_skew_symmetric_matrix())
True

Diagonal Matrix

A diagonal matrix is a square matrix whose:

  • Off-diagonal entries are all equal to zero.
  • At least one of the diagonal entries is non-zero

We can use is_diagonal_matrix() method to check whether a Matrix is a diagonal matrix or not.

from matops import Matrix

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

print(m.is_diagonal_matrix())
True

If any off-diagonal element is non-zero, it won't be a diagonal matrix.

from matops import Matrix

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

print(m.is_diagonal_matrix())
False

If all the diagonal entries are zero too then it won't be a diagonal matrix either. Instead, it will become a zero matrix.

from matops import Matrix

m = Matrix(
    [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
    ]
)

print(m.is_diagonal_matrix())
False

Scalar Matrix

A diagonal matrix is called a scalar matrix if all the diagonal entries are equal and non-zero. We can use is_scalar_matrix() method to check whether a Matrix is a scalar matrix or not.

from matops import Matrix

m = Matrix(
    [
        [7, 0, 0],
        [0, 7, 0],
        [0, 0, 7],
    ]
)

print(m.is_scalar_matrix())
True

If any off-diagonal element is non-zero, it won't be a diagonal matrix and hence it won't be a scalar matrix either.

from matops import Matrix

m = Matrix(
    [
        [7, 1, 0],
        [0, 7, 0],
        [0, 0, 7],
    ]
)

print(m.is_scalar_matrix())
False

If all the diagonal entries are not equal and non-zero then it won't be a scalar matrix.

from matops import Matrix

m = Matrix(
    [
        [1, 0, 0],
        [0, 7, 0],
        [0, 0, 7],
    ]
)

print(m.is_scalar_matrix())
False

Identity Matrix

A scalar matrix is called an identity matrix if all the diagonal entries are equal to 1. We can use is_identity_matrix() method to check whether a Matrix is an identity matrix or not.

from matops import Matrix

m = Matrix(
    [
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1],
    ]
)

print(m.is_identity_matrix())
True

Release files for matops 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for matops 0.1.4
File Size Uploaded
matops-0.1.4.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for matops 0.1.4
File Interpreter ABI Platform
matops-0.1.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 13.8 kB

Release files / matops-0.1.4.tar.gz

Download URL matops-0.1.4.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
be300a9fdf6d9d7768c54f3f8c33e6cedde71665c1d70f46cc8cd8abcb4f3bf1
BLAKE2b-256 checksum
How to use checksums
6cc8bcbe9853adf63e90de3430e7e2cff28843f64015ebd77e33d5c6ef3a8b9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release files / matops-0.1.4-py2.py3-none-any.whl

Download URL matops-0.1.4-py2.py3-none-any.whl
Size 5.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
26c942f85f886838c24af9af4eb3f53ee359802173409ebd01e2ef814b66fcba
BLAKE2b-256 checksum
How to use checksums
a567316d211b1016f003b8e82c5b7741c3d5ae3e1f11088ac2544f5b1bd179e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page