CppADCodeGen 2.4.3
A C++ Algorithmic Differentiation Package with Source Code Generation
Loading...
Searching...
No Matches
index_pattern_impl.hpp
1#ifndef CPPAD_CG_INDEX_PATTERN_IMPL_INCLUDED
2#define CPPAD_CG_INDEX_PATTERN_IMPL_INCLUDED
3/* --------------------------------------------------------------------------
4 * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5 * Copyright (C) 2013 Ciengis
6 *
7 * CppADCodeGen is distributed under multiple licenses:
8 *
9 * - Eclipse Public License Version 1.0 (EPL1), and
10 * - GNU General Public License Version 3 (GPL3).
11 *
12 * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13 * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14 * ----------------------------------------------------------------------------
15 * Author: Joao Leal
16 */
17
18namespace CppAD {
19namespace cg {
20
21template<class VectorSizeT>
22IndexPattern* IndexPattern::detect(const VectorSizeT& x2y) {
23 CPPADCG_ASSERT_UNKNOWN(x2y.size() > 0)
24
25 size_t maxCount = std::min<size_t>(std::max<size_t>(3ul, x2y.size() / 4), 8ul);
26 std::map<size_t, IndexPattern*> linearSections = SectionedIndexPattern::detectLinearSections(x2y, maxCount);
27
28 if (linearSections.size() == 1) {
29 return linearSections.begin()->second;
30 } else if (!linearSections.empty()) {
31 return new SectionedIndexPattern(linearSections);
32 } else {
33 return new Random1DIndexPattern(x2y);
34 }
35
36}
37
38IndexPattern* IndexPattern::detect(const std::map<size_t, size_t>& x2y) {
39 CPPADCG_ASSERT_UNKNOWN(!x2y.empty())
40
41 size_t maxCount = std::min<size_t>(std::max<size_t>(3ul, x2y.size() / 4), 8ul);
42 std::map<size_t, IndexPattern*> linearSections = SectionedIndexPattern::detectLinearSections(x2y, maxCount);
43
44 if (linearSections.size() == 1) {
45 return linearSections.begin()->second;
46 } else if (!linearSections.empty()) {
47 return new SectionedIndexPattern(linearSections);
48 } else {
49 return new Random1DIndexPattern(x2y);
50 }
51}
52
53inline bool IndexPattern::isConstant(const IndexPattern& ip) {
54 if (ip.getType() == IndexPatternType::Linear) {
55 const auto& lip = static_cast<const LinearIndexPattern&> (ip);
56 return lip.getLinearSlopeDy() == 0;
57 }
58 return false;
59}
60
61} // END cg namespace
62} // END CppAD namespace
63
64#endif
static IndexPattern * detect(const VectorSizeT &x2y)