1 #ifndef CPPAD_CG_LANGUAGE_LATEX_INDEX_PATTERNS_INCLUDED 2 #define CPPAD_CG_LANGUAGE_LATEX_INDEX_PATTERNS_INCLUDED 23 inline void LanguageLatex<Base>::generateNames4RandomIndexPatterns(
const std::set<RandomIndexPattern*>& randomPatterns) {
24 std::ostringstream os;
26 std::set<std::string> usedNames;
30 for (RandomIndexPattern* ip : randomPatterns) {
31 if (!ip->getName().empty()) {
32 usedNames.insert(ip->getName());
38 for (RandomIndexPattern* ip : randomPatterns) {
39 if (ip->getName().empty()) {
43 os << _C_STATIC_INDEX_ARRAY << c;
47 }
while (usedNames.find(name) != usedNames.end());
57 const std::string& indentation,
58 const std::set<RandomIndexPattern*>& randomPatterns) {
60 if (ip->getType() == IndexPatternType::Random1D) {
65 const std::map<size_t, size_t>& x2y = ip1->getValues();
67 std::vector<size_t> y(x2y.rbegin()->first + 1);
68 for (
const auto& p : x2y)
69 y[p.first] = p.second;
72 printStaticIndexArray(os, ip->getName(), y);
74 CPPADCG_ASSERT_UNKNOWN(ip->getType() == IndexPatternType::Random2D)
78 auto* ip2 = static_cast<Random2DIndexPattern*> (ip);
80 printStaticIndexMatrix(os, ip->getName(), ip2->getValues());
87 const std::string& name,
88 const std::vector<size_t>& values) {
89 os << name <<
" = \\left\\{";
90 if (!values.empty()) {
92 for (
size_t i = 1; i < values.size(); i++) {
93 os <<
", " << values[i];
96 os <<
"\\right\\}" << _endEq <<
" % size: " << values.size() << _endline;
101 const std::string& name,
102 const std::map<
size_t, std::map<size_t, size_t> >& values) {
106 std::map<size_t, std::map<size_t, size_t> >::const_iterator it;
107 std::map<size_t, size_t>::const_iterator ity2z;
109 if (!values.empty()) {
110 m = values.rbegin()->first + 1;
112 for (it = values.begin(); it != values.end(); ++it) {
113 if (!it->second.empty())
114 n = std::max<size_t>(n, it->second.rbegin()->first + 1);
118 os << name <<
" = \\left\\{";
120 for (it = values.begin(); it != values.end(); ++it) {
121 if (it->first != x) {
122 while (it->first != x) {
131 for (ity2z = it->second.begin(); ity2z != it->second.end(); ++ity2z) {
132 if (ity2z->first != y) {
133 while (ity2z->first != y) {
140 if (ity2z->first != it->second.rbegin()->first) os <<
",";
146 if (it->first != values.rbegin()->first) os <<
",";
150 os <<
"\\right\\}" << _endEq <<
"% size: " << m <<
" x " << n << _endline;
156 return indexPattern2String(ip,{&index});
162 std::stringstream ss;
163 switch (ip.getType()) {
164 case IndexPatternType::Linear:
166 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes")
167 const auto& lip = static_cast<const LinearIndexPattern&> (ip);
168 return linearIndexPattern2String(lip, *indexes[0]);
170 case IndexPatternType::Sectioned:
172 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes")
173 const auto* lip = static_cast<const SectionedIndexPattern*> (&ip);
174 const std::map<size_t, IndexPattern*>& sections = lip->getLinearSections();
175 size_t sSize = sections.size();
176 CPPADCG_ASSERT_UNKNOWN(sSize > 1)
178 auto its = sections.begin();
179 for (
size_t s = 0; s < sSize - 1; s++) {
182 size_t xStart = its->first;
184 ss <<
"\\left(" << (*indexes[0]->getName()) <<
"<" << xStart <<
"\\right)? " 185 << indexPattern2String(*lp, *indexes[0]) <<
": ";
187 ss << indexPattern2String(*its->second, *indexes[0]);
192 case IndexPatternType::Plane2D:
194 CPPADCG_ASSERT_KNOWN(indexes.size() >= 1,
"Invalid number of indexes")
195 std::string indexExpr;
197 bool useParens = pip.getPattern1() !=
nullptr && pip.getPattern2() !=
nullptr;
199 if (useParens) indexExpr +=
"\\left(";
201 if (pip.getPattern1() !=
nullptr)
202 indexExpr += indexPattern2String(*pip.getPattern1(), *indexes[0]);
204 if (useParens) indexExpr +=
"\\right) + \\left(";
206 if (pip.getPattern2() !=
nullptr)
207 indexExpr += indexPattern2String(*pip.getPattern2(), *indexes.back());
209 if (useParens) indexExpr +=
"\\right)";
213 case IndexPatternType::Random1D:
215 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes")
216 const auto& rip = static_cast<const Random1DIndexPattern&> (ip);
217 CPPADCG_ASSERT_KNOWN(!rip.getName().empty(),
"Invalid name for array")
218 return rip.getName() +
"\\left[" + (*indexes[0]->getName()) +
"\\right]";
220 case IndexPatternType::Random2D:
222 CPPADCG_ASSERT_KNOWN(indexes.size() == 2,
"Invalid number of indexes")
223 const auto& rip = static_cast<const Random2DIndexPattern&> (ip);
224 CPPADCG_ASSERT_KNOWN(!rip.getName().empty(),
"Invalid name for array")
225 return rip.getName() +
"\\left[" + (*indexes[0]->getName()) +
"\\right]\\left[" + (*indexes[1]->getName()) +
"\\right]";
228 CPPADCG_ASSERT_UNKNOWN(
false);
236 long dy = lip.getLinearSlopeDy();
237 long dx = lip.getLinearSlopeDx();
238 long b = lip.getLinearConstantTerm();
239 long xOffset = lip.getXOffset();
241 std::stringstream ss;
248 ss <<
" - " << xOffset <<
"\\right)";
255 ss <<
" \\cdot " << dy;
const std::string * getName() const
void printRandomIndexPatternDeclaration(std::ostringstream &os, const std::string &identation, const std::set< RandomIndexPattern *> &randomPatterns)