⍳/ ? What does it do ?

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

⍳/ ? What does it do ?

Postby hbarkhof on Wed May 30, 2018 12:04 pm

on page 380 of the book : "Mastering Dyalog APL" it shows

⍳/ (2 6 1 7) (2 4⍴3 7 8 4 2 5 6 0)

Resulting in :
5 4 5 5
1 5 2 5

Please explain what ⍳/ does. Thanks.
hbarkhof
 
Posts: 44
Joined: Mon Apr 09, 2018 8:37 am

Re: ⍳/ ? What does it do ?

Postby kai on Wed May 30, 2018 2:30 pm

The general definition of / is to put the function to its left between all items to its right.

Therefore +/1 2 3 becomes 1+2+3.

Accordingly ⍳/ (2 6 1 7) (2 4⍴3 7 8 4 2 5 6 0) becomes
2 6 1 7 ⍳ 2 4⍴3 7 8 4 2 5 6 0

That's all.

Because the concept is so general you can use the / (scan) operator with any conforming function:

      ∨/ 0 0 0 1 0 0
1
-/ 0 2 3 ¯9 0 0
10
≡/'APL' 'COBOL'
0
≡/'APL' 'APL'
1
User avatar
kai
 
Posts: 137
Joined: Thu Jun 18, 2009 5:10 pm
Location: Hillesheim / Germany

Re: ⍳/ ? What does it do ?

Postby hbarkhof on Thu May 31, 2018 9:07 am

Thanks Kai.

Unfortunally I don't see how (2 6 1 7) ⍳ 2 4⍴3 7 8 4 2 5 6 0
gives as result 5455 1525 ???

I do know that 2 4⍴3 7 8 4 2 5 6 0 gives
3784
2560
That is the easy part :) But with (2 6 1 7)⍳ in front of it ??
hbarkhof
 
Posts: 44
Joined: Mon Apr 09, 2018 8:37 am

Re: ⍳/ ? What does it do ?

Postby kai on Thu May 31, 2018 2:37 pm

I do know that 2 4⍴3 7 8 4 2 5 6 0 gives
3784
2560


You mean

3 7 8 4
2 5 6 0

rather than

3784
2560

I hope.

The fact that the right argument is a matrix rather than a vector can be ignored:

2 6 1 7 ⍳ 3 7 8 4 2 5 6 0

gives exactly the same result but in a different shape.

To phrase it differently: the fact that the right argument is a matrix has no impact whatsoever on how ⍳ works, it just defines the shape of the result.

That is another concept in APL you need to internalize. Study these examples:

Code: Select all
      (1 2 3)[2 3 1]
2 3 1
      (1 2 3)[2 3 4⍴2 3 1]
2 3 1 2
3 1 2 3
1 2 3 1
       
2 3 1 2
3 1 2 3
1 2 3 1


The shape of the argument (=what's inside []) has no influence on how indexing works, but it defines the shape of the result.

If it's still unclear keep asking.
User avatar
kai
 
Posts: 137
Joined: Thu Jun 18, 2009 5:10 pm
Location: Hillesheim / Germany

Re: ⍳/ ? What does it do ?

Postby ray on Thu May 31, 2018 11:20 pm

Code: Select all
    (2 6 1 7) ⍳ 2 4⍴3 7 8 4 2 5 6 0
5 4 5 5
1 5 2 5

The result has the same shape and rank as the right hand argument, so it is 2 rows by 4 columns.

In the top row the results are
Code: Select all
      2 6 1 7⍳3
5
      2 6 1 7⍳7
4
      2 6 1 7⍳8
5
      2 6 1 7⍳4
5


and the bottom row are
Code: Select all
     2 6 1 7⍳2
1
     2 6 1 7⍳5
5
     2 6 1 7⍳6
2
     2 6 1 7⍳0
5


Where do all the "5"s come from?
R←X⍳Y
"If an element of Y cannot be found in X, then the corresponding element of R will be ⎕IO+⊃⍴X."
All the "5" correspond with items in Y which are not in X, and since X is 4 items long, (in ⎕IO 1) these results will be 1+4←→5

In ⎕IO 0
Code: Select all
         ⎕IO←0
       (2 6 1 7) ⍳ 2 4⍴3 7 8 4 2 5 6 0
4 3 4 4
0 4 1 4



I hope this is making sense.
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 221
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: ⍳/ ? What does it do ?

Postby Morten|Dyalog on Fri Jun 01, 2018 7:47 am

ray wrote:The result has the same shape and rank as the right hand argument, so it is 2 rows by 4 columns.

This was true until dyadic iota was extended to left arguments of higher rank. Today, it is more correct to say that the search is for "major cells" of the left argument (sub-arrays of rank 1 less than the argument).

For example:
Code: Select all
      ⎕←mat←3 4⍴⍳12
1  2  3  4
5  6  7  8
9 10 11 12
      ⍴⎕←mat⍳⊖mat ⍝look for rows of reverse of mat in self, return 3-element vector
3 2 1
3
      ⍴⍴⎕←mat⍳5 6 7 8 ⍝ look for vector in mat, return scalar result
2
0

Rather than simply being of shape (⍴⍵), the result has the shape:

Code: Select all
(1-⍴⍴⍺)↓⍴⍵

For a vector left argument, the search is for scalars and (1-⍴⍴⍺) is 0.
User avatar
Morten|Dyalog
 
Posts: 453
Joined: Tue Sep 09, 2008 3:52 pm

Re: ⍳/ ? What does it do ?

Postby hbarkhof on Fri Jun 01, 2018 8:51 am

Now it is very clear to me. Thank you all !
Sorry for "Silly" question :)
More will follow , I'm afraid.
hbarkhof
 
Posts: 44
Joined: Mon Apr 09, 2018 8:37 am

Re: ⍳/ ? What does it do ?

Postby kai on Fri Jun 01, 2018 11:04 am

> Sorry for "Silly" question :)

We've all gone through this although you might come across some jerk every now and then who seems to have forgotten.

"If you ask a question it might make you look stupid for a couple of minutes, but if you don't ask you stay stupid, so you better keep asking."
User avatar
kai
 
Posts: 137
Joined: Thu Jun 18, 2009 5:10 pm
Location: Hillesheim / Germany


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest