Comparison with traditional mathematics: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(+.×)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(2 intermediate revisions by 2 users not shown)
Line 5: Line 5:
APL [[Iverson notation|developed from TMN]], and much of APL will be immediately recognised by a mathematician. Interestingly, it seems that the look of the typeface and identifiers (names of variables etc.) used in APL makes a big difference in the reader's perception. A fixed-width typewriter-style font with long identifiers gives the impression of computer source code, while an italic serif typeface and single-letter identifiers gives the impression of scientific formulae. Compare the impression you get when reading the following two expressions which is both valid APL (though it has superfluous parentheses) and traditional mathematics:
APL [[Iverson notation|developed from TMN]], and much of APL will be immediately recognised by a mathematician. Interestingly, it seems that the look of the typeface and identifiers (names of variables etc.) used in APL makes a big difference in the reader's perception. A fixed-width typewriter-style font with long identifiers gives the impression of computer source code, while an italic serif typeface and single-letter identifiers gives the impression of scientific formulae. Compare the impression you get when reading the following two expressions which is both valid APL (though it has superfluous parentheses) and traditional mathematics:
<div style="text-align:center">
<div style="text-align:center">
<source lang=apl inline>((x + y) × (x - y)) = (f ∘ g)(x, y)</source>
<syntaxhighlight lang=apl inline>((x + y) × (x - y)) = (f ∘ g)(x, y)</syntaxhighlight>


<math>((x + y) \times (x - y)) = (f \circ g)(x, y)</math>
<math>((x + y) \times (x - y)) = (f \circ g)(x, y)</math>
Line 18: Line 18:
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
|-
|-
| <math>\sim q</math>    || <source lang="apl" inline>~ q</source>  || [[Not]]
| <math>\sim q</math>    || <syntaxhighlight lang="apl" inline>~ q</syntaxhighlight>  || [[Not]]
|-
|-
| <math>p \vee q</math> || <source lang="apl" inline>p ∨ q</source> || [[Or]]
| <math>p \vee q</math> || <syntaxhighlight lang="apl" inline>p ∨ q</syntaxhighlight> || [[Or]]
|-
|-
| <math>p \wedge q</math>  || <source lang="apl" inline>p ∧ q</source> || [[And]]
| <math>p \wedge q</math>  || <syntaxhighlight lang="apl" inline>p ∧ q</syntaxhighlight> || [[And]]
|-
|-
| <math>a \times b</math> || <source lang="apl" inline>a × b</source> || [[Multiply]]
| <math>a \times b</math> || <syntaxhighlight lang="apl" inline>a × b</syntaxhighlight> || [[Multiply]]
|-
|-
| <math>a \div b</math>  || <source lang="apl" inline>a ÷ b</source> || [[Divide]]
| <math>a \div b</math>  || <syntaxhighlight lang="apl" inline>a ÷ b</syntaxhighlight> || [[Divide]]
|-
|-
| <math>a \in S</math>    || <source lang="apl" inline>a ∊ S</source> || [[Membership]]
| <math>a \in S</math>    || <syntaxhighlight lang="apl" inline>a ∊ S</syntaxhighlight> || [[Membership]]
|-
|-
| <math>A \cup B</math>  || <source lang="apl" inline>A ∪ B</source> || [[Union]]
| <math>A \cup B</math>  || <syntaxhighlight lang="apl" inline>A ∪ B</syntaxhighlight> || [[Union]]
|-
|-
| <math>A \cap B</math>  || <source lang="apl" inline>A ∩ B</source> || [[Intersection]]
| <math>A \cap B</math>  || <syntaxhighlight lang="apl" inline>A ∩ B</syntaxhighlight> || [[Intersection]]
|}
|}


Line 42: Line 42:
! style="width:25%" | Traditional notation !! style="width:25%" | <small>Parenthesised<br></small>APL !! style="width:25%" | <small>Normal<br></small>APL !! Article
! style="width:25%" | Traditional notation !! style="width:25%" | <small>Parenthesised<br></small>APL !! style="width:25%" | <small>Normal<br></small>APL !! Article
|-
|-
| <math>[a \equiv b]</math>  || <source lang="apl" inline>(a ≡ b)</source>    || <source lang="apl" inline>a ≡ b</source>  || [[Match]]
| <math>[a \equiv b]</math>  || <syntaxhighlight lang="apl" inline>(a ≡ b)</syntaxhighlight>    || <syntaxhighlight lang="apl" inline>a ≡ b</syntaxhighlight>  || [[Match]]
|-
|-
| <math>[a=b]</math>          || <source lang="apl" inline>(a = b)</source>    || <source lang="apl" inline>a = b</source>  || [[Equal]]
| <math>[a=b]</math>          || <syntaxhighlight lang="apl" inline>(a = b)</syntaxhighlight>    || <syntaxhighlight lang="apl" inline>a = b</syntaxhighlight>  || [[Equal]]
|-
|-
| <math>[a \ne b]</math>      || <source lang="apl" inline>(a ≠ b)</source>    || <source lang="apl" inline>a ≠ b</source>  || [[Not Equal]]
| <math>[a \ne b]</math>      || <syntaxhighlight lang="apl" inline>(a ≠ b)</syntaxhighlight>    || <syntaxhighlight lang="apl" inline>a ≠ b</syntaxhighlight>  || [[Not Equal]]
|-
|-
| <math>[a \le b]</math>      || <source lang="apl" inline>(a ≤ b)</source>    || <source lang="apl" inline>a ≤ b</source>  || [[Less Than or Equal]]
| <math>[a \le b]</math>      || <syntaxhighlight lang="apl" inline>(a ≤ b)</syntaxhighlight>    || <syntaxhighlight lang="apl" inline>a ≤ b</syntaxhighlight>  || [[Less Than or Equal]]
|-
|-
| <math>(f \circ g)(x)</math> || <source lang="apl" inline>(f ∘ g)(x)</source> || <source lang="apl" inline>f ∘ g x</source> || [[Bind]]
| <math>(f \circ g)(x)</math> || <syntaxhighlight lang="apl" inline>(f ∘ g)(x)</syntaxhighlight> || <syntaxhighlight lang="apl" inline>f ∘ g x</syntaxhighlight> || [[Bind]]
|-
|-
| <math>f(g(x))</math>        || <source lang="apl" inline>f(g(x))</source>    || <source lang="apl" inline>f g x</source>  ||
| <math>f(g(x))</math>        || <syntaxhighlight lang="apl" inline>f(g(x))</syntaxhighlight>    || <syntaxhighlight lang="apl" inline>f g x</syntaxhighlight>  ||
|-
|-
| <math>(f+g)(x)</math>      || <source lang="apl" inline>(f+g)(x)</source>  || <source lang="apl" inline>(f+g) x</source> || [[Train]]s
| <math>(f+g)(x)</math>      || <syntaxhighlight lang="apl" inline>(f+g)(x)</syntaxhighlight>  || <syntaxhighlight lang="apl" inline>(f+g) x</syntaxhighlight> || [[Train]]s
|}
|}


Line 68: Line 68:
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
|-
|-
| <math>n \pi</math>            || <source lang="apl" inline>○n</source> || [[Pi Times]]
| <math>n \pi</math>            || <syntaxhighlight lang="apl" inline>○n</syntaxhighlight> || [[Pi Times]]
|-
|-
| <math>x!</math>                || <source lang="apl" inline>!x</source> || [[Factorial]]
| <math>x!</math>                || <syntaxhighlight lang="apl" inline>!x</syntaxhighlight> || [[Factorial]]
|-
|-
| <math>|x|</math>              || <source lang="apl" inline>|x</source> || [[Magnitude]]
| <math>|x|</math>              || <syntaxhighlight lang="apl" inline>|x</syntaxhighlight> || [[Magnitude]]
|-
|-
| <math>\lceil x \rceil</math>  || <source lang="apl" inline>⌈x</source> || [[Ceiling]]
| <math>\lceil x \rceil</math>  || <syntaxhighlight lang="apl" inline>⌈x</syntaxhighlight> || [[Ceiling]]
|-
|-
| <math>\lfloor x \rfloor</math> || <source lang="apl" inline>⌊x</source> || [[Floor]]
| <math>\lfloor x \rfloor</math> || <syntaxhighlight lang="apl" inline>⌊x</syntaxhighlight> || [[Floor]]
|}
|}


Line 85: Line 85:
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
|-
|-
| <math>ab</math>            || <source lang="apl" inline>a×b</source>    || [[Multiply]]
| <math>ab</math>            || <syntaxhighlight lang="apl" inline>a×b</syntaxhighlight>    || [[Multiply]]
|-
|-
| <math>e^x</math>          || <source lang="apl" inline> *x</source>    || [[Exponential]]
| <math>e^x</math>          || <syntaxhighlight lang="apl" inline> *x</syntaxhighlight>    || [[Exponential]]
|-
|-
| <math>a^b</math>          || <source lang="apl" inline>a*b</source>    || [[Power]]
| <math>a^b</math>          || <syntaxhighlight lang="apl" inline>a*b</syntaxhighlight>    || [[Power]]
|-
|-
| <math>\sqrt[a]{b}</math>  || <source lang="apl" inline>a√b</source>    || [[Root]]
| <math>\sqrt[a]{b}</math>  || <syntaxhighlight lang="apl" inline>a√b</syntaxhighlight>    || [[Root]]
|-
|-
| <math>\tbinom{n}{k}</math> || <source lang="apl" inline>k!n</source>    || [[Binomial]]
| <math>\tbinom{n}{k}</math> || <syntaxhighlight lang="apl" inline>k!n</syntaxhighlight>    || [[Binomial]]
|-
|-
| <math>f^n(x)</math>        || <source lang="apl" inline>(f⍣n) x</source> || [[Power operator]]
| <math>f^n(x)</math>        || <syntaxhighlight lang="apl" inline>(f⍣n) x</syntaxhighlight> || [[Power operator]]
|-
|-
| <math>\sin^n(x)</math>    || <source lang="apl" inline>(1○x)*n</source> || [[Circle function]]
| <math>\sin^n(x)</math>    || <syntaxhighlight lang="apl" inline>(1○x)*n</syntaxhighlight> || [[Circle function]]
|-
|-
| <math>a\cdot b</math>      || <source lang="apl" inline>a+.×b</source>  || [[Inner product]]
| <math>a\cdot b</math>      || <syntaxhighlight lang="apl" inline>a+.×b</syntaxhighlight>  || [[Inner product]]
|}
|}


Line 108: Line 108:
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
|-
|-
| <math>f^n(x)</math>            || <source lang="apl" inline>(f⍣a) x</source>          || [[Power operator]]
| <math>f^n(x)</math>            || <syntaxhighlight lang="apl" inline>(f⍣n) x</syntaxhighlight>          || [[Power operator]]
|-
|-
| <math>\sin^n(x)</math>          || <source lang="apl" inline>(1○x)*n</source>          || [[Circle function]]
| <math>\sin^n(x)</math>          || <syntaxhighlight lang="apl" inline>(1○x)*n</syntaxhighlight>          || [[Circle function]]
|-
|-
| <math>a\pm b</math>            || <source lang="apl" inline>a(+,-)b</source>          || [[Train]]s
| <math>a\pm b</math>            || <syntaxhighlight lang="apl" inline>a(+,-)b</syntaxhighlight>          || [[Train]]s
|-
|-
| <math>a\mp b</math>            || <source lang="apl" inline>a(-,+)b</source>          || [[Train]]s
| <math>a\mp b</math>            || <syntaxhighlight lang="apl" inline>a(-,+)b</syntaxhighlight>          || [[Train]]s
|-
|-
| <math>\frac{a}{b}</math>        || <source lang="apl" inline>a÷b</source>              || [[Divide]]
| <math>\frac{a}{b}</math>        || <syntaxhighlight lang="apl" inline>a÷b</syntaxhighlight>              || [[Divide]]
|-
|-
| <math>e^x</math>                || <source lang="apl" inline> *x</source>              || [[Exponential]]
| <math>e^x</math>                || <syntaxhighlight lang="apl" inline> *x</syntaxhighlight>              || [[Exponential]]
|-
|-
| <math>a^b</math>                || <source lang="apl" inline>a*b</source>              || [[Power function]]
| <math>a^b</math>                || <syntaxhighlight lang="apl" inline>a*b</syntaxhighlight>              || [[Power function]]
|-
|-
| <math>\log_a{b}</math>          || <source lang="apl" inline>a⍟b</source>              || [[Logarithm]]
| <math>\log_a{b}</math>          || <syntaxhighlight lang="apl" inline>a⍟b</syntaxhighlight>              || [[Logarithm]]
|-
|-
| <math>\sqrt[a]{b}</math>        || <source lang="apl" inline>a√b</source>              || [[Root]]
| <math>\sqrt[a]{b}</math>        || <syntaxhighlight lang="apl" inline>a√b</syntaxhighlight>              || [[Root]]
|-
|-
| <math>\tbinom{n}{k}</math>      || <source lang="apl" inline>k!n</source>              || [[Binomial]]
| <math>\tbinom{n}{k}</math>      || <syntaxhighlight lang="apl" inline>k!n</syntaxhighlight>              || [[Binomial]]
|-
|-
| <math>\left\{
| <math>\left\{
Line 132: Line 132:
     a, & \text{if } p(x) \\
     a, & \text{if } p(x) \\
     b, & \text{if } q(x)
     b, & \text{if } q(x)
   \end{array}\right.</math>      || <source lang="apl">{p x: a  
   \end{array}\right.</math>      || <syntaxhighlight lang="apl">{p x: a  
  q x: b}</source> || [[dfn]]s
  q x: b}</syntaxhighlight> || [[dfn]]s
|-
|-
| <math>\sum_{i=1}^{N} v_i</math> || <source lang="apl" inline>+/v</source>              || [[Reduce]]
| <math>\sum_{i=1}^{N} v_i</math> || <syntaxhighlight lang="apl" inline>+/v</syntaxhighlight>              || [[Reduce]]
|-
|-
| <math>\sum_{n=1}^{N} n</math>  || <source lang="apl" inline>+/⍳N</source>              || [[Reduce]], [[Iota]]
| <math>\sum_{n=1}^{N} n</math>  || <syntaxhighlight lang="apl" inline>+/⍳N</syntaxhighlight>              || [[Reduce]], [[Iota]]
|-
|-
| <math>\prod_{n=1}^{N} n</math>  || <source lang="apl" inline>×/⍳N</source>              || [[Reduce]], [[Iota]]
| <math>\prod_{n=1}^{N} n</math>  || <syntaxhighlight lang="apl" inline>×/⍳N</syntaxhighlight>              || [[Reduce]], [[Iota]]
|}
|}


Line 149: Line 149:
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
! style="width:33%" | Traditional notation !! style="width:33%" | APL !! Article
|-
|-
| <math>e^x</math>                || <source lang="apl" inline> *x</source>              || [[Exponential]]
| <math>e^x</math>                || <syntaxhighlight lang="apl" inline> *x</syntaxhighlight>              || [[Exponential]]
|-
|-
| <math>\ln{x}</math>            || <source lang="apl" inline> ⍟b</source>              || [[Natural Logarithm]]
| <math>\ln{x}</math>            || <syntaxhighlight lang="apl" inline> ⍟b</syntaxhighlight>              || [[Natural Logarithm]]
|-
|-
| <math>\log_a{b}</math>          || <source lang="apl" inline>a⍟b</source>              || [[Logarithm]]
| <math>\log_a{b}</math>          || <syntaxhighlight lang="apl" inline>a⍟b</syntaxhighlight>              || [[Logarithm]]
|-
|-
| <math>a \bmod b</math>          || <source lang="apl" inline>b|a</source>              || [[Modulus]]
| <math>a \bmod b</math>          || <syntaxhighlight lang="apl" inline>b|a</syntaxhighlight>              || [[Modulus]]
|-
|-
| <math>\left\{
| <math>\left\{
Line 161: Line 161:
     a, & \text{if } p(x) \\
     a, & \text{if } p(x) \\
     b, & \text{if } q(x)
     b, & \text{if } q(x)
   \end{array}\right.</math>      || <source lang="apl">{p x: a  
   \end{array}\right.</math>      || <syntaxhighlight lang="apl">{p x: a  
  q x: b}</source> || [[dfn]]s
  q x: b}</syntaxhighlight> || [[dfn]]s
|-
|-
| <math>\text{let } n=42</math>  || <source lang="apl" inline>n←42</source>              || [[Assignment]]
| <math>\text{let } n=42</math>  || <syntaxhighlight lang="apl" inline>n←42</syntaxhighlight>              || [[Assignment]]
|}
|}


Line 174: Line 174:
! style="width:50%" | Traditional notation !! APL
! style="width:50%" | Traditional notation !! APL
|-
|-
| <math>\sum_{i=1}^{N} v_i</math> || <source lang="apl" inline>+/ v  </source>
| <math>\sum_{i=1}^{N} v_i</math> || <syntaxhighlight lang="apl" inline>+/ v  </syntaxhighlight>
|-
|-
| <math>\sum_{n=1}^{N} n</math>  || <source lang="apl" inline>+/ ⍳N </source>
| <math>\sum_{n=1}^{N} n</math>  || <syntaxhighlight lang="apl" inline>+/ ⍳N </syntaxhighlight>
|-
|-
| <math>\prod_{n=1}^{N} n</math>  || <source lang="apl" inline>×/ ⍳N </source>
| <math>\prod_{n=1}^{N} n</math>  || <syntaxhighlight lang="apl" inline>×/ ⍳N </syntaxhighlight>
|-
|-
| <math>\forall x, P(x)</math>    || <source lang="apl" inline>∧/P(x)</source>
| <math>\forall x, P(x)</math>    || <syntaxhighlight lang="apl" inline>∧/P(x)</syntaxhighlight>
|-
|-
| <math>\exists x, P(x)</math>    || <source lang="apl" inline>∨/P(x)</source>
| <math>\exists x, P(x)</math>    || <syntaxhighlight lang="apl" inline>∨/P(x)</syntaxhighlight>
|}
|}
== Practical example ==
== Practical example ==
Line 190: Line 190:
! style="width:50%" | Traditional notation !! APL  
! style="width:50%" | Traditional notation !! APL  
|-
|-
| <math>|\textbf{b}|=\sqrt[2]{\textbf{b}\cdot\textbf{b}}</math> || <source lang=apl>bNorm ← 2 Root b +.× b</source>
| <math>|\textbf{b}|=\sqrt[2]{\textbf{b}\cdot\textbf{b}}</math> || <syntaxhighlight lang=apl>bNorm ← 2 Root b +.× b</syntaxhighlight>
|-
|-
| <math>\hat{\textbf{b}} = \textbf{b}\div|\textbf{b}|</math>  ||  <source lang=apl>bHat ← b ÷ bNorm</source>
| <math>\hat{\textbf{b}} = \textbf{b}\div|\textbf{b}|</math>  ||  <syntaxhighlight lang=apl>bHat ← b ÷ bNorm</syntaxhighlight>
|-
|-
| <math>\textbf{a}_\textbf{b}=(\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>  ||  <source lang=apl>a_b ← (a +.× bHat) × bHat</source>
| <math>\textbf{a}_\textbf{b}=(\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>  ||  <syntaxhighlight lang=apl>a_b ← (a +.× bHat) × bHat</syntaxhighlight>
|}
|}
</div>
</div>
{{APL syntax}}[[Category:Essays]]
{{APL syntax}}[[Category:Essays]]

Latest revision as of 22:22, 10 September 2022

APL unifies the notations for certain mathematical constructs, while having a more consistent syntax. Below are some examples comparing traditional mathematical notation (denoted TMN in this article) with the equivalent APL. Note that some of these examples only work in certain APL dialects.

Similarities

APL developed from TMN, and much of APL will be immediately recognised by a mathematician. Interestingly, it seems that the look of the typeface and identifiers (names of variables etc.) used in APL makes a big difference in the reader's perception. A fixed-width typewriter-style font with long identifiers gives the impression of computer source code, while an italic serif typeface and single-letter identifiers gives the impression of scientific formulae. Compare the impression you get when reading the following two expressions which is both valid APL (though it has superfluous parentheses) and traditional mathematics:

((x + y) × (x - y)) = (f ∘ g)(x, y)

Now note that they are identical, letter for letter. Computer Scientists tend to have great difficulty in approaching APL until they let go of the notion that APL is just another programming language (despite its name!) and instead look at it as an executable mathematical notation.

It is notable that APL traditionally was written with a font wherein letters were always italicised, while everything else wasn't. Here is how it looks in the default font for SAX:

SAX font sample

Identical

For many basic operations, APL is identical to TMN:

Traditional notation APL Article
~ q Not
p ∨ q Or
p ∧ q And
a × b Multiply
a ÷ b Divide
a ∊ S Membership
A ∪ B Union
A ∩ B Intersection

Redundant brackets and parentheses

Often, the only difference is that APL allows omitting brackets or parenthesis. This is because the Iverson bracket is implied and functions have long right scope.

Traditional notation Parenthesised
APL
Normal
APL
Article
(a ≡ b) a ≡ b Match
(a = b) a = b Equal
(a ≠ b) a ≠ b Not Equal
(a ≤ b) a ≤ b Less Than or Equal
(f ∘ g)(x) f ∘ g x Bind
f(g(x)) f g x
(f+g)(x) (f+g) x Trains

Differences

While based on TMN, APL goes a long way to harmonise and generalise its notation.

Prefix

In APL, all functions that only take a single argument are placed to the immediate left of their argument. TMN uses prefix, omnifix, and suffix notations.

Traditional notation APL Article
○n Pi Times
!x Factorial
|x Magnitude
⌈x Ceiling
⌊x Floor

Explicitness

APL writes out all functions explicitly while TMN often implies functions by argument positioning without giving the function an explicit symbol.

Traditional notation APL Article
a×b Multiply
*x Exponential
a*b Power
a√b Root
k!n Binomial
(f⍣n) x Power operator
(1○x)*n Circle function
a+.×b Inner product

Linearity

APL is strictly linear in the sense that it is written as single lines of uniformly sized and positioned characters, as opposed to TMN which often uses font size and/or position to give meaning.

Traditional notation APL Article
(f⍣n) x Power operator
(1○x)*n Circle function
a(+,-)b Trains
a(-,+)b Trains
a÷b Divide
*x Exponential
a*b Power function
a⍟b Logarithm
a√b Root
k!n Binomial
{p x: a 
 q x: b}
dfns
+/v Reduce
+/⍳N Reduce, Iota
×/⍳N Reduce, Iota

Named functions

APL has no reserved words, and user defined names can have multiple characters. TMN uses multi-character names for many functions and uses many ad-hoc control words.

Traditional notation APL Article
*x Exponential
⍟b Natural Logarithm
a⍟b Logarithm
b|a Modulus
{p x: a 
 q x: b}
dfns
n←42 Assignment

Other differences

APL harmonises and generalises all folding and dispenses with TMN's counting variables. Every one of the following calculations is a reduction.

Traditional notation APL
+/ v
+/ ⍳N
×/ ⍳N
∧/P(x)
∨/P(x)

Practical example

The steps to produce the component of a vector in the direction of another vector go as follows:

Traditional notation APL
bNorm ← 2 Root b +.× b
bHat ← b ÷ bNorm
a_b ← (a +.× bHat) × bHat
APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment