CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sparse_rc.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_SPARSE_RC_HPP
2 # define CPPAD_UTILITY_SPARSE_RC_HPP
3 /* --------------------------------------------------------------------------
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
5 
6 CppAD is distributed under multiple licenses. This distribution is under
7 the terms of the
8  Eclipse Public License Version 1.0.
9 
10 A copy of this license is included in the COPYING file of this distribution.
11 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
12 -------------------------------------------------------------------------- */
13 
14 /*
15 $begin sparse_rc$$
16 $spell
17  CppAD
18  const
19  nnz
20  cppad
21  hpp
22  rc
23  nr
24  nc
25  resize
26 $$
27 $section Row and Column Index Sparsity Patterns$$
28 
29 $head Syntax$$
30 $codei%# include <cppad/utility/sparse_rc.hpp>
31 %$$
32 $codei%sparse_rc<%SizeVector%> %empty%
33 %$$
34 $codei%sparse_rc<%SizeVector%> %pattern%(%nr%, %nc%, %nnz%)
35 %$$
36 $icode%target% = %pattern%
37 %$$
38 $icode%resize%(%nr%, %nc%, %nnz%)
39 %$$
40 $icode%pattern%.set(%k%, %r%, %c%)
41 %$$
42 $icode%pattern%.nr()
43 %$$
44 $icode%pattern%.nc()
45 %$$
46 $icode%pattern%.nnz()
47 %$$
48 $codei%const %SizeVector%& %row%( %pattern%.row() )
49 %$$
50 $codei%const %SizeVector%& %col%( %pattern%.col() )
51 %$$
52 $icode%row_major% = %pattern%.row_major()
53 %$$
54 $icode%col_major% = %pattern%.col_major()
55 %$$
56 
57 $head SizeVector$$
58 We use $icode SizeVector$$ to denote $cref SimpleVector$$ class
59 $cref/with elements of type/SimpleVector/Elements of Specified Type/$$
60 $code size_t$$.
61 
62 $head empty$$
63 This is an empty sparsity pattern. To be specific,
64 the corresponding number of rows $icode nr$$,
65 number of columns $icode nc$$,
66 and number of possibly non-zero values $icode nnz$$,
67 are all zero.
68 
69 $head pattern$$
70 This object is used to hold a sparsity pattern for a matrix.
71 The sparsity $icode pattern$$ is $code const$$
72 except during its constructor, $code resize$$, and $code set$$.
73 
74 $head target$$
75 The target of the assignment statement must have prototype
76 $codei%
77  sparse_rc<%SizeVector%> %target%
78 %$$
79 After this assignment statement, $icode target$$ is an independent copy
80 of $icode pattern$$; i.e. it has all the same values as $icode pattern$$
81 and changes to $icode target$$ do not affect $icode pattern$$.
82 
83 $head nr$$
84 This argument has prototype
85 $codei%
86  size_t %nr%
87 %$$
88 It specifies the number of rows in the sparsity pattern.
89 The function call $code nr()$$ returns the value of $icode nr$$.
90 
91 $head nc$$
92 This argument has prototype
93 $codei%
94  size_t %nc%
95 %$$
96 It specifies the number of columns in the sparsity pattern.
97 The function call $code nc()$$ returns the value of $icode nc$$.
98 
99 $head nnz$$
100 This argument has prototype
101 $codei%
102  size_t %nnz%
103 %$$
104 It specifies the number of possibly non-zero
105 index pairs in the sparsity pattern.
106 The function call $code nnz()$$ returns the value of $icode nnz$$.
107 
108 $head resize$$
109 The current sparsity pattern is lost and a new one is started
110 with the specified parameters. The elements in the $icode row$$
111 and $icode col$$ vectors should be assigned using $code set$$.
112 
113 $head set$$
114 This function sets the values
115 $codei%
116  %row%[%k%] = %r%
117  %col%[%k%] = %c%
118 %$$
119 
120 $subhead k$$
121 This argument has type
122 $codei%
123  size_t %k%
124 %$$
125 and must be less than $icode nnz$$.
126 
127 $subhead r$$
128 This argument has type
129 $codei%
130  size_t %r%
131 %$$
132 It specifies the value assigned to $icode%row%[%k%]%$$ and must
133 be less than $icode nr$$.
134 
135 $subhead c$$
136 This argument has type
137 $codei%
138  size_t %c%
139 %$$
140 It specifies the value assigned to $icode%col%[%k%]%$$ and must
141 be less than $icode nc$$.
142 
143 $head row$$
144 This vector has size $icode nnz$$ and
145 $icode%row%[%k%]%$$
146 is the row index of the $th k$$ possibly non-zero
147 index pair in the sparsity pattern.
148 
149 $head col$$
150 This vector has size $icode nnz$$ and
151 $icode%col%[%k%]%$$ is the column index of the $th k$$ possibly non-zero
152 index pair in the sparsity pattern.
153 
154 $head row_major$$
155 This vector has prototype
156 $codei%
157  %SizeVector% %row_major%
158 %$$
159 and its size $icode nnz$$.
160 It sorts the sparsity pattern in row-major order.
161 To be specific,
162 $codei%
163  %col%[ %row_major%[%k%] ] <= %col%[ %row_major%[%k%+1] ]
164 %$$
165 and if $icode%col%[ %row_major%[%k%] ] == %col%[ %row_major%[%k%+1] ]%$$,
166 $codei%
167  %row%[ %row_major%[%k%] ] < %row%[ %row_major%[%k%+1] ]
168 %$$
169 This routine generates an assert if there are two entries with the same
170 row and column values (if $code NDEBUG$$ is not defined).
171 
172 $head col_major$$
173 This vector has prototype
174 $codei%
175  %SizeVector% %col_major%
176 %$$
177 and its size $icode nnz$$.
178 It sorts the sparsity pattern in column-major order.
179 To be specific,
180 $codei%
181  %row%[ %col_major%[%k%] ] <= %row%[ %col_major%[%k%+1] ]
182 %$$
183 and if $icode%row%[ %col_major%[%k%] ] == %row%[ %col_major%[%k%+1] ]%$$,
184 $codei%
185  %col%[ %col_major%[%k%] ] < %col%[ %col_major%[%k%+1] ]
186 %$$
187 This routine generates an assert if there are two entries with the same
188 row and column values (if $code NDEBUG$$ is not defined).
189 
190 $children%
191  example/utility/sparse_rc.cpp
192 %$$
193 $head Example$$
194 The file $cref sparse_rc.cpp$$
195 contains an example and test of this class.
196 It returns true if it succeeds and false otherwise.
197 
198 $end
199 */
200 /*!
201 \file sparse_rc.hpp
202 A Matrix sparsity pattern class.
203 */
204 # include <cstddef> // for size_t
205 # include <cppad/core/cppad_assert.hpp> // for CPPAD_ASSERT
206 # include <cppad/utility/index_sort.hpp> // for row and column major ordering
207 
208 namespace CppAD { // BEGIN CPPAD_NAMESPACE
209 
210 /// sparsity pattern for a matrix with indices of type size_t
211 template <class SizeVector>
212 class sparse_rc {
213 private:
214  /// number of rows in the sparsity pattern
215  size_t nr_;
216  /// number of columns in the sparsity pattern
217  size_t nc_;
218  /// number of possibly non-zero index pairs
219  size_t nnz_;
220  /// row_[k] is the row index for the k-th possibly non-zero entry
221  SizeVector row_;
222  /// col_[k] is the column index for the k-th possibly non-zero entry
223  SizeVector col_;
224 public:
225  /// default constructor
226  /// Eigen vector is ambiguous for row_(0), col_(0) so use default ctor
227  sparse_rc(void)
228  : nr_(0), nc_(0), nnz_(0)
229  { }
230  /// sizing constructor
231  /// Eigen vector is ambiguous for row_(0), col_(0) so use default ctor
232  sparse_rc(size_t nr, size_t nc, size_t nnz)
233  : nr_(nr), nc_(nc), nnz_(nnz)
234  { row_.resize(nnz);
235  col_.resize(nnz);
236  }
237  /// copy constructor
238  sparse_rc(const sparse_rc& other)
239  :
240  nr_(other.nr_) ,
241  nc_(other.nc_) ,
242  nnz_(other.nnz_) ,
243  row_(other.row_) ,
244  col_(other.col_)
245  { }
246  /// assignment
247  void operator=(const sparse_rc& pattern)
248  { nr_ = pattern.nr_;
249  nc_ = pattern.nc_;
250  nnz_ = pattern.nnz_;
251  // simple vector assignment requires vectors to have same size
252  row_.resize(nnz_);
253  col_.resize(nnz_);
254  row_ = pattern.row_;
255  col_ = pattern.col_;
256  }
257  /// resize
258  void resize(size_t nr, size_t nc, size_t nnz)
259  { nr_ = nr;
260  nc_ = nc;
261  nnz_ = nnz;
262  row_.resize(nnz);
263  col_.resize(nnz);
264  }
265  /// set row and column for a possibly non-zero element
266  void set(size_t k, size_t r, size_t c)
268  k < nnz_,
269  "The index k is not less than nnz in sparse_rc::set"
270  );
272  r < nr_,
273  "The index r is not less than nr in sparse_rc::set"
274  );
276  c < nc_,
277  "The index c is to not less than nc in sparse_rc::set"
278  );
279  row_[k] = r;
280  col_[k] = c;
281  //
282  }
283  /// number of rows in matrix
284  size_t nr(void) const
285  { return nr_; }
286  /// number of columns in matrix
287  size_t nc(void) const
288  { return nc_; }
289  /// number of possibly non-zero elements in matrix
290  size_t nnz(void) const
291  { return nnz_; }
292  /// row indices
293  const SizeVector& row(void) const
294  { return row_; }
295  /// column indices
296  const SizeVector& col(void) const
297  { return col_; }
298  /// row-major order
299  SizeVector row_major(void) const
300  { SizeVector keys(nnz_), row_major(nnz_);
301  for(size_t k = 0; k < nnz_; k++)
302  { CPPAD_ASSERT_UNKNOWN( row_[k] < nr_ );
303  keys[k] = row_[k] * nc_ + col_[k];
304  }
305  index_sort(keys, row_major);
306 # ifndef NDEBUG
307  for(size_t ell = 0; ell + 1 < nnz_; ell++)
308  { size_t k = row_major[ ell ];
309  size_t kp = row_major[ ell + 1 ];
311  row_[k] != row_[kp] || col_[k] != col_[kp],
312  "sparse_rc: row_major: duplicate entry in this pattern"
313  );
315  row_[k]<row_[kp] || (row_[k]==row_[kp] && col_[k]<col_[kp])
316  );
317  }
318 # endif
319  return row_major;
320  }
321  /// column-major indices
322  SizeVector col_major(void) const
323  { SizeVector keys(nnz_), col_major(nnz_);
324  for(size_t k = 0; k < nnz_; k++)
325  { CPPAD_ASSERT_UNKNOWN( col_[k] < nc_ );
326  keys[k] = col_[k] * nr_ + row_[k];
327  }
328  index_sort(keys, col_major);
329 # ifndef NDEBUG
330  for(size_t ell = 0; ell + 1 < nnz_; ell++)
331  { size_t k = col_major[ ell ];
332  size_t kp = col_major[ ell + 1 ];
334  col_[k] != col_[kp] || row_[k] != row_[kp],
335  "sparse_rc: col_major: duplicate entry in this pattern"
336  );
338  col_[k]<col_[kp] || (col_[k]==col_[kp] && row_[k]<row_[kp])
339  );
340  }
341 # endif
342  return col_major;
343  }
344 };
345 
346 } // END_CPPAD_NAMESPACE
347 
348 # endif
void operator=(const sparse_rc &pattern)
assignment
Definition: sparse_rc.hpp:247
size_t nnz_
number of possibly non-zero index pairs
Definition: sparse_rc.hpp:219
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
const SizeVector & col(void) const
column indices
Definition: sparse_rc.hpp:296
sparse_rc(void)
default constructor Eigen vector is ambiguous for row_(0), col_(0) so use default ctor ...
Definition: sparse_rc.hpp:227
sparse_rc(const sparse_rc &other)
copy constructor
Definition: sparse_rc.hpp:238
Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
SizeVector col_major(void) const
column-major indices
Definition: sparse_rc.hpp:322
size_t nc_
number of columns in the sparsity pattern
Definition: sparse_rc.hpp:217
sparse_rc(size_t nr, size_t nc, size_t nnz)
sizing constructor Eigen vector is ambiguous for row_(0), col_(0) so use default ctor ...
Definition: sparse_rc.hpp:232
size_t nr_
number of rows in the sparsity pattern
Definition: sparse_rc.hpp:215
size_t nr(void) const
number of rows in matrix
Definition: sparse_rc.hpp:284
SizeVector row_
row_[k] is the row index for the k-th possibly non-zero entry
Definition: sparse_rc.hpp:221
sparsity pattern for a matrix with indices of type size_t
Definition: sparse_rc.hpp:212
void index_sort(const VectorKey &keys, VectorSize &ind)
Compute the indices that sort a vector of keys.
Definition: index_sort.hpp:140
size_t nnz(void) const
number of possibly non-zero elements in matrix
Definition: sparse_rc.hpp:290
void resize(size_t nr, size_t nc, size_t nnz)
resize
Definition: sparse_rc.hpp:258
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
File used to implement the CppAD index sort utility.
SizeVector row_major(void) const
row-major order
Definition: sparse_rc.hpp:299
size_t nc(void) const
number of columns in matrix
Definition: sparse_rc.hpp:287
void set(size_t k, size_t r, size_t c)
set row and column for a possibly non-zero element
Definition: sparse_rc.hpp:266
SizeVector col_
col_[k] is the column index for the k-th possibly non-zero entry
Definition: sparse_rc.hpp:223
const SizeVector & row(void) const
row indices
Definition: sparse_rc.hpp:293