view floatrix.yeti @ 7:c5dc45708e74

Finish initial set of functions in primitive-array backed matrix; use in audiofile
author Chris Cannam
date Wed, 12 Dec 2012 17:12:34 +0000
parents 0718b9253e59
children dc72a1d15901
line wrap: on
line source
module floatrix;

zeros n = new double[n];
ones  n = (a = zeros n; for [0..n-1] do i: a[i] := 1.0 done; a);

zeroMatrix rows cols = array (map \(zeros cols) [1..rows]);

generateMatrix f rows cols =
   (m = zeroMatrix rows cols;
    for [0..rows-1] do row:
        for [0..cols-1] do col:
            n = f row col;
            m[row][col] := n;
        done;
    done;
    m);

constMatrix n = generateMatrix do row col: n done;

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

identityMatrix = constMatrix 1;

arrayWrap a is ~double[] -> array<number> = array(a);

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

cols = width;

height m = length m;

rows = height;

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

transposed m is array<~double[]> -> array<~double[]> = 
    generateMatrix do row col: m[col][row] done (cols m) (rows m);
        
interleaved m = 
   ({ cols, rows } = dimensions m;
    v = zeros (cols * rows);
    for [0..rows-1] do row:
        for [0..cols-1] do col:
            v[col * rows + row] := m[row][col];
        done;
    done;
    v);

deinterleaved rows v is number -> ~double[] -> 'a =
    generateMatrix do row col:
        v[rows * col + row]
    done rows (length (arrayWrap v) / rows);

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