CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sparse_rcv.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_SPARSE_RCV_HPP
2 # define CPPAD_UTILITY_SPARSE_RCV_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 ------------------------------------------------------------------------------
16 $begin sparse_rcv$$
17 $spell
18  CppAD
19  nr
20  nc
21  const
22  var
23  nnz
24  cppad
25  hpp
26  rcv
27  rc
28 $$
29 $section Sparse Matrix Row, Column, Value Representation$$
30 
31 $head Syntax$$
32 $codei%# include <cppad/utility/sparse_rcv.hpp>
33 %$$
34 $codei%sparse_rcv<%SizeVector%, %ValueVector%> %empty%
35 %$$
36 $codei%sparse_rcv<%SizeVector%, %ValueVector%> %matrix%(%pattern%)
37 %$$
38 $icode%target% = %matrix%
39 %$$
40 $icode%matrix%.set(%k%, %v%)
41 %$$
42 $icode%nr% = %matrix%.nr()
43 %$$
44 $icode%nc% = %matrix%.nc()
45 %$$
46 $icode%nnz% = %matrix%.nnz()
47 %$$
48 $codei%const %SizeVector%& %row%( %matrix%.row() )
49 %$$
50 $codei%const %SizeVector%& %col%( %matrix%.col() )
51 %$$
52 $codei%const %ValueVector%& %val%( %matrix%.val() )
53 %$$
54 $icode%row_major% = %matrix%.row_major()
55 %$$
56 $icode%col_major% = %matrix%.col_major()
57 %$$
58 
59 $head SizeVector$$
60 We use $cref/SizeVector/sparse_rc/SizeVector/$$ to denote the
61 $cref SimpleVector$$ class corresponding to $icode pattern$$.
62 
63 $head ValueVector$$
64 We use $icode ValueVector$$ to denote the
65 $cref SimpleVector$$ class corresponding to $icode val$$.
66 
67 $head empty$$
68 This is an empty sparse matrix object. To be specific,
69 the corresponding number of rows $icode nr$$,
70 number of columns $icode nc$$,
71 and number of possibly non-zero values $icode nnz$$,
72 are all zero.
73 
74 
75 $head pattern$$
76 This argument has prototype
77 $codei%
78  const sparse_rc<%SizeVector%>& %pattern%
79 %$$
80 It specifies the number of rows, number of columns and
81 the possibly non-zero entries in the $icode matrix$$.
82 
83 $head matrix$$
84 This is a sparse matrix object with the sparsity specified by $icode pattern$$.
85 Only the $icode val$$ vector can be changed. All other values returned by
86 $icode matrix$$ are fixed during the constructor and constant there after.
87 The $icode val$$ vector is only changed by the constructor
88 and the $code set$$ function.
89 There is one exception to the rule, where $icode matrix$$ corresponds
90 to $icode target$$ for an assignment statement.
91 
92 $head target$$
93 The target of the assignment statement must have prototype
94 $codei%
95  sparse_rcv<%SizeVector%, %ValueVector%> %target%
96 %$$
97 After this assignment statement, $icode target$$ is an independent copy
98 of $icode matrix$$; i.e. it has all the same values as $icode matrix$$
99 and changes to $icode target$$ do not affect $icode matrix$$.
100 
101 $head nr$$
102 This return value has prototype
103 $codei%
104  size_t %nr%
105 %$$
106 and is the number of rows in $icode matrix$$.
107 
108 $head nc$$
109 This argument and return value has prototype
110 $codei%
111  size_t %nc%
112 %$$
113 and is the number of columns in $icode matrix$$.
114 
115 $head nnz$$
116 We use the notation $icode nnz$$ to denote the number of
117 possibly non-zero entries in $icode matrix$$.
118 
119 $head set$$
120 This function sets the value
121 $codei%
122  %val%[%k%] = %v%
123 %$$
124 
125 $subhead k$$
126 This argument has type
127 $codei%
128  size_t %k%
129 %$$
130 and must be less than $icode nnz$$.
131 
132 $subhead v$$
133 This argument has type
134 $codei%
135  const %ValueVector%::value_type& %v%
136 %$$
137 It specifies the value assigned to $icode%val%[%k%]%$$.
138 
139 
140 $head row$$
141 This vector has size $icode nnz$$ and
142 $icode%row%[%k%]%$$
143 is the row index of the $th k$$ possibly non-zero
144 element in $icode matrix$$.
145 
146 $head col$$
147 This vector has size $icode nnz$$ and
148 $icode%col[%k%]%$$ is the column index of the $th k$$ possibly non-zero
149 element in $icode matrix$$
150 
151 $head val$$
152 This vector has size $icode nnz$$ and
153 $icode%val[%k%]%$$ is value of the $th k$$ possibly non-zero entry
154 in the sparse matrix (the value may be zero).
155 
156 $head row_major$$
157 This vector has prototype
158 $codei%
159  %SizeVector% %row_major%
160 %$$
161 and its size $icode nnz$$.
162 It sorts the sparsity pattern in row-major order.
163 To be specific,
164 $codei%
165  %col%[ %row_major%[%k%] ] <= %col%[ %row_major%[%k%+1] ]
166 %$$
167 and if $icode%col%[ %row_major%[%k%] ] == %col%[ %row_major%[%k%+1] ]%$$,
168 $codei%
169  %row%[ %row_major%[%k%] ] < %row%[ %row_major%[%k%+1] ]
170 %$$
171 This routine generates an assert if there are two entries with the same
172 row and column values (if $code NDEBUG$$ is not defined).
173 
174 $head col_major$$
175 This vector has prototype
176 $codei%
177  %SizeVector% %col_major%
178 %$$
179 and its size $icode nnz$$.
180 It sorts the sparsity pattern in column-major order.
181 To be specific,
182 $codei%
183  %row%[ %col_major%[%k%] ] <= %row%[ %col_major%[%k%+1] ]
184 %$$
185 and if $icode%row%[ %col_major%[%k%] ] == %row%[ %col_major%[%k%+1] ]%$$,
186 $codei%
187  %col%[ %col_major%[%k%] ] < %col%[ %col_major%[%k%+1] ]
188 %$$
189 This routine generates an assert if there are two entries with the same
190 row and column values (if $code NDEBUG$$ is not defined).
191 
192 $children%
193  example/utility/sparse_rcv.cpp
194 %$$
195 $head Example$$
196 The file $cref sparse_rcv.cpp$$
197 contains an example and test of this class.
198 It returns true if it succeeds and false otherwise.
199 
200 $end
201 */
202 /*!
203 \file sparse_rcv.hpp
204 A sparse matrix class.
205 */
206 # include <cppad/utility/sparse_rc.hpp>
207 
208 namespace CppAD { // BEGIN CPPAD_NAMESPACE
209 
210 /// Sparse matrices with elements of type Scalar
211 template <class SizeVector, class ValueVector>
212 class sparse_rcv {
213 private:
214  /// sparsity pattern
216  /// value_type
218  /// val_[k] is the value for the k-th possibly non-zero entry in the matrix
219  ValueVector val_;
220 public:
221  // ------------------------------------------------------------------------
222  /// default constructor
224  : pattern_(0, 0, 0), val_(0)
225  { }
226  /// constructor
228  :
229  pattern_(pattern) ,
230  val_(pattern_.nnz())
231  { }
232  /// assignment
233  void operator=(const sparse_rcv& matrix)
234  { pattern_ = matrix.pattern_;
235  // simple vector assignment requires vectors to have same size
236  val_.resize( matrix.nnz() );
237  val_ = matrix.val();
238  }
239  // ------------------------------------------------------------------------
240  void set(size_t k, const value_type& v)
242  pattern_.nnz(),
243  "The index k is not less than nnz in sparse_rcv::set"
244  );
245  val_[k] = v;
246  }
247  /// number of rows in matrix
248  size_t nr(void) const
249  { return pattern_.nr(); }
250  /// number of columns in matrix
251  size_t nc(void) const
252  { return pattern_.nc(); }
253  /// number of possibly non-zero elements in matrix
254  size_t nnz(void) const
255  { return pattern_.nnz(); }
256  /// row indices
257  const SizeVector& row(void) const
258  { return pattern_.row(); }
259  /// column indices
260  const SizeVector& col(void) const
261  { return pattern_.col(); }
262  /// value for possibly non-zero elements
263  const ValueVector& val(void) const
264  { return val_; }
265  /// row-major order
266  SizeVector row_major(void) const
267  { return pattern_.row_major(); }
268  /// column-major indices
269  SizeVector col_major(void) const
270  { return pattern_.col_major(); }
271 };
272 
273 } // END_CPPAD_NAMESPACE
274 
275 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
SizeVector col_major(void) const
column-major indices
Definition: sparse_rcv.hpp:269
sparse_rc< SizeVector > pattern_
sparsity pattern
Definition: sparse_rcv.hpp:215
void set(size_t k, const value_type &v)
Definition: sparse_rcv.hpp:240
size_t nnz(void) const
number of possibly non-zero elements in matrix
Definition: sparse_rcv.hpp:254
ValueVector::value_type value_type
value_type
Definition: sparse_rcv.hpp:217
const SizeVector & col(void) const
column indices
Definition: sparse_rcv.hpp:260
size_t nc(void) const
number of columns in matrix
Definition: sparse_rcv.hpp:251
Sparse matrices with elements of type Scalar.
Definition: sparse_rcv.hpp:212
A Matrix sparsity pattern class.
size_t nr(void) const
number of rows in matrix
Definition: sparse_rcv.hpp:248
sparsity pattern for a matrix with indices of type size_t
Definition: sparse_rc.hpp:212
SizeVector row_major(void) const
row-major order
Definition: sparse_rcv.hpp:266
sparse_rcv(const sparse_rc< SizeVector > &pattern)
constructor
Definition: sparse_rcv.hpp:227
ValueVector val_
val_[k] is the value for the k-th possibly non-zero entry in the matrix
Definition: sparse_rcv.hpp:219
sparse_rcv(void)
default constructor
Definition: sparse_rcv.hpp:223
void operator=(const sparse_rcv &matrix)
assignment
Definition: sparse_rcv.hpp:233
const SizeVector & row(void) const
row indices
Definition: sparse_rcv.hpp:257
const ValueVector & val(void) const
value for possibly non-zero elements
Definition: sparse_rcv.hpp:263
Scalar value_type