1 #ifndef _LILC_MATRIX_ILDL_HELPERS_H 2 #define _LILC_MATRIX_ILDL_HELPERS_H 7 typedef vector<int>::iterator idx_it;
13 template <
class el_type>
14 inline double dot_product(
const vector<el_type>& v,
const vector<el_type>& w) {
16 for (
int i = 0; i < v.size(); i++) {
25 template <
class el_type>
26 inline void vector_sum(
double a, vector<el_type>& v,
double b, vector<el_type>& w, vector<el_type>& u) {
27 for (
int i = 0; i < v.size(); i++) {
28 u[i] = a*v[i] + b*w[i];
39 template <
class el_type>
40 inline double max(vector<el_type>& v, vector<int>& curr_nnzs,
int& r) {
42 for (idx_it it = curr_nnzs.begin(), end = curr_nnzs.end(); it != end; ++it) {
43 if (abs(v[*it]) > res) {
58 template <
class el_type>
59 inline double norm(vector<el_type>& v, vector<int>& curr_nnzs, el_type p = 1) {
61 for (idx_it it = curr_nnzs.begin(), end = curr_nnzs.end(); it != end; ++it) {
62 res += pow(abs(v[*it]), p);
73 template <
class el_type>
74 inline double norm(vector<el_type>& v, el_type p = 1) {
76 for (
int i = 0; i < v.size(); i++) {
77 res += pow(abs(v[i]), p);
87 template <
class InputContainer,
class InputIterator>
88 inline void inplace_union(InputContainer& a, InputIterator
const& b_start, InputIterator
const& b_end)
93 std::copy(b_start, b_end, std::back_inserter(a));
96 std::inplace_merge(a.begin(), a.begin() + mid, a.end());
99 a.erase(std::unique(a.begin(), a.end()), a.end());
108 template <
class InputContainer,
class InputIterator>
109 inline void unordered_inplace_union(InputContainer& a, InputIterator
const& b_start, InputIterator
const& b_end, vector<bool>& in_set)
111 for (InputIterator it = a.begin(), end = a.end(); it != end; ++it) {
112 assert(*it < in_set.size() && *it >= 0);
116 for (InputIterator it = b_start; it != b_end; ++it) {
123 for (InputIterator it = a.begin(), end = a.end(); it != end; ++it) {
124 assert(*it < in_set.size() && *it >= 0);
136 template <
class el_type>
138 const vector<el_type>& v;
139 by_value(
const vector<el_type>& vec) : v(vec) {}
140 inline bool operator()(
int const &a,
int const &b)
const {
144 return abs(v[a]) > abs(v[b]);
152 template <
class el_type>
153 struct by_tolerance {
154 const vector<el_type>& v;
156 by_tolerance(
const vector<el_type>& vec,
const double& eps) : v(vec), eps(eps) {}
157 inline bool operator()(
int const &i)
const {
158 return abs(v[i]) < eps;
169 template <
class el_type>
170 inline void drop_tol(vector<el_type>& v, vector<int>& curr_nnzs,
const int& lfil,
const double& tol) {
172 el_type tolerance = tol*norm<el_type>(v, curr_nnzs);
173 const long double eps = 1e-13;
174 if (tolerance > eps) {
175 for (idx_it it = curr_nnzs.begin(), end = curr_nnzs.end(); it != end; ++it)
176 if (abs(v[*it]) < tolerance) v[*it] = 0;
179 by_value<el_type> sorter(v);
180 std::sort(curr_nnzs.begin(), curr_nnzs.end(), sorter);
183 for (
int i = lfil, end = curr_nnzs.size(); i < end ; ++i) {
187 by_tolerance<el_type> is_zero(v, eps);
188 curr_nnzs.erase( remove_if(curr_nnzs.begin(), curr_nnzs.end(), is_zero), curr_nnzs.end() );
189 curr_nnzs.resize( std::min(lfil, (
int) curr_nnzs.size()) );
191 std::sort(curr_nnzs.begin(), curr_nnzs.end());
196 template <
class el_type>
197 inline void update_single(
const int& k,
const int& j,
const el_type& l_ki,
const el_type& d, vector<el_type>& work, vector<int>& curr_nnzs,
lilc_matrix<el_type>& L, vector<bool>& in_set) {
203 el_type factor = l_ki * d;
204 for (i = offset; i < L.
m_idx[j].size(); ++i) {
205 int x = L.
m_idx[j][i];
206 work[x] -= factor * L.
m_x[j][i];
208 curr_nnzs.push_back(x);
222 template <
class el_type>
230 for (
int x : curr_nnzs) {
235 for (
int i = 0; i < (int) L.
list[r].size(); ++i) {
240 update_single(r, j, l_ri, D[j], work, curr_nnzs, L, in_set);
245 update_single(r, j + 1, l_ri, d_12, work, curr_nnzs, L, in_set);
246 }
else if (blk_sz == -2) {
248 update_single(r, j - 1, l_ri, d_12, work, curr_nnzs, L, in_set);
253 for (
int x : curr_nnzs) {
259 template <
class el_type>
260 inline void vec_add(vector<el_type>& v1, vector<int>& v1_nnzs, vector<el_type>& v2, vector<int>& v2_nnzs) {
262 inplace_union(v1_nnzs, v2_nnzs.begin(), v2_nnzs.end());
263 for (idx_it it = v1_nnzs.begin(), end = v1_nnzs.end(); it != end; ++it) {
268 inline void safe_swap(vector<int>& curr_nnzs,
const int& k,
const int& r) {
269 bool con_k =
false, con_r =
false;
270 vector<int>::iterator k_idx, r_idx;
271 for (idx_it it = curr_nnzs.begin(), end = curr_nnzs.end(); it != end; ++it) {
283 if (con_k) *k_idx = r;
284 if (con_r) *r_idx = k;
el_type & off_diagonal(int i)
Definition: block_diag_matrix.h:114
vector< elt_vector_type > m_x
The values of the nonzeros in the matrix.
Definition: lil_sparse_matrix.h:36
vector< idx_vector_type > m_idx
The row/col indices. The way m_idx is used depends on whether the matrix is in LIL-C or LIL-R...
Definition: lil_sparse_matrix.h:35
A list-of-lists (LIL) matrix in column oriented format.
Definition: lilc_matrix.h:9
A quick implementation of a diagonal matrix with 1x1 and 2x2 blocks.
Definition: block_diag_matrix.h:39
virtual el_type coeff(const int &i, const int &j, int offset=0) const
Finds the (i,j)th coefficient of the matrix.
Definition: lilc_matrix_declarations.h:80
std::vector< int > col_first
On iteration k, first[i] gives the number of non-zero elements on col i of A before A(i...
Definition: lilc_matrix_declarations.h:47
void ensure_invariant(const int &j, const int &k, Container &con, bool update_list=false)
Ensures two the invariants observed by A.first and A.list are held.
Definition: lilc_matrix_declarations.h:300
std::vector< std::vector< int > > list
A list of linked lists that gives the non-zero elements in each row of A. Since at any time we may sw...
Definition: lilc_matrix_declarations.h:44
int block_size(int i) const
Definition: block_diag_matrix.h:128