annotate matrix.yeti @ 89:ef650ce77237

Start trying to adapt grids
author Chris Cannam
date Mon, 18 Mar 2013 21:24:04 +0000
parents 12c6f103ba8e
children
rev   line source
Chris@1 1 module matrix;
Chris@1 2
Chris@18 3 // Basic matrices using number type (rather than primitive arrays)
Chris@18 4
Chris@2 5 zeros n = array(map \0 [1..n]);
Chris@2 6 ones n = array(map \1 [1..n]);
Chris@1 7
Chris@2 8 generateMatrix f rows cols = array
Chris@2 9 (map do row: array
Chris@2 10 (map (f row) [0..cols-1])
Chris@2 11 done [0..rows-1]);
Chris@1 12
Chris@2 13 constMatrix n = generateMatrix do row col: n done;
Chris@1 14
Chris@2 15 randomMatrix = generateMatrix do row col: Math#random() done;
Chris@2 16
Chris@2 17 zeroMatrix = constMatrix 0;
Chris@2 18 identityMatrix = constMatrix 1;
Chris@2 19
Chris@2 20 width m = if length m > 0 then length m[0] else 0 fi;
Chris@2 21
Chris@2 22 height m = length m;
Chris@2 23
Chris@3 24 dimensions m = { cols = width m, rows = height m };
Chris@3 25
Chris@2 26 transposed m = array
Chris@2 27 (map do n: array
Chris@2 28 (map do a: a[n-1] done m)
Chris@2 29 done [1..width m]);
Chris@2 30
Chris@2 31 interleaved m = array(concat(transposed m));
Chris@2 32
Chris@3 33 deinterleaved rows v =
Chris@3 34 generateMatrix do row col:
Chris@3 35 v[rows * col + row]
Chris@3 36 done rows (length v / rows);
Chris@2 37
Chris@3 38 {
Chris@3 39 zeros, ones,
Chris@3 40 generateMatrix, constMatrix, randomMatrix, zeroMatrix, identityMatrix,
Chris@3 41 width, height, dimensions,
Chris@3 42 transposed,
Chris@3 43 interleaved, deinterleaved,
Chris@3 44 }
Chris@3 45