# $Id: matrix.pliant,v 1.0 2002/02/07 18:27:00 dada Exp $
# http://dada.perl.it/shootout/

module "/pliant/language/context.pli"

gvar Int size := 30

function mkmatrix rows cols -> mx
  arg Int rows ; arg Int cols
  arg_w Array:(Array:Int) mx
  var Array:Int row
  var Int count := 1
  for (var Int r) 0 rows-1
    row:size := 0
    for (var Int c) 0 cols-1
      row += count
      count := count + 1
    mx += row
  return mx

function mmult rows cols m1 m2 m3
  arg Int rows ; arg Int cols
  arg Array:(Array:Int) m1
  arg Array:(Array:Int) m2
  arg_w Array:(Array:Int) m3
  var Array:Int row
  var Int val
  for (var Int i) 0 rows-1
    row:size := 0
    for (var Int j) 0 cols-1
      val := 0
      for (var Int k) 0 cols-1
        val := val + m1:i:k * m2:k:j
      row += val
    m3 += row

gvar Array:(Array:Int) m1
gvar Array:(Array:Int) m2
gvar Array:(Array:Int) mm

gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
  
  m1 := mkmatrix size size
  m2 := mkmatrix size size

  while n>0
    mmult size size m1 m2 mm
    n := n - 1
  console mm:0:0 " " mm:2:3 " " mm:3:2 " " mm:4:4 eol
else
  console "usage: matrix.pliant <number>" eol