0

I am trying to create a 3x3 Matrix class in python. I am having difficulty initializing the 2D array properly.

In the following code I am getting an error saying that Matrix3x3 has no attribute m. If I initialize m before the init, it seems like 'm' acts like a static variable, which I do not want.

class Matrix3x3:
    #[1 0 0]
    #[0 1 0]
    #[0 0 1]
    def __init__(self, m):
        #self.m[row][column]
        self.m[0][0] = m[0][0];
        self.m[0][1] = m[0][1];
        self.m[0][2] = m[0][2];

        self.m[1][0] = m[1][0];
        self.m[1][1] = m[1][1];
        self.m[1][2] = m[1][2];

        self.m[2][0] = m[2][0];
        self.m[2][1] = m[2][1];
        self.m[2][2] = m[2][2];
user2970916
  • 1,146
  • 3
  • 15
  • 37
  • Dont add `m` as argument to init..just declare m within constructor – Abhi Oct 03 '14 at 18:08
  • possible duplicate of [correct way to define class variables in Python](http://stackoverflow.com/questions/9056957/correct-way-to-define-class-variables-in-python) – Abhi Oct 03 '14 at 18:10
  • forget about `m` and just use `self.m = [[0 for _ in xrange(3)] for _ in xrange(3)]` – Padraic Cunningham Oct 03 '14 at 18:31
  • Why on earth do you need this? Matrix and array solutions already exist, particularly see numpy. – mdurant Oct 03 '14 at 19:08

4 Answers4

3

The problem is that m needs to exist before you can set its elements, and even then, trying to set the value of an element in a list will fail if the list is too short. So

lst = []
lst[0] = 0

will fail but

lst = []
lst.append(0)

will succeed. lst[0] = 0 should be thought of as trying to change the value of lst at index 0, and can't be used if lst has no index 0.

However, the simplest way to do what you want is either with list comprehensions,

class Matrix3x3:
    def __init__(self, m):
        self.m = [[m[i][j] for j in xrange(len(m[i]))] for i in xrange(len(m))]

or with deepcopy, if you don't plan on altering the input:

from copy import deepcopy

class Matrix3x3:
    def __init__(self, m):
        self.m = deepcopy(m)

Additionally, if this isn't for an assignment of some sort, you should consider instead using numpy.matrix, which has a bunch of built in matrix operations, rather than trying to roll your own.

colevk
  • 670
  • 1
  • 5
  • 10
0

Try this:)

matrix_size = 3
Matrix = [[1 if x==y else 0 for y in xrange(matrix_size)] for x in xrange(matrix_size)]

Mayby is not 100% readeble at first, but works:)

Beri
  • 11,470
  • 4
  • 35
  • 57
0

You must allocate m as a list of lists before setting its elements :

class Matrix3x3:
    #[1 0 0]
    #[0 1 0]
    #[0 0 1]
    def __init__(self, m):
        #allocate the lists : m is a list containing 3 lists of 3 None values
        self.m = [ [ None for j in range(3) ] for i in range(3) ]
        for i in range(3):
            for j in range(3):
                self.m[i][j] = m[i][j];

or even better directly :

class Matrix3x3:
    #[1 0 0]
    #[0 1 0]
    #[0 0 1]
    def __init__(self, m):
        # directly init self.m
        self.m = [ [ m[i][j] for j in range(3) ] for i in range(3) ]
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
-1

This class will generate a matrix 3x3

class Matrix3x3:
    def __init__(self):
        self.m = []
        for item in range(3):
            self.m.append([0,0,0])


mat = Matrix3x3()
print(mat.m)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

it can be expanded upon to create X sized matrices

class Matrix_x_sized:
    def __init__(self,x):
        self.m = []
        for item in range(x):
            self.m.append([0]*x)

mat2 = Matrix_x_sized(4)
mat2.m
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
TehTris
  • 3,139
  • 1
  • 21
  • 33