Hi!
I defined the following simple verb to calculate the maximum
subsequence sum
(http://www.cs.uwyo.edu/~paulmax/cosc3020/lab3/lab3.html):
mcs=:>./@:(-<./\)@:(0:,+/\)
in=:0 4 1 _2 1 _5 4 _3 5 _2 _1 2 6 _2 1 _1
mcs in
11
and it works just fine even with rather big in-memory vectors:
in2=:5e3-?3e7$1e4
mcs in2
13618177
But if you try to use it on big memory mapped binary file:
out=: 4 : 0
for. i.x do. (2 (3!:4) 5e3-?1e7$1e4) 1!:3 < y end.
)
10 out 'C:\WORK\random.bin' NB. Created 381 Mb file with 1e8 random integers
require 'jmf'
JINT map_jmf_ 'f';'c:\work\random.bin'
mcs f
|out of memory: mcs
| mcs f
which does make perfect sense.
But in reality mcs may be implemented as an ordinary accumulator (to
calculate mcs in one pass) and this implementation should work in J's
JMF since in that case we don't use memory for intermediate results.
Like the sum does:
+/ f
70956345
The problem is that I don't know how to implement appropriate
accumulator for this task.
I imagine it should be something similar to:
mcs2=: 4 : 0
'sum min max'=.y
nmax=.max>.nsum-nmin ] nmin=.min<.nsum ] nsum=.sum+x
nsum,nmin,nmax
)
{: > mcs2&.>/ (<0 0 0),~<"0 in
11
but this variant is 1. extremely slow and 2. cannot be used with JMF vector.
So finally the question: is there any way in J to calculate mcs on
JINT file of 381 Mb?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm