1

I have addition and subtraction done with my class. But when I try to do multiplication I'm having unnecessary outmost external array.

class Matrix:
    def __init__(self,*args):
        self.m = []
        self.m.extend(args)

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

It works. But for the multiplication

empty = Matrix([[0 for row in range(col_matrix)] for col in range(row_self)])

I get [[[0, 0], [0, 0]]] rather than [[0, 0], [0, 0]]. What can I do?

1
  • 1
    Nope because you use an asterisk... Commented Apr 5, 2017 at 9:06

1 Answer 1

1

It is not because of the extend itself: but because you use *args (with asterisk *). Indeed if you call:

Matrix([[0,0],[0,0]])

what will happen is that args is a tuple ([[0,0],[0,0]],) (with one element). So you extend the tuple and thus append the single element of the tuple: you append([[0,0],[0,0]]).

What you probably want is to extend all elements of args one-by-one, so:

class Matrix:
    def __init__(self,*args):
        self.m = []
        for argi in args:
            self.m.extend(argi)

In case you want to accept only one parameter, you can simplify the process into:

class Matrix:
    def __init__(self,data): # without asterisk
        self.m = []
        self.m.extend(data)

Or make it more elegant, like:

class Matrix:
    def __init__(self,data):
        self.m = list(data)

Note that you make a shallow copy: you better make a deep copy since otherwise the rows will still be the same and thus modifications to the rows in one matrix will reflect on the other matrix. So:

from copy import deepcopy

class Matrix:
    def __init__(self,data):
        self.m = deepcopy(data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.