Inner Product: Difference between revisions
m (mention monadic inner product) |
m (→Differences between dialects: add title for reference url) |
||
Line 116: | Line 116: | ||
== Differences between dialects == | == Differences between dialects == | ||
Implementations differ on the exact behaviour of inner product when the right operand is not a [[scalar function]]. Page 121 of the ISO/IEC 13751:2001(E) [[standard]] specifies that <syntaxhighlight lang=apl inline>X f.g Y</syntaxhighlight> is equivalent to <syntaxhighlight lang=apl>f/¨ (⊂[⍴⍴x]x)∘.g ⊂[1]y</syntaxhighlight> (assuming [[ | Implementations differ on the exact behaviour of inner product when the right operand is not a [[scalar function]]. Page 121 of the ISO/IEC 13751:2001(E) [[standard]] specifies that <syntaxhighlight lang=apl inline>X f.g Y</syntaxhighlight> is equivalent to <syntaxhighlight lang=apl>f/¨ (⊂[⍴⍴x]x)∘.g ⊂[1]y</syntaxhighlight> (assuming [[index origin]] 1). This is indeed what [[APL2]], [[APLX]], [[APL+Win]], and [[ngn/apl]] follow, while [[Dyalog APL]], [[NARS2000]] and [[GNU APL]] differ as described by [[Roger Hui]]:<ref>[[Roger Hui]]. ''inner product''. Internal Dyalog email. 24 July 2020.</ref> | ||
<blockquote> | <blockquote> | ||
The following dop models inner product in Dyalog APL, with caveats. If you find a case where <syntaxhighlight lang=apl inline>f.g</syntaxhighlight> differs from <syntaxhighlight lang=apl inline>f IP g</syntaxhighlight>, not covered by the caveats, I'd be interested. | The following dop models inner product in Dyalog APL, with caveats. If you find a case where <syntaxhighlight lang=apl inline>f.g</syntaxhighlight> differs from <syntaxhighlight lang=apl inline>f IP g</syntaxhighlight>, not covered by the caveats, I'd be interested. | ||
Line 142: | Line 142: | ||
The <syntaxhighlight lang=apl>⊃⍤0⊢(↓⍺)∘.(⍺⍺/⍵⍵¨)↓(¯1⌽⍳⍴⍴⍵)⍉⍵</syntaxhighlight> line of IP above can be rewritten as <syntaxhighlight lang=apl>⍺(⍺⍺⌿⍵⍵¨⍤¯1)⍤1 99⊢⍵</syntaxhighlight> which uses the more efficient major-cell-at-a-time algorithm (rather than row-by-column). The ISO/IEC 13751:2001(E) inner product, conversely, can only be calculated row-by-column, as computing the results one major cell (of the right argument) at a time relies on each application of the right operand being done between two scalars and producing a scalar result. | The <syntaxhighlight lang=apl>⊃⍤0⊢(↓⍺)∘.(⍺⍺/⍵⍵¨)↓(¯1⌽⍳⍴⍴⍵)⍉⍵</syntaxhighlight> line of IP above can be rewritten as <syntaxhighlight lang=apl>⍺(⍺⍺⌿⍵⍵¨⍤¯1)⍤1 99⊢⍵</syntaxhighlight> which uses the more efficient major-cell-at-a-time algorithm (rather than row-by-column). The ISO/IEC 13751:2001(E) inner product, conversely, can only be calculated row-by-column, as computing the results one major cell (of the right argument) at a time relies on each application of the right operand being done between two scalars and producing a scalar result. | ||
Some implementations extend the inner product by implementing Iverson's monadic variant<ref>https://www.jsoftware.com/papers/satn42.htm</ref>, which takes a single argument and performs the operation of computing the alternant, as modelled by [https://dfns.dyalog.com/n_alt.htm dfns.alt]. | Some implementations extend the inner product by implementing Iverson's monadic variant<ref>[[Ken Iverson|K.E. Iverson]]. [https://www.jsoftware.com/papers/satn42.htm Determinant-Like Functions Produced by the Dot Operator.] SHARP APL Technical Note 42. 1982-04-01.</ref>, which takes a single argument and performs the operation of computing the alternant, as modelled by [https://dfns.dyalog.com/n_alt.htm dfns.alt]. | ||
== External links == | == External links == |
Revision as of 01:52, 27 August 2023
.
|
Inner Product (.
) is a dyadic operator that produces a dyadic function when applied with two dyadic functions. It's a generalisation of the matrix product, allowing not just addition-multiplication, but any dyadic functions given as operands.
Examples
x ← 1 2 3 y ← 4 5 6 x ,.(⊂,) y ⍝ visualizing of pairing ┌─────────────┐ │┌───┬───┬───┐│ ││1 4│2 5│3 6││ │└───┴───┴───┘│ └─────────────┘ x {⊂⍺,'+',⍵}.{⊂⍺,'×',⍵} y ⍝ visualizing function application in matrix multiplication ┌───────────────────────────┐ │┌─────────────────────────┐│ ││┌─────┬─┬───────────────┐││ │││1 × 4│+│┌─────┬─┬─────┐│││ │││ │ ││2 × 5│+│3 × 6││││ │││ │ │└─────┴─┴─────┘│││ ││└─────┴─┴───────────────┘││ │└─────────────────────────┘│ └───────────────────────────┘ x+.×y ⍝ matrix multiplication 32
The shapes of the arguments must be compatible with each other: The last axis of the left argument must have the same length as the first axis of the right argument, or formally, for X f.g Y
it must be that (¯1↑⍴X)≡(1↑⍴Y)
. Although this rule differs from conformability, the arguments may also be subject to scalar or singleton extension. The shape of the result is (¯1↓⍴X),(1↓⍴Y)
.
For example, when applying inner product on two matrices, the number of columns in the left array must match with number of rows in the right array, otherwise we will get an error.
⎕ ← x ← 2 3⍴⍳10 1 2 3 4 5 6 ⎕ ← y ← 4 2⍴⍳10 1 2 3 4 5 6 7 8 x+.×y LENGTH ERROR x+.×y ∧ ⎕ ← y ← 3 2⍴⍳10 ⍝ reshape y to be compatible with x x+.×y 22 28 49 64
History
Inner product appeared in early Iverson Notation as and applied even to non-scalar functions, like Compress, Iverson bringing:[1]
When the inner product notation was linearised (made to fit on a single line of code) the glyph .
was chosed to denote what was previously indicated by positioning the two operands vertically aligned. Thus, the above correspond to the following modern APL:
⍝ For example, if A←3 4⍴1 3 2 0 2 1 0 1 4 0 0 2 B←4 2⍴4 1 0 3 0 2 2 0 ⍝ then A +.× B 4 14 10 5 20 4 A ∧.= B 0 1 0 0 1 0 A ∨.≠ B 1 0 1 1 0 1 (A ≠ 0) +./ B 4 6 6 4 6 1
Note that some dialects implement Compress (/
) as a monadic operator rather than as a function, which means it cannot be an operand in the inner product. Instead, a cover function is necessary:
∇z←a Compress b z←a/b ∇
Differences between dialects
Implementations differ on the exact behaviour of inner product when the right operand is not a scalar function. Page 121 of the ISO/IEC 13751:2001(E) standard specifies that X f.g Y
is equivalent to
f/¨ (⊂[⍴⍴x]x)∘.g ⊂[1]y
(assuming index origin 1). This is indeed what APL2, APLX, APL+Win, and ngn/apl follow, while Dyalog APL, NARS2000 and GNU APL differ as described by Roger Hui:[2]
The following dop models inner product in Dyalog APL, with caveats. If you find a case where
f.g
differs fromf IP g
, not covered by the caveats, I'd be interested.IP←{ assert((⊃⌽⍴⍺)≡≢⍵)∨(1=×/⍴⍺)∨1=×/⍴⍵: ⊃⍤0 ⊢ (↓⍺) ∘.(⍺⍺/⍵⍵¨) ↓(¯1⌽⍳⍴⍴⍵)⍉⍵ } assert←{⍺←'assertion failure' ⋄ 0∊⍵:⍺ ⎕SIGNAL 8 ⋄ shy←0}(Explanation: What's with the
⊃⍤0
inIP
? It's because∘.f
has an implicit each, applying⊂
to each item of its result. But the⍺⍺/
in(⍺⍺/⍵⍵¨)
also has an implicit each. So the⊃⍤0
gets rid of one of those encloses.)Caveats:
- You can not use the hybrid
/
directly as an operand as it runs afoul of the parser in weird and wonderful ways. Instead, you have to use{⍺/⍵}
. The same goes for\
and{⍺\⍵}
I guess.
- It differs from ISO/IEC 13751:2001(E) in using
⍵⍵¨
instead of just⍵⍵
in the central key expression (i.e.(⍺⍺/⍵⍵¨)
instead of(⍺⍺/⍵⍵)
). So does the primitivef.g
.
- It differs from ISO/IEC 13751:2001(E) in doing full-blown single extension instead of just scalar and 1-element vector extension (as in APL2). So does the primitive
f.g
. e.g.(3 4⍴5)+.×1 1 1 1⍴6 ⍝ works in Dyalog, not in ISO or APL2- It differs from NARS2000 or APL\360 in not permitting unit axis extension. So does the primitive
f.g
. e.g.(3 4⍴5)+.×1 5⍴6 ⍝ works in NARS2000 or APL\360, not in Dyalog APL
The
⊃⍤0⊢(↓⍺)∘.(⍺⍺/⍵⍵¨)↓(¯1⌽⍳⍴⍴⍵)⍉⍵
line of IP above can be rewritten as
⍺(⍺⍺⌿⍵⍵¨⍤¯1)⍤1 99⊢⍵
which uses the more efficient major-cell-at-a-time algorithm (rather than row-by-column). The ISO/IEC 13751:2001(E) inner product, conversely, can only be calculated row-by-column, as computing the results one major cell (of the right argument) at a time relies on each application of the right operand being done between two scalars and producing a scalar result.
Some implementations extend the inner product by implementing Iverson's monadic variant[3], which takes a single argument and performs the operation of computing the alternant, as modelled by dfns.alt.
External links
Documentation
Publications
Discussion of differences between dialects
- Dyalog / APL2000 discrepancy (Google Groups)
- multiple inner product (GNU APL mailing list)
- an other inner product ,., bug (GNU APL mailing list)
References
- ↑ Ken Iverson. A Programming Language. §1.11 The language.
- ↑ Roger Hui. inner product. Internal Dyalog email. 24 July 2020.
- ↑ K.E. Iverson. Determinant-Like Functions Produced by the Dot Operator. SHARP APL Technical Note 42. 1982-04-01.