matrix - Understanding APL's Inner Product -


here excerpt mastering dyalog apl book, chapter on inner products:

hms variable contains duration in hours, minutes, , seconds: hms ← 3 44 29 chapter j – operators 397  convert seconds. shall see 3 methods now, , 4th  method  given in chapter.      horrible solution                        (3600×hms[1]) + (60×hms[2]) + hms[3]      apl solution                        +/ 3600 60 1 × hms      excellent solution inner product   3600 60 1 +.× hms  

it says the second , third solutions equivalent in terms of number of characters typed and performance.

as understand it, apl programmers should use inner product, outer product, as possible. correct?

can give example when using inner product lead performance gains? happens when use inner product (on lower level)? first solution presented below horrible because doesn't use apl syntax in proper way or have worse performance?

i know there few questions want asking in general how inner/outer products work , when should apl programmer use them.

apl programmers should use inner product, outer product, as possible. correct?

it apl programmer , task @ hand, if makes apl code more concise , efficient, don't see why programmer wouldn't opt it.

in particular case 60⊥hms more concise , efficient inner product.

can give example when using inner product lead performance gains?

as typical in array-oriented programming, performance gains achieved doing things in 1 go. apl functions implicit loops---their implementation uses counter, limit it, , increment step. shorter code is, better, because not it's easier hold in one's head, it's more efficient interpreter has fewer passes on data. implementations loop fusion in attempt reduce overhead. have idiom recognition---certain combinations of squiggles special-cased in interpreter. doing things in 1 go allows interpreter clever optimisations using sse instruction set or gpus.

coming inner product, let's take example of a f.g b a , b vectors , see how f , g applied (in dyalog):

      f←{⎕←(⍕⍺),' f ',⍕⍵ ⋄ ⍺+⍵}       g←{⎕←(⍕⍺),' g ',⍕⍵ ⋄ ⍺×⍵}       0 1 2 3 4 f.g 5 6 7 8 9 4 g 9 3 g 8 24 f 36 2 g 7 14 f 60 1 g 6 6 f 74 0 g 5 0 f 80 80 

you can see above calls f , g interleaved. interpreter apples f , reduces on g simultaneously, in 1 pass, avoiding creation of temporary array, f/ g b do.

another example: http://archive.vector.org.uk/art10500200

you can test performance of different solutions , see 1 works best:

      )copy dfns.dws cmpx       ⍝ or: ")copy dfns cmpx" if using windows       hms ← 3 44 29       cmpx '(3600×hms[1]) + (60×hms[2]) + hms[3]' '+/ 3600 60 1 × hms' '3600 60 1 +.× hms' '60⊥hms'   (3600×hms[1]) + (60×hms[2]) + hms[3] → 2.7e¯6 |   0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕   +/ 3600 60 1 × hms                   → 9.3e¯7 | -66% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕   3600 60 1 +.× hms                    → 8.9e¯7 | -68% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕   60⊥hms                               → 4.8e¯7 | -83% ⎕⎕⎕⎕⎕⎕⎕ 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -