Mea culpa, mea (running) maxima culpa!
This new version of mrs prepends the required zero
to the first running sum rather than to the input
data; it needs a bit less space than Arie's
suggestion.
mrs =: >./@(- <./\)@(0 , +/\) NB. proposed new verb
mrsold =: >./@(- <./\)@(+/\) NB. previous effort
mis =: [: >./ [: (0 >. +)/\. 0 ,~ ] NB. Eugene's
(;mrsold;mrs;mis)"1 (i.6) |."0 1 i.6
+-----------+--+--+--+
|0 1 2 3 4 5|15|15|15|
+-----------+--+--+--+
|1 2 3 4 5 0|14|15|15|
+-----------+--+--+--+
|2 3 4 5 0 1|13|15|15|
+-----------+--+--+--+
|3 4 5 0 1 2|12|15|15|
+-----------+--+--+--+
|4 5 0 1 2 3|11|15|15|
+-----------+--+--+--+
|5 0 1 2 3 4|10|15|15|
+-----------+--+--+--+
The speed improvement (over mis) is still good.
Apologies to the forum and many thanks to Arie.
Mike
Arie Groeneveld wrote:
Hi,
Reading this subject and surfing to
http://ostermiller.org/calc/sum.html,
I did notice that not all the examples given there
come up with the same result using the two verbs
mentioned in the thread started by Mike Day:
mrs =: >./@(- <./\)@(+/\)
mis =: [: >./ [: (0 >. +)/\. 0 ,~ ]
------>
(mrs-:mis) 1 2 3 4 5
0
mrs 1 2 3 4 5
14
mis 1 2 3 4 5
15
The last one should be the answer according to Ostermiller?
This phenomenon seems to occur when all elements are > 0.
I made a kind of adjustment to the verb 'mrs' more or less
based on the rightmost part of the verb 'mis'. In other words
prefixing the sequence with a zero.
mrs2 =: >./@(- <./\)@(+/\)@(0,])
mrs2 2 4 9 12
27
mis 2 4 9 12
27
mrs2 2 _4 9 _12
9
mis 2 _4 9 _12
9
So, when the sequence is an alternating one, then
no difference will be noticed between mrs and mis?
Greetings
@@1<=>arie
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm