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.



58
59
60
61
# File 'lib/multiarray/multiarray.rb', line 58

def []( *args )
  target = Node.fit args
  target[ *args ]
end

.import(typecode, data, *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.

  • data (String, Malloc)

    String or memory object with raw data.

  • shape (Array<Integer>)

    Array with dimensions of array.

Returns:

  • (Node)

    Multi-dimensional array with imported data.



38
39
40
41
42
43
44
45
46
47
# File 'lib/multiarray/multiarray.rb', line 38

def import( typecode, data, *shape )
  t = Hornetseye::MultiArray typecode, shape.size
  if data.is_a? Malloc
    memory = data
  else
    memory = Malloc.new t.storage_size(*shape)
    memory.write data
  end
  t.new *(shape + [:memory => 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)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/multiarray/multiarray.rb', line 69

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(typecode, *shape) ⇒ Object



25
26
27
# File 'lib/multiarray/multiarray.rb', line 25

def new(typecode, *shape)
  Hornetseye::MultiArray(typecode, shape.size).new *shape
end

Instance Method Details

#erf(x, sigma) ⇒ Object



70
71
72
# File 'lib/multiarray/multiarray.rb', line 70

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

#gauss_gradient(x, sigma) ⇒ Object



73
74
75
76
# File 'lib/multiarray/multiarray.rb', line 73

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