1#ifndef CPPAD_CG_LANGUAGE_MATHML_INDEX_PATTERNS_INCLUDED
2#define CPPAD_CG_LANGUAGE_MATHML_INDEX_PATTERNS_INCLUDED
23inline void LanguageMathML<Base>::generateNames4RandomIndexPatterns(
const std::set<RandomIndexPattern*>&
randomPatterns) {
24 std::ostringstream os;
31 if (!
ip->getName().empty()) {
39 if (
ip->getName().empty()) {
43 os << _C_STATIC_INDEX_ARRAY <<
c;
49 ip->setName(
"<mi>" + name +
"</mi>");
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>";
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});
158inline void LanguageMathML<Base>::indexPattern2String(std::ostream& os,
159 const IndexPattern&
ip,
161 std::stringstream
ss;
162 switch (
ip.getType()) {
163 case IndexPatternType::Linear:
165 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes")
167 linearIndexPattern2String(os,
lip, *indexes[0]);
170 case IndexPatternType::Sectioned:
172 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes")
176 CPPADCG_ASSERT_UNKNOWN(
sSize > 1);
179 for (
size_t s = 0; s <
sSize - 1; s++) {
180 const IndexPattern*
lp =
its->second;
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")
197 bool useParens =
pip.getPattern1() !=
nullptr &&
pip.getPattern2() !=
nullptr;
201 if (
pip.getPattern1() !=
nullptr)
202 indexPattern2String(os, *
pip.getPattern1(), *indexes[0]);
206 if (
pip.getPattern2() !=
nullptr)
207 indexPattern2String(os, *
pip.getPattern2(), *indexes.back());
213 case IndexPatternType::Random1D:
215 CPPADCG_ASSERT_KNOWN(indexes.size() == 1,
"Invalid number of indexes")
217 CPPADCG_ASSERT_KNOWN(!
rip.getName().empty(), "
Invalid name
for array")
221 case IndexPatternType::Random2D:
223 CPPADCG_ASSERT_KNOWN(indexes.size() == 2,
"Invalid number of indexes")
225 CPPADCG_ASSERT_KNOWN(!
rip.getName().empty(), "
Invalid name
for array")
226 os <<
rip.getName() <<
232 CPPADCG_ASSERT_UNKNOWN(
false);
238inline void LanguageMathML<Base>::linearIndexPattern2String(std::ostream& os,
240 const OperationNode<Base>& index) {
241 long dy =
lip.getLinearSlopeDy();
242 long dx =
lip.getLinearSlopeDx();
243 long b =
lip.getLinearConstantTerm();
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)
void printStaticIndexMatrix(std::ostringstream &os, const std::string &name, const std::map< size_t, std::map< size_t, size_t > > &values)
bool GreaterThanZero(const cg::CG< Base > &x)