Module: MachineLearningWorkbench::Monkey::MatrixApproximatable

Defined in:
lib/machine_learning_workbench/monkey.rb

Instance Method Summary collapse

Instance Method Details

#approximates?(other, epsilon = 1e-5) ⇒ Boolean

Verifies if all values at corresponding indices approximate each other.

Parameters:

  • other (NMatrix)
  • epsilon (Float) (defaults to: 1e-5)

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/machine_learning_workbench/monkey.rb', line 176

def approximates? other, epsilon=1e-5
  return false unless self.shape == other.shape
  # two ways to go here:
  # - epsilon is aggregated: total cumulative accepted error
  #   => `(self - other).reduce(:+) < epsilon`
  # - epsilon is local: per element accepted error
  #   => `v.approximates? other[*idxs], epsilon`
  # Given the use I make (near-equality), I choose the first interpretation
  # Note the second is sensitive to opposite signs balancing up
  self.each_stored_with_indices.all? do |v,*idxs|
    v.approximates? other[*idxs], epsilon
  end
end