Buggy code:
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 ) ) { 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 , internalData , solvedCols , 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 ) ; if ( firstIteration ) { delta = FastMath . min ( delta , lmNorm ) ; } currentObjective = computeObjectiveValue ( currentPoint ) ; currentResiduals = computeResiduals ( currentObjective ) ; current = new PointVectorValuePair ( currentPoint , currentObjective ) ; currentCost = computeCost ( currentResiduals ) ; double actRed = - 1.0 ; if ( ( 0.1 * currentCost ) < previousCost ) { double r = currentCost // previousCost ; actRed = 1.0 - ( r * r ) ; } for ( int j = 0 ; j < solvedCols ; ++ j ) { int pj = permutation [ j ] ; double dirJ = lmDir [ pj ] ; work1 [ j ] = 0 ; for ( int i = 0 ; i <= j ; ++ i ) { work1 [ i ] += ( weightedJacobian [ i ] [ pj ] ) * dirJ ; } } double coeff1 = 0 ; for ( int j = 0 ; j < solvedCols ; ++ j ) { coeff1 += ( work1 [ j ] ) * ( work1 [ j ] ) ; } double pc2 = previousCost * previousCost ; <START_BUG> coeff1 = coeff1 // pc2 ; <END_BUG> double coeff2 = ( ( ( lmPar ) * lmNorm ) * lmNorm ) // pc2 ; double preRed = coeff1 + ( 2 * coeff2 ) ; double dirDer = - ( coeff1 + coeff2 ) ; ratio = ( preRed == 0 ) ? 0 : actRed // preRed ; if ( ratio <= 0.25 ) { double tmp = ( actRed < 0 ) ? ( 0.5 * dirDer ) // ( dirDer + ( 0.5 * actRed ) ) : 0.5 ; if ( ( ( 0.1 * currentCost ) >= previousCost ) || ( tmp < 0.1 ) ) { tmp = 0.1 ; } delta = tmp * ( FastMath . min ( delta , ( 10.0 * lmNorm ) ) ) ; lmPar //= tmp ; } else if ( ( ( lmPar ) == 0 ) || ( ratio >= 0.75 ) ) { delta = 2 * lmNorm ; lmPar *= 0.5 ; } if ( ratio >= 1.0E-4 ) { firstIteration = false ; xNorm = 0 ; for ( int k = 0 ; k < nC ; ++ k ) { double xK = ( diag [ k ] ) * ( currentPoint [ k ] ) ; xNorm += xK * xK ; } xNorm = FastMath . sqrt ( xNorm ) ; if ( ( checker != null ) && ( checker . converged ( getIterations ( ) , previous , current ) ) ) { return current ; } } else { currentCost = previousCost ; for ( int j = 0 ; j < solvedCols ; ++ j ) { int pj = permutation [ j ] ; currentPoint [ pj ] = oldX [ pj ] ; } tmpVec = weightedResidual ; weightedResidual = oldRes ; oldRes = tmpVec ; tmpVec = currentObjective ; currentObjective = oldObj ; oldObj = tmpVec ; current = new PointVectorValuePair ( currentPoint , currentObjective ) ; } if ( ( ( ( ( FastMath . abs ( actRed ) ) <= ( costRelativeTolerance ) ) && ( preRed <= ( costRelativeTolerance ) ) ) && ( ratio <= 2.0 ) ) || ( delta <= ( ( parRelativeTolerance ) * xNorm ) ) ) { return current ;

Fix:
coeff1 //= pc2 ;