Skip to content

Commit

Permalink
More safeguards against numerical ridiculousness.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmyklebu committed Apr 21, 2014
1 parent c288b6a commit ac673bd
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ object NNLSbyPCG {
def steplen(dir: DoubleMatrix, resid: DoubleMatrix): Double = {
val top = SimpleBlas.dot(dir, resid)
SimpleBlas.gemv(1.0, ata, dir, 0.0, scratch)
top / SimpleBlas.dot(scratch, dir)
// Push the denominator upward very slightly to avoid infinities and silliness
top / (SimpleBlas.dot(scratch, dir) + 1e-20)
}

// stopping condition
def stop(step: Double, ndir: Double, nx: Double): Boolean = {
((step != step)
|| (step < 1e-6)
|| (ndir < 1e-12 * nx))
((step != step) // NaN
|| (step < 1e-6) // too small or negative
|| (step > 1e40) // too small; almost certainly numerical problems
|| (ndir < 1e-12 * nx) // gradient relatively too small
|| (ndir < 1e-32) // gradient absolutely too small; numerical issues may lurk
)
}

val grad = new DoubleMatrix(n, 1)
Expand Down Expand Up @@ -134,5 +138,4 @@ object NNLSbyPCG {
}
x.data
}

}

0 comments on commit ac673bd

Please sign in to comment.