If I have a Narray with the shape 100, 10000 and want to expand it to say 100, 20000 (basically add rows) what is the proper way to achieve this? To expand massive Narrays I would like to avoid using a temporary Narray for memory reasons.
require "narray"
class NArray
def expand(*new_shape)
na = NArray.new(self.typecode,*new_shape)
range = self.shape.map{|n| 0...n}
na[*range] = self
return na
end
end
p a = NArray.float(2,3).indgen!
# => NArray.float(2,3):
# [ [ 0.0, 1.0 ],
# [ 2.0, 3.0 ],
# [ 4.0, 5.0 ] ]
p a.expand(3,4)
# => NArray.float(3,4):
# [ [ 0.0, 1.0, 0.0 ],
# [ 2.0, 3.0, 0.0 ],
# [ 4.0, 5.0, 0.0 ],
# [ 0.0, 0.0, 0.0 ] ]
There is no general way to expand a memory block without movement. Memory block can be extended only if enough free area follows, but such a case is usually unexpected.