Mercurial > hg > may
changeset 211:8e79cf8a4399 matrix_opaque_immutable
Start moving fvector+block to a single immutable vector type
author | Chris Cannam |
---|---|
date | Fri, 10 May 2013 22:48:55 +0100 |
parents | 3a2be6eb8bd3 |
children | e5158af5e8be |
files | yetilab/block/block.yeti yetilab/block/blocktype.yeti yetilab/block/fvector.yeti yetilab/block/test/test_fvector.yeti yetilab/block/test/test_vector.yeti yetilab/block/vector.yeti yetilab/block/vectortype.yeti yetilab/test/test.yeti |
diffstat | 8 files changed, 256 insertions(+), 269 deletions(-) [+] |
line wrap: on
line diff
--- a/yetilab/block/block.yeti Wed May 08 21:10:29 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ - -module yetilab.block.block; - -vec = load yetilab.block.fvector; - -load yetilab.block.blocktype; - -//!!! doc: block provides no api to modify contents, though it can be done through data accessor... consider formalising this somehow - -{ - zeros = vec.zeros, - consts = vec.consts, - ones = vec.ones, - block v = v, - data b = b, - vector b = vec.copyOf b, - floats = vec.floats, - fromFloats ff = vec.fromFloats ff, - fromList l = vec.vector l, - list = vec.list, - length = vec.length, - empty? = vec.empty?, - equal = vec.equal, - copyOf = vec.copyOf, - rangeOf = vec.rangeOf, - resizedTo = vec.resizedTo, - concat = vec.concat, -} as { - zeros is number -> block, - consts is number -> number -> block, - ones is number -> block, - block is ~double[] -> block, - data is block -> ~double[], - vector is block -> ~double[], - floats is block -> ~float[], - fromFloats is ~float[] -> block, - fromList is list?<number> -> block, - list is block -> list<number>, - length is block -> number, - empty? is block -> boolean, - equal is block -> block -> boolean, - copyOf is block -> block, - rangeOf is number -> number -> block -> block, //!!! not well-named now block arg is at the end - resizedTo is number -> block -> block, - concat is list?<block> -> block, -} - - -
--- a/yetilab/block/blocktype.yeti Wed May 08 21:10:29 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ - -module yetilab.block.blocktype; - -typedef opaque block = ~double[]; - -(); -
--- a/yetilab/block/fvector.yeti Wed May 08 21:10:29 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -module yetilab.block.fvector; - -import java.util: Arrays; - -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; - -vector 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; - -length' = - length . list'; - -empty?' = - empty? . list'; - -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, -length = length', -empty? = empty?', -list = list', -floats, fromFloats, -equal, -copyOf, rangeOf, resizedTo, -concat, -} -
--- a/yetilab/block/test/test_fvector.yeti Wed May 08 21:10:29 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ - -module yetilab.block.test.test_fvector; - -vec = load yetilab.block.fvector; - -{ compare } = load yetilab.test.test; - -[ - -"zeros-empty": \( - v = vec.zeros 0; - compare (vec.length v) 0; -), - -"zeros": \( - v = vec.zeros 3; - compare (vec.length v) 3 and - compare v[0] 0 and - compare v[1] 0 and - compare v[2] 0; -), - -"consts-empty": \( - v = vec.consts 4 0; - compare (vec.length v) 0; -), - -"consts": \( - v = vec.consts 4 3; - compare (vec.length v) 3 and - compare v[0] 4 and - compare v[1] 4 and - compare v[2] 4; -), - -"ones-empty": \( - v = vec.ones 0; - compare (vec.length v) 0; -), - -"ones": \( - v = vec.ones 3; - compare (vec.length v) 3 and - compare v[0] 1 and - compare v[1] 1 and - compare v[2] 1; -), - -"from-list-empty": \( - v = vec.vector []; - compare (vec.length v) 0; -), - -"from-list": \( - v = vec.vector [1,2,3,4]; - compare (vec.length v) 4 and - compare v[0] 1 and - compare v[1] 2 and - compare v[2] 3 and - compare v[3] 4; -), - -"equal-empty": \( - vec.equal (vec.vector []) (vec.vector []) -), - -"equal": \( - v = vec.vector [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.vector []) (vec.copyOf (vec.vector [])) -), - -"copyOf": \( - v = vec.vector [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.vector [1,2,3,4]; - vec.equal (vec.rangeOf 0 4 v) v and ( - vec.equal (vec.rangeOf 2 2 v) (vec.vector [3,4]) - ) -), - -"resizedTo": \( - vec.equal (vec.resizedTo 4 (vec.vector [])) (vec.zeros 4) and - vec.equal (vec.resizedTo 2 (vec.vector [1,2])) (vec.vector [1,2]) and - vec.equal (vec.resizedTo 3 (vec.vector [1,2])) (vec.vector [1,2,0]) and - vec.equal (vec.resizedTo 2 (vec.vector [1,2,3])) (vec.vector [1,2]); -), - -"concat2": \( - v = vec.vector [1,2,3]; - w = vec.vector [4,5,6]; - x = vec.concat [v, w]; - x' = vec.vector [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.vector []]) and - vec.equal x' (vec.concat [vec.vector [], x']) -), - -"concatn": \( - v = vec.vector [1,2,3]; - w = vec.vector [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.vector [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/block/test/test_vector.yeti Fri May 10 22:48:55 2013 +0100 @@ -0,0 +1,130 @@ + +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>; + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yetilab/block/vector.yeti Fri May 10 22:48:55 2013 +0100 @@ -0,0 +1,119 @@ + +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'; + +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?', + 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, + 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, +} + + +