Buggy code:
public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer { private int solvedCols ; private double [ ] diagR ; private double [ ] jacNorm ; private double [ ] beta ; private int [ ] permutation ; private int rank ; private double lmPar ; private double [ ] lmDir ; private final double initialStepBoundFactor ; private final double costRelativeTolerance ; private final double parRelativeTolerance ; private final double orthoTolerance ; private final double qrRankingThreshold ; private double [ ] weightedResidual ; private double [ ] [ ] weightedJacobian ; public LevenbergMarquardtOptimizer ( ) { } public LevenbergMarquardtOptimizer ( ConvergenceChecker < PointVectorValuePair > checker ) { } public LevenbergMarquardtOptimizer ( double initialStepBoundFactor , ConvergenceChecker < PointVectorValuePair > checker , double costRelativeTolerance , double parRelativeTolerance , double orthoTolerance , double threshold ) { } public LevenbergMarquardtOptimizer ( double costRelativeTolerance , double parRelativeTolerance , double orthoTolerance ) { } public LevenbergMarquardtOptimizer ( double initialStepBoundFactor , double costRelativeTolerance , double parRelativeTolerance , double orthoTolerance , double threshold ) { } @ Override protected PointVectorValuePair doOptimize ( ) { final int nR = getTarget ( ) . length ; final double [ ] currentPoint = getStartPoint ( ) ; final int nC = currentPoint . length ; solvedCols = FastMath . min ( nR , nC ) ; diagR = new double [ nC ] ; jacNorm = new double [ nC ] ; beta = new double [ nC ] ; permutation = new int [ nC ] ; lmDir = new double [ nC ] ; double delta = 0 ; double xNorm = 0 ; double [ ] diag = new double [ nC ] ; double [ ] oldX = new double [ nC ] ; double [ ] oldRes = new double [ nR ] ; double [ ] oldObj = new double [ nR ] ; double [ ] qtf = new double [ nR ] ; double [ ] work1 = new double [ nC ] ; double [ ] work2 = new double [ nC ] ; double [ ] work3 = new double [ nC ] ; final RealMatrix weightMatrixSqrt = getWeightSquareRoot ( ) ; double [ ] currentObjective = computeObjectiveValue ( currentPoint ) ; double [ ] currentResiduals = computeResiduals ( currentObjective ) ; PointVectorValuePair current = new PointVectorValuePair ( currentPoint , currentObjective ) ; double currentCost = computeCost ( currentResiduals ) ; lmPar = 0 ; boolean firstIteration = true ; int iter = 0 ; final ConvergenceChecker < PointVectorValuePair > checker = getConvergenceChecker ( ) ; while ( true ) { ++ iter ; final PointVectorValuePair previous = current ; <START_BUG> qrDecomposition ( computeJacobian ( currentPoint ) ) ; <END_BUG> weightedResidual = weightMatrixSqrt . operate ( currentResiduals ) ; for ( int i = 0 ; i < nR ; i ++ ) { qtf [ i ] = weightedResidual [ i ] ; } qTy ( qtf ) ; for ( int k = 0 ; k < ( solvedCols ) ; ++ k ) { int pk = permutation [ k ] ; weightedJacobian [ k ] [ pk ] = diagR [ pk ] ; } if ( firstIteration ) { xNorm = 0 ; for ( int k = 0 ; k < nC ; ++ k ) { double dk = jacNorm [ k ] ; if ( dk == 0 ) { dk = 1.0 ; } double xk = dk * ( currentPoint [ k ] ) ; xNorm += xk * xk ; diag [ k ] = dk ; } xNorm = FastMath . sqrt ( xNorm ) ; delta = ( xNorm == 0 ) ? initialStepBoundFactor : ( initialStepBoundFactor ) * xNorm ; } double maxCosine = 0 ; if ( currentCost != 0 ) { for ( int j = 0 ; j < ( solvedCols ) ; ++ j ) { int pj = permutation [ j ] ; double s = jacNorm [ pj ] ; if ( s != 0 ) { double sum = 0 ; for ( int i = 0 ; i <= j ; ++ i ) { sum += ( weightedJacobian [ i ] [ pj ] ) * ( qtf [ i ] ) ; } maxCosine = FastMath . max ( maxCosine , ( ( FastMath . abs ( sum ) ) // ( s * currentCost ) ) ) ; } } } if ( maxCosine <= ( orthoTolerance ) ) { setCost ( currentCost ) ; return current ; } for ( int j = 0 ; j < nC ; ++ j ) { diag [ j ] = FastMath . max ( diag [ j ] , jacNorm [ j ] ) ; } for ( double ratio = 0 ; ratio < 1.0E-4 ; ) { for ( int j = 0 ; j < ( solvedCols ) ; ++ j ) { int pj = permutation [ j ] ; oldX [ pj ] = currentPoint [ pj ] ; } final double previousCost = currentCost ; double [ ] tmpVec = weightedResidual ; weightedResidual = oldRes ; oldRes = tmpVec ; tmpVec = currentObjective ; currentObjective = oldObj ; oldObj = tmpVec ; determineLMParameter ( qtf , delta , diag , work1 , work2 , work3 ) ; double lmNorm = 0 ; for ( int j = 0 ; j < ( solvedCols ) ; ++ j ) { int pj = permutation [ j ] ; lmDir [ pj ] = - ( lmDir [ pj ] ) ; currentPoint [ pj ] = ( oldX [ pj ] ) + ( lmDir [ pj ] ) ; double s = ( diag [ pj ] ) * ( lmDir [ pj ] ) ; lmNorm += s * s ; } lmNorm = FastMath . sqrt ( lmNorm ) ;

Fix:
qrDecomposition ( computeWeightedJacobian ( currentPoint ) ) ;