CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
subgraph_sparsity.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_SUBGRAPH_SPARSITY_HPP
2 # define CPPAD_CORE_SUBGRAPH_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 subgraph_sparsity$$
16 $spell
17  const
18  subgraph
19  subgraphs
20  rc
21  Jacobian
22  bool
23 $$
24 
25 $section Subgraph Dependency Sparsity Patterns$$
26 
27 $head Syntax$$
28 $icode%f%.subgraph_sparsity(
29  %select_domain%, %select_range%, %transpose%, %pattern_out%
30 )%$$
31 
32 $head Notation$$
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 
37 $head Method$$
38 This routine uses a subgraph technique. To be specific,
39 for each dependent variable,
40 it a subgraph of the operation sequence
41 to determine which independent variables affect it.
42 This avoids to overhead of performing set operations
43 that is inherent in other methods for computing sparsity patterns.
44 
45 $head Atomic Function$$
46 The sparsity calculation for
47 $cref/atomic functions/atomic_afun/$$ in the $icode f$$ operation sequence
48 are not efficient. To be specific, each atomic function is treated as if
49 all of its outputs depend on all of its inputs.
50 This may be improved upon in the future; see the
51 $cref/subgraph atomic functions/wish_list/Atomic/Subgraph/$$
52 wish list item.
53 
54 $head BoolVector$$
55 The type $icode BoolVector$$ is a $cref SimpleVector$$ class with
56 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
57 $code bool$$.
58 
59 $head SizeVector$$
60 The type $icode SizeVector$$ is a $cref SimpleVector$$ class with
61 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
62 $code size_t$$.
63 
64 $head f$$
65 The object $icode f$$ has prototype
66 $codei%
67  ADFun<%Base%> %f%
68 %$$
69 
70 $head select_domain$$
71 The argument $icode select_domain$$ has prototype
72 $codei%
73  const %BoolVector%& %select_domain%
74 %$$
75 It has size $latex n$$ and specifies which independent variables
76 to include in the calculation.
77 If not all the independent variables are included in the calculation,
78 a forward pass on the operation sequence is used to determine which
79 nodes may be in the subgraphs.
80 
81 $head select_range$$
82 The argument $icode select_range$$ has prototype
83 $codei%
84  const %BoolVector%& %select_range%
85 %$$
86 It has size $latex m$$ and specifies which components of the range
87 to include in the calculation.
88 A subgraph is built for each dependent variable
89 and the selected set of independent variables.
90 
91 $head transpose$$
92 This argument has prototype
93 $codei%
94  bool %transpose%
95 %$$
96 If $icode transpose$$ it is false (true),
97 upon return $icode pattern_out$$ is a sparsity pattern for
98 $latex J(x)$$ ($latex J(x)^\R{T}$$) defined below.
99 
100 $head pattern_out$$
101 This argument has prototype
102 $codei%
103  sparse_rc<%SizeVector%>& %pattern_out%
104 %$$
105 This input value of $icode pattern_out$$ does not matter.
106 Upon return $icode pattern_out$$ is a
107 $cref/dependency pattern/dependency.cpp/Dependency Pattern/$$
108 for $latex F(x)$$.
109 The pattern has $latex m$$ rows, $latex n$$ columns.
110 If $icode%select_domain%[%j%]%$$ is true,
111 $icode%select_range%[%i%]%$$ is true, and
112 $latex F_i (x)$$ depends on $latex x_j$$,
113 then the pair $latex (i, j)$$ is in $icode pattern_out$$.
114 Not that this is also a sparsity pattern for the Jacobian
115 $latex \[
116  J(x) = R F^{(1)} (x) D
117 \] $$
118 where $latex D$$ ($latex R$$) is the diagonal matrix corresponding
119 to $icode select_domain$$ ($icode select_range$$).
120 
121 $head Example$$
122 $children%
123  example/sparse/subgraph_sparsity.cpp
124 %$$
125 The file
126 $cref subgraph_sparsity.cpp$$
127 contains an example and test of this operation.
128 It returns true if it succeeds and false otherwise.
129 
130 $end
131 -----------------------------------------------------------------------------
132 */
133 # include <cppad/core/ad_fun.hpp>
135 
136 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
137 
138 /*!
139 Subgraph sparsity patterns.
140 
141 \tparam Base
142 is the base type for this recording.
143 
144 \tparam SizeVector
145 is the simple vector with elements of type size_t that is used for
146 row, column index sparsity patterns.
147 
148 \param select_domain
149 sparsity pattern for the diagonal of the square matrix D.
150 
151 \param select_range
152 sparsity pattern for the diagnoal of the square matrix R
153 
154 \param transpose
155 If true, the return is a dependency sparsity pattern for
156 \f$ D F^{(1)} (x)^T R \f$
157 
158 \param pattern_out
159 The input value does not matter.
160 The return value is a dependency sparsity pattern for \f$ R F^{(1)} (x) D \f$
161 where F is the function corresponding to the operation sequence
162 and x is any argument value.
163 is the sparsity pattern transposed.
164 */
165 template <typename Base>
166 template <typename BoolVector, typename SizeVector>
168  const BoolVector& select_domain ,
169  const BoolVector& select_range ,
170  bool transpose ,
171  sparse_rc<SizeVector>& pattern_out )
172 {
173  // compute the sparsity pattern in row, col
177  &play_,
178  subgraph_info_,
179  dep_taddr_,
180  select_domain,
181  select_range,
182  row,
183  col
184  );
185  CPPAD_ASSERT_UNKNOWN( row.size() == col.size() );
186 
187  // return the sparsity pattern
188  size_t nr = dep_taddr_.size();
189  size_t nc = ind_taddr_.size();
190  size_t nnz = row.size();
191  if( transpose )
192  { pattern_out.resize(nc, nr, nnz);
193  for(size_t k = 0; k < nnz; k++)
194  pattern_out.set(k, col[k], row[k]);
195  }
196  else
197  { pattern_out.resize(nr, nc, nnz);
198  for(size_t k = 0; k < nnz; k++)
199  pattern_out.set(k, row[k], col[k]);
200  }
201  return;
202 }
203 } // END_CPPAD_NAMESPACE
204 # endif
void subgraph_sparsity(const BoolVector &select_domain, const BoolVector &select_range, bool transpose, sparse_rc< SizeVector > &pattern_out)
Subgraph sparsity patterns.
sparsity pattern for a matrix with indices of type size_t
Definition: sparse_rc.hpp:212
size_t size(void) const
current number of elements in this vector.
Definition: pod_vector.hpp:79
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.
void subgraph_sparsity(const player< Base > *play, subgraph_info &sub_info, const vector< size_t > &dep_taddr, const BoolVector &select_domain, const BoolVector &select_range, pod_vector< size_t > &row_out, pod_vector< size_t > &col_out)
Compute dependency sparsity pattern for an ADFun&lt;Base&gt; function.
Definition: sparsity.hpp:83
File used to define the ADFun&lt;Base&gt; class.
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
Compute dependency sparsity pattern using subgraph technique.