1 #ifndef CPPAD_CG_LANGUAGE_MATHML_INDEX_PATTERNS_INCLUDED 2 #define CPPAD_CG_LANGUAGE_MATHML_INDEX_PATTERNS_INCLUDED 22 inline void LanguageMathML<Base>::generateNames4RandomIndexPatterns(
const std::set<RandomIndexPattern*>& randomPatterns) {
23 std::ostringstream os;
25 std::set<std::string> usedNames;
29 for (RandomIndexPattern* ip : randomPatterns) {
30 if (!ip->getName().empty()) {
31 usedNames.insert(ip->getName());
37 for (RandomIndexPattern* ip : randomPatterns) {
38 if (ip->getName().empty()) {
42 os << _C_STATIC_INDEX_ARRAY << c;
46 }
while (usedNames.find(name) != usedNames.end());
48 ip->setName(
"<mi>" + name +
"</mi>");
56 const std::string& indentation,
57 const std::set<RandomIndexPattern*>& randomPatterns) {
59 if (ip->getType() == IndexPatternType::Random1D) {
64 const std::map<size_t, size_t>& x2y = ip1->getValues();
66 std::vector<size_t> y(x2y.rbegin()->first + 1);
67 for (
const std::pair<size_t, size_t>& p : x2y)
68 y[p.first] = p.second;
71 printStaticIndexArray(os, ip->getName(), y);
73 CPPADCG_ASSERT_UNKNOWN(ip->getType() == IndexPatternType::Random2D);
79 printStaticIndexMatrix(os, ip->getName(), ip2->getValues());
86 const std::string& name,
87 const std::vector<size_t>& values) {
88 os << name <<
" <mo>=</mo> <mfenced open='[' close=']' separators=','>";
89 for (
size_t i = 0; i < values.size(); i++) {
90 os <<
"<mn>" << values[i] <<
"</mn>";
92 os <<
"</mfenced>" << _endEq <<
" <!-- size: " << values.size() <<
" -->" << _endline;
97 const std::string& name,
98 const std::map<
size_t, std::map<size_t, size_t> >& values) {
105 std::map<size_t, std::map<size_t, size_t> >::const_iterator it;
106 std::map<size_t, size_t>::const_iterator ity2z;
108 if (!values.empty()) {
109 m = values.rbegin()->first + 1;
111 for (it = values.begin(); it != values.end(); ++it) {
112 if (!it->second.empty())
113 n = std::max(n, it->second.rbegin()->first + 1);
117 os << name <<
"<mo>=</mo><mtable>";
119 for (it = values.begin(); it != values.end(); ++it) {
120 if (it->first != x) {
121 while (it->first != x) {
122 os <<
"<mtr><mtd/></mtr>";
129 for (ity2z = it->second.begin(); ity2z != it->second.end(); ++ity2z) {
130 if (ity2z->first != y) {
131 while (ity2z->first != y) {
132 os <<
"<mtd><mn>0</mn></mtd>";
137 os <<
"<mtd><mn>" << ity2z->second <<
"</mn></mtd>";
146 os <<
"</mtable>" << _endEq <<
"<!-- size: " << m <<
" x " << n <<
" -->" << _endline;
153 indexPattern2String(os, ip,{&index});
160 std::stringstream ss;
161 switch (ip.getType()) {
162 case IndexPatternType::Linear:
164 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes");
166 linearIndexPattern2String(os, lip, *indexes[0]);
169 case IndexPatternType::Sectioned:
171 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes");
173 const std::map<size_t, IndexPattern*>& sections = lip->getLinearSections();
174 size_t sSize = sections.size();
175 CPPADCG_ASSERT_UNKNOWN(sSize > 1);
177 std::map<size_t, IndexPattern*>::const_iterator its = sections.begin();
178 for (
size_t s = 0; s < sSize - 1; s++) {
181 size_t xStart = its->first;
183 os <<
"<mfenced><mrow><mi class='index'>" << (*indexes[0]->getName()) <<
"</mi> <mo><</mo> <mn>" << xStart <<
"</mn></mrow></mfenced><mo>?</mo> ";
184 indexPattern2String(os, *lp, *indexes[0]);
187 indexPattern2String(os, *its->second, *indexes[0]);
192 case IndexPatternType::Plane2D:
194 CPPADCG_ASSERT_KNOWN(indexes.size() >= 1,
"Invalid number of indexes");
196 bool useParens = pip.getPattern1() !=
nullptr && pip.getPattern2() !=
nullptr;
198 if (useParens) os <<
"<mfenced><mrow>";
200 if (pip.getPattern1() !=
nullptr)
201 indexPattern2String(os, *pip.getPattern1(), *indexes[0]);
203 if (useParens) os <<
"</mrow></mfenced> <mo>+</mo> <mfenced><mrow>";
205 if (pip.getPattern2() !=
nullptr)
206 indexPattern2String(os, *pip.getPattern2(), *indexes.back());
208 if (useParens) os <<
"</mrow></mfenced>";
212 case IndexPatternType::Random1D:
214 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes");
216 CPPADCG_ASSERT_KNOWN(!rip.getName().empty(),
"Invalid name for array");
217 os << rip.getName() <<
"<mfenced open='[' close=']'><mi>" << (*indexes[0]->getName()) <<
"</mi></mfenced>";
220 case IndexPatternType::Random2D:
222 CPPADCG_ASSERT_KNOWN(indexes.size() == 2,
"Invalid number of indexes");
224 CPPADCG_ASSERT_KNOWN(!rip.getName().empty(),
"Invalid name for array");
225 os << rip.getName() <<
226 "<mfenced open='[' close=']'><mrow><mi>" << (*indexes[0]->getName()) <<
"</mi></mrow></mfenced>" 227 "<mfenced open='[' close=']'><mrow><mi>" << (*indexes[1]->getName()) <<
"</mi></mrow></mfenced>";
231 CPPADCG_ASSERT_UNKNOWN(
false);
240 long dy = lip.getLinearSlopeDy();
241 long dx = lip.getLinearSlopeDx();
242 long b = lip.getLinearConstantTerm();
243 long xOffset = lip.getXOffset();
247 os <<
"<mfenced><mrow>";
249 os <<
"<mi class='index'>" << (*index.
getName()) <<
"</mi>";
251 os <<
" <mo>-</mo><mn>" << xOffset <<
"</mn></mrow></mfenced>";
255 os <<
"<mo>/</mo><mn>" << dx <<
"</mn>";
258 os <<
"<mo>⋅</mo><mn>" << dy <<
"</mn>";
267 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)