Search⌘ K
AI Features

Matrix with Python

Explore how to build a Matrix module in Python by reading matrices from files, overloading operators for addition and multiplication, and using coroutines to handle matrix operations in series. Understand key concepts in object-oriented programming and control flow to create robust matrix manipulations.

This project is designed with the purpose of inspecting the object-oriented and control flow concepts.

Challenging questions❓

Task 1: Read matrix from a file

The task is to read the matrix from a file. This task has some specifications. The file should follow a specific format. For example, you can have:

1 2 3
4 5 6
7 8 9

The format of the file is up to you. The above data can be mapped as:

Write a function read_matrix(filename), where the filename is the name of the file containing the matrix. The function should read the file using the context manager protocol into a nested list. If we read the above file, we should get the list as: [[1, 2, 3], [4, 5, 6], [7, 8, 9]].

🧐 Note: Assume that data in the file is always a matrix.

That’s not the end yet. You have to create a Matrix object. The instance of Matrix class will only contain the nested list.


Task 2: Print the matrix

The simplest way to print the matrix is to print its nested list. If you wish to print a better visualization, it’s up to you. Print the matrix whenever print is called on the Matrix object.


Task 3: Add the matrix

This task is a little complicated. You have to overload the + operator to add two matrices.

✏️ Note: Originally, there are two text files, each containing a matrix. You can read both files and add the matrices you get. Or, you can create your test matrices using the Matrix() class. It’s up to you.

Don’t forget to cover the edge cases that restrict the addition. For example, the order of matrices needs to be the same. Do raise the error for the cases that aren’t allowed.

Here’s the critical part. Add two matrices, and if the addition is successful, only then print the result. Otherwise, raise the exception: Addition not possible!

Note: You cannot add a scalar to a matrix. For example: 1 + matrix or matrix + 1 isn’t allowed. Do not forget to handle this case.


Task 4: Multiply the matrix

You have to overload the * operator to multiply two matrices. Don’t forget to cover the edge cases that restrict the matrix multiplication. For example, the number of columns of the first matrix should be equal to the number of rows of the second matrix. Do raise the error for the cases that aren’t allowed.

This task is a little different from the previous one. What if we try to multiply a scalar with a matrix? Well, this is allowed. So, overloading must allow both types of multiplication.

Again, multiply two matrices, and if the multiplication is successful, only then print the result. Otherwise, raise the exception: Multiplication not possible!


Task 5: Add matrices in series

Design a coroutine add_series. It should add the matrices consecutively. Let’s suppose we have three matrices: m1, m2, and m3. When sending matrix m1 to the coroutine, the result would be m1. Then, if we send m2, the result would be m1 + m2. Similarly, on sending m3, the result would be m1 + m2 + m3. Here, task 3 will aid you. Use the overloaded + operator.

If the addition of matrices is successful, only then print the result. Otherwise, raise the exception: Addition not possible!


Task - 6: Multiply matrices in series

Design a coroutine multiply_series. It should multiply the matrices consecutively. Let’s suppose we have three matrices: m1, m2, and m3. When sending matrix m1 to the coroutine, the result would be m1. Then, if we send m2, the result would be m1 * m2. Similarly, on sending m3, the result would be m1 * m2 * m3. Here, task 4 will aid you. Use the overloaded * operator.

If the multiplication of matrices is successful, only then print the result. Otherwise, raise the exception: Multiplication not possible!


Task 7: Switch between multiplication and addition

In this task, you are to combine the above two tasks. The idea is to create a nested generator. Design a switch_add_multiply that receives a matrix. Before you do anything in this coroutine, control should transfer to the generator switch.

In switch, the transferred matrix would be multiplied with itself and then would be returned to switch_add_multiply. Here, the yielded matrix would be added to the final result generated in the previous result to update it (like in task 5).

If the whole process is successful, only then print the result. Otherwise, raise the exception: Not possible!


Task 8: Intensify the magnitude

Design a coroutine intensify_magnitude(matrix). It should receive the scalar and multiply it with the matrix for which the coroutine was designed. Here, task 4 will aid you.

If the multiplication is successful, only then print the result. Otherwise, raise the exception: Multiplication not possible!


Happy coding!

Get ready and code like a crackerjack. Good luck!