44 TripletEntry(
int r_,
int c_,
double x_) : r(r_), c(c_), x(x_) {}
48 bool operator()(
const TripletEntry& e1,
const TripletEntry& e2)
const 50 return e1.c < e2.c || (e1.c == e2.c && e1.r < e2.r);
55 bool writeVector(
const string& filename,
const double*v,
int n)
57 ofstream os(filename.c_str());
59 for (
int i=0; i<n; i++)
64 bool writeCCSMatrix(
const string& filename,
int rows,
int cols,
const int* Ap,
const int* Ai,
const double* Ax,
bool upperTriangleSymmetric)
66 vector<TripletEntry> entries;
67 entries.reserve((
size_t)Ap[cols]);
68 for (
int i=0; i < cols; i++) {
69 const int& rbeg = Ap[i];
70 const int& rend = Ap[i+1];
71 for (
int j = rbeg; j < rend; j++) {
72 entries.push_back(TripletEntry(Ai[j], i, Ax[j]));
73 if (upperTriangleSymmetric && Ai[j] != i)
74 entries.push_back(TripletEntry(i, Ai[j], Ax[j]));
77 sort(entries.begin(), entries.end(), TripletColSort());
79 string name = filename;
80 std::string::size_type lastDot = name.find_last_of(
'.');
81 if (lastDot != std::string::npos)
82 name = name.substr(0, lastDot);
84 std::ofstream fout(filename.c_str());
85 fout <<
"# name: " << name << std::endl;
86 fout <<
"# type: sparse matrix" << std::endl;
87 fout <<
"# nnz: " << entries.size() << std::endl;
88 fout <<
"# rows: " << rows << std::endl;
89 fout <<
"# columns: " << cols << std::endl;
91 fout << setprecision(9) << endl;
92 for (vector<TripletEntry>::const_iterator it = entries.begin(); it != entries.end(); ++it) {
93 const TripletEntry& entry = *it;
94 fout << entry.r+1 <<
" " << entry.c+1 <<
" " << entry.x << std::endl;
bool writeVector(const string &filename, const double *v, int n)
bool writeCCSMatrix(const string &filename, int rows, int cols, const int *Ap, const int *Ai, const double *Ax, bool upperTriangleSymmetric)