KrisLibrary  1.0.0
math/indexing.h
Go to the documentation of this file.
1 #ifndef MATH_INDEXING_H
2 #define MATH_INDEXING_H
3 
4 #include "VectorTemplate.h"
5 #include "MatrixTemplate.h"
6 #include "SparseVectorTemplate.h"
7 #include "SparseMatrixTemplate.h"
8 #include <vector>
9 
19 namespace Math {
20 
23 
25 template <class T>
26 inline void SetElements(VectorTemplate<T>& A,const std::vector<int>& indices,T fill)
27 {
28  for(size_t i=0;i<indices.size();i++)
29  A(indices[i]) = fill;
30 }
31 
38 template <class T>
39 inline void SetElements(VectorTemplate<T>& A,const std::vector<int>& indices,const VectorTemplate<T>& fill)
40 {
41  Assert((int)indices.size() == fill.n);
42  for(size_t i=0;i<indices.size();i++)
43  A(indices[i]) = fill(i);
44 }
45 
47 template <class T>
48 inline void GetElements(const VectorTemplate<T>& A,const std::vector<int>& indices,VectorTemplate<T>& B)
49 {
50  Assert((int)indices.size() == B.n);
51  for(size_t i=0;i<indices.size();i++)
52  B(i) = A(indices[i]);
53 }
54 
56 template <class T>
57 inline void CopyElements(VectorTemplate<T>& A,const std::vector<int>& aindices,
58  const VectorTemplate<T>& B,const std::vector<int>& bindices)
59 {
60  Assert(aindices.size() == bindices.size());
61  for(size_t i=0;i<aindices.size();i++)
62  A(aindices[i]) = B(bindices[i]);
63 }
64 
66 template <class T>
67 void RemoveElements(VectorTemplate<T>& A,const std::vector<int>& indices)
68 {
69  for(size_t i=0;i<indices.size();i++) {
70  int start=indices[i]+1;
71  int end=(i+1==indices.size() ? A.n : indices[i+1]);
72  Assert(end >= start);
73  Assert(start > 0 && end <= A.n);
74  for(int j=start;j<end;j++) {
75  A(j-i-1) = A(j);
76  }
77  }
78  A.n = A.n - (int)indices.size();
79 }
80 
82 template <class T>
83 void RemoveElements(SparseArray<T>& A,const std::vector<int>& indices)
84 {
85  if(indices.empty()) return;
86  SparseArray<T> res(A.n-(int)indices.size());
87  int curIndex=indices[0];
88  int curOffset=0;
89  for(typename SparseArray<T>::const_iterator i=A.begin();i!=A.end();i++) {
90  while(i->first>curIndex && curOffset < (int)indices.size()) {
91  curOffset++;
92  curIndex=indices[curOffset];
93  }
94  if(i->first != curIndex) {
95  Assert(i->first >= curOffset);
96  res.push_back(i->first-curOffset,i->second);
97  }
98  }
99  A = res;
100 }
101 
107 template <class T>
108 void AddElements(VectorTemplate<T>& A,const std::vector<int>& indices,T fill)
109 {
110  Assert(A.getCapacity() >= A.base + A.stride*(A.n+(int)indices.size()));
111  A.n = A.n + (int)indices.size();
112  Assert(A.isValid());
113  for(int i=(int)indices.size()-1;i>=0;i--) {
114  int start=indices[i]+1;
115  int end=(i+1==(int)indices.size() ? A.n : indices[i+1]);
116  Assert(end >= start);
117  Assert(start > 0 && end <= A.n);
118  for(int j=end-1;j>=start;j--) {
119  A(j) = A(j-i-1);
120  }
121  A(indices[i]) = fill;
122  }
123 }
124 
126 template <class T>
127 inline void SetRows(MatrixTemplate<T>& A,const std::vector<int>& indices,T fill)
128 {
129  for(size_t i=0;i<indices.size();i++)
130  A.setRow(indices[i],fill);
131 }
132 
133 
134 
136 template <class T>
137 inline void SetElements(MatrixTemplate<T>& A,const std::vector<int>& rows,const std::vector<int>& cols,T fill)
138 {
139  for(size_t i=0;i<rows.size();i++)
140  for(size_t j=0;j<cols.size();j++)
141  A(rows[i],cols[j]) = fill;
142 }
143 
144 
151 template <class T>
152 inline void SetElements(MatrixTemplate<T>& A,const std::vector<int>& rows,const std::vector<int>& cols,const MatrixTemplate<T>& fill)
153 {
154  Assert((int)rows.size() == fill.m);
155  Assert((int)cols.size() == fill.n);
156  for(size_t i=0;i<rows.size();i++)
157  for(size_t j=0;j<cols.size();j++)
158  A(rows[i],cols[j]) = fill(i,j);
159 }
160 
162 template <class T>
163 inline void GetElements(const MatrixTemplate<T>& A,const std::vector<int>& rows,const std::vector<int>& cols,MatrixTemplate<T>& B)
164 {
165  Assert((int)rows.size() == B.m);
166  Assert((int)cols.size() == B.n);
167  for(size_t i=0;i<rows.size();i++)
168  for(size_t j=0;j<cols.size();j++)
169  B(i,j) = A(rows[i],cols[j]);
170 }
171 
173 template <class T>
175  const std::vector<int>& arows,const std::vector<int>& acols,
176  const MatrixTemplate<T>& B,
177  const std::vector<int>& brows,const std::vector<int>& bcols)
178 {
179  Assert(arows.size() == brows.size());
180  Assert(acols.size() == bcols.size());
181  for(size_t i=0;i<arows.size();i++)
182  for(size_t j=0;j<acols.size();j++)
183  A(arows[i],acols[j]) = B(brows[i],bcols[j]);
184 }
185 
187 template <class T>
188 void RemoveElements(MatrixTemplate<T>& A,const std::vector<int>& rows,const std::vector<int>& cols)
189 {
190  for(size_t i=0;i<rows.size();i++) {
191  int rstart=rows[i]+1;
192  int rend=(i+1==rows.size() ? A.m : rows[i+1]);
193  Assert(rend >= rstart);
194  Assert(rstart > 0 && rend <= A.m);
195  for(size_t j=0;j<cols.size();j++) {
196  int cstart=cols[j]+1;
197  int cend=(j+1==cols.size() ? A.n : cols[i+1]);
198  Assert(cend >= cstart);
199  Assert(cstart > 0 && cend <= A.n);
200  for(int k=rstart;k<rend;k++)
201  for(int l=cstart;l<cend;l++)
202  A(k-i-1,l-j-1) = A(k,l);
203  }
204  }
205  A.m = A.m - (int)rows.size();
206  A.n = A.n - (int)cols.size();
207 }
208 
209 
211 template <class T>
212 inline void SetColumns(MatrixTemplate<T>& A,const std::vector<int>& indices,T fill)
213 {
214  for(size_t i=0;i<indices.size();i++)
215  A.setCol(indices[i],fill);
216 }
217 
219 template <class T>
220 inline void SetRows(MatrixTemplate<T>& A,const std::vector<int>& indices,const VectorTemplate<T>& fill)
221 {
222  for(size_t i=0;i<indices.size();i++)
223  A.copyRow(indices[i],fill);
224 }
225 
227 template <class T>
228 inline void SetColumns(MatrixTemplate<T>& A,const std::vector<int>& indices,const VectorTemplate<T>& fill)
229 {
230  for(size_t i=0;i<indices.size();i++)
231  A.copyColumn(indices[i],fill);
232 }
233 
240 template <class T>
241 inline void SetRows(MatrixTemplate<T>& A,const std::vector<int>& indices,const MatrixTemplate<T>& fill)
242 {
243  VectorTemplate<T> filli;
244  for(size_t i=0;i<indices.size();i++) {
245  fill.getRowRef(i,filli);
246  A.copyRow(indices[i],filli);
247  }
248 }
249 
256 template <class T>
257 inline void SetColumns(MatrixTemplate<T>& A,const std::vector<int>& indices,const MatrixTemplate<T>& fill)
258 {
259  VectorTemplate<T> filli;
260  for(size_t i=0;i<indices.size();i++) {
261  fill.getColRef(i,filli);
262  A.copyColumn(indices[i],filli);
263  }
264 }
265 
270 template <class T>
271 inline void GetRows(const MatrixTemplate<T>& A,const std::vector<int>& indices,MatrixTemplate<T>& B)
272 {
274  for(size_t i=0;i<indices.size();i++) {
275  B.getRowRef(i,Bi);
276  A.getRowCopy(indices[i],Bi);
277  }
278 }
279 
284 template <class T>
285 inline void GetColumns(const MatrixTemplate<T>& A,const std::vector<int>& indices,MatrixTemplate<T>& B)
286 {
288  for(size_t i=0;i<indices.size();i++) {
289  B.getColRef(i,Bi);
290  A.getColCopy(indices[i],Bi);
291  }
292 }
293 
295 template <class T>
296 void RemoveRows(MatrixTemplate<T>& A,const std::vector<int>& indices)
297 {
298  VectorTemplate<T> src,dest;
299  for(size_t i=0;i<indices.size();i++) {
300  int start=indices[i]+1;
301  int end=(i+1==indices.size() ? A.m : indices[i+1]);
302  Assert(end >= start);
303  Assert(start > 0 && end <= A.m);
304  for(int j=start;j!=end;j++) {
305  A.getRowRef(j,src);
306  A.getRowRef(j-i-1,dest);
307  dest.copy(src);
308  }
309  }
310  A.m = A.m - (int)indices.size();
311 }
312 
314 template <class T>
315 void RemoveColumns(MatrixTemplate<T>& A,const std::vector<int>& indices)
316 {
317  VectorTemplate<T> src,dest;
318  for(size_t i=0;i<indices.size();i++) {
319  int start=indices[i]+1;
320  int end=(i+1==indices.size() ? A.n : indices[i+1]);
321  Assert(end >= start);
322  Assert(start > 0 && end <= A.n);
323  for(int j=start;j<end;j++) {
324  A.getColRef(j,src);
325  A.getColRef(j-i-1,dest);
326  dest.copy(src);
327  }
328  }
329  A.n = A.n - (int)indices.size();
330 }
331 
333 template <class T>
334 void RemoveRows(SparseMatrixTemplate_RM<T>& A,const std::vector<int>& indices)
335 {
336  for(size_t i=0;i<indices.size();i++) {
337  int start=indices[i]+1;
338  int end=(i+1==indices.size() ? A.m : indices[i+1]);
339  Assert(end >= start);
340  Assert(start > 0 && end <= A.m);
341  for(int j=start;j!=end;j++) {
342  //copy row j to j-i-1
343  A.rows[j-i-1].entries.clear();
344  A.rows[j-i-1].entries=A.rows[j].entries();
345  }
346  }
347  A.m = A.m - (int)indices.size();
348 }
349 
351 template <class T>
352 void RemoveColumns(SparseMatrixTemplate_RM<T>& A,const std::vector<int>& indices)
353 {
354  for(int i=0;i<A.m;i++)
355  RemoveElements(A.rows[i],indices);
356  A.n = A.n - (int)indices.size();
357 }
358 
361 } //namespace Math
362 
363 #endif
364 
365 
366 
367 
A sparse 1D array class.
Definition: SparseArray.h:13
void GetColumns(const MatrixTemplate< T > &A, const std::vector< int > &indices, MatrixTemplate< T > &B)
Copies the indexed columns in A into B.
Definition: math/indexing.h:285
void SetColumns(MatrixTemplate< T > &A, const std::vector< int > &indices, T fill)
Sets the indexed columns of A to fill.
Definition: math/indexing.h:212
Row-major sparse matrix.
Definition: SparseMatrixTemplate.h:44
void SetElements(VectorTemplate< T > &A, const std::vector< int > &indices, T fill)
Sets A[indices] = fill.
Definition: math/indexing.h:26
Several sparse matrix classes.
void RemoveElements(VectorTemplate< T > &A, const std::vector< int > &indices)
Removes the indexed elements of A (shrinking A)
Definition: math/indexing.h:67
void RemoveRows(MatrixTemplate< T > &A, const std::vector< int > &indices)
Removes the rows of A indexed by the sorted list indices.
Definition: math/indexing.h:296
void AddElements(VectorTemplate< T > &A, const std::vector< int > &indices, T fill)
The inverse of RemoveElements.
Definition: math/indexing.h:108
void RemoveColumns(MatrixTemplate< T > &A, const std::vector< int > &indices)
Removes the columns of A indexed by the sorted indices.
Definition: math/indexing.h:315
A matrix over the field T.
Definition: function.h:10
void GetRows(const MatrixTemplate< T > &A, const std::vector< int > &indices, MatrixTemplate< T > &B)
Copies the indexed rows in A into B.
Definition: math/indexing.h:271
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
A vector over the field T.
Definition: function.h:9
void GetElements(const VectorTemplate< T > &A, const std::vector< int > &indices, VectorTemplate< T > &B)
Sets B = A[indices].
Definition: math/indexing.h:48
void SetRows(MatrixTemplate< T > &A, const std::vector< int > &indices, T fill)
Sets the indexed rows of A to fill.
Definition: math/indexing.h:127
void CopyElements(VectorTemplate< T > &A, const std::vector< int > &aindices, const VectorTemplate< T > &B, const std::vector< int > &bindices)
Sets A[aindices] = B[bindices].
Definition: math/indexing.h:57