openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
SF_linalg_utils.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
13 #ifndef _SF_LINALG_H
14 #define _SF_LINALG_H
15 
16 #include "SF_vector.h"
17 #include "SF_sort.h"
18 
19 namespace SF {
20 
29 template<class T, class S>
31 {
32 public:
33 
38  inline void operator()(const vector<T> &_acnt,
39  const vector<T> &_acol,
40  const vector<S> &_aele,
41  const vector<T> &_bcnt,
42  const vector<T> &_bcol,
43  const vector<S> &_bele,
44  const T _csize,
45  vector<T> &_ccnt,
46  vector<T> &_ccol,
47  vector<S> &_cele)
48  {
49  int arows = (int)_acnt.size();
50  const T *acnt = &_acnt[0];
51  const T *acol = &_acol[0];
52  const S *aele = &_aele[0];
53  const T *bcnt = &_bcnt[0];
54  const T *bcol = &_bcol[0];
55  const S *bele = &_bele[0];
56 
57  int brows = (int)_bcnt.size();
58  vector<T> _bdsp(brows+1);
59  dsp_from_cnt(_bcnt, _bdsp);
60 
61  _ccnt.resize(_csize);
62  _ccnt.zero();
63 
64  vector<T> _clst(_csize, -1);
65  T *ccnt = _ccnt.data();
66  T *clst = _clst.data();
67  T *bdsp = _bdsp.data();
68  int csize = 0;
69  for(int m = 0, i = 0; i < arows; i++)
70  {
71  for(int j = 0; j < acnt[i]; j++)
72  {
73  T c = acol[m];
74  m++;
75  for(int k = bdsp[c]; k < bdsp[c] + bcnt[c]; k++)
76  {
77  T d = bcol[k];
78  if(clst[d] != i)
79  {
80  clst[d] = i;
81  ccnt[d]++;
82  csize++;
83  }
84  }
85  }
86  }
87 
88  _clst.assign(size_t(_csize), -1);
89  clst = _clst.data();
90 
91  vector<T> _cdsp(_csize+1);
92  dsp_from_cnt(_ccnt, _cdsp);
93 
94  _ccol.resize(csize);
95  _cele.resize(csize);
96  T *ccol = _ccol.data();
97  S *cele = _cele.data();
98  T *cdsp = _cdsp.data();
99  for(int m = 0, i = 0; i < arows; i++)
100  {
101  for(int j = 0; j < acnt[i]; j++)
102  {
103  T c = acol[m];
104  S s = aele[m];
105  m++;
106  for(int k = bdsp[c]; k < bdsp[c] + bcnt[c]; k++)
107  {
108  T d = bcol[k];
109  T e = cdsp[d];
110  S t = s * bele[k];
111  if(clst[d] != i)
112  {
113  clst[d] = i;
114  ccol[e] = i;
115  cele[e] = t;
116  cdsp[d] = e + 1;
117  }
118  else
119  {
120  cele[e - 1] += t;
121  }
122  }
123  }
124  }
125  }
126 };
127 
128 
143 template<class T>
144 inline void multiply_connectivities(const vector<T> & a_cnt,
145  const vector<T> & a_con,
146  const vector<T> & b_cnt,
147  const vector<T> & b_con,
148  vector<T> & c_cnt,
149  vector<T> & c_con)
150 {
151  int numnodes = a_cnt.size();
152 
153  vector<T> a_ele, b_ele, c_ele;
154  a_ele.assign(a_con.size(), 1);
155  b_ele.assign(b_con.size(), 1);
156 
158  multiply(a_cnt, a_con, a_ele, b_cnt, b_con, b_ele, numnodes, c_cnt, c_con, c_ele);
159 }
160 
170 template<class T>
171 inline void transpose_connectivity(const vector<T> & a_cnt,
172  const vector<T> & a_con,
173  vector<T> & b_cnt,
174  vector<T> & b_con)
175 {
176  if(a_con.size() == 0) return;
177 
178  // get the largest node index to determine number of nodes
179  T numnodes = *(std::max_element(a_con.begin(), a_con.end())) + 1;
180  vector<T> b_row;
181 
182  // we compute bcol := arow
183  b_con.resize(a_con.size());
184  for(size_t i=0, k=0; i < a_cnt.size(); i++)
185  for(int j=0; j < a_cnt[i]; j++, k++) b_con[k] = i;
186 
187  b_row.assign(a_con.begin(), a_con.end());
188  binary_sort_sort(b_row, b_con);
189 
190  b_cnt.resize(numnodes); b_cnt.zero();
191  count(b_row, b_cnt);
192 }
193 
194 }
195 
196 #endif
197 
198 
Various sorting algorithms.
The vector class and related algorithms.
Functor for the sparse matrix multiply-transpose operation.
void operator()(const vector< T > &_acnt, const vector< T > &_acol, const vector< S > &_aele, const vector< T > &_bcnt, const vector< T > &_bcol, const vector< S > &_bele, const T _csize, vector< T > &_ccnt, vector< T > &_ccol, vector< S > &_cele)
Execute the matrix multiply-transpose operation.
A vector storing arbitrary data.
Definition: SF_vector.h:28
size_t size() const
The current size of the vector.
Definition: SF_vector.h:89
void resize(size_t n)
Resize a vector.
Definition: SF_vector.h:194
const T * end() const
Pointer to the vector's end.
Definition: SF_vector.h:113
void assign(InputIterator s, InputIterator e)
Assign a memory range.
Definition: SF_vector.h:146
void zero()
Definition: SF_vector.h:233
const T * begin() const
Pointer to the vector's start.
Definition: SF_vector.h:101
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:76
Definition: dense_mat.hpp:19
void dsp_from_cnt(const vector< T > &cnt, vector< T > &dsp)
Compute displacements from counts.
Definition: SF_vector.h:295
void transpose_connectivity(const vector< T > &a_cnt, const vector< T > &a_con, vector< T > &b_cnt, vector< T > &b_con)
Transpose CRS matrix graph A into B.
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
Definition: SF_vector.h:317
void multiply_connectivities(const vector< T > &a_cnt, const vector< T > &a_con, const vector< T > &b_cnt, const vector< T > &b_con, vector< T > &c_cnt, vector< T > &c_con)
void binary_sort_sort(vector< T > &_V, vector< T > &_W)
Definition: SF_sort.h:311