Return jacobians and gradients to the user.

1. Added CRSMatrix object which will store the initial
   and final jacobians if requested by the user.
2. Conversion routine and test for converting a
   CompressedRowSparseMatrix to CRSMatrix.
3. New Evaluator::Evaluate function to do the actual evaluation.
4. Changes to Program::StateVectorToParmeterBlocks and
   Program::SetParameterBlockStatePtrstoUserStatePtrs so that
   they do not try to set the state of constant parameter blocks.
5. Tests for Evaluator::Evaluate.
6. Minor cleanups in SolverImpl.
7. Minor cpplint cleanups triggered by this CL.

Change-Id: I3ac446484692f943c28f2723b719676f8c83ca3d
diff --git a/internal/ceres/evaluator.cc b/internal/ceres/evaluator.cc
index ea05aef..5e64a92 100644
--- a/internal/ceres/evaluator.cc
+++ b/internal/ceres/evaluator.cc
@@ -28,14 +28,18 @@
 //
 // Author: keir@google.com (Keir Mierle)
 
+#include <vector>
 #include <glog/logging.h>
-#include "ceres/evaluator.h"
 #include "ceres/block_evaluate_preparer.h"
 #include "ceres/block_jacobian_writer.h"
 #include "ceres/compressed_row_jacobian_writer.h"
-#include "ceres/scratch_evaluate_preparer.h"
+#include "ceres/compressed_row_sparse_matrix.h"
+#include "ceres/crs_matrix.h"
 #include "ceres/dense_jacobian_writer.h"
+#include "ceres/evaluator.h"
+#include "ceres/internal/port.h"
 #include "ceres/program_evaluator.h"
+#include "ceres/scratch_evaluate_preparer.h"
 
 namespace ceres {
 namespace internal {
@@ -67,5 +71,76 @@
   }
 }
 
+bool Evaluator::Evaluate(Program* program,
+                         int num_threads,
+                         double* cost,
+                         vector<double>* residuals,
+                         vector<double>* gradient,
+                         CRSMatrix* output_jacobian) {
+  CHECK_GE(num_threads, 1)
+      << "This is a Ceres bug; please contact the developers!";
+  CHECK_NOTNULL(cost);
+
+  // Setup the Parameter indices and offsets before an evaluator can
+  // be constructed and used.
+  program->SetParameterOffsetsAndIndex();
+
+  Evaluator::Options evaluator_options;
+  evaluator_options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
+  evaluator_options.num_threads = num_threads;
+
+  string error;
+  scoped_ptr<Evaluator> evaluator(
+      Evaluator::Create(evaluator_options, program, &error));
+  if (evaluator.get() == NULL) {
+    LOG(ERROR) << "Unable to create an Evaluator object. "
+               << "Error: " << error
+               << "This is a Ceres bug; please contact the developers!";
+    return false;
+  }
+
+  if (residuals !=NULL) {
+    residuals->resize(evaluator->NumResiduals());
+  }
+
+  if (gradient != NULL) {
+    gradient->resize(evaluator->NumEffectiveParameters());
+  }
+
+  scoped_ptr<CompressedRowSparseMatrix> jacobian;
+  if (output_jacobian != NULL) {
+    jacobian.reset(
+        down_cast<CompressedRowSparseMatrix*>(evaluator->CreateJacobian()));
+  }
+
+  // Point the state pointers to the user state pointers. This is
+  // needed so that we can extract a parameter vector which is then
+  // passed to Evaluator::Evaluate.
+  program->SetParameterBlockStatePtrsToUserStatePtrs();
+
+  // Copy the value of the parameter blocks into a vector, since the
+  // Evaluate::Evaluate method needs its input as such. The previous
+  // call to SetParameterBlockStatePtrsToUserStatePtrs ensures that
+  // these values are the ones corresponding to the actual state of
+  // the parameter blocks, rather than the temporary state pointer
+  // used for evaluation.
+  Vector parameters(program->NumParameters());
+  program->ParameterBlocksToStateVector(parameters.data());
+
+  if (!evaluator->Evaluate(parameters.data(),
+                           cost,
+                           residuals != NULL ? &(*residuals)[0] : NULL,
+                           gradient != NULL ? &(*gradient)[0] : NULL,
+                           jacobian.get())) {
+    return false;
+  }
+
+  if (output_jacobian != NULL) {
+    jacobian->ToCRSMatrix(output_jacobian);
+  }
+
+  return true;
+}
+
 }  // namespace internal
 }  // namespace ceres