CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rev_jac_sparsity.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_REV_JAC_SPARSITY_HPP
2 # define CPPAD_CORE_REV_JAC_SPARSITY_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 $begin rev_jac_sparsity$$
16 $spell
17  Jacobian
18  jac
19  bool
20  const
21  rc
22  cpp
23 $$
24 
25 $section Reverse Mode Jacobian Sparsity Patterns$$
26 
27 $head Syntax$$
28 $icode%f%.rev_jac_sparsity(
29  %pattern_in%, %transpose%, %dependency%, %internal_bool%, %pattern_out%
30 )%$$
31 
32 $head Purpose$$
33 We use $latex F : \B{R}^n \rightarrow \B{R}^m$$ to denote the
34 $cref/AD function/glossary/AD Function/$$ corresponding to
35 the operation sequence stored in $icode f$$.
36 Fix $latex R \in \B{R}^{\ell \times m}$$ and define the function
37 $latex \[
38  J(x) = R * F^{(1)} ( x )
39 \] $$
40 Given the $cref/sparsity pattern/glossary/Sparsity Pattern/$$ for $latex R$$,
41 $code rev_jac_sparsity$$ computes a sparsity pattern for $latex J(x)$$.
42 
43 $head x$$
44 Note that the sparsity pattern $latex J(x)$$ corresponds to the
45 operation sequence stored in $icode f$$ and does not depend on
46 the argument $icode x$$.
47 (The operation sequence may contain
48 $cref CondExp$$ and $cref VecAD$$ operations.)
49 
50 $head SizeVector$$
51 The type $icode SizeVector$$ is a $cref SimpleVector$$ class with
52 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
53 $code size_t$$.
54 
55 $head f$$
56 The object $icode f$$ has prototype
57 $codei%
58  ADFun<%Base%> %f%
59 %$$
60 
61 $head pattern_in$$
62 The argument $icode pattern_in$$ has prototype
63 $codei%
64  const sparse_rc<%SizeVector%>& %pattern_in%
65 %$$
66 see $cref sparse_rc$$.
67 If $icode transpose$$ it is false (true),
68 $icode pattern_in$$ is a sparsity pattern for $latex R$$ ($latex R^\R{T}$$).
69 
70 $head transpose$$
71 This argument has prototype
72 $codei%
73  bool %transpose%
74 %$$
75 See $cref/pattern_in/rev_jac_sparsity/pattern_in/$$ above and
76 $cref/pattern_out/rev_jac_sparsity/pattern_out/$$ below.
77 
78 $head dependency$$
79 This argument has prototype
80 $codei%
81  bool %dependency%
82 %$$
83 see $cref/pattern_out/rev_jac_sparsity/pattern_out/$$ below.
84 
85 $head internal_bool$$
86 If this is true, calculations are done with sets represented by a vector
87 of boolean values. Otherwise, a vector of sets of integers is used.
88 
89 $head pattern_out$$
90 This argument has prototype
91 $codei%
92  sparse_rc<%SizeVector%>& %pattern_out%
93 %$$
94 This input value of $icode pattern_out$$ does not matter.
95 If $icode transpose$$ it is false (true),
96 upon return $icode pattern_out$$ is a sparsity pattern for
97 $latex J(x)$$ ($latex J(x)^\R{T}$$).
98 If $icode dependency$$ is true, $icode pattern_out$$ is a
99 $cref/dependency pattern/dependency.cpp/Dependency Pattern/$$
100 instead of sparsity pattern.
101 
102 $head Sparsity for Entire Jacobian$$
103 Suppose that
104 $latex R$$ is the $latex m \times m$$ identity matrix.
105 In this case, $icode pattern_out$$ is a sparsity pattern for
106 $latex F^{(1)} ( x )$$ ( $latex F^{(1)} (x)^\R{T}$$ )
107 if $icode transpose$$ is false (true).
108 
109 $head Example$$
110 $children%
111  example/sparse/rev_jac_sparsity.cpp
112 %$$
113 The file
114 $cref rev_jac_sparsity.cpp$$
115 contains an example and test of this operation.
116 It returns true if it succeeds and false otherwise.
117 
118 $end
119 -----------------------------------------------------------------------------
120 */
121 # include <cppad/core/ad_fun.hpp>
123 
124 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
125 
126 /*!
127 Reverse Jacobian sparsity patterns.
128 
129 \tparam Base
130 is the base type for this recording.
131 
132 \tparam SizeVector
133 is the simple vector with elements of type size_t that is used for
134 row, column index sparsity patterns.
135 
136 \param pattern_in
137 is the sparsity pattern for for R or R^T depending on transpose.
138 
139 \param transpose
140 Is the input and returned sparsity pattern transposed.
141 
142 \param dependency
143 Are the derivatives with respect to left and right of the expression below
144 considered to be non-zero:
145 \code
146  CondExpRel(left, right, if_true, if_false)
147 \endcode
148 This is used by the optimizer to obtain the correct dependency relations.
149 
150 \param internal_bool
151 If this is true, calculations are done with sets represented by a vector
152 of boolean values. Otherwise, a vector of standard sets is used.
153 
154 \param pattern_out
155 The value of transpose is false (true),
156 the return value is a sparsity pattern for J(x) ( J(x)^T ) where
157 \f[
158  J(x) = R * F^{(1)} (x)
159 \f]
160 Here F is the function corresponding to the operation sequence
161 and x is any argument value.
162 */
163 template <class Base>
164 template <class SizeVector>
166  const sparse_rc<SizeVector>& pattern_in ,
167  bool transpose ,
168  bool dependency ,
169  bool internal_bool ,
170  sparse_rc<SizeVector>& pattern_out )
171 { // number or rows, columns, and non-zeros in pattern_in
172  size_t nr_in = pattern_in.nr();
173  size_t nc_in = pattern_in.nc();
174  //
175  size_t ell = nr_in;
176  size_t m = nc_in;
177  if( transpose )
178  std::swap(ell, m);
179  //
181  m == Range() ,
182  "rev_jac_sparsity: number columns in R "
183  "is not equal number of dependent variables."
184  );
185  // number of independent variables
186  size_t n = Domain();
187  //
188  bool zero_empty = true;
189  bool input_empty = true;
190  if( internal_bool )
191  { // allocate memory for bool sparsity calculation
192  // (sparsity pattern is emtpy after a resize)
193  local::sparse_pack internal_jac;
194  internal_jac.resize(num_var_tape_, ell);
195  //
196  // set sparsity patttern for dependent variables
198  zero_empty ,
199  input_empty ,
200  ! transpose ,
201  dep_taddr_ ,
202  internal_jac ,
203  pattern_in
204  );
205 
206  // compute sparsity for other variables
208  &play_,
209  dependency,
210  n,
211  num_var_tape_,
212  internal_jac
213  );
214  // get sparstiy pattern for independent variables
216  ! transpose, ind_taddr_, internal_jac, pattern_out
217  );
218  }
219  else
220  { // allocate memory for bool sparsity calculation
221  // (sparsity pattern is emtpy after a resize)
222  local::sparse_list internal_jac;
223  internal_jac.resize(num_var_tape_, ell);
224  //
225  // set sparsity patttern for dependent variables
227  zero_empty ,
228  input_empty ,
229  ! transpose ,
230  dep_taddr_ ,
231  internal_jac ,
232  pattern_in
233  );
234 
235  // compute sparsity for other variables
237  &play_,
238  dependency,
239  n,
240  num_var_tape_,
241  internal_jac
242  );
243  // get sparstiy pattern for independent variables
245  ! transpose, ind_taddr_, internal_jac, pattern_out
246  );
247  }
248  return;
249 }
250 } // END_CPPAD_NAMESPACE
251 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
Vector of sets of positive integers, each set stored as a singly linked list.
Definition: sparse_list.hpp:35
void get_internal_sparsity(bool transpose, const vector< size_t > &internal_index, const InternalSparsity &internal_pattern, sparse_rc< SizeVector > &pattern_out)
Get sparsity pattern for a sub-set of variables.
size_t nr(void) const
number of rows in matrix
Definition: sparse_rc.hpp:284
Vector of sets of postivie integers, each set stored as a packed boolean array.
Definition: sparse_pack.hpp:33
sparsity pattern for a matrix with indices of type size_t
Definition: sparse_rc.hpp:212
void resize(size_t n_set, size_t end)
Start a new vector of sets.
size_t nc(void) const
number of columns in matrix
Definition: sparse_rc.hpp:287
File used to define the ADFun&lt;Base&gt; class.
void set_internal_sparsity(bool zero_empty, bool input_empty, bool transpose, const vector< size_t > &internal_index, InternalSparsity &internal_pattern, const sparse_rc< SizeVector > &pattern_in)
Update the internal sparsity pattern for a sub-set of rows.
Routines that enable code to be independent of which internal spasity pattern is used.
void rev_jac_sparsity(const sparse_rc< SizeVector > &pattern_in, bool transpose, bool dependency, bool internal_bool, sparse_rc< SizeVector > &pattern_out)
Reverse Jacobian sparsity patterns.
void rev_jac_sweep(const local::player< Base > *play, bool dependency, size_t n, size_t numvar, Vector_set &var_sparsity)
Given the sparsity pattern for the dependent variables, RevJacSweep computes the sparsity pattern for...
void resize(size_t n_set, size_t end)
Change number of sets, set end, and initialize all sets as empty.