1 #ifndef CPPAD_CG_LANGUAGE_MATHML_INDEX_PATTERNS_INCLUDED 2 #define CPPAD_CG_LANGUAGE_MATHML_INDEX_PATTERNS_INCLUDED 23 inline void LanguageMathML<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());
49 ip->setName(
"<mi>" + name +
"</mi>");
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);
80 printStaticIndexMatrix(os, ip->getName(), ip2->getValues());
87 const std::string& name,
88 const std::vector<size_t>& values) {
89 os << name <<
" <mo>=</mo> <mfenced open='[' close=']' separators=','>";
90 for (
size_t i = 0; i < values.size(); i++) {
91 os <<
"<mn>" << values[i] <<
"</mn>";
93 os <<
"</mfenced>" << _endEq <<
" <!-- size: " << values.size() <<
" -->" << _endline;
98 const std::string& name,
99 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 <<
"<mo>=</mo><mtable>";
120 for (it = values.begin(); it != values.end(); ++it) {
121 if (it->first != x) {
122 while (it->first != x) {
123 os <<
"<mtr><mtd/></mtr>";
130 for (ity2z = it->second.begin(); ity2z != it->second.end(); ++ity2z) {
131 if (ity2z->first != y) {
132 while (ity2z->first != y) {
133 os <<
"<mtd><mn>0</mn></mtd>";
138 os <<
"<mtd><mn>" << ity2z->second <<
"</mn></mtd>";
147 os <<
"</mtable>" << _endEq <<
"<!-- size: " << m <<
" x " << n <<
" -->" << _endline;
154 indexPattern2String(os, ip,{&index});
161 std::stringstream ss;
162 switch (ip.getType()) {
163 case IndexPatternType::Linear:
165 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes")
166 const auto& lip = static_cast<const LinearIndexPattern&> (ip);
167 linearIndexPattern2String(os, 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 os <<
"<mfenced><mrow><mi class='index'>" << (*indexes[0]->getName()) <<
"</mi> <mo><</mo> <mn>" << xStart <<
"</mn></mrow></mfenced><mo>?</mo> ";
185 indexPattern2String(os, *lp, *indexes[0]);
188 indexPattern2String(os, *its->second, *indexes[0]);
193 case IndexPatternType::Plane2D:
195 CPPADCG_ASSERT_KNOWN(indexes.size() >= 1,
"Invalid number of indexes")
196 const auto& pip = static_cast<const Plane2DIndexPattern&> (ip);
197 bool useParens = pip.getPattern1() !=
nullptr && pip.getPattern2() !=
nullptr;
199 if (useParens) os <<
"<mfenced><mrow>";
201 if (pip.getPattern1() !=
nullptr)
202 indexPattern2String(os, *pip.getPattern1(), *indexes[0]);
204 if (useParens) os <<
"</mrow></mfenced> <mo>+</mo> <mfenced><mrow>";
206 if (pip.getPattern2() !=
nullptr)
207 indexPattern2String(os, *pip.getPattern2(), *indexes.back());
209 if (useParens) os <<
"</mrow></mfenced>";
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 os << rip.getName() <<
"<mfenced open='[' close=']'><mi>" << (*indexes[0]->getName()) <<
"</mi></mfenced>";
221 case IndexPatternType::Random2D:
223 CPPADCG_ASSERT_KNOWN(indexes.size() == 2,
"Invalid number of indexes")
224 const auto& rip = static_cast<const Random2DIndexPattern&> (ip);
225 CPPADCG_ASSERT_KNOWN(!rip.getName().empty(),
"Invalid name for array")
226 os << rip.getName() <<
227 "<mfenced open='[' close=']'><mrow><mi>" << (*indexes[0]->getName()) <<
"</mi></mrow></mfenced>" 228 "<mfenced open='[' close=']'><mrow><mi>" << (*indexes[1]->getName()) <<
"</mi></mrow></mfenced>";
232 CPPADCG_ASSERT_UNKNOWN(
false);
241 long dy = lip.getLinearSlopeDy();
242 long dx = lip.getLinearSlopeDx();
243 long b = lip.getLinearConstantTerm();
244 long xOffset = lip.getXOffset();
248 os <<
"<mfenced><mrow>";
250 os <<
"<mi class='index'>" << (*index.
getName()) <<
"</mi>";
252 os <<
" <mo>-</mo><mn>" << xOffset <<
"</mn></mrow></mfenced>";
256 os <<
"<mo>/</mo><mn>" << dx <<
"</mn>";
259 os <<
"<mo>⋅</mo><mn>" << dy <<
"</mn>";
268 os <<
"<mn>" << b <<
"</mn>";
void printRandomIndexPatternDeclaration(std::ostringstream &os, const std::string &identation, const std::set< RandomIndexPattern *> &randomPatterns)
const std::string * getName() const
void printStaticIndexMatrix(std::ostringstream &os, const std::string &name, const std::map< size_t, std::map< size_t, size_t > > &values)