diff --git a/source/_static/code/matrix.py b/source/_static/code/matrix.py index 1fb86480b98cc5fedc49c0cf8c8ab792b37dd717..58784a7705844805c0c11acb588d41b0b9c9e63f 100644 --- a/source/_static/code/matrix.py +++ b/source/_static/code/matrix.py @@ -91,18 +91,14 @@ def to_str(matrix): # by design all matrix cols have same size for row in zip(*matrix): cells = [str(cell) for cell in row] - s += " ".join(cells) + "\n" + s += "\t".join(cells) + "\n" return s def mult(matrix, val): """ - :param matrix: the matrix to compute the size + :param matrix: the matrix :type matrix: matrix - :param rows_no: the number of rows - :type rows_no: int - :param col_no: the number of columns - :type col_no: int :param val: the value to mult the matrix with :type val: int :return: a new matrix corresponding the scalar product of matrix * val @@ -115,6 +111,21 @@ def mult(matrix, val): return new_matrix +def mult_inplace(matrix, val): + """ + compute the scalar product of a matrix and a value + do this operation in place + + :param matrix: the matrix + :type matrix: matrix + :param val: the value to mult the matrix with + :type val: int + """ + for col in matrix: + for row_nb, cell in enumerate(col): + col[row_nb] = cell * val + + def get_row(matrix, row_no): """ :param matrix: the matrix to compute the size @@ -221,8 +232,7 @@ def replace_row(matrix, row_no, row): for col_no, value in enumerate(row): set_cell(matrix, row_no, col_no, value) - - + if __name__ == '__main__': m = create(5, 3) print(m) @@ -233,4 +243,7 @@ if __name__ == '__main__': print(to_str(m)) print("get row 0", get_row(m, 0)) print("get col 0", get_col(m, 0)) - + + m2 = create(3, 2, 4) + mult_inplace(m2, 2) + print(to_str(m2))