openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
SF_linalg_utils.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
28 #ifndef _SF_LINALG_H
29 #define _SF_LINALG_H
30 
31 #include "SF_vector.h"
32 #include "SF_sort.h"
33 
34 namespace SF {
35 
44 template<class T, class S>
46 {
47 public:
48 
53  inline void operator()(const vector<T> &_acnt,
54  const vector<T> &_acol,
55  const vector<S> &_aele,
56  const vector<T> &_bcnt,
57  const vector<T> &_bcol,
58  const vector<S> &_bele,
59  const T _csize,
60  vector<T> &_ccnt,
61  vector<T> &_ccol,
62  vector<S> &_cele)
63  {
64  int arows = (int)_acnt.size();
65  const T *acnt = &_acnt[0];
66  const T *acol = &_acol[0];
67  const S *aele = &_aele[0];
68  const T *bcnt = &_bcnt[0];
69  const T *bcol = &_bcol[0];
70  const S *bele = &_bele[0];
71 
72  int brows = (int)_bcnt.size();
73  vector<T> _bdsp(brows+1);
74  dsp_from_cnt(_bcnt, _bdsp);
75 
76  _ccnt.resize(_csize);
77  _ccnt.zero();
78 
79  vector<T> _clst(_csize, -1);
80  T *ccnt = _ccnt.data();
81  T *clst = _clst.data();
82  T *bdsp = _bdsp.data();
83  int csize = 0;
84  for(int m = 0, i = 0; i < arows; i++)
85  {
86  for(int j = 0; j < acnt[i]; j++)
87  {
88  T c = acol[m];
89  m++;
90  for(int k = bdsp[c]; k < bdsp[c] + bcnt[c]; k++)
91  {
92  T d = bcol[k];
93  if(clst[d] != i)
94  {
95  clst[d] = i;
96  ccnt[d]++;
97  csize++;
98  }
99  }
100  }
101  }
102 
103  _clst.assign(size_t(_csize), -1);
104  clst = _clst.data();
105 
106  vector<T> _cdsp(_csize+1);
107  dsp_from_cnt(_ccnt, _cdsp);
108 
109  _ccol.resize(csize);
110  _cele.resize(csize);
111  T *ccol = _ccol.data();
112  S *cele = _cele.data();
113  T *cdsp = _cdsp.data();
114  for(int m = 0, i = 0; i < arows; i++)
115  {
116  for(int j = 0; j < acnt[i]; j++)
117  {
118  T c = acol[m];
119  S s = aele[m];
120  m++;
121  for(int k = bdsp[c]; k < bdsp[c] + bcnt[c]; k++)
122  {
123  T d = bcol[k];
124  T e = cdsp[d];
125  S t = s * bele[k];
126  if(clst[d] != i)
127  {
128  clst[d] = i;
129  ccol[e] = i;
130  cele[e] = t;
131  cdsp[d] = e + 1;
132  }
133  else
134  {
135  cele[e - 1] += t;
136  }
137  }
138  }
139  }
140  }
141 };
142 
143 
158 template<class T>
159 inline void multiply_connectivities(const vector<T> & a_cnt,
160  const vector<T> & a_con,
161  const vector<T> & b_cnt,
162  const vector<T> & b_con,
163  vector<T> & c_cnt,
164  vector<T> & c_con)
165 {
166  int numnodes = a_cnt.size();
167 
168  vector<T> a_ele, b_ele, c_ele;
169  a_ele.assign(a_con.size(), 1);
170  b_ele.assign(b_con.size(), 1);
171 
173  multiply(a_cnt, a_con, a_ele, b_cnt, b_con, b_ele, numnodes, c_cnt, c_con, c_ele);
174 }
175 
185 template<class T>
186 inline void transpose_connectivity(const vector<T> & a_cnt,
187  const vector<T> & a_con,
188  vector<T> & b_cnt,
189  vector<T> & b_con)
190 {
191  if(a_con.size() == 0) return;
192 
193  // get the largest node index to determine number of nodes
194  T numnodes = *(std::max_element(a_con.begin(), a_con.end())) + 1;
195  vector<T> b_row;
196 
197  // we compute bcol := arow
198  b_con.resize(a_con.size());
199  for(size_t i=0, k=0; i < a_cnt.size(); i++)
200  for(int j=0; j < a_cnt[i]; j++, k++) b_con[k] = i;
201 
202  b_row.assign(a_con.begin(), a_con.end());
203  binary_sort_sort(b_row, b_con);
204 
205  b_cnt.resize(numnodes); b_cnt.zero();
206  count(b_row, b_cnt);
207 }
208 
209 }
210 
211 #endif
212 
213 
Definition: dense_mat.hpp:34
The vector class and related algorithms.
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 zero()
Definition: SF_vector.h:248
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.
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
Definition: SF_vector.h:332
T * data()
Pointer to the vector&#39;s start.
Definition: SF_vector.h:91
const T * end() const
Pointer to the vector&#39;s end.
Definition: SF_vector.h:128
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.
Various sorting algorithms.
size_t size() const
The current size of the vector.
Definition: SF_vector.h:104
Functor for the sparse matrix multiply-transpose operation.
A vector storing arbitrary data.
Definition: SF_vector.h:42
const T * begin() const
Pointer to the vector&#39;s start.
Definition: SF_vector.h:116
void assign(InputIterator s, InputIterator e)
Assign a memory range.
Definition: SF_vector.h:161
void resize(size_t n)
Resize a vector.
Definition: SF_vector.h:209
void binary_sort_sort(vector< T > &_V, vector< T > &_W)
Definition: SF_sort.h:321
void dsp_from_cnt(const vector< T > &cnt, vector< T > &dsp)
Compute displacements from counts.
Definition: SF_vector.h:310