Mercurial > hg > may
changeset 222:77c6a81c577f matrix_opaque_immutable
Move block directory -> vector
line wrap: on
line diff
--- a/yetilab/block/blockfuncs.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ - -module yetilab.block.blockfuncs; - -vec = load yetilab.block.vector; - -load yetilab.block.vectortype; - -//!!! "internal" vector function to retrieve data for read-only -// purposes without copying -raw = - (raw' v is ~double[] -> ~double[] = v; - raw' as vector -> ~double[]); - -sum' v = - (dat = raw v; - tot = new double[1]; - for [0..length dat - 1] do i: - tot[0] := tot[0] + dat[i] - done; - tot[0]); - -mean v = - case vec.length v of - 0: 0; - len: sum' v / len - esac; - -multiply b1 b2 = - (v1 = raw b1; - v2 = raw b2; - len = if length v1 < length v2 then length v1 else length v2 fi; - out = new double[len]; - for [0..len-1] do i: - out[i] := v1[i] * v2[i] - done; - vec.vector out); - -divideBy n v = - vec.fromList (map (/ n) (vec.list v)); - -sqr v = - multiply v v; - -rms = - sqrt . mean . sqr; - -sqrt' = - vec.fromList . (map sqrt) . vec.list; - -fftshift v = - (len = vec.length v; - half = int(len/2 + 0.5); // round up for odd-length sequences - vec.concat [vec.rangeOf half (len-half) v, vec.rangeOf 0 half v]); - -ifftshift v = - (len = vec.length v; - half = int(len/2); // round down for odd-length sequences - vec.concat [vec.rangeOf half (len-half) v, vec.rangeOf 0 half v]); - -{ -sum is vector -> number = sum', -mean is vector -> number, -multiply is vector -> vector -> vector, -divideBy is number -> vector -> vector, -sqr is vector -> vector, -sqrt is vector -> vector = sqrt', -rms is vector -> number, -fftshift is vector -> vector, -ifftshift is vector -> vector, -} - - -
--- a/yetilab/block/complex.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ - -module yetilab.block.complex; - -load yetilab.block.vectortype; -load yetilab.block.complextype; - -vec = load yetilab.block.vector; - -import java.lang: ClassCastException; - -class Cplx(double real, double imag) - int getReal() - real, - int getImag() - imag, - double getMagnitude() - sqrt (real * real + imag * imag), - double getAngle() - Math#atan2(imag, real), - String toString() - if real == int real and imag == int imag then - if imag < 0 then - " \(int real) - \(int (-imag))i" - else - " \(int real) + \(int imag)i" - fi - else - if imag < 0 then - " \(real) - \((-imag))i" - else - " \(real) + \(imag)i" - fi - fi, - int hashCode() - Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(), - boolean equals(Object other) - try - c = other unsafely_as ~Cplx; - c#getReal() == real and c#getImag() == imag - catch ClassCastException: - false - yrt, -end; - -real c1 is ~Cplx -> number = - c1#getReal(); - -imaginary c1 is ~Cplx -> number = - c1#getImag(); - -complex re im is number -> number -> ~Cplx = - new Cplx(re, im); - -magnitude c is ~Cplx -> number = - c#getMagnitude(); - -angle c is ~Cplx -> number = - c#getAngle(); - -add c1 c2 is ~Cplx -> ~Cplx -> ~Cplx = - complex (real c1 + real c2) (imaginary c1 + imaginary c2); - -scale r c is number -> ~Cplx -> ~Cplx = - complex (r * real c) (r * imaginary c); - -zeros n is number -> array<~Cplx> = - array (map \(complex 0 0) [1..n]); - -magnitudes cc is list?<~Cplx> -> vector = - vec.fromList (map magnitude cc); - -angles cc is list?<~Cplx> -> vector = - vec.fromList (map angle cc); - -{ - real, - imaginary, - complex, - magnitude, - angle, - add, - scale, - zeros, - magnitudes, - angles, -} as { - real is cplx -> number, - imaginary is cplx -> number, - complex is number -> number -> cplx, - magnitude is cplx -> number, - angle is cplx -> number, - add is cplx -> cplx -> cplx, - scale is number -> cplx -> cplx, - zeros is number -> array<cplx>, - magnitudes is list?<cplx> -> vector, - angles is list?<cplx> -> vector, -} -
--- a/yetilab/block/complextype.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ - -module yetilab.block.complextype; - -typedef opaque cplx = ~Cplx; - -(); -
--- a/yetilab/block/test/test_blockfuncs.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ - -module yetilab.block.test.test_blockfuncs; - -stdSqrt = sqrt; - -{ zeros, consts, ones, fromList, list } = load yetilab.block.vector; -{ sum, mean, multiply, divideBy, sqr, sqrt, rms, fftshift, ifftshift } = load yetilab.block.blockfuncs; -{ compare } = load yetilab.test.test; - -[ - -"sum": \( - compare ((sum . zeros) 0) 0 and - compare ((sum . zeros) 5) 0 and - compare ((sum . ones) 5) 5 and - compare ((sum . fromList) [1,-2,3,0]) 2 -), - -"mean": \( - compare ((mean . zeros) 0) 0 and - compare ((mean . zeros) 5) 0 and - compare ((mean . ones) 5) 1 and - compare ((mean . fromList) [1,-2,3,0]) 0.5 -), - -"multiply": \( - compare (list (multiply (zeros 0) (ones 5))) [] and - compare (list (multiply (consts (-3) 4) (fromList [1,2,3]))) [-3,-6,-9] -), - -"divideBy": \( - compare (list (divideBy 5 (ones 0))) [] and - compare (list (divideBy 5 (fromList [1,2,-3]))) [0.2,0.4,-0.6] -), - -"sqr": \( - compare ((list . sqr . zeros) 0) [] and - compare ((list . sqr . ones) 5) [1,1,1,1,1] and - compare ((list . sqr . fromList) [0.5,-2,3,0]) [0.25,4,9,0] -), - -"sqrt": \( - compare ((list . sqrt . zeros) 0) [] and - compare ((list . sqrt . ones) 5) [1,1,1,1,1] and - compare ((list . sqrt . fromList) [0.25,4,9,0]) [0.5,2,3,0] -), - -"rms": \( - compare ((rms . zeros) 0) 0 and - compare ((rms . ones) 5) 1 and - compare ((rms . fromList) [-1,2,2]) (stdSqrt 3) -), - -"fftshift": \( - compare ((list . fftshift . zeros) 0) [] and - compare ((list . fftshift . fromList) [1,2,3,4]) [3,4,1,2] and - compare ((list . fftshift . fromList) [1,2,3,4,5]) [4,5,1,2,3] -), - -"ifftshift": \( - compare ((list . ifftshift . zeros) 0) [] and - compare ((list . ifftshift . fromList) [3,4,1,2]) [1,2,3,4] and - compare ((list . ifftshift . fromList) [4,5,1,2,3]) [1,2,3,4,5] -), - -] is hash<string, () -> boolean>; - -
--- a/yetilab/block/test/test_complex.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -module yetilab.block.test.test_complex; - -{ real, imaginary, complex, magnitude, angle, add, scale, zeros, magnitudes, angles } - = load yetilab.block.complex; - -{ compare } = load yetilab.test.test; - -vec = load yetilab.block.vector; - -[ - -"complex": \( - compare (complex 1 2) (complex 1 2) and - complex (-1) 2 != complex 1 2 -), - -"real": \( - compare (real (complex 3 2)) 3 -), - -"imaginary": \( - compare (imaginary (complex 3 4)) 4 -), - -"magnitude": \( - compare (magnitude (complex (-3) 4)) 5 -), - -"angle": \( - compare (angle (complex 1 0)) 0 and - compare (angle (complex 1 1)) (pi/4) and - compare (angle (complex 0 1)) (pi/2) and - compare (angle (complex (-1) 0)) pi and - compare (angle (complex 0 (-1))) (-pi/2) -), - -"add": \( - compare (add (complex 2 3) (complex (-4) 5)) (complex (-2) 8) -), - -"scale": \( - compare (scale 4 (complex 2 3)) (complex 8 12) -), - -"zeros": \( - compare (zeros 0) (array []) and - compare (zeros 3) (array [complex 0 0, complex 0 0, complex 0 0]) -), - -"magnitudes": \( - compare (vec.list (magnitudes [ complex (-3) 4, complex 4 3, complex 0 0 ])) - [ 5, 5, 0 ] and - compare (vec.list (magnitudes (array []))) [] -), - -"angles": \( - compare (vec.list (angles [ complex 1 0, complex (-1) 0, complex 0 (-1) ])) - [ 0, pi, -pi/2 ] and - compare (vec.list (angles (array []))) [] -), - - -] is hash<string, () -> boolean>; - - -
--- a/yetilab/block/test/test_vector.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,130 +0,0 @@ - -module yetilab.block.test.test_vector; - -vec = load yetilab.block.vector; - -{ compare } = load yetilab.test.test; - -[ - -"zeros-empty": \( - v = vec.zeros 0; - compare (vec.length v) 0; -), - -"zeros": \( - v = vec.zeros 3; - a = vec.array v; - compare (vec.length v) 3 and - compare a[0] 0 and - compare a[1] 0 and - compare a[2] 0; -), - -"consts-empty": \( - v = vec.consts 4 0; - compare (vec.length v) 0; -), - -"consts": \( - v = vec.consts 4 3; - a = vec.array v; - compare (vec.length v) 3 and - compare a[0] 4 and - compare a[1] 4 and - compare a[2] 4; -), - -"ones-empty": \( - v = vec.ones 0; - compare (vec.length v) 0; -), - -"ones": \( - v = vec.ones 3; - a = vec.array v; - compare (vec.length v) 3 and - compare a[0] 1 and - compare a[1] 1 and - compare a[2] 1; -), - -"from-list-empty": \( - v = vec.fromList []; - compare (vec.length v) 0; -), - -"from-list": \( - v = vec.fromList [1,2,3,4]; - a = vec.array v; - compare (vec.length v) 4 and - compare a[0] 1 and - compare a[1] 2 and - compare a[2] 3 and - compare a[3] 4; -), - -"equal-empty": \( - vec.equal (vec.fromList []) (vec.fromList []) -), - -"equal": \( - v = vec.fromList [1,1,1,1]; - w = vec.ones 4; - w' = vec.zeros 4; - w'' = vec.ones 3; - vec.equal v w and not vec.equal v w' and not vec.equal v w''; -), -/* -"copyOf-empty": \( - vec.equal (vec.fromList []) (vec.copyOf (vec.fromList [])) -), - -"copyOf": \( - v = vec.fromList [1,2,3,4]; - w = vec.copyOf v; - vec.equal v w and ( - v[0] := 0; // check result is not aliasing inputs - not vec.equal v w - ); -), -*/ -"rangeOf": \( - v = vec.fromList [1,2,3,4]; - vec.equal (vec.rangeOf 0 4 v) v and ( - vec.equal (vec.rangeOf 2 2 v) (vec.fromList [3,4]) - ) -), - -"resizedTo": \( - vec.equal (vec.resizedTo 4 (vec.fromList [])) (vec.zeros 4) and - vec.equal (vec.resizedTo 2 (vec.fromList [1,2])) (vec.fromList [1,2]) and - vec.equal (vec.resizedTo 3 (vec.fromList [1,2])) (vec.fromList [1,2,0]) and - vec.equal (vec.resizedTo 2 (vec.fromList [1,2,3])) (vec.fromList [1,2]); -), - -"concat2": \( - v = vec.fromList [1,2,3]; - w = vec.fromList [4,5,6]; - x = vec.concat [v, w]; - x' = vec.fromList [1,2,3,4,5,6]; - vec.equal x x' and -/* (v[0] := 0; // check result is not aliasing inputs - w[0] := 0; - vec.equal x x') and */ - vec.equal x' (vec.concat [x', vec.fromList []]) and - vec.equal x' (vec.concat [vec.fromList [], x']) -), - -"concatn": \( - v = vec.fromList [1,2,3]; - w = vec.fromList [4,5,6]; - vec.equal (vec.concat []) (vec.zeros 0) and - vec.equal (vec.concat [v]) v and - vec.equal (vec.concat [v,w,v]) (vec.fromList [1,2,3,4,5,6,1,2,3]) -), - -] is hash<string, () -> boolean>; - - -
--- a/yetilab/block/vector.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,124 +0,0 @@ - -module yetilab.block.vector; - -load yetilab.block.vectortype; - -import java.util: Arrays; - -//!!! This is supposed to be 100% immutable and without copying when duplicating for read only - -zeros n = - new double[n]; - -consts m n = - (a = zeros n; - for [0..n-1] do i: - a[i] := m; - done; - a); - -ones = consts 1.0; - -fromList l is list?<number> -> ~double[] = - (arr = array(l); - len = length arr; - v = zeros len; - for [0..len-1] do i: - v[i] := arr[i]; - done; - v); - -list' a is ~double[] -> list<number> = - list a; - -array' a is ~double[] -> array<number> = - array a; - -length' = - length . list'; - -empty?' = - empty? . list'; - -at' n v is number -> ~double[] -> number = - v[n]; - -floats a is ~double[] -> ~float[] = - (len = length' a; - f = new float[len]; - for [0..len-1] do i: - f[i] := a[i]; - done; - f); - -fromFloats ff is ~float[] -> ~double[] = - (len = length (list ff); - a = new double[len]; - for [0..len-1] do i: - a[i] := ff[i]; - done; - a); - -equal v1 v2 = - list' v1 == list' v2; - -copyOf v is ~double[] -> ~double[] = - Arrays#copyOf(v, list' v |> length); - -rangeOf start len v is number -> number -> ~double[] -> ~double[] = - Arrays#copyOfRange(v, start, start + len); - -resizedTo n v is number -> ~double[] -> ~double[] = - Arrays#copyOf(v, n); - -concat vv is list?<~double[]> -> ~double[] = - (len = sum (map length' vv); - vout = zeros len; - var base = 0; - for vv do v: - vlen = length' v; - for [0..vlen-1] do i: vout[base + i] := v[i] done; - base := base + vlen; - done; - vout); - -{ - zeros, - consts, - ones, - vector v = v, - primitive = copyOf, - floats, - fromFloats, - fromList, - list = list', - array = array', - length = length', - empty? = empty?', - at = at', - equal, - rangeOf, - resizedTo, - concat, -} as { - zeros is number -> vector, - consts is number -> number -> vector, - ones is number -> vector, - vector is ~double[] -> vector, - primitive is vector -> ~double[], - floats is vector -> ~float[], - fromFloats is ~float[] -> vector, - fromList is list?<number> -> vector, - list is vector -> list<number>, - array is vector -> array<number>, - length is vector -> number, - empty? is vector -> boolean, - at is number -> vector -> number, - equal is vector -> vector -> boolean, - rangeOf is number -> number -> vector -> vector, //!!! not well-named now vector arg is at the end - resizedTo is number -> vector -> vector, - concat is list?<vector> -> vector, -} - - -
--- a/yetilab/block/vectortype.yeti Sat May 11 14:50:43 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ - -module yetilab.block.vectortype; - -typedef opaque vector = ~double[]; - -(); -
--- a/yetilab/feature/features.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/feature/features.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,8 +1,8 @@ module yetilab.feature.features; -vec = load yetilab.block.vector; -cplx = load yetilab.block.complex; +vec = load yetilab.vector.vector; +cplx = load yetilab.vector.complex; fr = load yetilab.stream.framer; magdiff frame1 frame2 =
--- a/yetilab/matrix/matrix.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/matrix/matrix.yeti Sat May 11 15:58:36 2013 +0100 @@ -11,10 +11,10 @@ //!!! check that we are not unnecessarily copying in the transform functions -vec = load yetilab.block.vector; -bf = load yetilab.block.blockfuncs; +vec = load yetilab.vector.vector; +bf = load yetilab.vector.blockfuncs; -load yetilab.block.vectortype; +load yetilab.vector.vectortype; load yetilab.matrix.matrixtype; size m =
--- a/yetilab/matrix/matrixtype.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/matrix/matrixtype.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,7 +1,7 @@ module yetilab.matrix.matrixtype; -load yetilab.block.vectortype; +load yetilab.vector.vectortype; typedef opaque matrix = RowM. array<vector> | ColM. array<vector>;
--- a/yetilab/matrix/test/test_matrix.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/matrix/test/test_matrix.yeti Sat May 11 15:58:36 2013 +0100 @@ -2,9 +2,9 @@ module yetilab.matrix.test.test_matrix; mat = load yetilab.matrix.matrix; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; -load yetilab.block.vectortype; +load yetilab.vector.vectortype; load yetilab.matrix.matrixtype; import yeti.lang: FailureException;
--- a/yetilab/stream/audiofile.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/audiofile.yeti Sat May 11 15:58:36 2013 +0100 @@ -12,7 +12,7 @@ import java.nio: ByteBuffer, ByteOrder; ch = load yetilab.stream.channels; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; decode8u bytes doubles n is ~byte[] -> ~double[] -> number -> () = (for [0..n-1] do i:
--- a/yetilab/stream/channels.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/channels.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,10 +1,10 @@ module yetilab.stream.channels; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; mat = load yetilab.matrix.matrix; -load yetilab.block.vectortype; +load yetilab.vector.vectortype; //!!! "internal" vector function to retrieve data for read-only // purposes without copying
--- a/yetilab/stream/framer.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/framer.yeti Sat May 11 15:58:36 2013 +0100 @@ -6,8 +6,8 @@ * overlapping) frames of data. */ -vec = load yetilab.block.vector; -bf = load yetilab.block.blockfuncs; +vec = load yetilab.vector.vector; +bf = load yetilab.vector.blockfuncs; af = load yetilab.stream.audiofile; win = load yetilab.transform.window; fft = load yetilab.transform.fft;
--- a/yetilab/stream/playback.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/playback.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,7 +1,7 @@ module yetilab.stream.playback; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; af = load yetilab.stream.audiofile; ch = load yetilab.stream.channels;
--- a/yetilab/stream/syntheticstream.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/syntheticstream.yeti Sat May 11 15:58:36 2013 +0100 @@ -2,9 +2,9 @@ module yetilab.stream.syntheticstream; ch = load yetilab.stream.channels; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; -load yetilab.block.vectortype; +load yetilab.vector.vectortype; load yetilab.stream.streamtype; generated sampleRate generator =
--- a/yetilab/stream/test/audiofile_reference.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/test/audiofile_reference.yeti Sat May 11 15:58:36 2013 +0100 @@ -3,7 +3,7 @@ syn = load yetilab.stream.syntheticstream; filt = load yetilab.stream.filter; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; pulseChannel rate = (pulseFreq = 2;
--- a/yetilab/stream/test/test_audiofile.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/test/test_audiofile.yeti Sat May 11 15:58:36 2013 +0100 @@ -2,7 +2,7 @@ module yetilab.stream.test.test_audiofile; af = load yetilab.stream.audiofile; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; mat = load yetilab.matrix.matrix; { compare } = load yetilab.test.test;
--- a/yetilab/stream/test/test_channels.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/test/test_channels.yeti Sat May 11 15:58:36 2013 +0100 @@ -3,7 +3,7 @@ ch = load yetilab.stream.channels; mat = load yetilab.matrix.matrix; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; { compare, compareUsing } = load yetilab.test.test;
--- a/yetilab/stream/test/test_filter.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/test/test_filter.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,7 +1,7 @@ module yetilab.stream.test.test_filter; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; mat = load yetilab.matrix.matrix; syn = load yetilab.stream.syntheticstream; filt = load yetilab.stream.filter;
--- a/yetilab/stream/test/test_framer.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/test/test_framer.yeti Sat May 11 15:58:36 2013 +0100 @@ -2,7 +2,7 @@ module yetilab.stream.test.test_framer; fr = load yetilab.stream.framer; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; mat = load yetilab.matrix.matrix; syn = load yetilab.stream.syntheticstream;
--- a/yetilab/stream/test/test_syntheticstream.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/stream/test/test_syntheticstream.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,7 +1,7 @@ module yetilab.stream.test.test_syntheticstream; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; mat = load yetilab.matrix.matrix; syn = load yetilab.stream.syntheticstream;
--- a/yetilab/test/all.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/test/all.yeti Sat May 11 15:58:36 2013 +0100 @@ -4,9 +4,9 @@ { runTests } = load yetilab.test.test; tests = [ -"vector" : load yetilab.block.test.test_vector, -"blockfuncs" : load yetilab.block.test.test_blockfuncs, -"complex" : load yetilab.block.test.test_complex, +"vector" : load yetilab.vector.test.test_vector, +"blockfuncs" : load yetilab.vector.test.test_blockfuncs, +"complex" : load yetilab.vector.test.test_complex, "framer" : load yetilab.stream.test.test_framer, "channels" : load yetilab.stream.test.test_channels, "audiofile" : load yetilab.stream.test.test_audiofile,
--- a/yetilab/transform/fft.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/transform/fft.yeti Sat May 11 15:58:36 2013 +0100 @@ -3,10 +3,10 @@ import edu.emory.mathcs.jtransforms.fft: DoubleFFT_1D; -vec = load yetilab.block.vector; -complex = load yetilab.block.complex; +vec = load yetilab.vector.vector; +complex = load yetilab.vector.complex; -load yetilab.block.complextype; +load yetilab.vector.complextype; packedToComplex len p is number -> ~double[] -> array<cplx> = (n = len / 2;
--- a/yetilab/transform/test/test_fft.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/transform/test/test_fft.yeti Sat May 11 15:58:36 2013 +0100 @@ -2,8 +2,8 @@ module yetilab.transform.test.test_fft; { realForward, realInverse } = load yetilab.transform.fft; -{ list, fromList } = load yetilab.block.vector; -{ complex } = load yetilab.block.complex; +{ list, fromList } = load yetilab.vector.vector; +{ complex } = load yetilab.vector.complex; { compare } = load yetilab.test.test;
--- a/yetilab/transform/window.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/transform/window.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,8 +1,8 @@ module yetilab.transform.window; -vec = load yetilab.block.vector; -bf = load yetilab.block.blockfuncs; +vec = load yetilab.vector.vector; +bf = load yetilab.vector.blockfuncs; cosinewin a0 a1 a2 a3 n = vec.fromList
--- a/yetilab/vamp/test/test_vamp.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/vamp/test/test_vamp.yeti Sat May 11 15:58:36 2013 +0100 @@ -4,7 +4,7 @@ synthetic = load yetilab.stream.syntheticstream; filter = load yetilab.stream.filter; mat = load yetilab.matrix.matrix; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; { compare, compareUsing } = load yetilab.test.test;
--- a/yetilab/vamp/vamp.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/vamp/vamp.yeti Sat May 11 15:58:36 2013 +0100 @@ -10,7 +10,7 @@ import java.util: Map, List; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; fr = load yetilab.stream.framer; af = load yetilab.stream.audiofile; mat = load yetilab.matrix.matrix; @@ -235,7 +235,7 @@ //!!! bring vector typedef into scope -- this shouldn't be necessary, //but see comment in processed below -load yetilab.block.vectortype; +load yetilab.vector.vectortype; processed { p, sampleRate, hop } frames count // I don't know why this type declaration is necessary. Without
--- a/yetilab/vamp/vamppost.yeti Sat May 11 14:50:43 2013 +0100 +++ b/yetilab/vamp/vamppost.yeti Sat May 11 15:58:36 2013 +0100 @@ -1,7 +1,7 @@ module yetilab.vamp.vamppost; mat = load yetilab.matrix.matrix; -vec = load yetilab.block.vector; +vec = load yetilab.vector.vector; fillOneSamplePerStep config features = (fill' n pending features =
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/vector/blockfuncs.yeti Sat May 11 15:58:36 2013 +0100 @@ -0,0 +1,73 @@ + +module yetilab.vector.blockfuncs; + +vec = load yetilab.vector.vector; + +load yetilab.vector.vectortype; + +//!!! "internal" vector function to retrieve data for read-only +// purposes without copying +raw = + (raw' v is ~double[] -> ~double[] = v; + raw' as vector -> ~double[]); + +sum' v = + (dat = raw v; + tot = new double[1]; + for [0..length dat - 1] do i: + tot[0] := tot[0] + dat[i] + done; + tot[0]); + +mean v = + case vec.length v of + 0: 0; + len: sum' v / len + esac; + +multiply b1 b2 = + (v1 = raw b1; + v2 = raw b2; + len = if length v1 < length v2 then length v1 else length v2 fi; + out = new double[len]; + for [0..len-1] do i: + out[i] := v1[i] * v2[i] + done; + vec.vector out); + +divideBy n v = + vec.fromList (map (/ n) (vec.list v)); + +sqr v = + multiply v v; + +rms = + sqrt . mean . sqr; + +sqrt' = + vec.fromList . (map sqrt) . vec.list; + +fftshift v = + (len = vec.length v; + half = int(len/2 + 0.5); // round up for odd-length sequences + vec.concat [vec.rangeOf half (len-half) v, vec.rangeOf 0 half v]); + +ifftshift v = + (len = vec.length v; + half = int(len/2); // round down for odd-length sequences + vec.concat [vec.rangeOf half (len-half) v, vec.rangeOf 0 half v]); + +{ +sum is vector -> number = sum', +mean is vector -> number, +multiply is vector -> vector -> vector, +divideBy is number -> vector -> vector, +sqr is vector -> vector, +sqrt is vector -> vector = sqrt', +rms is vector -> number, +fftshift is vector -> vector, +ifftshift is vector -> vector, +} + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/vector/complex.yeti Sat May 11 15:58:36 2013 +0100 @@ -0,0 +1,98 @@ + +module yetilab.vector.complex; + +load yetilab.vector.vectortype; +load yetilab.vector.complextype; + +vec = load yetilab.vector.vector; + +import java.lang: ClassCastException; + +class Cplx(double real, double imag) + int getReal() + real, + int getImag() + imag, + double getMagnitude() + sqrt (real * real + imag * imag), + double getAngle() + Math#atan2(imag, real), + String toString() + if real == int real and imag == int imag then + if imag < 0 then + " \(int real) - \(int (-imag))i" + else + " \(int real) + \(int imag)i" + fi + else + if imag < 0 then + " \(real) - \((-imag))i" + else + " \(real) + \(imag)i" + fi + fi, + int hashCode() + Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(), + boolean equals(Object other) + try + c = other unsafely_as ~Cplx; + c#getReal() == real and c#getImag() == imag + catch ClassCastException: + false + yrt, +end; + +real c1 is ~Cplx -> number = + c1#getReal(); + +imaginary c1 is ~Cplx -> number = + c1#getImag(); + +complex re im is number -> number -> ~Cplx = + new Cplx(re, im); + +magnitude c is ~Cplx -> number = + c#getMagnitude(); + +angle c is ~Cplx -> number = + c#getAngle(); + +add c1 c2 is ~Cplx -> ~Cplx -> ~Cplx = + complex (real c1 + real c2) (imaginary c1 + imaginary c2); + +scale r c is number -> ~Cplx -> ~Cplx = + complex (r * real c) (r * imaginary c); + +zeros n is number -> array<~Cplx> = + array (map \(complex 0 0) [1..n]); + +magnitudes cc is list?<~Cplx> -> vector = + vec.fromList (map magnitude cc); + +angles cc is list?<~Cplx> -> vector = + vec.fromList (map angle cc); + +{ + real, + imaginary, + complex, + magnitude, + angle, + add, + scale, + zeros, + magnitudes, + angles, +} as { + real is cplx -> number, + imaginary is cplx -> number, + complex is number -> number -> cplx, + magnitude is cplx -> number, + angle is cplx -> number, + add is cplx -> cplx -> cplx, + scale is number -> cplx -> cplx, + zeros is number -> array<cplx>, + magnitudes is list?<cplx> -> vector, + angles is list?<cplx> -> vector, +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/vector/complextype.yeti Sat May 11 15:58:36 2013 +0100 @@ -0,0 +1,7 @@ + +module yetilab.vector.complextype; + +typedef opaque cplx = ~Cplx; + +(); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/vector/test/test_blockfuncs.yeti Sat May 11 15:58:36 2013 +0100 @@ -0,0 +1,68 @@ + +module yetilab.vector.test.test_blockfuncs; + +stdSqrt = sqrt; + +{ zeros, consts, ones, fromList, list } = load yetilab.vector.vector; +{ sum, mean, multiply, divideBy, sqr, sqrt, rms, fftshift, ifftshift } = load yetilab.vector.blockfuncs; +{ compare } = load yetilab.test.test; + +[ + +"sum": \( + compare ((sum . zeros) 0) 0 and + compare ((sum . zeros) 5) 0 and + compare ((sum . ones) 5) 5 and + compare ((sum . fromList) [1,-2,3,0]) 2 +), + +"mean": \( + compare ((mean . zeros) 0) 0 and + compare ((mean . zeros) 5) 0 and + compare ((mean . ones) 5) 1 and + compare ((mean . fromList) [1,-2,3,0]) 0.5 +), + +"multiply": \( + compare (list (multiply (zeros 0) (ones 5))) [] and + compare (list (multiply (consts (-3) 4) (fromList [1,2,3]))) [-3,-6,-9] +), + +"divideBy": \( + compare (list (divideBy 5 (ones 0))) [] and + compare (list (divideBy 5 (fromList [1,2,-3]))) [0.2,0.4,-0.6] +), + +"sqr": \( + compare ((list . sqr . zeros) 0) [] and + compare ((list . sqr . ones) 5) [1,1,1,1,1] and + compare ((list . sqr . fromList) [0.5,-2,3,0]) [0.25,4,9,0] +), + +"sqrt": \( + compare ((list . sqrt . zeros) 0) [] and + compare ((list . sqrt . ones) 5) [1,1,1,1,1] and + compare ((list . sqrt . fromList) [0.25,4,9,0]) [0.5,2,3,0] +), + +"rms": \( + compare ((rms . zeros) 0) 0 and + compare ((rms . ones) 5) 1 and + compare ((rms . fromList) [-1,2,2]) (stdSqrt 3) +), + +"fftshift": \( + compare ((list . fftshift . zeros) 0) [] and + compare ((list . fftshift . fromList) [1,2,3,4]) [3,4,1,2] and + compare ((list . fftshift . fromList) [1,2,3,4,5]) [4,5,1,2,3] +), + +"ifftshift": \( + compare ((list . ifftshift . zeros) 0) [] and + compare ((list . ifftshift . fromList) [3,4,1,2]) [1,2,3,4] and + compare ((list . ifftshift . fromList) [4,5,1,2,3]) [1,2,3,4,5] +), + +] is hash<string, () -> boolean>; + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/vector/test/test_complex.yeti Sat May 11 15:58:36 2013 +0100 @@ -0,0 +1,66 @@ +module yetilab.vector.test.test_complex; + +{ real, imaginary, complex, magnitude, angle, add, scale, zeros, magnitudes, angles } + = load yetilab.vector.complex; + +{ compare } = load yetilab.test.test; + +vec = load yetilab.vector.vector; + +[ + +"complex": \( + compare (complex 1 2) (complex 1 2) and + complex (-1) 2 != complex 1 2 +), + +"real": \( + compare (real (complex 3 2)) 3 +), + +"imaginary": \( + compare (imaginary (complex 3 4)) 4 +), + +"magnitude": \( + compare (magnitude (complex (-3) 4)) 5 +), + +"angle": \( + compare (angle (complex 1 0)) 0 and + compare (angle (complex 1 1)) (pi/4) and + compare (angle (complex 0 1)) (pi/2) and + compare (angle (complex (-1) 0)) pi and + compare (angle (complex 0 (-1))) (-pi/2) +), + +"add": \( + compare (add (complex 2 3) (complex (-4) 5)) (complex (-2) 8) +), + +"scale": \( + compare (scale 4 (complex 2 3)) (complex 8 12) +), + +"zeros": \( + compare (zeros 0) (array []) and + compare (zeros 3) (array [complex 0 0, complex 0 0, complex 0 0]) +), + +"magnitudes": \( + compare (vec.list (magnitudes [ complex (-3) 4, complex 4 3, complex 0 0 ])) + [ 5, 5, 0 ] and + compare (vec.list (magnitudes (array []))) [] +), + +"angles": \( + compare (vec.list (angles [ complex 1 0, complex (-1) 0, complex 0 (-1) ])) + [ 0, pi, -pi/2 ] and + compare (vec.list (angles (array []))) [] +), + + +] is hash<string, () -> boolean>; + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/vector/test/test_vector.yeti Sat May 11 15:58:36 2013 +0100 @@ -0,0 +1,130 @@ + +module yetilab.vector.test.test_vector; + +vec = load yetilab.vector.vector; + +{ compare } = load yetilab.test.test; + +[ + +"zeros-empty": \( + v = vec.zeros 0; + compare (vec.length v) 0; +), + +"zeros": \( + v = vec.zeros 3; + a = vec.array v; + compare (vec.length v) 3 and + compare a[0] 0 and + compare a[1] 0 and + compare a[2] 0; +), + +"consts-empty": \( + v = vec.consts 4 0; + compare (vec.length v) 0; +), + +"consts": \( + v = vec.consts 4 3; + a = vec.array v; + compare (vec.length v) 3 and + compare a[0] 4 and + compare a[1] 4 and + compare a[2] 4; +), + +"ones-empty": \( + v = vec.ones 0; + compare (vec.length v) 0; +), + +"ones": \( + v = vec.ones 3; + a = vec.array v; + compare (vec.length v) 3 and + compare a[0] 1 and + compare a[1] 1 and + compare a[2] 1; +), + +"from-list-empty": \( + v = vec.fromList []; + compare (vec.length v) 0; +), + +"from-list": \( + v = vec.fromList [1,2,3,4]; + a = vec.array v; + compare (vec.length v) 4 and + compare a[0] 1 and + compare a[1] 2 and + compare a[2] 3 and + compare a[3] 4; +), + +"equal-empty": \( + vec.equal (vec.fromList []) (vec.fromList []) +), + +"equal": \( + v = vec.fromList [1,1,1,1]; + w = vec.ones 4; + w' = vec.zeros 4; + w'' = vec.ones 3; + vec.equal v w and not vec.equal v w' and not vec.equal v w''; +), +/* +"copyOf-empty": \( + vec.equal (vec.fromList []) (vec.copyOf (vec.fromList [])) +), + +"copyOf": \( + v = vec.fromList [1,2,3,4]; + w = vec.copyOf v; + vec.equal v w and ( + v[0] := 0; // check result is not aliasing inputs + not vec.equal v w + ); +), +*/ +"rangeOf": \( + v = vec.fromList [1,2,3,4]; + vec.equal (vec.rangeOf 0 4 v) v and ( + vec.equal (vec.rangeOf 2 2 v) (vec.fromList [3,4]) + ) +), + +"resizedTo": \( + vec.equal (vec.resizedTo 4 (vec.fromList [])) (vec.zeros 4) and + vec.equal (vec.resizedTo 2 (vec.fromList [1,2])) (vec.fromList [1,2]) and + vec.equal (vec.resizedTo 3 (vec.fromList [1,2])) (vec.fromList [1,2,0]) and + vec.equal (vec.resizedTo 2 (vec.fromList [1,2,3])) (vec.fromList [1,2]); +), + +"concat2": \( + v = vec.fromList [1,2,3]; + w = vec.fromList [4,5,6]; + x = vec.concat [v, w]; + x' = vec.fromList [1,2,3,4,5,6]; + vec.equal x x' and +/* (v[0] := 0; // check result is not aliasing inputs + w[0] := 0; + vec.equal x x') and */ + vec.equal x' (vec.concat [x', vec.fromList []]) and + vec.equal x' (vec.concat [vec.fromList [], x']) +), + +"concatn": \( + v = vec.fromList [1,2,3]; + w = vec.fromList [4,5,6]; + vec.equal (vec.concat []) (vec.zeros 0) and + vec.equal (vec.concat [v]) v and + vec.equal (vec.concat [v,w,v]) (vec.fromList [1,2,3,4,5,6,1,2,3]) +), + +] is hash<string, () -> boolean>; + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/vector/vector.yeti Sat May 11 15:58:36 2013 +0100 @@ -0,0 +1,124 @@ + +module yetilab.vector.vector; + +load yetilab.vector.vectortype; + +import java.util: Arrays; + +//!!! This is supposed to be 100% immutable and without copying when duplicating for read only + +zeros n = + new double[n]; + +consts m n = + (a = zeros n; + for [0..n-1] do i: + a[i] := m; + done; + a); + +ones = consts 1.0; + +fromList l is list?<number> -> ~double[] = + (arr = array(l); + len = length arr; + v = zeros len; + for [0..len-1] do i: + v[i] := arr[i]; + done; + v); + +list' a is ~double[] -> list<number> = + list a; + +array' a is ~double[] -> array<number> = + array a; + +length' = + length . list'; + +empty?' = + empty? . list'; + +at' n v is number -> ~double[] -> number = + v[n]; + +floats a is ~double[] -> ~float[] = + (len = length' a; + f = new float[len]; + for [0..len-1] do i: + f[i] := a[i]; + done; + f); + +fromFloats ff is ~float[] -> ~double[] = + (len = length (list ff); + a = new double[len]; + for [0..len-1] do i: + a[i] := ff[i]; + done; + a); + +equal v1 v2 = + list' v1 == list' v2; + +copyOf v is ~double[] -> ~double[] = + Arrays#copyOf(v, list' v |> length); + +rangeOf start len v is number -> number -> ~double[] -> ~double[] = + Arrays#copyOfRange(v, start, start + len); + +resizedTo n v is number -> ~double[] -> ~double[] = + Arrays#copyOf(v, n); + +concat vv is list?<~double[]> -> ~double[] = + (len = sum (map length' vv); + vout = zeros len; + var base = 0; + for vv do v: + vlen = length' v; + for [0..vlen-1] do i: vout[base + i] := v[i] done; + base := base + vlen; + done; + vout); + +{ + zeros, + consts, + ones, + vector v = v, + primitive = copyOf, + floats, + fromFloats, + fromList, + list = list', + array = array', + length = length', + empty? = empty?', + at = at', + equal, + rangeOf, + resizedTo, + concat, +} as { + zeros is number -> vector, + consts is number -> number -> vector, + ones is number -> vector, + vector is ~double[] -> vector, + primitive is vector -> ~double[], + floats is vector -> ~float[], + fromFloats is ~float[] -> vector, + fromList is list?<number> -> vector, + list is vector -> list<number>, + array is vector -> array<number>, + length is vector -> number, + empty? is vector -> boolean, + at is number -> vector -> number, + equal is vector -> vector -> boolean, + rangeOf is number -> number -> vector -> vector, //!!! not well-named now vector arg is at the end + resizedTo is number -> vector -> vector, + concat is list?<vector> -> vector, +} + + +