23 row_size_(in_matrix.NumRows()),
24 col_size_(in_matrix.NumCols())
42 if ((i<0) || (i >= row_size_) || (j < 0) || (j >= col_size_))
45 <<
"SparseMatrix::Insert encountered out of bounds,"
46 <<
" i=" << i <<
" j=" << j
47 <<
" bounds(" << row_size_ <<
"," << col_size_ <<
")";
51 auto relative_location = std::find(rowI_indices_[i].begin(),
52 rowI_indices_[i].end(), j);
53 bool already_there = (relative_location != rowI_indices_[i].end());
57 size_t jr = relative_location - rowI_indices_[i].begin();
58 rowI_values_[i][jr] = value;
62 rowI_values_[i].push_back(value);
63 rowI_indices_[i].push_back(j);
73 if ((i<0) || (i >= row_size_) || (j < 0) || (j >= col_size_))
76 <<
"SparseMatrix::Insert encountered out of bounds,"
77 <<
" i=" << i <<
" j=" << j
78 <<
" bounds(" << row_size_ <<
"," << col_size_ <<
")";
82 auto relative_location = std::find(rowI_indices_[i].begin(),
83 rowI_indices_[i].end(), j);
84 bool already_there = (relative_location != rowI_indices_[i].end());
88 size_t jr = relative_location - rowI_indices_[i].begin();
89 rowI_values_[i][jr] += value;
93 rowI_values_[i].push_back(value);
94 rowI_indices_[i].push_back(j);
104 size_t num_rows = rowI_values_.size();
106 if (diag.size() != rowI_values_.size())
109 <<
"Incompatible matrix-vector size encountered "
110 <<
"in call to SparseMatrix::SetDiagonal.";
115 for (
size_t i=0; i<num_rows; i++)
117 auto relative_location = std::find(rowI_indices_[i].begin(),
118 rowI_indices_[i].end(), i);
119 bool already_there = (relative_location != rowI_indices_[i].end());
123 size_t jr = relative_location - rowI_indices_[i].begin();
124 rowI_values_[i][jr] = diag[i];
128 rowI_values_[i].push_back(diag[i]);
129 rowI_indices_[i].push_back(i);
141 if ((i<0) || (i >= rowI_indices_.size()))
144 <<
"Index i out of bounds"
145 <<
" in call to SparseMatrix::ValueIJ"
150 if (not rowI_indices_[i].empty())
152 auto relative_location = std::find(rowI_indices_[i].begin(),
153 rowI_indices_[i].end(), j);
154 bool non_zero = (relative_location != rowI_indices_[i].end());
157 size_t jr = relative_location - rowI_indices_[i].begin();
158 retval = rowI_values_[i][jr];
168 for (
size_t i=0; i < rowI_indices_.size(); ++i)
170 auto& indices = rowI_indices_[i];
171 auto& values = rowI_values_[i];
175 std::vector<std::pair<size_t,double>> target;
176 target.reserve(indices.size());
178 auto index = indices.begin();
179 auto value = values.begin();
180 for (;index!=indices.end(); ++index, ++value)
182 target.emplace_back(*index,*value);
188 bool operator()(std::pair<size_t,double> a, std::pair<size_t,double> b)
190 return a.first < b.first;
195 std::stable_sort(target.begin(), target.end(), compare_index);
200 for (
auto& iv_pair : target)
202 indices.push_back(iv_pair.first);
203 values.push_back(iv_pair.second);
213 std::stringstream out;
216 for (
size_t i=0; i < row_size_; i++)
218 for (
size_t j=0; j < col_size_; j++)
220 auto relative_location = std::find(rowI_indices_[i].begin(),
221 rowI_indices_[i].end(), j);
222 bool non_zero = (relative_location != rowI_indices_[i].end());
226 size_t jr = relative_location - rowI_indices_[i].begin();
228 << std::setprecision(2)
231 << rowI_values_[i][jr] <<
" ";
236 << std::setprecision(0)
252 if (rowI_values_.empty())
255 <<
"Illegal call to unitialized SparseMatrix matrix.";
265 {
return {*
this, row_id};}
268 {
return {*
this, row_id};}
static void Exit(int error_code)
void Insert(size_t i, size_t j, double value)
std::string PrintStr() const
SparseMatrix(size_t num_rows, size_t num_cols)
RowIteratorContext Row(size_t row_id)
std::vector< std::vector< double > > rowI_values_
void CheckInitialized() const
void InsertAdd(size_t i, size_t j, double value)
double ValueIJ(size_t i, size_t j) const
std::vector< std::vector< size_t > > rowI_indices_
size_t row_size_
Maximum number of rows for this matrix.
void SetDiagonal(const std::vector< double > &diag)