6 added 124 characters in body
source | link

Dyalog APL, 101 99 64 6262 59 bytes

3 bytes saved by @Adám

{+/¨+/∘,¨⍵∘רd∘=¨k[⍋k←↑∪/∪/,d←∘.{((+/1x×××⌊/∘|)⍺⍵},)⍨(⍳x)-⌈.5×x←≢⍵]}

Try it online!Try it online!

Using Dennis' awesome algorithm.

Dyalog APL, 101 99 64 62 bytes

{+/¨+/¨⍵∘רd∘=¨k[⍋k←↑∪/∪/d←∘.{(+/1x×××⌊/∘|)⍺⍵}⍨(⍳x)-⌈.5×x←≢⍵]}

Try it online!

Using Dennis' awesome algorithm.

Dyalog APL, 101 99 64 62 59 bytes

3 bytes saved by @Adám

{+/∘,¨⍵∘רd∘=¨k[⍋k←↑∪/,d←∘.((+/1x×××⌊/∘|),)⍨(⍳x)-⌈.5×x←≢⍵]}

Try it online!

Using Dennis' awesome algorithm.

5 deleted 2316 characters in body
source | link

Dyalog APL, 101 9999 64 62 bytes

{,/↑,+/,¨+/¨(⍉2↑o)(+¨⍵∘רd∘=¨k[⍋k←↑∪/+/⍵×∘d←∘.{⍺ ⍵∊⍨⌈x}⍨⍳≢⍵)(⊖⍉2↓o←++/¨+1x×××⌊/¨⍵∘ר(0 3 1 2∘|)∘.{⌽∘⍉⍣⍺⊢⍵⍺⍵}(⍳⌊x←.5×≢⍵⍳x)∘.=⊂∘-⌈.⌈⍨⍳≢⍵)5×x←≢⍵]}

Try it online!Try it online!

How? Using Dennis' awesome algorithm.

      m                ⍝ the matrix
1 2 3 2 1
0 3 2 3 0
4 2 5 6 3
7 4 7 9 4
0 6 7 2 5
      {⍳≢⍵} m         ⍝ range of length
1 2 3 4 5
      {⊂∘.⌈⍨⍳≢⍵} m    ⍝ outer product with maximum
┌─────────┐
│1 2 3 4 5│
│2 2 3 4 5│
│3 3 3 4 5│
│4 4 4 4 5│
│5 5 5 5 5│
└─────────┘
      {(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ check equality for n, for every n to the size of m / 2
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
└─────────┴─────────┘
      {(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ rotate right n times for every n on the left
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│1 0 0 0 0│0 1 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 1 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 1│0 0 0 1 0│
└─────────┴─────────┘
      {⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ apply masks to original matrix
┌─────────┬─────────┐
│1 0 0 0 0│0 2 0 0 0│
│0 0 0 0 0│0 3 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│7 4 0 0 0│
│0 0 0 0 0│0 6 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 2 0│
│0 0 0 0 0│0 0 0 3 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 9 4│
│0 0 0 0 5│0 0 0 2 0│
└─────────┴─────────┘
      o ← {+/¨+/¨⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m ⋄ o
      ⍝ sum of each matrix
1  5
0 17
1  5
5 15
      ⊖⍉2↓o   ⍝ right side of the array
5 15
1  5
      {+/+/⍵×∘.{⍺ ⍵∊⍨⌈.5×≢⍵}⍨⍳≢⍵} m   ⍝ middle element of the array
20
      ⍉2↑o    ⍝ left side of the array
1  0
5 17

Dyalog APL, 101 99 bytes

{,/↑,/,/¨(⍉2↑o)(+/+/⍵×∘.{⍺ ⍵∊⍨⌈x}⍨⍳≢⍵)(⊖⍉2↓o←+/¨+/¨⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵)}

Try it online!

How?

      m                ⍝ the matrix
1 2 3 2 1
0 3 2 3 0
4 2 5 6 3
7 4 7 9 4
0 6 7 2 5
      {⍳≢⍵} m         ⍝ range of length
1 2 3 4 5
      {⊂∘.⌈⍨⍳≢⍵} m    ⍝ outer product with maximum
┌─────────┐
│1 2 3 4 5│
│2 2 3 4 5│
│3 3 3 4 5│
│4 4 4 4 5│
│5 5 5 5 5│
└─────────┘
      {(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ check equality for n, for every n to the size of m / 2
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
└─────────┴─────────┘
      {(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ rotate right n times for every n on the left
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│1 0 0 0 0│0 1 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 1 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 1│0 0 0 1 0│
└─────────┴─────────┘
      {⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ apply masks to original matrix
┌─────────┬─────────┐
│1 0 0 0 0│0 2 0 0 0│
│0 0 0 0 0│0 3 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│7 4 0 0 0│
│0 0 0 0 0│0 6 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 2 0│
│0 0 0 0 0│0 0 0 3 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 9 4│
│0 0 0 0 5│0 0 0 2 0│
└─────────┴─────────┘
      o ← {+/¨+/¨⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m ⋄ o
      ⍝ sum of each matrix
1  5
0 17
1  5
5 15
      ⊖⍉2↓o   ⍝ right side of the array
5 15
1  5
      {+/+/⍵×∘.{⍺ ⍵∊⍨⌈.5×≢⍵}⍨⍳≢⍵} m   ⍝ middle element of the array
20
      ⍉2↑o    ⍝ left side of the array
1  0
5 17

Dyalog APL, 101 99 64 62 bytes

{+/¨+/¨⍵∘רd∘=¨k[⍋k←↑∪//d←∘.{(+/1x×××⌊/∘|)⍺⍵}(⍳x)-⌈.5×x←≢⍵]}

Try it online!

Using Dennis' awesome algorithm.

4 added 2589 characters in body
source | link

How?

      m                ⍝ the matrix
1 2 3 2 1
0 3 2 3 0
4 2 5 6 3
7 4 7 9 4
0 6 7 2 5
      {⍳≢⍵} m         ⍝ range of length
1 2 3 4 5
      {⊂∘.⌈⍨⍳≢⍵} m    ⍝ outer product with maximum
┌─────────┐
│1 2 3 4 5│
│2 2 3 4 5│
│3 3 3 4 5│
│4 4 4 4 5│
│5 5 5 5 5│
└─────────┘
      {(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ check equality for n, for every n to the size of m / 2
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
└─────────┴─────────┘
      {(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ rotate right n times for every n on the left
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│1 0 0 0 0│0 1 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 1 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 1│0 0 0 1 0│
└─────────┴─────────┘
      {⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ apply masks to original matrix
┌─────────┬─────────┐
│1 0 0 0 0│0 2 0 0 0│
│0 0 0 0 0│0 3 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│7 4 0 0 0│
│0 0 0 0 0│0 6 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 2 0│
│0 0 0 0 0│0 0 0 3 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 9 4│
│0 0 0 0 5│0 0 0 2 0│
└─────────┴─────────┘
      o ← {+/¨+/¨⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m ⋄ o
      ⍝ sum of each matrix
1  5
0 17
1  5
5 15
      ⊖⍉2↓o   ⍝ right side of the array
5 15
1  5
      {+/+/⍵×∘.{⍺ ⍵∊⍨⌈.5×≢⍵}⍨⍳≢⍵} m   ⍝ middle element of the array
20
      ⍉2↑o    ⍝ left side of the array
1  0
5 17

How?

      m                ⍝ the matrix
1 2 3 2 1
0 3 2 3 0
4 2 5 6 3
7 4 7 9 4
0 6 7 2 5
      {⍳≢⍵} m         ⍝ range of length
1 2 3 4 5
      {⊂∘.⌈⍨⍳≢⍵} m    ⍝ outer product with maximum
┌─────────┐
│1 2 3 4 5│
│2 2 3 4 5│
│3 3 3 4 5│
│4 4 4 4 5│
│5 5 5 5 5│
└─────────┘
      {(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ check equality for n, for every n to the size of m / 2
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
└─────────┴─────────┘
      {(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ rotate right n times for every n on the left
┌─────────┬─────────┐
│1 0 0 0 0│0 1 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│1 1 0 0 0│
│1 0 0 0 0│0 1 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 1 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 1 1│
│0 0 0 0 1│0 0 0 1 0│
└─────────┴─────────┘
      {⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m
      ⍝ apply masks to original matrix
┌─────────┬─────────┐
│1 0 0 0 0│0 2 0 0 0│
│0 0 0 0 0│0 3 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│7 4 0 0 0│
│0 0 0 0 0│0 6 0 0 0│
├─────────┼─────────┤
│0 0 0 0 1│0 0 0 2 0│
│0 0 0 0 0│0 0 0 3 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
├─────────┼─────────┤
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 0 0│
│0 0 0 0 0│0 0 0 9 4│
│0 0 0 0 5│0 0 0 2 0│
└─────────┴─────────┘
      o ← {+/¨+/¨⍵∘ר(0 3 1 2)∘.{⌽∘⍉⍣⍺⊢⍵}(⍳⌊x←.5×≢⍵)∘.=⊂∘.⌈⍨⍳≢⍵} m ⋄ o
      ⍝ sum of each matrix
1  5
0 17
1  5
5 15
      ⊖⍉2↓o   ⍝ right side of the array
5 15
1  5
      {+/+/⍵×∘.{⍺ ⍵∊⍨⌈.5×≢⍵}⍨⍳≢⍵} m   ⍝ middle element of the array
20
      ⍉2↑o    ⍝ left side of the array
1  0
5 17
3 deleted 206 characters in body
source | link
2 deleted 3 characters in body
source | link
1
source | link