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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
#include "Storage.hh"
#include "algorithms/join_gamma.hh"
#include "Exceptions.hh"
#include "Cleanup.hh"
#include "properties/Integer.hh"
using namespace cadabra;
join_gamma::join_gamma(const Kernel& kernel, Ex& tr_, bool e, bool g)
: Algorithm(kernel, tr_), expand(e), use_generalised_delta_(g)
{
}
void join_gamma::regroup_indices_(sibling_iterator gam1, sibling_iterator gam2,
unsigned int i, std::vector<Ex>& r1, std::vector<Ex>& r2)
{
unsigned int num1=tr.number_of_children(gam1);
unsigned int len1=0;
unsigned int len2=0;
sibling_iterator g1=tr.begin(gam1);
while(len1<num1-i) {
r1.push_back(*g1);
++g1;
++len1;
}
sibling_iterator g2=tr.begin(gam2);
while(g2!=tr.end(gam2)) {
if(len2>=i)
r2.push_back(*g2);
++g2;
++len2;
}
if(i>0) {
g2=tr.begin(gam2);
g1=tr.end(gam1);
--g1;
len1=0;
for(len1=0; len1<i; ++len1) {
r1.push_back(*g1);
r2.push_back(*g2);
--g1;
++g2;
}
}
}
void join_gamma::append_prod_(const std::vector<Ex>& r1, const std::vector<Ex>& r2,
unsigned int num1, unsigned int num2, unsigned int i, multiplier_t mult,
Ex& rep, iterator loc)
{
Ex::iterator gamma;
bool hasgamma =(num1-i>0 || num2-i>0);
bool hasdelta =(i>0);
bool hasmoredeltas=(i>1 && !use_generalised_delta_);
str_node::bracket_t subsbr=gamma_bracket_;
if((hasgamma && hasdelta) || hasmoredeltas) {
loc=rep.append_child(loc, str_node("\\prod", gamma_bracket_, (*loc).fl.parent_rel));
loc->multiplier=rat_set.insert(mult).first;
subsbr=str_node::b_none;
}
if(num1-i>0 || num2-i>0) {
gamma=rep.append_child(loc, str_node(*gamma_name_->name, subsbr));
for(unsigned int j=0; j<num1-i; ++j)
rep.append_child(gamma, r1[j].begin());
for(unsigned int j=0; j<num2-i; ++j)
rep.append_child(gamma, r2[j].begin()); //str_node(*r2[j].name, str_node::b_none, r2[j].fl.parent_rel));
if(!hasdelta)
gamma->multiplier=rat_set.insert(mult).first;
}
Ex::iterator delt;
if(use_generalised_delta_ && i>0) {
if(gm1->metric.size()==0)
throw ConsistencyException("The gamma matrix property does not contain metric information.");
delt=rep.append_child(loc, gm1->metric.begin());
delt->fl.bracket=subsbr;
tr.erase_children(delt);
if(!hasgamma)
delt->multiplier=rat_set.insert(mult).first;
}
for(unsigned int j=0; j<i; ++j) {
if(!use_generalised_delta_) {
if(gm1->metric.size()==0)
throw ConsistencyException("The gamma matrix property does not contain metric information.");
delt=rep.append_child(loc, gm1->metric.begin());
delt->fl.bracket=subsbr;
tr.erase_children(delt);
}
if(tree_exact_less(&kernel.properties, r1[j+num1-i], r2[j+num2-i]) || use_generalised_delta_) {
rep.append_child(delt, r1[j+num1-i].begin());
rep.append_child(delt, r2[j+num2-i].begin());
}
else {
rep.append_child(delt, r2[j+num2-i].begin());
rep.append_child(delt, r1[j+num1-i].begin());
}
}
}
bool join_gamma::can_apply(iterator st)
{
if(*st->name=="\\prod") {
sibling_iterator fc=tr.begin(st);
while(fc!=tr.end(st)) {
gm1=kernel.properties.get<GammaMatrix>(fc);
if(gm1) {
std::string target=get_index_set_name(begin_index(fc));
++fc;
if(fc!=tr.end(st)) {
gm2=kernel.properties.get<GammaMatrix>(fc);
if(gm2) {
if(target==get_index_set_name(begin_index(fc))) {
only_expand.clear();
// FIXME: handle only expansion into single term
// else if(it->is_rational()) {
// only_expand.push_back(to_long(*it->multiplier));
return true;
}
else --fc;
}
}
}
++fc;
}
}
return false;
}
Algorithm::result_t join_gamma::apply(iterator& st)
{
assert(*st->name=="\\prod");
sibling_iterator gam1=tr.begin(st);
sibling_iterator gam2;
while(gam1!=tr.end(st)) {
const GammaMatrix *gm1=kernel.properties.get<GammaMatrix>(gam1);
if(gm1) {
gamma_name_=gam1;
gam2=gam1;
++gam2;
if(gam2!=tr.end(st)) {
const GammaMatrix *gm2=kernel.properties.get<GammaMatrix>(gam2);
if(gm2)
break;
}
}
++gam1;
}
if(gam1==tr.end(st)) {
st=tr.end();
return result_t::l_error;
}
gamma_bracket_=gam2->fl.bracket;
Ex rep;
sibling_iterator top=rep.set_head(str_node("\\sum"));
// Figure out the dimension of the gamma matrix.
long number_of_dimensions=-1; // i.e. not known.
index_iterator firstind=begin_index(gam1);
while(firstind!=end_index(gam1)) { // select the maximum value; FIXME: be more refined...
const Integer *ipr=kernel.properties.get<Integer>(firstind, true);
if(ipr) {
if(ipr->difference.begin()->is_integer()) {
number_of_dimensions=std::max(number_of_dimensions, to_long(*ipr->difference.begin()->multiplier));
}
}
else {
number_of_dimensions=-1;
break;
}
++firstind;
}
if(number_of_dimensions!=-1) {
firstind=begin_index(gam2);
while(firstind!=end_index(gam2)) { // select the maximum value; FIXME: be more refined...
const Integer *ipr=kernel.properties.get<Integer>(firstind, true);
if(ipr) {
if(ipr->difference.begin()->is_integer()) {
number_of_dimensions=std::max(number_of_dimensions, to_long(*ipr->difference.begin()->multiplier));
}
}
else {
number_of_dimensions=-1;
break;
}
++firstind;
}
}
// iterators over the two index ranges
unsigned int num1=tr.number_of_children(gam1);
unsigned int num2=tr.number_of_children(gam2);
for(unsigned int i=0; i<=std::min(num1, num2); ++i) {
// Ignore gammas with more than 'd' indices.
if(number_of_dimensions>0) {
if(num1+num2 > number_of_dimensions+2*i) {
continue;
}
}
if(only_expand.size()!=0) {
if(std::find(only_expand.begin(), only_expand.end(), (int)(num1+num2-2*i))==only_expand.end())
// if((int)(num1+num2-2*i)!=only_expand)
continue;
}
std::vector<Ex> r1, r2;
regroup_indices_(gam1, gam2, i, r1, r2);
multiplier_t mult=(combin::fact(multiplier_t(num1))*combin::fact(multiplier_t(num2)))/
(combin::fact(multiplier_t(num1-i))*combin::fact(multiplier_t(num2-i))*combin::fact(multiplier_t(i)));
// debugout << "join: contracting " << i << " indices..." << std::endl;
if(!expand) {
append_prod_(r1, r2, num1, num2, i, mult, rep, top);
}
else {
combin::combinations<Ex> c1(r1);
combin::combinations<Ex> c2(r2);
if(num1-i>0)
c1.sublengths.push_back(num1-i);
if(num2-i>0)
c2.sublengths.push_back(num2-i);
if(use_generalised_delta_ && i>0)
c1.sublengths.push_back(i);
else {
for(unsigned int k=0; k<i; ++k)
c1.sublengths.push_back(1); // the individual \deltas, antisymmetrise 'first' group
}
if(i>0)
c2.sublengths.push_back(i); // the individual \deltas, do not antisymmetrise again.
// Collect information about which indices to write in implicit antisymmetric form.
// FIXME: this should move into combinatorics.hh
// iterator it=args_begin();
// while(it!=args_end()) {
// if(*it->name=="\\comma") {
// sibling_iterator cst=tr.begin(it);
// combin::range_t asymrange1, asymrange2;
// while(cst!=tr.end(it)) {
// for(unsigned int i1=0; i1<r1.size(); ++i1) {
// if(subtree_exact_equal(&kernel.properties, r1[i1].begin(), cst, 0)) {
// asymrange1.push_back(i1);
// break;
// }
// }
// for(unsigned int i2=0; i2<r2.size(); ++i2) {
// if(subtree_exact_equal(&kernel.properties, r2[i2].begin(), cst, 0)) {
// asymrange2.push_back(i2);
// break;
// }
// }
// ++cst;
// }
// c1.input_asym.push_back(asymrange1);
// c2.input_asym.push_back(asymrange2);
// }
// ++it;
// }
c1.permute();
c2.permute();
for(unsigned int k=0; k<c1.size(); ++k) {
for(unsigned int l=0; l<c2.size(); ++l) {
if(interrupted) {
// FIXME: handle interrupts gracefully.
// txtout << "join interrupted while producing GammaMatrix[" << num1+num2-2*i
// << "] terms." << std::endl;
interrupted=false;
st=tr.end();
return result_t::l_error;
}
int sgn=
combin::ordersign(c1[k].begin(), c1[k].end(), r1.begin(), r1.end())
*combin::ordersign(c2[l].begin(), c2[l].end(), r2.begin(), r2.end());
multiplier_t mul=1;
if(use_generalised_delta_)
mul=combin::fact(i);
append_prod_(c1[k], c2[l], num1, num2, i,
multiplier_t(c1.multiplier(k))*multiplier_t(c2.multiplier(l))*sgn*mul, rep, top);
}
}
}
}
// Finally, replace the old product by the new sum of products.
if(rep.number_of_children(rep.begin())==0) {
multiply(st->multiplier,0);
return result_t::l_applied;
}
else if(rep.number_of_children(rep.begin())==1) {
rep.flatten(rep.begin());
rep.erase(rep.begin());
}
if(tr.number_of_children(st)>2) { // erase one gamma, replace the other one
multiply(rep.begin()->multiplier, *gam1->multiplier);
multiply(rep.begin()->multiplier, *gam2->multiplier);
tr.replace(tr.erase(gam1), rep.begin());
}
else {
multiply(rep.begin()->multiplier, *st->multiplier);
st = tr.replace(st, rep.begin());
}
cleanup_dispatch(kernel, tr, st);
// cleanup_expression(tr, st);
// cleanup_nests(tr, st);
return result_t::l_applied;
}
|