Reducing over Point objects

Julia has many statistics-computing functions, typically a “reduce” kind of function, that can take either a simple vector, or go over an array and compute the output over row, columns, etc according to the dims parameter.

Is there a version for this that goes over a collection of vectors, tuples or Points?

According to my understanding, the function map is way more useful than reduce in your statistics context. Because reduce is for 2-arg functions e.g. * and +.

Could you give some examples of what exactly you want to achieve? I would expect reduce to already work for a well-chosen function.

julia> x = rand(Tuple{Float64, Float64}, 3)
3-element Vector{Tuple{Float64, Float64}}:
 (0.25205814867458654, 0.14284143829270268)
 (0.10081672872462866, 0.5909316913764835)
 (0.14787926347212343, 0.4637800398205445)

julia> reduce((a, b) -> max.(a, b), x)
(0.25205814867458654, 0.5909316913764835)

julia> reduce((a, b) -> a .+ b, x)
(0.5007541408713386, 1.1975531694897308)
2 Likes

You can also just do reduce(.+, x).

If your type supports a + function, like a vector, you can just use a function like sum and it will work.

1 Like

Admittedly, reduce is in some cases fast. But I still don’t think it is proper to be used in statistics, e.g.

julia> v = [1,2,3,4]; # a vector of samples

julia> maximum(abs2, v)
16

julia> maximum(abs2, reverse(v))
16

julia> reduce((a, b) -> max(abs2(a), abs2(b)), v)
256

julia> reduce((a, b) -> max(abs2(a), abs2(b)), reverse(v))
65536

As the doc suggests, reduce is almost exclusively only for +, *, max, min.

Well, these are just two different things that are being computed. Not sure if that makes reduce more or less suitable for statistics than map

But we don’t even have to choose between map and reduce, we can just use mapreduce :grin:

julia> v = [1,2,3,4];

julia> mapreduce(abs2, max, v)
16

julia> mapreduce(abs2, max, reverse(v))
16

I’m not sure about this either, but the docs do mention explicitly that maximum, sum, etc. should be preferred over writing out reduce(max, ...), reduce(+, ...) etc. if possible. So in that example, I agree that maximum(abs2, v) is more intuitive.

1 Like