diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-09-27 08:12:51 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-09-27 08:12:51 +0200 |
commit | 9c2076f6b79476ef404e3de5ba1cd5c4affe2300 (patch) | |
tree | 83ac63eb380f6d199614c7e3fb0bed9f7355029b /applause.lua | |
parent | fc2ea203d1e920e718f930ae3f678f588f261051 (diff) | |
download | applause2-9c2076f6b79476ef404e3de5ba1cd5c4affe2300.tar.gz |
fixed the minus, div, mod and pow operations for streams
* this has long been broken. ZipStream implicitly assumes an associative
operation when "inlining" nested ZipStreams.
Since the above mentions operations are not associative,
the result was a faulty calculation.
Example: s1 - (s2 - s3) was calculated like (s1 - s2) - s3.
* For the time being, the inlining is avoided by using unique
lambdas.
Diffstat (limited to 'applause.lua')
-rw-r--r-- | applause.lua | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/applause.lua b/applause.lua index ae92e02..2b86280 100644 --- a/applause.lua +++ b/applause.lua @@ -733,12 +733,21 @@ do end do - local function subOp(x1, x2) return x1-x2 end + -- FIXME: Minus is not associative, so we use an unique + -- function for every ZipStream to prevent them being + -- collapsed. E.g. s1 - (s2 - s3) must not result in a single + -- ZipStream. Perhaps there should be a special stream for + -- associative operations, or collapsing should happen in Stream.minus(). + -- It is also possible that the collapsing is slower than having + -- ZipStreams with only two operands since the resulting stream + -- tick arrays cannot be inlined well. + -- + --local function subOp(x1, x2) return x1-x2 end function Stream.minus(v1, v2) return type(v2) == "number" and MapStream:new(v1, function(x) return x-v2 end) or - ZipStream:new(subOp, v1, v2) + ZipStream:new(function(x1, x2) return x1-x2 end, v1, v2) end Stream.__sub = Stream.minus end @@ -757,35 +766,38 @@ do end do - local function divOp(x1, x2) return x1/x2 end + -- FIXME: See above minus() + --local function divOp(x1, x2) return x1/x2 end function Stream.div(v1, v2) return type(v2) == "number" and MapStream:new(v1, function(x) return x/v2 end) or - ZipStream:new(divOp, v1, v2) + ZipStream:new(function(x1, x2) return x1/x2 end, v1, v2) end Stream["\u{00F7}"] = Stream.div -- APL Divide Stream.__div = Stream.div end do - local function modOp(x1, x2) return x1%x2 end + -- FIXME: See above minus() + --local function modOp(x1, x2) return x1%x2 end function Stream.mod(v1, v2) return type(v2) == "number" and MapStream:new(v1, function(x) return x%v2 end) or - ZipStream:new(modOp, v1, v2) + ZipStream:new(function(x1, x2) return x1%x2 end, v1, v2) end Stream.__mod = Stream.mod end do - local function powOp(x1, x2) return x1^x2 end + -- FIXME: See above minus() + --local function powOp(x1, x2) return x1^x2 end function Stream.pow(v1, v2) return type(v2) == "number" and MapStream:new(v1, function(x) return x^v2 end) or - ZipStream:new(powOp, v1, v2) + ZipStream:new(function(x1, x2) return x1^x2 end, v1, v2) end Stream["\u{22C6}"] = Stream.pow -- APL Exponentiation Stream.__pow = Stream.pow @@ -1441,6 +1453,13 @@ end -- ZipStream combines any number of streams into a single -- stream using a function. This is the basis of the "+" -- and "*" operations. +-- +-- NOTE (FIXME?): This "inlines" ZipStream arguments with +-- the same function as an optimization. This ONLY WORKS +-- for associative operations and more than 3 operands are +-- probably slower than two ZipStreams except for very large +-- numbers of ZipStreams (should be benchmarked). +-- In this case, we might just remove that optimization. ZipStream = DeriveClass(MuxableStream) -- We have a leading non-stream argument |