def gen_matrix_message()

in tutorials/LinearAlgebra/testing.py [0:0]


def gen_matrix_message(matrices, strings):
    # Calculate the length of every string
    strlengths = []
    for s in strings:
        strlengths.append(len(s))
    
    # Find tallest matrix
    hmax = 0
    for mat in matrices:
        h = len(mat)
        if h > hmax: hmax = h
    
    # Calculate the rows where each matrix would be positioned
    starts = []
    ends = []
    lengths = []
    for mat in matrices:
        h = len(mat)
        start = (hmax + 1 - h) // 2
        starts.append(start)
        ends.append(start + h - 1)
        lengths.append((14 if isinstance(mat[0][0], complex) else 7) * len(mat[0]) + 3)
    
    # Start building the string
    middle = hmax // 2
    ans = ""
    for i in range(hmax):
        for j in range(len(matrices)):
            if i == middle:
                ans += strings[j]
            else:
                ans += ' ' * strlengths[j]
            
            if starts[j] <= i <= ends[j]:
                row = i - starts[j]
                ans += '| '
                ans += format_row(matrices[j][row])
                ans += '|'
            else:
                ans += ' ' * lengths[j]
        
        if i == middle:
            ans += strings[-1]
        else:
            ans += ' ' * strlengths[-1]
        
        ans += '\n'
    
    return ans