/* Matrix transposition (Rosetta code) in Picat. http://rosettacode.org/wiki/Matrix_transposition """ Transpose an arbitrarily sized rectangular Matrix. """ transpose/1 is a predicate from the util package. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => M = [[0.0, 0.1, 0.2, 0.3], [0.4, 0.5, 0.6, 0.7], [0.8, 0.9, 1.0, 1.1]], print_matrix(M), M2 = [[a,b,c,d,e], [f,g,h,i,j], [k,l,m,n,o], [p,q,r,s,t], [u,v,w,z,y]], print_matrix(M2), M3 = make_matrix(1..24,8), print_matrix(M3), S = "Picat, a simple - and yet powerful - logic-based multi-paradigm language", print_matrix(make_matrix(S,S.len.sqrt.ceiling)), nl. % % Print original matrix and its transpose % print_matrix(M) => println("Matrix:"), foreach(Row in M) println(Row) end, println("\nTransposed:"), foreach(Row in M.transpose()) println(Row) end, nl. % % Make a matrix of list L with Rows rows % (and L.length div Rows columns) % make_matrix(L,Rows) = M => M = [], Cols = L.length div Rows, foreach(I in 1..Rows) NewRow = new_list(Cols), foreach(J in 1..Cols) NewRow[J] := L[ (I-1)*Cols + J] end, M := M ++ [NewRow] end.