# Fast.ai APL study session 12

Discussion of the 12th study group can go here. Overview

This is a wiki post - feel free to edit to add links from the lesson or other useful info.

## Study session resources

While you’ve not really covered trains yet (though you’ve seen the `mean←+⌿÷≢` example), `sign←÷∘|⍨` can be written as the train (fork) `sign←⊢÷|` as follows.

Without going into all the details, think of the traditional mathematical notation (TMN) where (f+g)(x)=f(x)+g(x). Forks (3-trains) are just a generalisation of this pattern to all functions (though the middle one has to be dyadic). For `n÷|n` we have the + become a `÷`, while g is `|`, but what is f‽ We just want `n` unmodified. This calls for an identity function à la f(x)=x, which is APL’s `⊢`. So…
TMN: f(x)=x, g(x)=|x|, sgn(x)=(f÷g)(x)
APL: `f←⊢ ⋄ g←| ⋄ sgn←f÷g` or simply `sgn←⊢÷|`

2 Likes

So
1 2 3 +⊂ 4 5 6
┌─────┬─────┬─────┐
│5 6 7│6 7 8│7 8 9│
└─────┴─────┴─────┘
Is an APL loop equivalent?

Dyadic `+` is a scalar function meaning that it will pervasively apply to corresponding elements. Since there are 3 elements on the left, but only 1 on the right (`⊂` always gives a scalar result) it will “broadcast” that single element to all of them, so `1 2 3 + ⊂4 5 6` is equivalent to `1 2 3 + (4 5 6)(4 5 6)(4 5 6)`. Now it pairs up `1` with its `4 5 6` and `2` with its `4 5 6` and `3` with its `4 5 6`, giving `(1+4 5 6)(2+4 5 6)(3+4 5 6)`. Call it a loop if you want .

P.S. Put lines with triple-backticks before and after your code block to make it format in monospace font and respect leading spaces.

3 Likes

Here is an explaination of the `DOMAIN ERROR` encountered in the session:

``````    (÷|)⍨ 3j4      ⍝ Duplicate input f⍨Y ⇔ Y f⍨ Y
3j4 (÷|)  3j4      ⍝ "Two Train" ⍺ (g h) ⍵ ⇔ g (⍺ h ⍵)
÷   (3j4|3j4) ⍝ Self modulus is always zero
÷   (0)       ⍝ DOMAIN ERROR: Divide by zero
``````

I have been practicing my “Trainspotting The APL Wiki on Tacit Programming is a great reference.

4 Likes