Code editing task: tensor_operations
Topic: Math; change kind: perfective

Original code:
class Tensor:

    def __init__(self, matrix):
        self.matrix = matrix
    
    def m(self):
        return len(self.matrix)
    
    def n(self):
        return len(self.matrix[0])
    
    def relu(self):
        for i in range(self.m()):
            for j in range(self.n()):
                self.matrix[i][j] = max(0, self.matrix[i][j])

    def flatten(self):
        sofar = []
        for i in range(self.n()):
            for j in range(self.m()):
                sofar.append(self.matrix[j][i])
        return sofar

Instruction (descriptive):
Change `flatten` in the Tensor class to flatten the tensor in `self.matrix` from left to right, row by row.

Instruction (lazy):
Change `flatten` to flatten lists left to right, top down.

Edited code:
class Tensor:

    def __init__(self, matrix):
        self.matrix = matrix
    
    def m(self):
        return len(self.matrix)
    
    def n(self):
        return len(self.matrix[0])
    
    def relu(self):
        for i in range(self.m()):
            for j in range(self.n()):
                self.matrix[i][j] = max(0, self.matrix[i][j])

    def flatten(self):
        return [self.matrix[i][j] for i in range(self.m()) for j in range(self.n())]

Tests:
### START TESTS ###
if True:  # pragma: no cover

    m1 = [[9, -2, 6, 13, -8],
        [17, -22, 4, 11, 19],
        [ 5, 12, -25, 3, -16],
        [-10, 18, 7, -20, 14],
        [23, -15, 21, 24, -1]]
    
    m2 = [[3, -5, 7, -2, 4, -8, 6, 1, -9],
        [ 10, -1, 2, -6, 9, -4, 8, -7, 5],
        [ -2, 7, -4, 8, -3, 6, -9, 5, -1]]

    m3 = [[3, -5, 7, -2, 4, -8],
        [6, 1, -9, 10, -1, 2],
        [-6, 9, -4, 8, -7, 5],
        [-2, 7, -4, 8, -3, 6]]

    m4 = [[34, 72, 19, 85, 46, 23, 55, 91],
        [8, 66, 75, 43, 28, 15, 94, 58],
        [82, 39, 20, 4, 71, 31, 70, 10],
        [57, 78, 26, 11, 64, 33, 88, 89],
        [16, 45, 95, 3, 83, 9, 40, 77],
        [49, 76, 36, 7, 54, 29, 50, 60],
        [30, 21, 98, 27, 73, 67, 68, 35]]

    t1 = Tensor(m1)
    t2 = Tensor(m2)
    t3 = Tensor(m3)
    t4 = Tensor(m4)

    assert t1.m() == 5
    assert t1.n() == 5

    assert t2.m() == 3
    assert t2.n() == 9

    assert t3.m() == 4
    assert t3.n() == 6

    assert t4.m() == 7
    assert t4.n() == 8
    
    t1.relu()
    t3.relu()

    assert t1.matrix == [[9, 0, 6, 13, 0],
                        [17, 0, 4, 11, 19],
                        [5, 12, 0, 3, 0],
                        [0, 18, 7, 0, 14],
                        [23, 0, 21, 24, 0]]
    
    assert t2.matrix == [[3, -5, 7, -2, 4, -8, 6, 1, -9],
                        [ 10, -1, 2, -6, 9, -4, 8, -7, 5],
                        [ -2, 7, -4, 8, -3, 6, -9, 5, -1]]
    
    assert t3.matrix == [[3, 0, 7, 0, 4, 0],
                        [6, 1, 0, 10, 0, 2],
                        [0, 9, 0, 8, 0, 5],
                        [0, 7, 0, 8, 0, 6]]

    assert t4.matrix == [[34, 72, 19, 85, 46, 23, 55, 91],
                        [8, 66, 75, 43, 28, 15, 94, 58],
                        [82, 39, 20, 4, 71, 31, 70, 10],
                        [57, 78, 26, 11, 64, 33, 88, 89],
                        [16, 45, 95, 3, 83, 9, 40, 77],
                        [49, 76, 36, 7, 54, 29, 50, 60],
                        [30, 21, 98, 27, 73, 67, 68, 35]]
    
    assert t1.flatten() == [9, 0, 6, 13, 0, 17, 0, 4, 11, 19, 5, 12, 0, 3, 0, 0, 18, 7, 0, 14, 23, 0, 21, 24, 0]

    assert t2.flatten() == [3, -5, 7, -2, 4, -8, 6, 1, -9, 10, -1, 2, -6, 9, -4, 8, -7, 5, -2, 7, -4, 8, -3, 6, -9, 5, -1]

    assert t3.flatten() == [3, 0, 7, 0, 4, 0, 6, 1, 0, 10, 0, 2, 0, 9, 0, 8, 0, 5, 0, 7, 0, 8, 0, 6]

    assert t4.flatten() == [34, 72, 19, 85, 46, 23, 55, 91, 8, 66, 75, 43, 28, 15, 94, 58, 82, 39, 20, 4, 71, 31, 70, 10, 57, 78, 26, 11, 64, 33, 88, 89, 16, 45, 95, 3, 83, 9, 40, 77, 49, 76, 36, 7, 54, 29, 50, 60, 30, 21, 98, 27, 73, 67, 68, 35]