Skip to content

Conversation

@alshedivat
Copy link

@alshedivat alshedivat commented May 27, 2024

when qrot[r, r] is close to 0, ruby might represent it with a small negative value (e.g., -2.8475347e-16), which results in error when passed to Math.sqrt. adding a small epsilon constant to qrot[r, r] inside Math.sqrt solves the issue.

@cardmagic
Copy link
Contributor

FYI, the original classifier gem (from which classifier-reborn was forked) already addresses this numerical stability issue with a slightly different approach:

singular_values = q_rotation_matrix.row_size.times.map do |r|
  Math.sqrt([q_rotation_matrix[r, r].to_f, 0.0].max)
end

Instead of adding an epsilon, it clamps negative values to zero using [value, 0.0].max before taking the square root. This avoids artificially inflating zero singular values while still preventing the Math.sqrt error from tiny negative floating point values.

See: https://github.com/cardmagic/classifier/blob/master/lib/classifier/extensions/vector.rb#L110-L112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants