Searching for matrix in matrix

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Searching for matrix in matrix

Postby nanthil on Mon May 14, 2018 3:24 pm

I'm getting to the point in learning APL that I know what most of the functions do, but not how to solve problems with them, so I went looking for some. I found some kata to solve to practice APL, after first solving in a known language. I solved this one using javascript, with no mutation, and only pure functions. Upon reflection of the solution, I recognized a much simpler solution using matrices.

https://www.codewars.com/kata/battleshi ... -validator

Since no ship placements may touch, there are only 3 "interesting" configurations.

Corner = n⍴1, n+2↑0
      1 1 1 1 0
0 0 0 0 0


Wall = 0, n⍴1, n+3↑0, or in APL, since zeros propagate left, 2×(n+2) ↑ (0, n⍴1)
2x because 2 rows, n + 2 because there has to be padding on all sides.
      0 1 1 1 1 0
0 0 0 0 0 0


Middle = 3×(n+2) ↑ (n+3 ↑ 0, n⍴1)
      0 0 0 0 0 0
0 1 1 1 1 0
0 0 0 0 0 0


      board  ← (10 10 ⍴ ⍳100) ∊ 0 10 20 30 2 3 4 6 7 8 22 23 24 26 27 29 39 59 69 80 82 60 62
corner ← {2 (⍵+1) ⍴ (2×(2+⍵)) ↑ ⍵ ⍴ 1}
wall ← {2 (⍵+2) ⍴ (2×(2+⍵)) ↑ ∊ 0 (⍵ ⍴ 1)}
middle ← {3 (⍵+2) ⍴ (3×(2+⍵)) ↑ ∊ ((⍵+3) ↑ 0) (⍵ ⍴ 1)}

board


https://ngn.github.io/apl/web/

All that being said. I'm having trouble actually figuring out how to do the search. I've been through the tutor, but none of my guesses actually amount to anything. I was thinking of searching the matrix for every ⍴⍵ in board, but that leads to rank errors, because I naively assumed the search would bound itself to the board.

At first I thought this, or some variation would work out of the box:
      (corner 4) ∊ bord


For example ⍴corner 4 is 2 5
      2 5⍴¨board


I forget all the permutations of that that I've tried, but I've been at it since Friday evening, and I'm stuck.

If the board is

      1 0 0...
1 0 0...
1 0 0...
1 0 0...
0 0 0...


how can I find some ⍉,⊖,⌽ of a 2xn or 3xn M in the board?
      1 1 1 1 0 
0 0 0 0 0
nanthil
 
Posts: 3
Joined: Sun May 13, 2018 9:48 pm

Re: Searching for matrix in matrix

Postby Roger|Dyalog on Tue May 15, 2018 1:13 am

Have you tried ⍷?
Roger|Dyalog
 
Posts: 117
Joined: Thu Jul 28, 2011 10:53 am

Re: Searching for matrix in matrix

Postby nanthil on Tue May 15, 2018 8:25 pm

Color me embarrassed.

Thanks.
nanthil
 
Posts: 3
Joined: Sun May 13, 2018 9:48 pm

Re: Searching for matrix in matrix

Postby Roger|Dyalog on Wed May 16, 2018 12:20 pm

No need to feel embarrassed. For many years in my own APL journey I had the APL reference card opened beside the terminal (at that time it was a terminal, not a computer), the better to jog my memory on what are the available facilities. For many years, even after I had become familiar with all the APL primitives. Dyalog APL also has a reference card. Alternatively, you may find the "chiclet" buttons at the top of the session window sufficient as an aide-mémoire.
Roger|Dyalog
 
Posts: 117
Joined: Thu Jul 28, 2011 10:53 am

Re: Searching for matrix in matrix

Postby Fiona|Dyalog on Thu May 17, 2018 9:37 am

The current reference card can be downloaded from the Documentation Centre on our website – http://docs.dyalog.com/16.0/ReferenceCard.pdf

If you find this useful, you can email docs@dyalog.com and request a hard copy.
User avatar
Fiona|Dyalog
 
Posts: 49
Joined: Mon Apr 22, 2013 12:59 pm


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest