Stagger, stack, sum

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












Inspired by this Stack Overflow question.



The challenge



Input



An array of square matrices containing non-negative integers.



Output



A square matrix built from the input matrices as follows.



Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.



For clarity, consider the following example input matrices ($N=2$, $P=3$):



 3 5
4 10

6 8
12 11

2 0
9 1


  1. Start with the first input matrix.

  2. Shift the second input matrix N−1 steps down and N−1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.


  3. Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with 0 to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is



     3 5 0
    4 16 8
    0 12 11



  4. For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives



     3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1


  5. The ouput is the $((N−1)P+1)times((N−1)P+1)$ matrix obtained after including the last input matrix.


Additional rules and clarifications




  • $N$ and $P$ are positive integers.

  • You can optionally take $N$ and $P$ as additional inputs.

  • Input and output can be taken by any reasonable means. Their format is flexible as usual.

  • Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.

  • Shortest code in bytes wins.

Test cases:



In each case, input matrices are shown first, then the output.




  1. $N=2$, $P=3$:



     3 5
    4 10

    6 8
    12 11

    2 0
    9 1

    3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1



  2. $N=2$, $P=1$:



     3 5
    4 10

    3 5
    4 10



  3. $N=1$, $P=4$:



     4

    7

    23

    5

    39



  4. $N=3$, $P=2$:



    11 11 8
    6 8 12
    11 0 4

    4 1 13
    9 19 11
    13 4 2

    11 11 8 0 0
    6 8 12 0 0
    11 0 8 1 13
    0 0 9 19 11
    0 0 13 4 2



  5. $N=2$, $P=4$:



    14 13
    10 0

    13 20
    21 3

    9 22
    0 8

    17 3
    19 16

    14 13 0 0 0
    10 13 20 0 0
    0 21 12 22 0
    0 0 0 25 3
    0 0 0 19 16










share|improve this question























  • How long is your MATL solution for this?
    – Giuseppe
    1 hour ago











  • @Arnauld Thanks! Corrected
    – Luis Mendo
    30 mins ago










  • @Giuseppe I haven’t tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
    – Luis Mendo
    28 mins ago















up vote
7
down vote

favorite












Inspired by this Stack Overflow question.



The challenge



Input



An array of square matrices containing non-negative integers.



Output



A square matrix built from the input matrices as follows.



Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.



For clarity, consider the following example input matrices ($N=2$, $P=3$):



 3 5
4 10

6 8
12 11

2 0
9 1


  1. Start with the first input matrix.

  2. Shift the second input matrix N−1 steps down and N−1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.


  3. Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with 0 to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is



     3 5 0
    4 16 8
    0 12 11



  4. For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives



     3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1


  5. The ouput is the $((N−1)P+1)times((N−1)P+1)$ matrix obtained after including the last input matrix.


Additional rules and clarifications




  • $N$ and $P$ are positive integers.

  • You can optionally take $N$ and $P$ as additional inputs.

  • Input and output can be taken by any reasonable means. Their format is flexible as usual.

  • Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.

  • Shortest code in bytes wins.

Test cases:



In each case, input matrices are shown first, then the output.




  1. $N=2$, $P=3$:



     3 5
    4 10

    6 8
    12 11

    2 0
    9 1

    3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1



  2. $N=2$, $P=1$:



     3 5
    4 10

    3 5
    4 10



  3. $N=1$, $P=4$:



     4

    7

    23

    5

    39



  4. $N=3$, $P=2$:



    11 11 8
    6 8 12
    11 0 4

    4 1 13
    9 19 11
    13 4 2

    11 11 8 0 0
    6 8 12 0 0
    11 0 8 1 13
    0 0 9 19 11
    0 0 13 4 2



  5. $N=2$, $P=4$:



    14 13
    10 0

    13 20
    21 3

    9 22
    0 8

    17 3
    19 16

    14 13 0 0 0
    10 13 20 0 0
    0 21 12 22 0
    0 0 0 25 3
    0 0 0 19 16










share|improve this question























  • How long is your MATL solution for this?
    – Giuseppe
    1 hour ago











  • @Arnauld Thanks! Corrected
    – Luis Mendo
    30 mins ago










  • @Giuseppe I haven’t tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
    – Luis Mendo
    28 mins ago













up vote
7
down vote

favorite









up vote
7
down vote

favorite











Inspired by this Stack Overflow question.



The challenge



Input



An array of square matrices containing non-negative integers.



Output



A square matrix built from the input matrices as follows.



Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.



For clarity, consider the following example input matrices ($N=2$, $P=3$):



 3 5
4 10

6 8
12 11

2 0
9 1


  1. Start with the first input matrix.

  2. Shift the second input matrix N−1 steps down and N−1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.


  3. Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with 0 to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is



     3 5 0
    4 16 8
    0 12 11



  4. For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives



     3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1


  5. The ouput is the $((N−1)P+1)times((N−1)P+1)$ matrix obtained after including the last input matrix.


Additional rules and clarifications




  • $N$ and $P$ are positive integers.

  • You can optionally take $N$ and $P$ as additional inputs.

  • Input and output can be taken by any reasonable means. Their format is flexible as usual.

  • Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.

  • Shortest code in bytes wins.

Test cases:



In each case, input matrices are shown first, then the output.




  1. $N=2$, $P=3$:



     3 5
    4 10

    6 8
    12 11

    2 0
    9 1

    3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1



  2. $N=2$, $P=1$:



     3 5
    4 10

    3 5
    4 10



  3. $N=1$, $P=4$:



     4

    7

    23

    5

    39



  4. $N=3$, $P=2$:



    11 11 8
    6 8 12
    11 0 4

    4 1 13
    9 19 11
    13 4 2

    11 11 8 0 0
    6 8 12 0 0
    11 0 8 1 13
    0 0 9 19 11
    0 0 13 4 2



  5. $N=2$, $P=4$:



    14 13
    10 0

    13 20
    21 3

    9 22
    0 8

    17 3
    19 16

    14 13 0 0 0
    10 13 20 0 0
    0 21 12 22 0
    0 0 0 25 3
    0 0 0 19 16










share|improve this question















Inspired by this Stack Overflow question.



The challenge



Input



An array of square matrices containing non-negative integers.



Output



A square matrix built from the input matrices as follows.



Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.



For clarity, consider the following example input matrices ($N=2$, $P=3$):



 3 5
4 10

6 8
12 11

2 0
9 1


  1. Start with the first input matrix.

  2. Shift the second input matrix N−1 steps down and N−1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.


  3. Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with 0 to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is



     3 5 0
    4 16 8
    0 12 11



  4. For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives



     3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1


  5. The ouput is the $((N−1)P+1)times((N−1)P+1)$ matrix obtained after including the last input matrix.


Additional rules and clarifications




  • $N$ and $P$ are positive integers.

  • You can optionally take $N$ and $P$ as additional inputs.

  • Input and output can be taken by any reasonable means. Their format is flexible as usual.

  • Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.

  • Shortest code in bytes wins.

Test cases:



In each case, input matrices are shown first, then the output.




  1. $N=2$, $P=3$:



     3 5
    4 10

    6 8
    12 11

    2 0
    9 1

    3 5 0 0
    4 16 8 0
    0 12 13 0
    0 0 9 1



  2. $N=2$, $P=1$:



     3 5
    4 10

    3 5
    4 10



  3. $N=1$, $P=4$:



     4

    7

    23

    5

    39



  4. $N=3$, $P=2$:



    11 11 8
    6 8 12
    11 0 4

    4 1 13
    9 19 11
    13 4 2

    11 11 8 0 0
    6 8 12 0 0
    11 0 8 1 13
    0 0 9 19 11
    0 0 13 4 2



  5. $N=2$, $P=4$:



    14 13
    10 0

    13 20
    21 3

    9 22
    0 8

    17 3
    19 16

    14 13 0 0 0
    10 13 20 0 0
    0 21 12 22 0
    0 0 0 25 3
    0 0 0 19 16







code-golf number array-manipulation integer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 30 mins ago

























asked 1 hour ago









Luis Mendo

72.9k885284




72.9k885284











  • How long is your MATL solution for this?
    – Giuseppe
    1 hour ago











  • @Arnauld Thanks! Corrected
    – Luis Mendo
    30 mins ago










  • @Giuseppe I haven’t tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
    – Luis Mendo
    28 mins ago

















  • How long is your MATL solution for this?
    – Giuseppe
    1 hour ago











  • @Arnauld Thanks! Corrected
    – Luis Mendo
    30 mins ago










  • @Giuseppe I haven’t tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
    – Luis Mendo
    28 mins ago
















How long is your MATL solution for this?
– Giuseppe
1 hour ago





How long is your MATL solution for this?
– Giuseppe
1 hour ago













@Arnauld Thanks! Corrected
– Luis Mendo
30 mins ago




@Arnauld Thanks! Corrected
– Luis Mendo
30 mins ago












@Giuseppe I haven’t tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
– Luis Mendo
28 mins ago





@Giuseppe I haven’t tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
– Luis Mendo
28 mins ago











4 Answers
4






active

oldest

votes

















up vote
2
down vote














R, 88 81 bytes





function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o


Try it online!



Takes a list of matrices, A, N, and P.



Builds the requisite matrix of zeros o and adds elementwise the contents of A to the appropriate submatrices in o.






share|improve this answer





























    up vote
    1
    down vote













    JavaScript (ES6), 104 bytes



    Takes input as (n,p,a).





    (n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))


    Try it online!



    How?



    Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:



    $$w=(n-1)times(p-1)+n$$



    For each cell at $(x,y)$, we compute:



    $$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$



    where undefined cells are replaced with zeros.






    share|improve this answer





























      up vote
      0
      down vote














      Python 2, 124 bytes





      def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a


      Try it online!






      share|improve this answer



























        up vote
        0
        down vote














        Jelly, 20 bytes



        µ;€0Zð⁺0ṁ+ṚU$}ṚU+⁸µ/


        Try it online!



        Bah, Jelly has an attitude today...





        share




















          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "200"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173445%2fstagger-stack-sum%23new-answer', 'question_page');

          );

          Post as a guest






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote














          R, 88 81 bytes





          function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o


          Try it online!



          Takes a list of matrices, A, N, and P.



          Builds the requisite matrix of zeros o and adds elementwise the contents of A to the appropriate submatrices in o.






          share|improve this answer


























            up vote
            2
            down vote














            R, 88 81 bytes





            function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o


            Try it online!



            Takes a list of matrices, A, N, and P.



            Builds the requisite matrix of zeros o and adds elementwise the contents of A to the appropriate submatrices in o.






            share|improve this answer
























              up vote
              2
              down vote










              up vote
              2
              down vote










              R, 88 81 bytes





              function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o


              Try it online!



              Takes a list of matrices, A, N, and P.



              Builds the requisite matrix of zeros o and adds elementwise the contents of A to the appropriate submatrices in o.






              share|improve this answer















              R, 88 81 bytes





              function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o


              Try it online!



              Takes a list of matrices, A, N, and P.



              Builds the requisite matrix of zeros o and adds elementwise the contents of A to the appropriate submatrices in o.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 min ago

























              answered 33 mins ago









              Giuseppe

              15.1k31051




              15.1k31051




















                  up vote
                  1
                  down vote













                  JavaScript (ES6), 104 bytes



                  Takes input as (n,p,a).





                  (n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))


                  Try it online!



                  How?



                  Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:



                  $$w=(n-1)times(p-1)+n$$



                  For each cell at $(x,y)$, we compute:



                  $$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$



                  where undefined cells are replaced with zeros.






                  share|improve this answer


























                    up vote
                    1
                    down vote













                    JavaScript (ES6), 104 bytes



                    Takes input as (n,p,a).





                    (n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))


                    Try it online!



                    How?



                    Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:



                    $$w=(n-1)times(p-1)+n$$



                    For each cell at $(x,y)$, we compute:



                    $$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$



                    where undefined cells are replaced with zeros.






                    share|improve this answer
























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      JavaScript (ES6), 104 bytes



                      Takes input as (n,p,a).





                      (n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))


                      Try it online!



                      How?



                      Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:



                      $$w=(n-1)times(p-1)+n$$



                      For each cell at $(x,y)$, we compute:



                      $$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$



                      where undefined cells are replaced with zeros.






                      share|improve this answer














                      JavaScript (ES6), 104 bytes



                      Takes input as (n,p,a).





                      (n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))


                      Try it online!



                      How?



                      Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:



                      $$w=(n-1)times(p-1)+n$$



                      For each cell at $(x,y)$, we compute:



                      $$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$



                      where undefined cells are replaced with zeros.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 16 mins ago

























                      answered 42 mins ago









                      Arnauld

                      65.9k583278




                      65.9k583278




















                          up vote
                          0
                          down vote














                          Python 2, 124 bytes





                          def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a


                          Try it online!






                          share|improve this answer
























                            up vote
                            0
                            down vote














                            Python 2, 124 bytes





                            def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a


                            Try it online!






                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote










                              Python 2, 124 bytes





                              def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a


                              Try it online!






                              share|improve this answer













                              Python 2, 124 bytes





                              def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a


                              Try it online!







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 12 mins ago









                              ovs

                              17.5k21058




                              17.5k21058




















                                  up vote
                                  0
                                  down vote














                                  Jelly, 20 bytes



                                  µ;€0Zð⁺0ṁ+ṚU$}ṚU+⁸µ/


                                  Try it online!



                                  Bah, Jelly has an attitude today...





                                  share
























                                    up vote
                                    0
                                    down vote














                                    Jelly, 20 bytes



                                    µ;€0Zð⁺0ṁ+ṚU$}ṚU+⁸µ/


                                    Try it online!



                                    Bah, Jelly has an attitude today...





                                    share






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote










                                      Jelly, 20 bytes



                                      µ;€0Zð⁺0ṁ+ṚU$}ṚU+⁸µ/


                                      Try it online!



                                      Bah, Jelly has an attitude today...





                                      share













                                      Jelly, 20 bytes



                                      µ;€0Zð⁺0ṁ+ṚU$}ṚU+⁸µ/


                                      Try it online!



                                      Bah, Jelly has an attitude today...






                                      share











                                      share


                                      share










                                      answered 1 min ago









                                      Erik the Outgolfer

                                      29.6k42899




                                      29.6k42899



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173445%2fstagger-stack-sum%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Comments

                                          Popular posts from this blog

                                          Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                          Confectionery