You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my case, meshgrid is the most frequently used feature that Numo:: NArray doesn't have.
I would appreciate it if you could consider the implementation.
Nice. But is this exactly the same as numpy's meshgrid?
(Even though the keyword argument is obviously less than numpy's meshgrid)
I think this should be added to NArray if there are no performance issues. meshgrid is used a lot.
There are a lot of methods in the NArray class that have been implemented in Ruby.
Important methods such as hstack and vstack are also implemented in Ruby.
I don't think you have to necessarily implement meshgrid in C.
This is a sample implementation of meshgrid in Ruby that can accept full numpy's options.
require"numo/narray"classNumo::NArray# full versiondefself.meshgrid(*axes,indexing: "xy",copy: true,sparse: false)ifsparse != trueandcopy != trueraiseNotImplementedError,%{can not create coordinate array as a view into the original axis vector}endcaseindexingwhen"xy"# no operationwhen"ij"axes=axes.map{|axis| axis.seq}elseraiseArgumentError,%{indexing option should be one of "xy" and "ij"}endshape=axes.map(&:size)ifsparseaxes.map.with_indexdo |axis,k|
extended_shape=Array.new(shape.size){ |i| (i == k) ? axis.size : 1}ifcopyaxis.reshape(*extended_shape)elseaxis.reshape!(*extended_shape)endendelseaxes.map.with_indexdo |axis,k|
extended_shape=Array.new(shape.size){ |i| (i == k) ? axis.size : 1}ifcopyaxis.class.new(*shape).store(axis.reshape(*extended_shape))else# Currently, the behavior for here can not be implemented because of the lack of # the ability to generate a view that represent a repetition of the original vector.endendendendend
In Numo::NArray, the behavior specified by the option copy=false and sparse=false can not be implemented because of the lack of the ability to generate a view that represent a repetition of the original vector. On the other hand, the behavior for copy=false and sparse=true can be simulated using reshape! method.
Currently, I think it's better to leave out the copy option from the method because it's confusing. In that case, if sparse=true is specified, it's better to keep the behavior corresponding to copy=false (the one that uses #reshape!) to save memory. This is because it is considered that few people change the contents of the sparse grid arrays returned from meshgrid.
The latter simple verions is here,
classNumo::NArray# simple version witout copy optiondefself.meshgrid(*axes,indexing: "xy",sparse: false)caseindexingwhen"xy"# no operationwhen"ij"axes=axes.map{|axis| axis.seq}elseraiseArgumentError,%{indexing option should be one of "xy" and "ij"}endshape=axes.map(&:size)ifsparseaxes.map.with_indexdo |axis,k|
extended_shape=Array.new(shape.size){ |i| (i == k) ? axis.size : 1}axis.reshape!(*extended_shape)endelseaxes.map.with_indexdo |axis,k|
extended_shape=Array.new(shape.size){ |i| (i == k) ? axis.size : 1}axis.class.new(*shape).store(axis.reshape(*extended_shape))endendendend
Sorry. I misunderstood the order of the repetitive dimensions. Here's the corrected version.
classNumo::NArray# simple version witout copy optiondefself.meshgrid(*axes,indexing: "xy",sparse: false)caseindexingwhen"xy"# no operationwhen"ij"axes=axes.map{|axis| axis.seq}elseraiseArgumentError,%{indexing option should be one of "xy" and "ij"}endshape=axes.map(&:size)ifsparseaxes.map.with_indexdo |axis,k|
extended_shape=(shape.size-1).downto(0).map{ |i| (i == k) ? axis.size : 1}axis.reshape!(*extended_shape)endelseaxes.map.with_indexdo |axis,k|
extended_shape=(shape.size-1).downto(0).map{ |i| (i == k) ? axis.size : 1}axis.class.new(*shape).store(axis.reshape(*extended_shape))# broadcasting to `shape` occurs hereendendendend
classNumo::NArray# simple version witout copy optiondefself.meshgrid(*axes,indexing: "xy",sparse: false)caseindexingwhen"xy"# no operationwhen"ij"axes=axes.map{|axis| axis.seq}elseraiseArgumentError,%{indexing option should be one of "xy" and "ij"}endshape=axes.map(&:size).reverseifsparseaxes.map.with_indexdo |axis,k|
extended_shape=(shape.size-1).downto(0).map{ |i| (i == k) ? axis.size : 1}axis.reshape!(*extended_shape)endelseaxes.map.with_indexdo |axis,k|
extended_shape=(shape.size-1).downto(0).map{ |i| (i == k) ? axis.size : 1}axis.class.new(*shape).store(axis.reshape(*extended_shape))# broadcasting to `shape` occurs hereendendendend
Activity
kojix2 commentedon Mar 1, 2020
Honestly, I don't know the right way to create a 3D mesh grid with Numo::NArray.
himotoyoshi commentedon Aug 4, 2020
This is a sample implementation of
meshgridthat works for any dimensions and any data types.kojix2 commentedon Aug 5, 2020
Nice. But is this exactly the same as numpy's
meshgrid?(Even though the keyword argument is obviously less than numpy's meshgrid)
I think this should be added to NArray if there are no performance issues.
meshgridis used a lot.himotoyoshi commentedon Aug 5, 2020
Sorry, it is not.
It is just a sample code for your comment as a Proof of Concept in Ruby level (not in C-extension).
More works are needed to realize the same behavior of numpy's meshgrid and also need the input check.
kojix2 commentedon Aug 5, 2020
Good.
There are a lot of methods in the NArray class that have been implemented in Ruby.
Important methods such as
hstackandvstackare also implemented in Ruby.I don't think you have to necessarily implement meshgrid in C.
https://github.com/ruby-numo/numo-narray/blob/master/lib/numo/narray/extra.rb
himotoyoshi commentedon Aug 5, 2020
This is a sample implementation of
meshgridin Ruby that can accept full numpy's options.In Numo::NArray, the behavior specified by the option
copy=falseandsparse=falsecan not be implemented because of the lack of the ability to generate a view that represent a repetition of the original vector. On the other hand, the behavior forcopy=falseandsparse=truecan be simulated usingreshape!method.Currently, I think it's better to leave out the
copyoption from the method because it's confusing. In that case, ifsparse=trueis specified, it's better to keep the behavior corresponding tocopy=false(the one that uses#reshape!) to save memory. This is because it is considered that few people change the contents of the sparse grid arrays returned frommeshgrid.The latter simple verions is here,
himotoyoshi commentedon Aug 20, 2020
Sorry. I misunderstood the order of the repetitive dimensions. Here's the corrected version.
himotoyoshi commentedon Aug 21, 2020
Sorry, It wasn't fixed yet.
For example,
It generate the result,
kojix2 commentedon Mar 6, 2021
Another implementation of the mesh grid by @show-o-atakun
The demand for meshgrid is continuing.
I hope that some implementation of meshgrid will be added to NArray.