3706
Comment:

3716

Deletions are marked like this.  Additions are marked like this. 
Line 7:  Line 7: 
{{{  
Line 85:  Line 86: 
}}} 
This is a readonlypage
These operators where posted by John Scholes on November the 4th 2006 on Dyalog's web site:
⍝ Variations on primitive scan: nvec ← {axis} ##.mscan nvec ⍝ Minus scan. nvec ← {axis} ##.dscan nvec ⍝ Divide scan. vect ← (fn ##.ascan) vect ⍝ Associative vector scan. array ← {ascan} (fn ##.ascana) array ⍝ Associative higher rank scan. Provided by Phil Last and Nicolas Delcros, these operators outperform their primitive counterparts by scanning cumulatively from left to right. The primitive scan operator is defined as a vector of reductions: ¯¯¯¯¯¯¯¯¯ ⍺⍺\⍵ → ⍺⍺/¨(1 to ⍴⍵)↑¨⊂⍵ For example: +\3 1 4 1 6 → +/¨(1 to 5)↑¨⊂3 1 4 1 6 → +/¨(3)(3 1)(3 1 4)(3 1 4 1)(3 1 4 1 6) → (+/3)(+/3 1)(+/3 1 4)(+/3 1 4 1)(+/3 1 4 1 6) → 3 4 8 9 15 Defn: An associative dyadic function f is one where: (A f B) f C ←→ A f (B f C). ¯¯¯¯¯¯¯¯¯¯¯ For associative operand functions, such as + and ×, the interpreter can "cheat" by accumulating the result in a single lefttoright pass of the vector argu ment, but for nonassociative functions, it is obliged to do it the slow way us ing reductions of increasingly longer sequences as above, resulting in an O(n×n) algorithm. Mscan and dscan provide linear O(n) functions to simulate \ and ÷\ respective ly. \⍳1000 ⍝ slow primitive minusscan. 1 ¯1 2 ¯2 3 ¯3 4 ¯4 5 ¯5 ... mscan ⍳1000 ⍝ quick minusscan. 1 ¯1 2 ¯2 3 ¯3 4 ¯4 5 ¯5 ... ÷\⍳1000 ⍝ slow primitive dividescan. 1 0.5 1.5 0.375 1.875 0.3125 ... dscan ⍳1000 ⍝ quick dividescan. 1 0.5 1.5 0.375 1.875 0.3125 ... In general, the interpreter can "cheat" only if it can determine that scan's op erand function is associative. For example, even though dfn {⍺+⍵} is clearly as sociative, the interpreter cannot know this and so uses the slow O(n×n) method. In cases such as these, where it is known that the operand function is associat ive, ascan can be used to force a lefttoright cumulative O(n) scan. {⍺+⍵}\⍳10000 ⍝ slow primitive scan. 1 3 6 10 15 21 28 36 45 55 ... {⍺+⍵}ascan⍳1000 ⍝ quick associative scan. 1 3 6 10 15 21 28 36 45 55 ... Note that if the operand turns out to be nonassociative, ascan will return a result that differs from primitive scan. {⍺⍵}\⍳10 ⍝ slow primitive scan. 1 ¯1 2 ¯2 3 ¯3 4 ¯4 5 ¯5 {⍺⍵}ascan⍳10 ⍝ quick left to right accumulation. 1 ¯1 ¯4 ¯8 ¯13 ¯19 ¯26 ¯34 ¯43 ¯53 nums← ↑('one' 'two' 'three')('un' 'deux' 'trois')('yan' 'tan' 'tethera') disp {⍺,'',⍵} ascan nums ⍝ lefttoright scan along rows ┌→──┬───────┬───────────────┐ ↓one│onetwo│ onetwothree │ ├──→┼──────→┼──────────────→┤ │un │undeux│ undeuxtrois │ ├──→┼──────→┼──────────────→┤ │yan│yantan│yantantethera│ └──→┴──────→┴──────────────→┘