view matrix.yeti @ 89:ef650ce77237

Start trying to adapt grids
author Chris Cannam
date Mon, 18 Mar 2013 21:24:04 +0000
parents 12c6f103ba8e
children
line wrap: on
line source
module matrix;

// Basic matrices using number type (rather than primitive arrays)

zeros n = array(map \0 [1..n]);
ones  n = array(map \1 [1..n]);

generateMatrix f rows cols = array
   (map do row: array
       (map (f row) [0..cols-1])
    done [0..rows-1]);

constMatrix n = generateMatrix do row col: n done;

randomMatrix = generateMatrix do row col: Math#random() done;

zeroMatrix = constMatrix 0;
identityMatrix = constMatrix 1;

width m = if length m > 0 then length m[0] else 0 fi;

height m = length m;

dimensions m = { cols = width m, rows = height m };

transposed m = array
   (map do n: array
       (map do a: a[n-1] done m)
    done [1..width m]);

interleaved m = array(concat(transposed m));

deinterleaved rows v =
    generateMatrix do row col:
        v[rows * col + row]
    done rows (length v / rows);

{
zeros, ones,
generateMatrix, constMatrix, randomMatrix, zeroMatrix, identityMatrix,
width, height, dimensions,
transposed,
interleaved, deinterleaved,
}