Class: Hornetseye::MultiArray
- Extended by:
- MultiArrayConstructor
- Defined in:
- lib/multiarray/multiarray.rb
Overview
This class provides methods for initialising multi-dimensional arrays
Class Method Summary collapse
-
.[](*args) ⇒ Node
Convert Ruby array to uniform multi-dimensional array.
-
.import(typecode, string, *shape) ⇒ Node
Import array from string.
-
.laplacian_of_gaussian(sigma = 1.4, size = 9) ⇒ Object
Compute Laplacian of Gaussian filter.
-
.new(element_type, *array_shape) ⇒ Node
Create new multi-dimensional array.
Instance Method Summary collapse
Methods included from MultiArrayConstructor
Class Method Details
.[](*args) ⇒ Node
Convert Ruby array to uniform multi-dimensional array
Type matching is used to find a common element type. Furthermore the required shape of the array is determined. Finally the elements are coopied to the resulting array.
80 81 82 83 |
# File 'lib/multiarray/multiarray.rb', line 80 def []( *args ) target = Node.fit args target[ *args ] end |
.import(typecode, string, *shape) ⇒ Node
Import array from string
Create an array from raw data provided as a string.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/multiarray/multiarray.rb', line 60 def import( typecode, string, *shape ) t = Hornetseye::MultiArray typecode, *shape if string.is_a? Malloc memory = string else memory = Malloc.new t.storage_size memory.write string end t.new memory end |
.laplacian_of_gaussian(sigma = 1.4, size = 9) ⇒ Object
Compute Laplacian of Gaussian filter
@return The filter.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/multiarray/multiarray.rb', line 91 def laplacian_of_gaussian( sigma = 1.4, size = 9 ) def erf( x, sigma ) 0.5 * Math.erf( x / ( Math.sqrt( 2.0 ) * sigma.abs ) ) end def gauss_gradient( x, sigma ) -x / ( Math.sqrt( 2.0 * Math::PI * sigma.abs**5 ) ) * Math.exp( -x**2 / ( 2.0 * sigma**2 ) ) end retval = new DFLOAT, size, size sum = 0 for y in 0 .. size - 1 y0 = y - 0.5 * size y1 = y0 + 1 y_grad_diff = gauss_gradient( y1, sigma ) - gauss_gradient( y0, sigma ) y_erf_diff = erf( y1, sigma ) - erf( y0, sigma ) for x in 0..size-1 x0 = x - 0.5 * size x1 = x0 + 1 x_grad_diff = gauss_gradient( x1, sigma ) - gauss_gradient( x0, sigma ) x_erf_diff = erf( x1, sigma ) - erf( x0, sigma ) retval[ y, x ] = y_grad_diff * x_erf_diff + y_erf_diff * x_grad_diff end end retval end |
.new(element_type, *array_shape) ⇒ Node
Create new multi-dimensional array
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/multiarray/multiarray.rb', line 31 def new( element_type, *array_shape ) typecode = element_type.typecode shape = element_type.shape + array_shape = shape.last.is_a?( Hash ) ? shape.pop : {} count = [ :count ] || 1 if shape.empty? memory = [ :memory ] || typecode.memory_type.new( typecode.storage_size * count ) Hornetseye::Pointer( typecode ).new memory else size = shape.pop stride = shape.inject( 1 ) { |a,b| a * b } Hornetseye::lazy( size ) do |index| pointer = new typecode, *( shape + [ :count => count * size, :memory => [ :memory ] ] ) Lookup.new pointer, index, INT.new( stride ) end end end |