Re: Is there a fold function in Julia

1,230 views
Skip to first unread message

Patrick O'Leary

unread,
Sep 29, 2012, 8:16:56 AM9/29/12
to juli...@googlegroups.com
In imperative parlance, you're looking for reduce(). Quoting the standard library reference:

reduce(op, v0, itr)

Reduce the given collection with the given operator, i.e. accumulate v = op(v,elt) for each element, where v starts as v0. Reductions for certain commonly-used operators are available in a more convenient 1-argument form: max(itr), min(itr), sum(itr), prod(itr), any(itr), all(itr).

HTH!

On Saturday, September 29, 2012 5:36:25 AM UTC-5, sth4nth wrote:
I glanced over the doc and did not find such a thing. I hope there will be more high order functions in Julia, since they are so useful for making the code shorter.

sth4nth

unread,
Sep 29, 2012, 8:52:21 AM9/29/12
to juli...@googlegroups.com
Is there any function to fold a matrix along a specified direction like:
reduce(+,zeros(size(A,2)),A,2)
which is equivalent to
sum(A,2)

Patrick O'Leary於 2012年9月29日星期六UTC+8下午8時16分56秒寫道:

sth4nth

unread,
Sep 29, 2012, 2:49:20 PM9/29/12
to juli...@googlegroups.com
Since no one replied my post, I guess the answer is no. However I insist, we should have these high order functions (map, reduce, etc.) to work with arbitrary multidimensional arrays along any dimensions. Many other function can be simplified with them. For example, the bsxfun is simply a map on matrix after all.

Stefan Karpinski

unread,
Sep 29, 2012, 2:55:15 PM9/29/12
to juli...@googlegroups.com
The amap function might be what you want:

julia> X = randn(10,10)
10x10 Float64 Array:
  0.397156  -0.796456    1.82355    0.394129  :   0.513475  -0.370503    0.332741 
 -0.781556  -0.0584675  -1.96832   -0.506893      1.32597   -1.65392    -2.16575  
  0.561797  -1.43147     1.316      1.11655      -0.799299   0.0273233   1.06115  
 -0.3356     0.276764    1.65696   -0.826575      0.668099  -1.73315    -0.999732 
 -0.171138  -0.765757    0.7234    -0.674408     -1.36367    1.20259    -1.52304  
  0.300607   0.246647   -0.416823  -1.2961    :   1.26605   -0.148389    1.52666  
  0.454862  -1.48598    -1.60415   -0.697126     -0.259474  -1.12126    -0.0186562
 -1.42273    1.02565    -1.25794    0.154765      0.170823   1.23981    -0.768643 
  0.990472  -1.59111     0.427652   0.640483      0.126559  -1.03455     1.22607  
  0.326189   1.4725      0.109597  -0.239435     -0.429424  -0.972138    2.70702  

julia> amap(sum,X,1)
10-element Float64 Array:
  5.05391 
 -2.51706 
  1.79049 
  3.762   
 -5.44134 
  1.21872 
 -3.64299 
  0.238103
  3.30936 
  1.12233 

julia> amap(sum,X,2)
10-element Float64 Array:
  0.32006 
 -3.10767 
  0.809929
 -1.93461 
  5.39081 
  3.38843 
  1.99382 
  1.21911 
 -4.56418 
  1.37782 

--
 
 
 

John Myles White

unread,
Sep 29, 2012, 2:58:44 PM9/29/12
to juli...@googlegroups.com
It would be good to have a "higher-order programming" section in the manual that highlights functions like this.

 -- John

--
 
 
 

sth4nth

unread,
Sep 29, 2012, 3:13:08 PM9/29/12
to juli...@googlegroups.com
Cool! That's exactly what I want! I also found the areduce function. However, it seems these functions are not well documented. I cannot found them here (http://docs.julialang.org/en/latest/genindex/). I think John's proposal is great. It'd better highlight them in the manual. Thanks.
Reply all
Reply to author
Forward
0 new messages