I just want somebody to explain precisely what this function does (this is in Py
ID: 3665794 • Letter: I
Question
I just want somebody to explain precisely what this function does (this is in Python 3). Basically, I just want more detailed comments. I understand the math but not the code.
def like_a_gauss(mat):
"""
Changes mat into Reduced Row-Echelon Form.
"""
# Let's do forward step first.
# at the end of this for loop, the matrix is in Row-Echelon format.
for i in range(min(len(mat), len(mat[0]))):
# every iteration, ignore one more row and column
for r in range(i, len(mat)):
# find the first row with a nonzero entry in first column
zero_row = mat[r][i] == 0
if zero_row:
continue
# swap current row with first row
mat[i], mat[r] = mat[r], mat[i]
# add multiples of the new first row to lower rows so lower
# entries of first column is zero
first_row_first_col = mat[i][i]
for rr in range(i + 1, len(mat)):
this_row_first = mat[rr][i]
scalarMultiple = -1 * this_row_first / first_row_first_col
for cc in range(i, len(mat[0])):
mat[rr][cc] += mat[i][cc] * scalarMultiple
break
Explanation / Answer
def like_a_gauss(mat):
"""
Changes mat into Reduced Row-Echelon Form.
"""
# Let's do forward step first.
# at the end of this for loop, the matrix is in Row-Echelon format.
for i in range(min(len(mat), len(mat[0]))):
# every iteration, ignore one more row and column
for r in range(i, len(mat)):
# find the first row with a nonzero entry in first column
zero_row = mat[r][i] == 0
if zero_row:
continue
# swap current row with first row
mat[i], mat[r] = mat[r], mat[i]
# add multiples of the new first row to lower rows so lower
# entries of first column is zero
first_row_first_col = mat[i][i]
for rr in range(i + 1, len(mat)):
this_row_first = mat[rr][i]
scalarMultiple = -1 * this_row_first / first_row_first_col
for cc in range(i, len(mat[0])):
mat[rr][cc] += mat[i][cc] * scalarMultiple
break
The First for loop will take the range in between min(len(mat) to len(mat[0])) i.e for loop range is start from 0 to total number of elements in matrix..
Exmaple:
for i in range(0, 9)
The second for loop have the will tae the range in between i value i.e from first loop and total number of elements of in matrix i.e len(mat)
Example:
for(i, 9) consider matrix have 9 elements
if the mat[r][i] => mat[0][0] = 0 => then if condition will pass for all the iteration of loops..it means if given matrix contains zeros in elements.those rows will continue to next
iteration..
then in next line swapping of elements will take place between the row and column of matrix..
=> after swapping the elements of matrix then mat[i][i] will assgin to first_row_first_col i.e diagonally index elements are assgin to first_row_first_col
=> then in next step for loop will divide the each row values divide with each elements of diagonally..i.e first_row_first_col then result will assign to scalarMultiple
=> then each column of value is multiplied by scalarMultiple value..like that the operation will continues..
I think you got the explanation ..and let me know if you need more explanation