Skip to content

Commit

Permalink
avoid division by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Mar 23, 2015
1 parent 0b48061 commit 2671b10
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 6 additions & 1 deletion ext/nmatrix/math.cpp
Expand Up @@ -435,6 +435,10 @@ namespace nm {

if (M == 2) {
DType det = A[0] * A[lda+1] - A[1] * A[lda];
if (det == 0) {
rb_raise(nm_eNotInvertibleError,
"matrix must have non-zero determinant to be invertible (not getting this error does not mean matrix is invertible if you're dealing with floating points)");
}
B[0] = A[lda+1] / det;
B[1] = -A[1] / det;
B[ldb] = -A[lda] / det;
Expand All @@ -445,7 +449,8 @@ namespace nm {
DType det;
det_exact<DType>(M, A_elements, lda, reinterpret_cast<void*>(&det));
if (det == 0) {
rb_raise(nm_eNotInvertibleError, "matrix must have non-zero determinant to be invertible (not getting this error does not mean matrix is invertible if you're dealing with floating points)");
rb_raise(nm_eNotInvertibleError,
"matrix must have non-zero determinant to be invertible (not getting this error does not mean matrix is invertible if you're dealing with floating points)");
}

B[0] = ( A[lda+1] * A[2*lda+2] - A[lda+2] * A[2*lda+1]) / det; // A = ei - fh
Expand Down
1 change: 0 additions & 1 deletion lib/nmatrix/math.rb
Expand Up @@ -119,7 +119,6 @@ def invert lda=nil, ldb=nil
if self.integer_dtype?
__inverse_exact__(inverse.cast(dtype: :rational128), lda, ldb)
else
dtype = self.dtype
__inverse_exact__(inverse, lda, ldb)
end
end
Expand Down

0 comments on commit 2671b10

Please sign in to comment.