2D partitioned cumulative sum

16

1

Challenge

Given a matrix M with r rows and c columns, and two Boolean lists V of length r and H of length c, calculate the partitioned cumulative vertical and horizontal sums.

Rules

  • r and c are greater than or equal to one

  • H and V begin with a true value

  • The values in M are within your language's reasonable numeric domain.

  • Partitioning and summing begins in the top left corner.

Walk through

Given M:

┌──────────────┐
│ 1  2  3  4  5│
│ 6  7  8  9 10│
│11 12 13 14 15│
│16 17 18 19 20│
└──────────────┘

H: 1 0 1 0 0

V: 1 1 0 1

Split M into groups of columns, beginning a new group at every true value of H

┌─────┬────────┐
│ 1  2│ 3  4  5│
│ 6  7│ 8  9 10│
│11 12│13 14 15│
│16 17│18 19 20│
└─────┴────────┘

Split each group of columns into groups of rows, beginning a new group at every true value of V:

┌─────┬────────┐
│ 1  2│ 3  4  5│
├─────┼────────┤
│ 6  7│ 8  9 10│
│11 12│13 14 15│
├─────┼────────┤
│16 17│18 19 20│
└─────┴────────┘

Cumulatively sum each cell horizontally:

┌─────┬────────┐
│ 1  3│ 3  7 12│
├─────┼────────┤
│ 6 13│ 8 17 27│
│11 23│13 27 42│
├─────┼────────┤
│16 33│18 37 57│
└─────┴────────┘

Cumulatively sum each cell vertically:

┌─────┬────────┐
│ 1  3│ 3  7 12│
├─────┼────────┤
│ 6 13│ 8 17 27│
│17 36│21 44 69│
├─────┼────────┤
│16 33│18 37 57│
└─────┴────────┘

Result:

┌──────────────┐
│ 1  3  3  7 12│
│ 6 13  8 17 27│
│17 36 21 44 69│
│16 33 18 37 57│
└──────────────┘

Additional test cases

M:

┌───────────┐
│15 11 11 17│
│13 20 18  8│
└───────────┘

H: 1 0 0 1V: 1 0

Result:

┌───────────┐
│15 26 37 17│
│28 59 88 25│
└───────────┘

M:

┌─┐
│7│
└─┘

Result (H and V must be 1):

┌─┐
│7│
└─┘

M:

┌──┐
│ 3│
│-1│
│ 4│
└──┘

V: 1 1 0 (H must be 1)

Result:

┌──┐
│ 3│
│-1│
│ 3│
└──┘

M:

┌───────────────────────────────────────────────────────┐
│10    7.7 1.9 1.5 5.4  1.2 7.8 0.6 4.3 1.2  4.5 5.4 0.3│
│ 2.3  3.8 4.1 4.5 1    7.7 3   3.4 6.9 5.8  9.5 1.3 7.5│
│ 9.1  3.7 7.2 9.8 3.9 10   7.6 9.6 7.3 6.2  3.3 9.2 9.4│
│ 4.3  4.9 7.6 2   1.4  5.8 8.1 2.4 1.1 2.3  7.3 3.6 6  │
│ 9.3 10   5.8 9.6 5.7  8.1 2.1 3.9 4   1.3  6.3 3.1 9  │
│ 6.6  1.4 0.5 6.5 4.6  2.1 7.5 4.3 9   7.2  2.8 3.6 4.6│
│ 1.7  9.9 2.4 4.5 1.3  2.6 6.4 7.8 6.2 3.2 10   5.2 8.9│
│ 9.9  5.3 4.5 6.3 1.4  3.1 2.3 7.9 7.8 7.9  9.6 4   5.8│
└───────────────────────────────────────────────────────┘

H: 1 0 0 1 0 1 1 1 0 1 1 1 0

V: 1 0 0 0 0 1 0 0

Result:

┌────────────────────────────────────────────────────────────────┐
│10   17.7 19.6  1.5  6.9  1.2  7.8  0.6  4.9  1.2  4.5  5.4  5.7│
│12.3 23.8 29.8  6   12.4  8.9 10.8  4   15.2  7   14    6.7 14.5│
│21.4 36.6 49.8 15.8 26.1 18.9 18.4 13.6 32.1 13.2 17.3 15.9 33.1│
│25.7 45.8 66.6 17.8 29.5 24.7 26.5 16   35.6 15.5 24.6 19.5 42.7│
│35   65.1 91.7 27.4 44.8 32.8 28.6 19.9 43.5 16.8 30.9 22.6 54.8│
│ 6.6  8    8.5  6.5 11.1  2.1  7.5  4.3 13.3  7.2  2.8  3.6  8.2│
│ 8.3 19.6 22.5 11   16.9  4.7 13.9 12.1 27.3 10.4 12.8  8.8 22.3│
│18.2 34.8 42.2 17.3 24.6  7.8 16.2 20   43   18.3 22.4 12.8 32.1│
└────────────────────────────────────────────────────────────────┘

Adám

Posted 2017-07-26T23:07:11.837

Reputation: 37 779

Answers

9

Jelly, 10 bytes

Zœṗ@+\€Ẏð/

Try it online! and The Last Test Case (With a G at the end for readability).

Input is taken as a list [M, H, V].

Explanation

Zœṗ@+\€Ẏð/  Input: [M, H, V]
        ð/  Insert the previous (f) as a dyadic link
            Forms f( f(M, H) , V)
            For f(x, y):
Z             Transpose x
 œṗ@          Partition the rows of x^T at each true in y
    +\€       Compute the cumulative sums in each partition
       Ẏ      Tighten (Joins all the lists at the next depth)

miles

Posted 2017-07-26T23:07:11.837

Reputation: 15 654

You can use a footer like this so that you don't have to tamper with your actual code.

– Erik the Outgolfer – 2017-07-27T13:14:54.807

7

APL (Dyalog), 13 bytes

Takes ist of V H M as argument.

{⍉⊃,/+\¨⍺⊂⍵}/

Try it online!

{}/ insert (reduce by) the following anonymous function, where the term in the left is represented by ⍺ and the term on the right is represented by ⍵. Due to APL functions being right associative, this is therefore V f (H f M).

⍺⊂⍵ partition ⍵ according to ⍺

+\¨ cumulative sum of each part

,/ reduce by concatenation (this encloses the result to reduce rank)

 disclose

 transpose

Adám

Posted 2017-07-26T23:07:11.837

Reputation: 37 779

6

Python 2 + numpy, 143 138 117 115 110 108 bytes

-21 bytes thanks to Adám!

lambda M,*L:reduce(lambda m,l:vstack(map(lambda p:cumsum(p,0),split(m,*where(l)))).T,L,M)
from numpy import*

Try it online!

notjagan

Posted 2017-07-26T23:07:11.837

Reputation: 4 011

1ask for partition, split, and cumsum once, transpose, repeat. – Adám – 2017-07-27T00:22:16.977

@Adám Thanks, I didn't think of that for some reason. – notjagan – 2017-07-27T00:29:54.500

I liked the list lookup of two functions anyway :) – Jonathan Allan – 2017-07-27T00:30:16.763

2Please make header "Python 3 + numpy" – Leaky Nun – 2017-07-27T04:26:30.627

5

Jelly,  15  14 bytes

œṗ+\€Ẏ
ḢçЀZð⁺

A dyadic link taking H,V on the left and M on the right and returning the resulting matrix.

Try it online!

Alternatively as a single line also for 14: Ḣœṗ+\€Ẏ$¥Ð€Zð⁺

How?

œṗ+\€Ẏ - Link 1: partition and cumSum: list of partition bools, list of values
œṗ     - partition (the values) at truthy indexes (of the bools)
    €  - for €ach part:
  +\   -   cumulative reduce by addition
     Ẏ - tighten (flattens back into a list)

ḢçЀZð⁺ - Main link: list of lists, [H,V]; list of lists, M
      ⁺ - perform this twice:
     ð  - [it's a dyadic chain for the second pass, first pass is dyadic implicitly]
Ḣ       -   head, pop it & modify (so H the first time, V the second)
  Ѐ    -   map across right: (M the first time, the intermediate result the second)
 ç      -     the last link (1) as a dyad
    Z   -   transpose the result (do the rows first time, and the columns the second)

Previous:

œṗ@+\€Ẏ
ç€Zç€⁵Z

A full program printing a representation of the result.

Jonathan Allan

Posted 2017-07-26T23:07:11.837

Reputation: 67 804

Whoa -50% from previous Jelly answer! – Adám – 2017-07-26T23:39:46.187

Whoa what? Wow. I really need to study how you did this... Incredible compared to mine! – HyperNeutrino – 2017-07-26T23:40:46.660

Oh this is doing the two directions separately, right? Smart. – HyperNeutrino – 2017-07-26T23:41:11.560

I think it's doing roughly the same thing... – Jonathan Allan – 2017-07-26T23:41:34.580

Good method. Means I can beat this with APL. I've got 14 bytes. – Adám – 2017-07-26T23:50:28.413

@Adám ...I'm almost certain there is a shorter way by taking H,V as one argument ad using reduce, but I can't quite work out how. – Jonathan Allan – 2017-07-26T23:54:52.523

I am amazed that Jelly is so golfable - first 31 then 14 and now a 10... – Jerry Jeremiah – 2017-07-27T03:55:52.383

@JerryJeremiah I thought a reduce would be available for less, but did not think to start with all 3 inputs in 1 list - that was key. – Jonathan Allan – 2017-07-27T11:45:22.620

4

MATL, 19 bytes

,!ix"0GYs@12XQ!g]v!

Inputs are M (matrix), H (column vector), V (column vector). The row separator is ;.

Try it online! Or verify all test cases: 1, 2, 3, 4, 5.

Explanation

This does the cumulative sum horizontally, then vertically.

,          % Do the following twice
  !        %   First time this inputs M implicitly. Transpose. Second time
           %   it transposes the result of the horizontal cumulative sum
  ix       %   Input H (first time) or V (second time). Delete it; but gets
           %   copied into clipboard G
  "        %   For each column of the matrix
    0G     %     Push most recent input: H (first time) or V (second)
    Ys     %     Cumulative sum. This produces a vector of integer values
           %     such that all columns (first time) or rows (second) of M 
           %     with the same value in this vector should be cumulatively
           %     summed
    @      %     Push current column of M transposed (first time) or M after
           %     horizontal cumulative sum (second time)
    12XQ   %     Cumulative sum. Gives a cell array of row vectors
    !g     %     Join those vectors into one row vector
  ]        %   End
  v        %   Concatenate the row vectors vertically into a matrix
  !        %   Transpose. This corrects for the fact that each column vector
           %   of the matrix was cumulatively summed into a row vector
           % Implicit end. Implicit display

Luis Mendo

Posted 2017-07-26T23:07:11.837

Reputation: 87 464

1Most impressive. I guess Matlab was kinda made for stuff like this. – Adám – 2017-07-26T23:29:09.063

@Adám I'm sure APL's length won't be very different :-) – Luis Mendo – 2017-07-26T23:33:26.243

My reference implementation used to generate the test cases is 26 bytes. – Adám – 2017-07-26T23:35:00.750

@Adám Darn! APL beating Jelly? This is unacceptable! (must golf my solution... lol) xD – HyperNeutrino – 2017-07-26T23:37:10.627

@HyperNeutrino Well, Jelly doesn't have both rank and depth like APL and J have. – Adám – 2017-07-26T23:37:56.017

@Adám Yeah... Jelly wasn't a great choice. I seems to me that Matrix Laboratories (MATLAB if I remember correctly?) might be a better choice for matrices :))) – HyperNeutrino – 2017-07-26T23:38:35.480

@HyperNeutrino Heh, I should have gone full 3D or nD. – Adám – 2017-07-26T23:39:21.597

@Adám 3D could make a nice challenge (in the not-too-near future). The problem may be how to represent the ouput – Luis Mendo – 2017-07-26T23:40:34.407

@LuisMendo Easy; stack of matrices. Or with box drawing.

– Adám – 2017-07-26T23:42:33.870

3

J, 20 bytes

;@(<@(+/\);.1|:)&.>/

Try it online!

Input is taken as an array of boxes containing [V, H, M].

Explanation

;@(<@(+/\);.1|:)&.>/  Input: [V H M]
  (     g      )   /  Insert g and reduce (right-to-left)
                      Forms V g H g M = V g (H g M)
                & >     Unbox each
             |:         Transpose the right arg
          ;.1           Partition
      +/\               Reduce each prefix using addition (cumulative sum)
   <@                   Box each partition
;@                      Raze (Concatenate the contents in each box)
                &.>     Box the result

miles

Posted 2017-07-26T23:07:11.837

Reputation: 15 654

2

Mathematica, 212 bytes

(T=Transpose;A=AppendTo;J=Flatten;f[s_]:=Block[{},t=2;r=1;w={};While[t<=Length@s,If[s[[t]]==0,r++,w~A~r;r=1];t++];w~A~r];K[x_,y_]:=Accumulate/@#&/@(FoldPairList[TakeDrop,#,f@y]&/@x);d=J/@K[#,#2];T[J/@K[T@d,#3]])&


input
[M,H,V]

[{{15, 11, 11, 17}, {13, 20, 18, 8}}, {1, 0, 0, 1}, {1, 0}]

J42161217

Posted 2017-07-26T23:07:11.837

Reputation: 15 931

2

C# (.NET Core), 164 bytes

(M,H,V)=>{int a=M.Length,b=M[0].Length,i,j;for(i=0;i<a;i++)for(j=0;j<b;j++)if(!H[j])M[i][j]+=M[i][j-1];for(i=0;i<a;i++)for(j=0;j<b;j++)if(!V[i])M[i][j]+=M[i-1][j];}

Try it online!

Basically it does exactly as specified in the OP. It first iterates to sum horizontally and then it iterates again to sum vertically.

Charlie

Posted 2017-07-26T23:07:11.837

Reputation: 11 448

2

Haskell, 129 bytes 119 bytes

s m v=tail$scanl(\a(x,s)->if s then x else zipWith(+)a x)[](zip m v)
t=foldr(zipWith(:))$repeat[]
f m h v=t$s(t$s m v)h

Try it online!

Saved 10 bytes thanks to @ceasedtoturncounterclockwis

t (for transpose) switches rows and columns. A quick explanation:

foldr(zipWith(:))(repeat[])(r1,...,rn) =
zipWith(:) r1 (zipWith(:) r2 (... zipWith(:) rn (repeat [])))

Read from right to left: we browse rows from bottom to up, and push each value in its destination column.

s is basically a rolling sum of vectors, but resets when a True value arises in v

f sums the rows with s following v and do the same with the columns following h

jferard

Posted 2017-07-26T23:07:11.837

Reputation: 1 764

t=foldr(zipWith(:))(repeat[]). Not only shorter, also much less inefficient. – ceased to turn counterclockwis – 2017-07-27T11:00:21.460

@ceasedtoturncounterclockwis Thanks for the tip. – jferard – 2017-07-27T12:49:32.077

1

JavaScript (ES6), 88 bytes

(a,h,v)=>a.map(b=>b.map((e,i)=>t=h[i]?e:t+e)).map((b,j)=>t=v[j]?b:t.map((e,i)=>e+b[i]))

Neil

Posted 2017-07-26T23:07:11.837

Reputation: 95 035

0

Jelly, 31 bytes

+\€€
œṗḊZ€⁵œṗ$€Ḋ€Ç€ÇZ€€Z€;/€€;/

Try it online!

Gah this is way too long for Jelly xD

BTW, 11/31 bytes in this program consists of euro characters. That's over a third of the program!

HyperNeutrino

Posted 2017-07-26T23:07:11.837

Reputation: 26 575

Too many Euros. – Adám – 2017-07-26T23:31:14.443

@Adám My thoughts exactly :P Working with doubly partitioned matrices isn't as fun as I thought it would be, because I'm doing second-level to third-level mapping xD – HyperNeutrino – 2017-07-26T23:32:32.543

Why are you wasting your money like this €-€ – V. Courtois – 2017-07-27T07:53:43.010