Untitled

mail@pastecode.io avatar
unknown
python
a month ago
1.8 kB
1
Indexable
Never
def RREF_Matrix(strArr):
    rows = ",".join(strArr).split(",<>,")
    matrix = []

    for row in rows:
        matrix.append([int(elem) for elem in row.split(",")])

    def rref(matrix):
        pivot = 0
        for row_id in range(len(matrix)):
            if pivot >= len(matrix[0]):
                return 
            temp_row = row_id
            while matrix[temp_row][pivot] == 0:
                temp_row += 1
                if temp_row == len(matrix):
                    temp_row = row_id
                    pivot += 1
                    if pivot == len(matrix[0]):
                        return 
            if temp_row != row_id:
                matrix[temp_row], matrix[row_id] = matrix[row_id], matrix[temp_row]
            
            divisor = matrix[row_id][pivot]
            for elem_id in range(len(matrix[row_id])):
                matrix[row_id][elem_id] = matrix[row_id][elem_id] // divisor

            for row_id_II in range(len(matrix)):
                if row_id_II != row_id:
                    factor = matrix[row_id_II][pivot]
                    for elem_id in range(len(matrix[row_id_II])):
                        matrix[row_id_II][elem_id] = matrix[row_id_II][elem_id] - \
                            matrix[row_id][elem_id]*factor
            pivot += 1

    rref(matrix)
    
    # Convert matrix to string format
    result = []
    for row in matrix:
        result.append("".join(map(str, row)))
    
    return "".join(result)

# Test the function
print(RREF_Matrix(["2", "4", "8", "<>", "6", "12", "14"]))  # Output should be "120001"

# Test the function
print(RREF_Matrix(["2", "2", "4", "<>", "1", "1", "8", "<>", "7", "6", "5"]))  # Output should be "100010001"
Leave a Comment