ruby-numo / numo-narray

Ruby/Numo::NArray - New NArray class library
http://ruby-numo.github.io/narray/
BSD 3-Clause "New" or "Revised" License
415 stars 41 forks source link

Numo::NArray#map_over_axis #133

Closed kojix2 closed 4 years ago

kojix2 commented 5 years ago

Hello. Enhancement

map_over_axis

positive opinion

I want map_over_axis.

narray.rank # => 2
narray.map(axis: 0) do |na|
  idx = na.eq(0).where
  sum = na.sum
  mean = sum / (na.size - idx.size)
  na[idx] = mean
end

This method will not very fast because it will be implemented in Ruby, not C.

map or collect is one of the most popular methods among Rubyists. Block is an impressive feature of Ruby.

But Narray is a library of matrix and vector operations. So map and collect may not always be recommended.

Ruby accepts a variety of ideas. "apply" and "lambda".

foo = -> (x) { x + x }
narray.apply(foo)

We have each_over_axis, So it will be map_over_axis.

a possible negative opinion

Users of narray should avoid using map. If you implement "map_over_axis", the rubyst will tend to try to compute everything with map, which in turn will impede their fast computation. As a scientific computational library, NArray should not introduce another paradigm such as Ruby array behavior. Once you add a bad idea, it's hard to undo. You should be consistent.


I use https://miraitranslate.com/trial/

masa16 commented 5 years ago

This is an aside from your proposal, but your example can be written without loop as

idx = narray.eq(0).where
narray[idx] = Float::NAN
mean = narray.mean(axis:0, nan:true)
axis = narray.new_narray.seq % narray.shape[1]
narray[idx] = mean[axis[idx]]
kojix2 commented 5 years ago

Wow, last two lines look like a magic.

The user who tries to execute anything with map is me.