using Printf Base.show(io::IO, f::Float64) = @printf(io, "%.2f", f) """ random_ternary_matrix(nbrows::Int,nbcols::Int) Returns a random nbrows-by-nbcols matrix with three types of elements: -Inf, 0, and 1. """ function random_ternary_matrix(nbrows::Int,nbcols::Int) result = zeros(nbrows, nbcols) for i=1:nbrows for j=1:nbcols result[i, j] = log2(abs(rand(Int) % 3)) end end return result end """ tropical_matrix_multiply(A::Array{Float64,2},B::Array{Float64,2}) Returns the result of the tropical matrix multiplication of A with B. """ function tropical_matrix_multiply(A::Array{Float64,2},B::Array{Float64,2}) nbrowsA, nbcolsA = size(A) nbrowsB, nbcolsB = size(B) result = zeros(nbrowsA, nbcolsB) for i=1:nbrowsA for j=1:nbcolsB result[i, j] = -Inf for k=1:nbcolsA result[i, j] = max(result[i, j], A[i, k] + B[k, j]) end end end return result end """ Generates two random matrices and shows the result of the tropical matrix multiplication. """ function main() A = random_ternary_matrix(3, 5) println("The matrix A :") show(stdout, "text/plain", A); println(""); B = random_ternary_matrix(5, 4) println("The matrix B :") show(stdout, "text/plain", B); println(""); C = tropical_matrix_multiply(A, B) println("The matrix A*B :") show(stdout, "text/plain", C); println(""); end main()