Matrix in Python-Part3 (World of Square Matrices)

leangaurav
Industrial Python
Published in
3 min readMay 26, 2019

Before this we discussed about matrices with any dimension. No relation between number of rows and columns. But just making number of rows and columns equal gives rise to lots of possibilities and once you enter the realm of matrices you will find a lot of interest around square matrices.

In this part we will see different kinds of square matrices. There will be some special kind of square matrices which will show us some interesting properties of these matrices. Lets start with the simplest first.

Identity Matrix

Identity matrix has all its main-diagonal elements set to 1 and everything else is 0. That’s it !

With all main diagonal elements as one an Identity Matrix looks something like this:

Identity matrices are denoted by Iₙ. Only one dimension n needs to be mentioned since Identity matrices are square matrices. Identity matrix is called so because it acts as a multiplicative identity for other matrices. Just like 1 is multiplicative identity for real numbers.

Now lets define a function to create identity matrices. I will use the zeros function defined previously as it is already optimized to use list comprehension and then set the main diagonal elements to 1s.

In the code you also see that doing transpose of an Identity matrix gives you the same matrix. That’s another property of Identity Matrix.

Some more Utility functions

Before continuing with other types of square matrices, lets define one more function: transpose_square.

This function takes the advantage that the number of row and columns is same and thus transforms the original matrix to its transpose in-place. By in place I mean that using the existing list without creating additional lists as their size doesn’t need to change. So the function doesn’t return any new matrix also.

Now we will continue with square matrices

Symmetric Matrix

A matrix whose transpose it equal to it self is a symmetric matrix.

M = Mᵀ

This is a property of a matrix and not some kind of transformation. So we define a function that tells us whether a square matrix is symmetric or not. Instead of creating a symmetric matrix to test our function, we will use a property to generate symmetric matrix.

M + Mᵀ is a symmetric matrix

See the code below:

mat3 looks a bit weird while printing due to misalignment. Anyways we will fix that later. For now I think you should have understood the concept.

The next one is a sibling of symmetric matrix.

Skew-Symmetric Matrix

I will just write the property

M = — Mᵀ

So matrix is equal to negative of its transpose. This time you have to define the function is_skew_symmetric yourself. To generate a skew symmetric matrix from a normal square matrix we do this:

M — Mᵀ is a skew symmetric matrix

Fill in the template code:

Now that you have reached till here… I hope writing all the code yourself on the way, I would like to quote something.

A wise man once said: Practice makes both men and women perfect.

So Practice… just practice and write tons of code!

And the wise man who said this might be me :)

This part is incomplete right now. Wait till next week for completion(1st June 2019)

--

--

leangaurav
Industrial Python

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav