docs: public boolean optimize(ResidualFunction function, DMatrixRMaj parameters )
      {
          configure(function,parameters.getNumElements());
  
          // save the cost of the initial parameters so that it knows if it improves or not
          double previousCost = initialCost = cost(parameters);
  
          // iterate until the difference between the costs is insignificant
          double lambda = initialLambda;
  
          // if it should recompute the Jacobian in this iteration or not
          boolean computeHessian = true;
  
          for( int iter = 0; iter < maxIterations; iter++ ) {
              if( computeHessian ) {
                  // compute some variables based on the gradient
                  computeGradientAndHessian(parameters);
                  computeHessian = false;
  
                  // check for convergence using gradient test
                  boolean converged = true;
                  for (int i = 0; i < g.getNumElements(); i++) {
                      if( Math.abs(g.data[i]) > gtol ) {
                          converged = false;
                          break;
                      }
                  }
                  if( converged )
                      return true;
              }
  
              // H = H + lambda*I
              for (int i = 0; i < H.numRows; i++) {
                  H.set(i,i, Hdiag.get(i) + lambda);
              }
  
              // In robust implementations failure to solve is handled much better
              if( !CommonOps_DDRM.solve(H, g, negativeStep) ) {
                  return false;
              }
  
              // compute the candidate parameters
              CommonOps_DDRM.subtract(parameters, negativeStep, candidateParameters);
  
              double cost = cost(candidateParameters);
              if( cost <= previousCost ) {
                  // the candidate parameters produced better results so use it
                  computeHessian = true;
                  parameters.set(candidateParameters);
  
                  // check for convergence
                  // ftol <= (cost(k) - cost(k+1))/cost(k)
                  boolean converged = ftol*previousCost >= previousCost-cost;
  
                  previousCost = cost;
                  lambda /= 10.0;
  
                  if( converged ) {
                      return true;
                  }
              } else {
                  lambda *= 10.0;
              }
  
          }
          finalCost = previousCost;
          return true;
      }