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.
Asked
Active
Viewed 143 times
1 Answers
1
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.

masa16
- 461
- 3
- 5
-
Thanks masa. I don't know how NArray is implemented, but my limited C experience tells me that it should be possible to realloc && resize the memory block holding the NArray. Perhaps in the future? – maasha Mar 14 '12 at 13:04
-
My comment above is true for realloc. When allocating larger size, the realloc function will return a different pointer. – masa16 Mar 14 '12 at 13:28