Task

Invert matrix   A   using Gauss-Jordan method.

A   being an   n × n   matrix.




func gauss_jordan_invert (M) {
 
    var I = M.len.of {|i|
        M.len.of {|j|
            i == j ? 1 : 0
        }
    }
 
    var A = gather {
        ^M -> each {|i| take(M[i] + I[i]) }
    }
 
    rref(A).map { .last(M.len) }
}
 
var A = [
    [-1, -2, 3, 2],
    [-4, -1, 6, 2],
    [ 7, -8, 9, 1],
    [ 1, -2, 1, 3],
]
 
say gauss_jordan_invert(A).map {
    .map { "%6s" % .as_rat }.join("  ")
}.join("\n")