1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include "PreClean.hh"
#include "Cleanup.hh"
namespace cadabra {
void pre_clean_dispatch(const Kernel& kernel, Ex& ex, Ex::iterator& it)
{
if(*it->name!="1" && it->is_unsimplified_rational()) cleanup_rational(kernel, ex, it);
if(*it->name=="\\frac") cleanup_frac(kernel, ex, it);
else if(*it->name=="\\sub") cleanup_sub(kernel, ex, it);
else if(*it->name=="\\sqrt") cleanup_sqrt(kernel, ex, it);
else if((*it->name).substr(0,2)=="UP" || (*it->name).substr(0,2)=="DN") cleanup_updown(kernel, ex, it);
cleanup_indexbracket(kernel, ex, it);
}
void pre_clean_dispatch_deep(const Kernel& k, Ex& tr)
{
return cleanup_dispatch_deep(k, tr, &pre_clean_dispatch);
}
void cleanup_updown(const Kernel&, Ex&, Ex::iterator& st)
{
std::string rn=*st->name;
bool isup=true;
if(rn.substr(0,2)=="DN")
isup=false;
rn=rn.substr(2);
// tr.flatten(st);
// st=tr.erase(st);
if(isup) st->fl.parent_rel=str_node::p_super;
else st->fl.parent_rel=str_node::p_sub;
st->name=name_set.insert(rn).first;
}
void cleanup_rational(const Kernel&, Ex&, Ex::iterator& st)
{
multiplier_t num(*st->name);
num.canonicalize();
st->name=name_set.insert("1").first;
multiply(st->multiplier,num);
}
void cleanup_frac(const Kernel&, Ex& tr, Ex::iterator& st)
{
// Catch \frac{} nodes with one argument; those are supposed to be read as 1/(...).
// The only exception is \frac{#}, which needs to stay as it is.
if(tr.number_of_children(st)==1) {
if(tr.begin(st)->is_range_wildcard()) return;
tr.insert(tr.begin(st), str_node("1"));
}
// Turn this into a \prod node. Everything except the first child
// should be wrapped in a \pow{..}{-1} node.
auto sib=tr.begin(st);
++sib;
while(sib!=tr.end(st)) {
sib = tr.wrap(sib, str_node("\\pow"));
multiply( tr.append_child(sib, str_node("1"))->multiplier, -1 );
++sib;
}
st->name=name_set.insert("\\prod").first;
// assert(tr.number_of_children(st)>1);
// Ex::sibling_iterator it=tr.begin(st);
// multiplier_t rat;
//
// bool allnumerical=true;
// rat=*(it->multiplier);
// if(it->is_rational()==false)
// allnumerical=false;
//
// one(it->multiplier);
// ++it;
// while(it!=tr.end(st)) {
// if(*it->multiplier==0) {
// // CHECK: do these zeroes get handled correctly elsewhere?
// return;
// }
// rat/=*it->multiplier;
// one(it->multiplier);
// if(it->is_rational()==false) allnumerical=false;
// ++it;
// }
// if(allnumerical) { // can remove the \frac altogether
// tr.erase_children(st);
// st->name=name_set.insert("1").first;
// }
// else { // just remove the all-numerical child nodes
// it=tr.begin(st);
// ++it;
// while(it!=tr.end(st)) {
// if(it->is_rational())
// it=tr.erase(it);
// else ++it;
// }
// if(tr.number_of_children(st)==1) {
// tr.begin(st)->fl.bracket=st->fl.bracket;
// tr.begin(st)->fl.parent_rel=st->fl.parent_rel;
// multiply(tr.begin(st)->multiplier, *st->multiplier);
// tr.flatten(st);
// st=tr.erase(st);
// }
// }
// multiply(st->multiplier, rat);
}
void cleanup_sqrt(const Kernel&, Ex& tr, Ex::iterator& st)
{
st->name=name_set.insert("\\pow").first;
multiply(tr.append_child(st, str_node("1"))->multiplier, multiplier_t(1)/2);
}
void cleanup_sub(const Kernel&, Ex& tr, Ex::iterator& it)
{
assert(tr.number_of_children(it)>1); // To guarantee that we have really cleaned up that old stuff.
it->name=name_set.insert("\\sum").first;
Ex::sibling_iterator sit=tr.begin(it);
// Make sure that all terms have the right sign, and zeroes are removed.
if(*sit->multiplier==0) sit=tr.erase(sit);
else ++sit;
while(sit!=tr.end(it)) {
if(*sit->multiplier==0)
sit=tr.erase(sit);
else {
flip_sign(sit->multiplier);
++sit;
}
}
// Single-term situation: remove the \sum.
if(tr.number_of_children(it)==0) {
zero(it->multiplier);
it->name=name_set.insert("1").first;
}
else {
if(tr.number_of_children(it)==1) {
sit=tr.begin(it);
sit->fl.parent_rel=it->fl.parent_rel;
sit->fl.bracket=it->fl.bracket;
multiply(sit->multiplier, *it->multiplier);
tr.flatten(it);
it=tr.erase(it);
}
}
}
void cleanup_indexbracket(const Kernel&, Ex& tr, Ex::iterator& it)
{
if((*it->name).size()==0) {
auto sib=tr.begin(it);
if(sib->fl.parent_rel!=str_node::p_super && sib->fl.parent_rel!=str_node::p_sub) {
++sib;
while(sib!=tr.end(it)) {
if(sib->fl.parent_rel==str_node::p_super || sib->fl.parent_rel==str_node::p_sub) {
it->name=name_set.insert("\\indexbracket").first;
return;
}
++sib;
}
}
}
else if(*it->name=="\\prod" || *it->name=="\\sum") {
auto sib=tr.begin(it);
while(sib!=tr.end(it)) {
if(sib->fl.parent_rel==str_node::p_super || sib->fl.parent_rel==str_node::p_sub) {
auto ibrack=tr.insert(it,str_node("\\indexbracket"));
Ex::sibling_iterator nxt=it;
++nxt;
tr.reparent(ibrack,Ex::sibling_iterator(it),nxt);
it=tr.begin(ibrack);
auto sib=tr.begin(it);
while(sib!=tr.end(it)) {
if(sib->fl.parent_rel==str_node::p_super || sib->fl.parent_rel==str_node::p_sub) {
tr.append_child(ibrack,*sib);
sib=tr.erase(sib);
}
else ++sib;
}
it=ibrack;
return;
}
++sib;
}
}
}
std::string replace_all(std::string const& original, std::string const& from, std::string const& to )
{
std::string results;
std::string::const_iterator end = original.end();
std::string::const_iterator current = original.begin();
std::string::const_iterator next = std::search( current, end, from.begin(), from.end() );
while ( next != end ) {
results.append( current, next );
results.append( to );
current = next + from.size();
next = std::search( current, end, from.begin(), from.end() );
}
results.append( current, next );
return results;
}
}
|