I am trying to generate and solve a matrix that tends towards being singular using variable arbitrary precision to ensure accuracy. Consider, for example, a matrix with tunable singularity:
matrix[n_, prec_] := {{1, 0}, {0, N[1/n, prec]}}
We can use a while loop with Check
to determine the appropriate level of precision needed to avoid a badly conditioned matrix:
prec = 1;
While[
Check[LinearSolve@matrix[10^6, prec], err] === err,
prec = prec + 1;
]
sol = LinearSolve@matrix[10^4, prec]
Running this snippet indicates that prec
must be 8 to avoid a badly conditioned matrix.
This isn't a big deal for this matrix
function, because it's computationally trivial to generate and solve. However, for larger, more expensive to compute matrix
functions, it would be nice to not waste the results of the evaluation of matrix
and the LinearSolve
used in the Check
.
Is there a way to avoid that wasted computation? Additionally, is there a better way to do this automatic precision tuning (other than adjusting the step size for the increase in prec
)?
Check[sol = LinearSolve@matrix[10^6, prec], err] === err
do what you want? $\endgroup$LinearSolve
, so just runningLinearSolve
and monitoring for the warning is sufficient. But, it's a good idea to use the"ConditionNumber"
argument to estimate the precision needed. Going to think on that. $\endgroup$