Question about Trains

General APL language issues

Question about Trains

Postby PGilbert on Sun May 17, 2015 12:04 am

The following is working as expected (using ⎕ML←3):

Code: Select all
      mat←5 3⍴⍳15

      mat
 1  2  3
 4  5  6
 7  8  9
10 11 12
13 14 15

    +/¨(↓mat)∊¨⊂1 8 9 32
1 0 2 0 0


it gives the sum of the occurrence of 1 8 9 32 in each rows of mat. Is there is a faster way to get the same results with a train function ? In our application 'mat' has millions of rows. I have read the documentation but I can't make it work.
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Question about Trains

Postby Roger|Dyalog on Sun May 17, 2015 4:38 am

For speed on large arguments, trains are almost irrelevant. Using trains vs. alternatives can reduce interpretative overhead, but that overhead would be relatively tiny on large arguments.

Instead, for the particular problem, try:

Code: Select all
      mat←5 3⍴⍳15
      +/ mat ∊ 1 8 9 32
1 0 2 0 0

The key here, as in other computations where speed is paramount, is to allow each primitive to "see" arguments that are as large as possible. When you split the arguments, enclose pieces, etc., then the pieces presented to each primitive are small.

Let r c ← ⍴mat be the number of rows and columns of mat. In (↓mat)∊¨blah, ∊ is invoked r times with vectors of length c; in mat∊blah, ∊ is invoked once on a left argument of size r×c. Likewise for +/ vs. +/¨. Plus the cost of ↓mat.
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Question about Trains

Postby PGilbert on Sun May 17, 2015 12:17 pm

Thanks Roger, this is answering my question very well.
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest