Can this function be written without the loop?
7 posts
• Page 1 of 1
Can this function be written without the loop?
This function produces the difference table of a boolean vector:
y←fanion x;n
n←⍴x
y←⍳0
:For n :In ⍳n
y←y,⊂x
x←(1↓x)≠¯1↓x
:EndFor
Can it be written without the For loop?
The result is usually displayed as a triangular structure in which each item in a row is the ≠ (exclusiveor) of the two items immediately above it:
fandisp x;n;i;⎕IO
⎕IO←1
n←⍴x
x←⍉(1,n)⍴x
⎕←' ',x[1;1]
i←2
:While i≤n
⎕←(i↑' '),x[i;1]
i←i+1
:EndWhile
y←fanion x;n
n←⍴x
y←⍳0
:For n :In ⍳n
y←y,⊂x
x←(1↓x)≠¯1↓x
:EndFor
Can it be written without the For loop?
The result is usually displayed as a triangular structure in which each item in a row is the ≠ (exclusiveor) of the two items immediately above it:
fandisp x;n;i;⎕IO
⎕IO←1
n←⍴x
x←⍉(1,n)⍴x
⎕←' ',x[1;1]
i←2
:While i≤n
⎕←(i↑' '),x[i;1]
i←i+1
:EndWhile
 Stu
 Posts: 97
 Joined: Thu Dec 31, 2015 1:30 am
Re: Can this function be written without the loop?
Hmm...
what about replacing the whole function with a simple dfun:
VeliMatti
what about replacing the whole function with a simple dfun:
 Code: Select all
⍬{0∊⍴⍵:⍺ ⋄ (⍺,⊂⍵)∇ 2≠/⍵}
VeliMatti
 VeliMatti
 Posts: 56
 Joined: Sat Nov 28, 2009 3:12 pm
Re: Can this function be written without the loop?
This is really nice: ⍬{0∊⍴⍵:⍺ ⋄ (⍺,⊂⍵)∇ 2≠/⍵}, but I don't completely understand the recursive step. How does 2≠/⍵ form the first difference of ⍵?
 Stu
 Posts: 97
 Joined: Thu Dec 31, 2015 1:30 am
Re: Can this function be written without the loop?
See help for Nwise reduction:
X [the left argument] can be thought of as the width of a ‘window’ which moves along vectors drawn from the Kth axis of Y. [the right argument]
and
If X is negative, each subvector is reversed before being reduced.
which with X=2 produces the same result as your
In fact yours is actually equivalent to
The recursion is being applied between the result so far and the amended x.
X [the left argument] can be thought of as the width of a ‘window’ which moves along vectors drawn from the Kth axis of Y. [the right argument]
and
If X is negative, each subvector is reversed before being reduced.
which with X=2 produces the same result as your
(1↓x)≠¯1↓x
In fact yours is actually equivalent to
¯2≠/xor
2≠⍨/xas
2≠/xwould be equivalent to
(¯1↓x)≠1↓xcomparing the first with the second, second with third &c. while yours compares second with first, third with second usw. As ≠ is commutable it makes no difference in this case.
The recursion is being applied between the result so far and the amended x.

Phil Last  Posts: 557
 Joined: Thu Jun 18, 2009 6:29 pm
Re: Can this function be written without the loop?
fanion←{{⍵,⊂2≠/⊃⌽⍵}⍣(¯1+≢⍵)⊂⍵}
⊢ b ← 1=?7⍴2
0 0 1 1 1 0 1
fanion b
┌─────────────┬───────────┬─────────┬───────┬─────┬───┬─┐
│0 0 1 1 1 0 1│0 1 0 0 1 1│1 1 0 1 0│0 1 1 1│1 0 0│1 0│1│
└─────────────┴───────────┴─────────┴───────┴─────┴───┴─┘
 RogerDyalog
 Posts: 215
 Joined: Thu Jul 28, 2011 10:53 am
Re: Can this function be written without the loop?
fanion←{{⍵,⊂2≠/⊃⌽⍵}⍣(¯1+≢⍵)⊂⍵}
At this point allow me put in a good word for extending the power operator to allow a nonscalar "exponent" (right operand), modelled as follows:
powop←{⍺←⊣ ⋄ ⍺∘⍺⍺{⍺⍺⍣⍵⊢⍵⍵}⍵¨⍵⍵}
With that, the fanion function can be:
fan1←{2 ≠/powop(⍳≢⍵) ⊢⍵}
⍝ fan1←{2 ≠/⍣(⍳≢⍵) ⊢⍵}
b
0 0 1 1 1 0 1
fan1 b
┌─────────────┬───────────┬─────────┬───────┬─────┬───┬─┐
│0 0 1 1 1 0 1│0 1 0 0 1 1│1 1 0 1 0│0 1 1 1│1 0 0│1 0│1│
└─────────────┴───────────┴─────────┴───────┴─────┴───┴─┘
fanion b
┌─────────────┬───────────┬─────────┬───────┬─────┬───┬─┐
│0 0 1 1 1 0 1│0 1 0 0 1 1│1 1 0 1 0│0 1 1 1│1 0 0│1 0│1│
└─────────────┴───────────┴─────────┴───────┴─────┴───┴─┘
For fan1, you need the appropriate modifications if ⎕io is not 0.
 RogerDyalog
 Posts: 215
 Joined: Thu Jul 28, 2011 10:53 am
Re: Can this function be written without the loop?
Thanks to everyone who responded to my question. I'm impressed, again, at the power of the new capabilities incorporated into APL since I learned the original version of the language in the 1970's.
 Stu
 Posts: 97
 Joined: Thu Dec 31, 2015 1:30 am
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group