Can someone help me regarding the subtraction and multiplication of two matrices which I created using arrays (without numpy) and I am doing it using object oriented by making class and functions. I had created 2 matrices and print them by calling the class in objects and now I have to make a function in the same class which subtracts and another function which multiplies these 2 matrices.
Basically you’re talking about Operator Overloading,
In python we can implement __sub__ method for subtraction __add__ for addition __mul__ for multiplication etc…
Read more here
Basically, you want to overload the arithmetic operators, for example
def __add__(self, other):
if self.order != other.order:
raise ValueError("Addition requires matrices of the same order")
return Matrix(
[
[self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns)]
for i in range(self.num_rows)
]
)
So, you can now do, assuming you have already implemented your basic Matrix class