Class: Hornetseye::MultiArray

Inherits:
Object
  • Object
show all
Extended by:
MultiArrayConstructor
Defined in:
lib/multiarray/multiarray.rb

Overview

This class provides methods for initialising multi-dimensional arrays

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MultiArrayConstructor

constructor_shortcut

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.

Parameters:

Returns:

  • (Node)

    Uniform multi-dimensional 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.

Parameters:

  • typecode (Class)

    Type of the elements in the string.

  • string (String)

    String with raw data.

  • shape (Array<Integer>)

    Array with dimensions of array.

Returns:

  • (Node)

    Multi-dimensional array with imported data.



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.

Parameters:

  • sigma (Float) (defaults to: 1.4)

    Spread of filter.

  • size (Integer) (defaults to: 9)

    Size of filter (e.g. 9 for 9x9 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

Parameters:

  • element_type (Class)

    The type of the elements.

  • *array_shape (Array<Integer>)

    The shape of the multi-dimensional array.

Returns:

  • (Node)

    Returns uninitialised native 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
  options = shape.last.is_a?( Hash ) ? shape.pop : {}
  count = options[ :count ] || 1
  if shape.empty?
    memory = options[ :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 => options[ :memory ] ] )
      Lookup.new pointer, index, INT.new( stride )
    end
  end
end

Instance Method Details

#erf(x, sigma) ⇒ Object



92
93
94
# File 'lib/multiarray/multiarray.rb', line 92

def erf( x, sigma )
  0.5 * Math.erf( x / ( Math.sqrt( 2.0 ) * sigma.abs ) )
end

#gauss_gradient(x, sigma) ⇒ Object



95
96
97
98
# File 'lib/multiarray/multiarray.rb', line 95

def gauss_gradient( x, sigma )
  -x / ( Math.sqrt( 2.0 * Math::PI * sigma.abs**5 ) ) *
    Math.exp( -x**2 / ( 2.0 * sigma**2 ) )
end