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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#include "Cleanup.hh"
#include "Exceptions.hh"
#include "algorithms/vary.hh"
#include "algorithms/substitute.hh"
#include "algorithms/product_rule.hh"
#include "properties/Derivative.hh"
#include "properties/Accent.hh"
using namespace cadabra;
// #define DEBUG 1
vary::vary(const Kernel& k, Ex& tr, Ex& args_)
: Algorithm(k, tr), args(args_)
{
}
bool vary::can_apply(iterator it)
{
if(it->is_zero()) return false;
if(*it->name=="\\prod") return true;
if(*it->name=="\\commutator") return true;
if(*it->name=="\\anticommutator") return true;
if(*it->name=="\\sum") return true;
if(*it->name=="\\pow") return true;
if(*it->name=="\\int") return true;
if(*it->name=="\\equals") return false;
if(is_single_term(it)) return true;
if(is_nonprod_factor_in_prod(it)) return true;
const Derivative *der = kernel.properties.get<Derivative>(it);
if(der) return true;
const Accent *acc = kernel.properties.get<Accent>(it);
if(acc) return true;
if(!tr.is_head(it)) {
der = kernel.properties.get<Derivative>(tr.parent(it));
if(der) return true;
acc = kernel.properties.get<Accent>(tr.parent(it));
if(acc) return true;
}
return false;
}
/*
D(A) C + D(A);
@vary(%)(
*/
Algorithm::result_t vary::apply(iterator& it)
{
result_t res=result_t::l_no_action;
if(*it->name=="\\prod" || *it->name=="\\commutator" || *it->name=="\\anticommutator") {
Ex result;
result.set_head(str_node("\\sum"));
iterator newsum=result.begin();
// Iterate over all factors, attempting a substitute. If this
// succeeds, copy the term to the "result" tree. Then restore the
// original. We have to do the substitute on the original tree so
// that index relabelling takes into account the rest of the tree.
Ex prodcopy(it); // keep a copy to restore after each substitute
vary varyfac(kernel, tr, args);
int pos=0;
for(;;) {
sibling_iterator fcit=tr.begin(it);
fcit+=pos;
if(fcit==tr.end(it)) break;
iterator fcit2(fcit);
if(varyfac.can_apply(fcit2)) {
result_t res = varyfac.apply(fcit2);
if(fcit2->is_zero()==false && res==result_t::l_applied) {
auto toclean = result.append_child(newsum, it);
// the varied factor itself cannot get rid of nested
// products, that's for us to do at the top level prod.
cleanup_dispatch(kernel, tr, toclean);
// std::cerr << toclean << std::endl;
}
// restore original
it=tr.replace(it, prodcopy.begin());
}
++pos;
}
if(tr.number_of_children(newsum)>0) {
it=tr.move_ontop(it, newsum);
}
else { // varying any of the factors produces nothing, variation is zero
zero(it->multiplier);
}
// std::cerr << it << std::endl;
cleanup_dispatch(kernel, tr, it);
// std::cerr << it << std::endl;
res=result_t::l_applied;
return res;
}
const Derivative *der = kernel.properties.get<Derivative>(it);
const Accent *acc = kernel.properties.get<Accent>(it);
if(der || acc) {
vary vry(kernel, tr, args);
sibling_iterator sib=tr.begin(it);
bool has_applied=false;
while(sib!=tr.end(it)) {
iterator app=sib;
++sib;
if(app->is_index()) continue;
if(vry.can_apply(app)) {
if(vry.apply(app)==result_t::l_applied) {
has_applied=true;
res=result_t::l_applied;
}
}
}
// If no variation took place, set to zero if we are termlike.
if(!has_applied && is_termlike(it)) {
zero(it->multiplier);
return result_t::l_applied;
}
return res;
}
if(*it->name=="\\sum") { // call vary on every term
vary vry(kernel, tr, args);
sibling_iterator sib=tr.begin(it);
while(sib!=tr.end(it)) {
iterator app=sib;
++sib;
if(vry.can_apply(app)) {
res = vry.apply(app);
}
else {
// remove this term
res=result_t::l_applied;
node_zero(app);
}
}
cleanup_dispatch(kernel, tr, it);
return res;
}
if(*it->name=="\\int") { // call vary on first argument
vary vry(kernel, tr, args);
iterator sib=tr.begin(it);
if(vry.can_apply(sib))
res=vry.apply(sib);
return res;
}
if(*it->name=="\\pow") {
Ex backup(it);
// Wrap the power in a \cdbDerivative and then call @prodrule.
it=tr.wrap(it, str_node("\\cdbDerivative"));
product_rule pr(kernel, tr);
pr.can_apply(it);
pr.apply(it);
// Find the '\cdbDerivative node again'.
sibling_iterator sib=tr.begin(it);
res=result_t::l_no_action;
while(sib!=tr.end(it)) {
if(*sib->name=="\\cdbDerivative") {
tr.flatten(sib);
sib=tr.erase(sib);
vary vry(kernel, tr, args);
iterator app=sib;
if(vry.can_apply(app)) {
res=vry.apply(app);
}
break;
}
++sib;
}
if(res!=result_t::l_applied) {
// restore original
it=tr.replace(it, backup.begin());
}
return res;
}
if(tr.is_head(it)==false) {
der = kernel.properties.get<Derivative>(tr.parent(it));
acc = kernel.properties.get<Accent>(tr.parent(it));
if(der || acc || is_single_term(it)) { // easy: just vary this term by substitution
// std::cerr << "single term " << *it->name << std::endl;
substitute subs(kernel, tr, args);
if(subs.can_apply(it)) {
if(subs.apply(it)==result_t::l_applied) {
return result_t::l_applied;
}
}
if(is_termlike(it)) {
zero(it->multiplier);
return result_t::l_applied;
}
return result_t::l_no_action;
}
}
if(is_nonprod_factor_in_prod(it)) {
substitute subs(kernel, tr, args);
if(subs.can_apply(it)) {
if(subs.apply(it)==result_t::l_applied) {
return result_t::l_applied;
}
}
if(is_termlike(it)) {
zero(it->multiplier);
return result_t::l_applied;
}
return result_t::l_no_action;
}
// If we get here, we are talking about a single term, e.g.
// ex:= x_{m};
if(is_single_term(it)) {
substitute subs(kernel, tr, args);
if(subs.can_apply(it)) {
if(subs.apply(it)==result_t::l_applied) {
return result_t::l_applied;
}
}
}
// If we get here we have a single term for which we do not know
// (yet) how to take a variational derivative. For instance some
// unknown function f(a) varied wrt. a. This should spit out
// \delta{f(a)}{\delta{a}}\delta{a} or something like that.
throw RuntimeException("Do not yet know how to vary that expression.");
// std::cerr << "No idea how to vary single term " << Ex(it) << std::endl;
return res;
}
|