Class: Numo::NArray

Inherits:
Object
  • Object
show all
Defined in:
lib/spatial_stats/narray_ext.rb

Overview

Extension to Numo::NArray base class.

Instance Method Summary collapse

Instance Method Details

#row_standardizeNumo::NArray

For a 2-D NArray, transform the non-zero values so that the sum of each row is 1.

@ example

Numo::DFloat [[0, 1, 1], [1, 1, 1]].row_standardize
Numo::DFloat [[0, 0.5, 0.5], [0.33333, 0.33333, 0.33333]]

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spatial_stats/narray_ext.rb', line 19

def row_standardize
  # every row will sum up to 1, or if they are all 0, do nothing
  standardized = each_over_axis.map do |row|
    sum = row.sum
    if sum.zero?
      # for some reason, we have to do this instead of just returning
      # row. If row is returned, it is cast as [0,0,0] => [0,1,0] for
      # example.
      self.class.zeros(row.size)
    else
      row / sum
    end
  end
  self.class.cast(standardized)
end

#windowNumo::NArray

For a 2-D, n x n NArray, if the trace is 0, add an n x n eye matrix to the matrix and return the result.

@ example

Numo::DFloat [[0, 1, 0], [1, 0, 1], [0, 1, 0]].window
Numo::DFloat [[1, 1, 0], [1, 1, 1], [0, 1, 1]]

@ example

# Input will be equivalent to output in this case
Numo::DFloat [[1, 1, 0], [1, 0, 1], [0, 1, 0]].window
Numo::DFloat [[1, 1, 0], [1, 0, 1], [0, 1, 0]]

Returns:



50
51
52
53
54
55
56
57
58
# File 'lib/spatial_stats/narray_ext.rb', line 50

def window
  # in windowed calculations, the diagonal is set to 1
  # if trace (sum of diag) is 0, add it, else return input
  if trace.zero?
    self + self.class.eye(shape[0])
  else
    self
  end
end