mirror of
https://github.com/ceres-solver/ceres-solver.git
synced 2026-08-29 08:34:37 +08:00
Write a MATLAB/Octave script for readling in the linear least squares problem
This commit is contained in:
@@ -680,30 +680,58 @@ bool DumpLinearLeastSquaresProblemToTextFile(const string& directory,
|
||||
string filename_prefix =
|
||||
StringPrintf(format_string.c_str(), iteration);
|
||||
|
||||
LOG(INFO) << "writing to: " << filename_prefix << "*";
|
||||
|
||||
string matlab_script;
|
||||
StringAppendF(&matlab_script,
|
||||
"function lsqp = lm_iteration_%03d()\n",
|
||||
iteration,
|
||||
iteration);
|
||||
|
||||
StringAppendF(&matlab_script,
|
||||
"lsqp.num_rows = %d;\n", A->num_rows());
|
||||
StringAppendF(&matlab_script,
|
||||
"lsqp.num_cols = %d;\n", A->num_cols());
|
||||
|
||||
{
|
||||
string filename = filename_prefix + "_A.txt";
|
||||
LOG(INFO) << "writing to: " << filename;
|
||||
FILE* fptr = fopen(filename.c_str(), "w");
|
||||
CHECK_NOTNULL(fptr);
|
||||
A->ToTextFile(fptr);
|
||||
fclose(fptr);
|
||||
StringAppendF(&matlab_script,
|
||||
"tmp = load('%s', '-ascii');\n", filename.c_str());
|
||||
StringAppendF(
|
||||
&matlab_script,
|
||||
"lsqp.A = sparse(tmp(:, 1) + 1, tmp(:, 2) + 1, tmp(:, 3), %d, %d);\n",
|
||||
A->num_rows(),
|
||||
A->num_cols());
|
||||
}
|
||||
|
||||
|
||||
if (D != NULL) {
|
||||
string filename = filename_prefix + "_D.txt";
|
||||
WriteArrayToFileOrDie(filename, D, A->num_cols());
|
||||
StringAppendF(&matlab_script,
|
||||
"lsqp.D = load('%s', '-ascii');\n", filename.c_str());
|
||||
}
|
||||
|
||||
if (b != NULL) {
|
||||
string filename = filename_prefix + "_b.txt";
|
||||
WriteArrayToFileOrDie(filename, b, A->num_rows());
|
||||
StringAppendF(&matlab_script,
|
||||
"lsqp.b = load('%s', '-ascii');\n", filename.c_str());
|
||||
}
|
||||
|
||||
if (x != NULL) {
|
||||
string filename = filename_prefix + "_x.txt";
|
||||
WriteArrayToFileOrDie(filename, x, A->num_cols());
|
||||
StringAppendF(&matlab_script,
|
||||
"lsqp.x = load('%s', '-ascii');\n", filename.c_str());
|
||||
}
|
||||
|
||||
string matlab_filename = filename_prefix + ".m";
|
||||
WriteStringToFileOrDie(matlab_script, matlab_filename);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user