Search code examples
pythonmatrixinstantiation

Create a class that takes a matrix for instantiation


I'm asked to create a class that that takes an m by n array for instantiation. Then give the class a method which does any operation on the matrix, lets say doubles its diagonal entries and returns it.


Solution

  • Start from this simple skeleton:

    class Matrix:
    
        def __init__(self, matrix):
            self.matrix = matrix
    
        def double_diagnonal_entries(self):
            # do calcs
            return self.matrix
    

    Note, that if you need to implement some basic matrix ops like addition you might consider operator overloading such as:

    def __add__(self, another_matrix):
        # do the math
        return sum_matrix