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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
|
/*
Cadabra: a field-theory motivated computer algebra system.
Copyright (C) 2001-2015 Kasper Peeters <kasper.peeters@phi-sci.com>
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stddef.h>
#include "Algorithm.hh"
#include "Storage.hh"
#include "Props.hh"
#include "Cleanup.hh"
#include <typeinfo>
#include <boost/version.hpp>
#if BOOST_VERSION > 105500
#include <boost/core/demangle.hpp>
#endif
//#include <boost/stacktrace.hpp>
#include "properties/Accent.hh"
#include "properties/Derivative.hh"
#include "properties/Indices.hh"
#include "properties/Coordinate.hh"
#include "properties/Symbol.hh"
#include "properties/Trace.hh"
#include "properties/DependsBase.hh"
#include <sstream>
//#define DEBUG
using namespace cadabra;
Algorithm::Algorithm(const Kernel& k, Ex& tr_)
: IndexClassifier(k),
interrupted(false),
number_of_calls(0), number_of_modifications(0),
suppress_normal_output(false),
discard_command_node(false),
tr(tr_),
pm(0),
traverse_ldots(false)
{
}
Algorithm::~Algorithm()
{
}
void Algorithm::set_progress_monitor(ProgressMonitor *pm_)
{
pm=pm_;
}
Algorithm::result_t Algorithm::apply_pre_order(bool repeat)
{
#if BOOST_VERSION > 105500
ScopedProgressGroup(pm, boost::core::demangle(typeid(*this).name()));
#else
ScopedProgressGroup(typeid(*this).name());
#endif
result_t ret=result_t::l_no_action;
Ex::iterator start=tr.begin();
while(start!=tr.end()) {
if(traverse_ldots || !tr.is_hidden(start)) {
if(start->is_index()==false) {
auto aor=apply_once(start);
if(aor==result_t::l_applied || aor==result_t::l_applied_no_new_dummies) {
ret=result_t::l_applied;
// Need to cleanup on the entire tree above us.
if(!repeat) {
start.skip_children();
++start;
}
}
else ++start;
}
else ++start;
}
else {
++start;
}
}
cleanup_dispatch_deep(kernel, tr);
return ret;
}
Algorithm::result_t Algorithm::apply_generic(bool deep, bool repeat, unsigned int depth)
{
auto it = tr.begin();
return apply_generic(it, deep, repeat, depth);
}
Algorithm::result_t Algorithm::apply_generic(Ex::iterator& it, bool deep, bool repeat, unsigned int depth)
{
#if BOOST_VERSION > 105500
ScopedProgressGroup(pm, boost::core::demangle(typeid(*this).name()));
#else
ScopedProgressGroup(typeid(*this).name());
#endif
result_t ret=result_t::l_no_action;
Ex::fixed_depth_iterator start=tr.begin_fixed(it, depth, false);
#ifdef DEBUG
std::cerr << "apply_generic at " << it.node << " " << *it->name << " " << *start->name << std::endl;
#endif
while(tr.is_valid(start)) {
// std::cerr << "evaluate main loop at " << *start->name << std::endl;
#ifdef DEBUG
std::cerr << "main loop for " << typeid(*this).name() << ":\n" << Ex(start) << std::endl;
#endif
result_t thisret=result_t::l_no_action;
Ex::iterator enter(start);
Ex::fixed_depth_iterator next(start);
++next;
// if(tr.is_valid(next))
// std::cerr << "next = " << *next->name << std::endl;
do {
// std::cout << "apply at " << *enter->name << std::endl;
bool work_is_topnode=(enter==it);
if(deep && depth==0)
thisret = apply_deep(enter);
else
thisret = apply_once(enter);
if(work_is_topnode)
it=enter;
// FIXME: handle l_error or remove
if(thisret==result_t::l_applied || thisret==result_t::l_applied_no_new_dummies)
ret=result_t::l_applied;
}
while(depth==0 && repeat && (thisret==result_t::l_applied || thisret==result_t::l_applied_no_new_dummies));
if(depth==0) {
// std::cerr << "break " << std::endl;
break;
}
else {
// std::cerr << "no break " << std::endl;
}
start=next;
}
// std::cerr << "pre-exit node " << it.node << std::endl;
// If we are acting at fixed depth, we will not have gone up in the
// tree, so missed one cleanup action. Do it now.
if(depth>0) {
Ex::fixed_depth_iterator start=tr.begin_fixed(it, depth-1, false);
while(tr.is_valid(start)) {
Ex::iterator work=start;
++start;
bool cpy=false;
if(work==it) cpy=true;
cleanup_dispatch(kernel, tr, work);
if(cpy) it=work;
}
}
// std::cerr << "exit node " << it.node << std::endl;
// if(tr.is_valid(it)) {
// std::cerr << "exit " << *it->name << std::endl;
// std::cerr << "exit apply_generic\n" << Ex(it) << std::endl;
// }
return ret;
}
Algorithm::result_t Algorithm::apply_once(Ex::iterator& it)
{
// std::cerr << "=== apply_once ===" << std::endl;
if(traverse_ldots || !tr.is_hidden(it)) {
if(can_apply(it)) {
result_t res=apply(it);
// std::cerr << "apply algorithm at " << *it->name << std::endl;
if(res==result_t::l_applied || res==result_t::l_applied_no_new_dummies) {
cleanup_dispatch(kernel, tr, it);
return res;
}
}
}
return result_t::l_no_action;
}
Algorithm::result_t Algorithm::apply_deep(Ex::iterator& it)
{
// This recursive algorithm walks the tree depth-first
// (parent-after-child). The algorithm is applied on each node if
// can_apply returns true. When the iterator goes up one level
// (i.e. from a child to a parent), and any changes have been made
// so far at the child level level, cleanup and simplification
// routines will be called. The only nodes that can be removed from
// the tree are nodes at a lower level than the simplification
// node.
// std::cout << "=== apply_deep ===" << std::endl;
// tr.print_recursive_treeform(std::cout, it);
post_order_iterator current=it;
current.descend_all();
post_order_iterator last=it;
int deepest_action = -1;
// std::cout << "apply_deep: it = " << *it->name << std::endl;
bool stop_after_this_one=false;
result_t some_changes_somewhere=result_t::l_no_action;
for(;;) {
// std::cout << "reached " << *current->name << std::endl;
#ifdef DEBUG
std::cout << "apply_deep " << typeid(*this).name() << ": current = " << *current->name << std::endl;
#endif
if(current.node==last.node) {
// std::cout << "stop after this one" << std::endl;
stop_after_this_one=true;
}
if(deepest_action > tr.depth(current)) {
#ifdef DEBUG
std::cerr << "simplify; we are at " << *(current->name) << std::endl;
#endif
iterator work=current;
bool work_is_topnode=(work==it);
cleanup_dispatch(kernel, tr, work);
current=work;
if(work_is_topnode)
it=work;
#ifdef DEBUG
std::cerr << "current now " << *(current->name) << std::endl;
tr.print_recursive_treeform(std::cerr, current);
#endif
deepest_action = tr.depth(current); // needs to propagate upwards
}
if((traverse_ldots || !tr.is_hidden(current)) && can_apply(current)) {
#ifdef DEBUG
std::cout << "acting at " << *current->name << std::endl;
#endif
iterator work=current;
post_order_iterator next(current);
++next;
bool work_is_topnode=(work==it);
result_t res = apply(work);
if(res==Algorithm::result_t::l_applied || res==Algorithm::result_t::l_applied_no_new_dummies) {
some_changes_somewhere=result_t::l_applied;
if(res==Algorithm::result_t::l_applied) {
// std::cerr << "rename replacement on " << work << std::endl;
rename_replacement_dummies(work, true);
}
deepest_action=tr.depth(work);
// If we got a zero at 'work', we need to propagate this up the tree and
// then restart our post-order traversal such that everything that has
// been removed from the tree by this zero will no longer be considered.
if(*work->multiplier==0) {
#ifdef DEBUG
std::cerr << "propagate zero up the tree" << std::endl;
tr.print_recursive_treeform(std::cerr, it);
#endif
post_order_iterator moved_next=work;
propagate_zeroes(moved_next, it);
#ifdef DEBUG
tr.print_recursive_treeform(std::cerr, it);
std::cerr << Ex(it) << std::endl;
#endif
next=moved_next;
}
// The 'work' iterator can now point to a new node. If we were acting at the
// top node, we need to propagate the change in 'work' to 'it' so the caller
// knows where the new top node is.
if(work_is_topnode)
it=work;
}
// The algorithm may have replaced the 'work' node, so instead of walking from
// there, we continue at the node which was next in line before we called 'apply'.
current=next;
}
else {
++current;
}
if(stop_after_this_one)
break;
}
#ifdef DEBUG
std::cerr << "recursive end **" << std::endl;
#endif
return some_changes_somewhere;
}
void Algorithm::propagate_zeroes(post_order_iterator& it, const iterator& topnode)
{
assert(*it->multiplier==0);
if(it==topnode) return;
if(tr.is_head(it)) return;
iterator walk=tr.parent(it);
#ifdef DEBUG
std::cerr << "propagate_zeroes at " << *walk->name << std::endl;
#endif
// if(!tr.is_valid(walk))
// return;
const Derivative *der=kernel.properties.get<Derivative>(walk);
const Trace *trace=kernel.properties.get<Trace>(walk);
if(*walk->name=="\\prod" || der || trace) {
if(der && it->is_index()) return;
walk->multiplier=rat_set.insert(0).first;
it=walk;
propagate_zeroes(it, topnode);
// Removing happens in the next step.
}
else if(*walk->name=="\\pow") {
if(tr.index(it)==0) { // the argument
walk->multiplier=rat_set.insert(0).first;
it=walk;
propagate_zeroes(it, topnode);
}
else { // the exponent
rset_t::iterator rem=walk->multiplier;
tr.erase(it);
tr.flatten(walk);
it=tr.erase(walk);
node_one(it);
it->multiplier=rem;
}
}
else if(*walk->name=="\\sum") {
if(tr.number_of_children(walk)>2) {
if(tr.is_valid(tr.next_sibling(it))) {
it=tr.erase(it);
it.descend_all();
}
else {
iterator ret=tr.parent(it);
tr.erase(it);
it=ret;
}
}
else {
// If the sum is the top node, we cannot flatten it because
// we are not allowed to invalidate the topnode iterator
if(walk==topnode) {
#ifdef DEBUG
std::cerr << "\\sum at top, cannot flatten" << std::endl;
#endif
// it=tr.next_sibling(it); // Added but wrong?
return;
}
tr.erase(it);
iterator singlearg=tr.begin(walk);
if(singlearg!=tr.end(walk)) {
singlearg->fl.bracket=walk->fl.bracket; // to remove brackets of the sum
if(*tr.parent(walk)->name=="\\prod") {
multiply(tr.parent(walk)->multiplier, *singlearg->multiplier);
cadabra::one(singlearg->multiplier);
}
}
tr.flatten(walk);
it=tr.erase(walk);
if(*it->name=="\\prod" && *tr.parent(it)->name=="\\prod") {
tr.flatten(it);
it=tr.erase(it);
}
}
}
else {
iterator nn=tr.insert_after(it, str_node("1"));
nn->fl.parent_rel=it->fl.parent_rel;
nn->fl.bracket=it->fl.bracket;
it=tr.erase(it);
zero(it->multiplier);
}
return;
}
void Algorithm::pushup_multiplier(iterator it)
{
if(!tr.is_valid(it)) return;
if(*it->multiplier!=1) {
if(*it->name=="\\sum") {
// txtout << "SUM" << std::endl;
sibling_iterator sib=tr.begin(it);
while(sib!=tr.end(it)) {
multiply(sib->multiplier, *it->multiplier);
// txtout << "going up" << std::endl;
if(tr.is_head(it)==false)
pushup_multiplier(tr.parent(it));
// txtout << "back and back up" << std::endl;
pushup_multiplier(sib);
// txtout << "back" << std::endl;
++sib;
}
::one(it->multiplier);
}
else {
// txtout << "PUSHUP: " << *it->name << std::endl;
if(tr.is_head(it)==false) {
// txtout << "test propinherit" << std::endl;
// iterator tmp=tr.parent(it);
// tmp not always valid?!? This one crashes hard with a loop!?!
// txtout << " of " << *tmp->name << std::endl;
const PropertyInherit *pin=kernel.properties.get<PropertyInherit>(tr.parent(it));
if(pin || *(tr.parent(it)->name)=="\\prod") {
multiply(tr.parent(it)->multiplier, *it->multiplier);
::one(it->multiplier); // moved up, was at end of block, correct?
// txtout << "going up" << std::endl;
pushup_multiplier(tr.parent(it));
// txtout << "back" << std::endl;
}
// else txtout << "not relevant" << std::endl;
}
}
}
}
void Algorithm::node_zero(iterator it)
{
::zero(it->multiplier);
tr.erase_children(it);
it->name=name_set.insert("1").first;
}
void Algorithm::node_one(iterator it)
{
::one(it->multiplier);
tr.erase_children(it);
it->name=name_set.insert("1").first;
}
void Algorithm::node_integer(iterator it, int num)
{
::one(it->multiplier);
tr.erase_children(it);
it->name=name_set.insert("1").first;
::multiply(it->multiplier, num);
}
int Algorithm::index_parity(iterator it) const
{
sibling_iterator frst=tr.begin(tr.parent(it));
sibling_iterator fnd(it);
int sgn=1;
while(frst!=fnd) {
sgn=-sgn;
++frst;
}
return sgn;
}
unsigned int Algorithm::number_of_indices(iterator it)
{
unsigned int res=0;
index_iterator indit=begin_index(it);
while(indit!=end_index(it)) {
++res;
++indit;
}
return res;
}
std::string Algorithm::get_index_set_name(iterator it) const
{
const Indices *ind=kernel.properties.get<Indices>(it, true);
if(ind) {
return ind->set_name;
// TODO: The logic was once as below, but it is no longer clear to
// me why that would ever make sense.
// if(ind->parent_name!="") return ind->parent_name;
// else return ind->set_name;
}
else return " undeclared";
}
index_iterator Algorithm::begin_index(iterator it) const
{
return index_iterator::begin(kernel.properties, it);
}
index_iterator Algorithm::end_index(iterator it) const
{
return index_iterator::end(kernel.properties, it);
}
bool Algorithm::check_index_consistency(iterator it) const
{
index_map_t ind_free, ind_dummy;
classify_indices(it,ind_free,ind_dummy);
return true;
}
bool Algorithm::check_degree_consistency(iterator ) const
{
return true; // FIXME: this needs to be implemented.
}
bool Algorithm::check_consistency(iterator it) const
{
Stopwatch w1;
w1.start();
// debugout << "checking consistency ... " << std::flush;
assert(tr.is_head(it)==false);
// iterator entry=it;
iterator end=it;
end.skip_children();
++end;
while(it!=end) {
if(interrupted)
throw InterruptionException("check_consistency");
if(*it->name=="\\sum") {
if(*it->multiplier!=1)
throw ConsistencyException("Found \\sum node with non-unit multiplier.");
else if(Ex::number_of_children(it)<2)
throw ConsistencyException("Found a \\sum node with 0 or 1 child nodes.");
else {
sibling_iterator sumch=it.begin();
str_node::bracket_t firstbracket=sumch->fl.bracket;
while(*sumch->name=="\\sum" || *sumch->name=="\\prod") {
++sumch;
if(sumch==it.end()) break;
else firstbracket=sumch->fl.bracket;
}
sumch=it.begin();
while(sumch!=it.end()) {
if(*sumch->name!="\\sum" && *sumch->name!="\\prod") {
if(sumch->fl.bracket!=firstbracket)
throw ConsistencyException("Found a \\sum node with different brackets on its children.");
}
// else if(*sumch->name=="\\sum") {
// sibling_iterator sumchch=sumch.begin();
// while(sumchch!=sumch.end()) {
// if(sumchch->fl.bracket==str_node::b_none) {
// tr.print_recursive_treeform(debugout, entry);
// throw ConsistencyException("Found a sum node with \\sum child without bracketed children.");
// }
// ++sumchch;
// }
// }
++sumch;
}
}
}
else if(*it->name=="\\prod") {
if(Ex::number_of_children(it)<=1)
throw ConsistencyException("Found \\prod node with only 0 or 1 children.");
sibling_iterator ch=it.begin();
str_node::bracket_t firstbracket=ch->fl.bracket;
while(*ch->name=="\\sum" || *ch->name=="\\prod") {
++ch;
if(ch==it.end()) break;
else firstbracket=ch->fl.bracket;
}
ch=it.begin();
while(ch!=it.end()) {
if(*ch->name!="\\prod" && *ch->name!="\\sum") {
if(ch->fl.bracket!=firstbracket)
throw ConsistencyException("Found \\prod node with different brackets on its children.");
}
if(*ch->multiplier!=1) {
throw ConsistencyException("Found \\prod child with non-unit multiplier.");
// debugout << "node name " << *ch->name << ", multiplier " << *ch->multiplier << std::endl;
// inconsistent=true;
// break;
}
++ch;
}
}
else if(*it->name=="\\sequence") {
if(Ex::number_of_children(it)!=2)
throw ConsistencyException("Found \\sequence node with incorrect (non-2) number of children.");
}
++it;
}
w1.stop();
// debugout << "checking done..." << w1 << std::endl;
return true;
}
void Algorithm::report_progress(const std::string&, int, int, int count)
{
// bool display=false;
// if(count==2) display=true;
// else {
// if(report_progress_stopwatch.stopped()) {
// display=true;
// report_progress_stopwatch.start();
// }
// else {
// if(report_progress_stopwatch.seconds()>0 || report_progress_stopwatch.useconds()>300000L) {
// display=true;
// report_progress_stopwatch.reset();
// }
// }
// }
// if(display) { // prevents updates at a rate of more than one per second
// if(eo->output_format==Ex_output::out_xcadabra) {
// txtout << "<progress>" << std::endl
// << str << std::endl
// << todo << std::endl
// << done << std::endl
// << count << std::endl
// << "</progress>" << std::endl;
// }
// else {
// if(count==2)
// txtout << str << " (" << done << " of " << todo << " completed)" << std::endl;
// }
// }
}
bool Algorithm::rename_replacement_dummies(iterator two, bool still_inside_algo)
{
#ifdef DEBUG
std::cerr << "renaming in " << two << std::endl;
#endif
// std::cout << "full story " << *two->name << std::endl;
// print_classify_indices(two);
// std::cout << "replacement" << std::endl;
// print_classify_indices(std::cout, two);
index_map_t ind_free, ind_dummy;
index_map_t ind_free_full, ind_dummy_full;
if(false && still_inside_algo) {
if(tr.is_head(two)==false)
classify_indices_up(tr.parent(two), ind_free_full, ind_dummy_full);
}
else {
classify_indices_up(two, ind_free_full, ind_dummy_full); // the indices in everything except the replacement
}
classify_indices(two, ind_free, ind_dummy); // the indices in the replacement subtree
#ifdef DEBUG
std::cerr << "dummies of " << *two->name << std::endl;
for(auto& ii: ind_dummy)
std::cerr << ii.first << std::endl;
std::cerr << "free indices above us" << std::endl;
for(auto& ii: ind_free_full)
std::cerr << ii.first << std::endl;
std::cerr << "dummy indices above us" << std::endl;
for(auto& ii: ind_dummy_full)
std::cerr << ii.first << std::endl;
#endif
index_map_t must_be_empty;
index_map_t newly_generated;
// Catch double index pairs
determine_intersection(ind_dummy_full, ind_dummy, must_be_empty);
index_map_t::iterator it=must_be_empty.begin();
while(it!=must_be_empty.end()) {
#ifdef DEBUG
std::cerr << "double index pair" << std::endl;
#endif
Ex the_key=(*it).first;
const Indices *dums=kernel.properties.get<Indices>(it->second, true);
if(!dums)
throw ConsistencyException("Failed to find dummy property for $"+*it->second->name+"$ while renaming dummies.");
// txtout << "failed to find dummy property for " << *it->second->name << std::endl;
assert(dums);
Ex relabel
=get_dummy(dums, &ind_dummy_full, &ind_dummy, &ind_free_full, &ind_free, &newly_generated);
newly_generated.insert(index_map_t::value_type(Ex(relabel),(*it).second));
// txtout << " renamed to " << *relabel << std::endl;
do {
tr.replace_index((*it).second, relabel.begin(), true);
// (*it).second->name=relabel;
++it;
}
while(it!=must_be_empty.end() && tree_exact_equal(&kernel.properties, (*it).first,the_key, 1, true, -2, true));
}
// Catch triple indices (two cases: dummy pair in replacement, free index elsewhere and
// dummy elsewhere, free index in replacement)
must_be_empty.clear();
// newly_generated.clear(); // DO NOT ERASE, IDIOT!
determine_intersection(ind_free_full, ind_dummy, must_be_empty);
//for(auto& ii: must_be_empty) {
// std::cerr << ii.first << std::endl;
// }
it=must_be_empty.begin();
while(it!=must_be_empty.end()) {
//std::cerr << "triple index pair " << it->first << std::endl;
Ex the_key=(*it).first;
const Indices *dums=kernel.properties.get<Indices>(it->second, true);
if(!dums)
throw ConsistencyException("Failed to find dummy property for $"+*it->second->name+"$ while renaming dummies.");
assert(dums);
Ex relabel
=get_dummy(dums, &ind_dummy_full, &ind_dummy, &ind_free_full, &ind_free, &newly_generated);
relabel.begin()->fl.parent_rel=it->second->fl.parent_rel;
newly_generated.insert(index_map_t::value_type(relabel,(*it).second));
do {
tr.replace_index((*it).second, relabel.begin(), true);
++it;
}
while(it!=must_be_empty.end() && tree_exact_equal(&kernel.properties, (*it).first,the_key, 1, true, -2, true));
}
must_be_empty.clear();
// newly_generated.clear();
determine_intersection(ind_free, ind_dummy_full, must_be_empty);
it=must_be_empty.begin();
while(it!=must_be_empty.end()) {
// std::cerr << "triple index pair 2" << std::endl;
Ex the_key=(*it).first;
const Indices *dums=kernel.properties.get<Indices>(it->second, true);
if(!dums)
throw ConsistencyException("Failed to find dummy property for $"+*it->second->name+"$ while renaming dummies.");
assert(dums);
Ex relabel
=get_dummy(dums, &ind_dummy_full, &ind_dummy, &ind_free_full, &ind_free, &newly_generated);
relabel.begin()->fl.parent_rel=it->second->fl.parent_rel;
newly_generated.insert(index_map_t::value_type(relabel,(*it).second));
do {
tr.replace_index((*it).second, relabel.begin(), true);
++it;
}
while(it!=must_be_empty.end() && tree_exact_equal(&kernel.properties, (*it).first,the_key, 1, true, -2, true));
}
return true;
}
bool Algorithm::contains(sibling_iterator from, sibling_iterator to, sibling_iterator arg)
{
while(from!=to) {
if(from->name==arg->name) return true;
++from;
}
return false;
}
Algorithm::range_vector_t::iterator Algorithm::find_arg_superset(range_vector_t& ran,
sibling_iterator it)
{
sibling_iterator nxt=it;
++nxt;
return find_arg_superset(ran, it, nxt);
}
//void Algorithm::find_argument_lists(range_vector_t& ran, bool only_comma_lists) const
// {
// sibling_iterator argit=args_begin();
// while(argit!=args_end()) {
// if(*argit->name=="\\comma") {
// ran.push_back(range_t(tr.begin(argit), tr.end(argit)));
// }
// else if(!only_comma_lists) {
// sibling_iterator argnxt=argit; ++argnxt;
// ran.push_back(range_t(argit, argnxt));
// }
// ++argit;
// }
// }
template<class Iter>
Algorithm::range_vector_t::iterator Algorithm::find_arg_superset(range_vector_t& ran, Iter st, Iter nd)
{
range_vector_t::iterator ranit=ran.begin();
while(ranit!=ran.end()) {
sibling_iterator findthese=st;
bool contained=true;
while(findthese!=nd) {
if(contains((*ranit).first, (*ranit).second, findthese)) {
++findthese;
}
else {
contained=false;
break;
}
}
if(contained) return ranit;
++ranit;
}
return ran.end();
}
bool Algorithm::is_termlike(iterator it)
{
if(*it->name=="\\equals") return false;
if(!is_factorlike(it))
if(*it->name!="\\sum")
if(it->fl.parent_rel==str_node::p_none)
return true;
return false;
}
bool Algorithm::is_factorlike(iterator it)
{
if(Ex::is_head(it)) return false;
if(*Ex::parent(it)->name=="\\prod")
return true;
return false;
}
// This only returns true if the indicated node is a single non-reserved node (non-prod, non-sum, ...)
// at the top level of an expression (real top, top of equation lhs/rhs, top of integral argument, ...).
bool Algorithm::is_single_term(iterator it)
{
if(*it->name!="\\prod" && *it->name!="\\sum" && *it->name!="\\asymimplicit"
&& *it->name!="\\comma" && *it->name!="\\equals" && *it->name!="\\arrow") {
if(tr.is_head(it) || *tr.parent(it)->name=="\\equals" || *tr.parent(it)->name=="\\int") return true;
else if(*tr.parent(it)->name=="\\sum")
return true;
else if(*tr.parent(it)->name!="\\prod" && it->fl.parent_rel==str_node::parent_rel_t::p_none
&& kernel.properties.get<Accent>(tr.parent(it))==0 ) {
#ifdef DEBUG
std::cerr << "Found single term in " << tr.parent(it) << std::endl;
#endif
return true;
}
}
return false;
}
bool Algorithm::is_nonprod_factor_in_prod(iterator it)
{
if(*it->name!="\\prod" && *it->name!="\\sum" && *it->name!="\\asymimplicit" && *it->name!="\\comma"
&& *it->name!="\\equals") {
try {
if(tr.is_head(it)==false && *tr.parent(it)->name=="\\prod")
return true;
}
catch(navigation_error& ex) {
// no parent, ignore
}
// else return true;
}
return false;
}
bool Algorithm::prod_wrap_single_term(iterator& it)
{
if(is_single_term(it)) {
force_node_wrap(it, "\\prod");
return true;
}
else return false;
}
bool Algorithm::sum_wrap_single_term(iterator& it)
{
if(is_single_term(it)) {
force_node_wrap(it, "\\sum");
return true;
}
else return false;
}
void Algorithm::force_node_wrap(iterator& it, std::string nm)
{
iterator prodnode=tr.insert(it, str_node(nm));
sibling_iterator fr=it, to=it;
++to;
tr.reparent(prodnode, fr, to);
prodnode->fl.bracket=it->fl.bracket;
it->fl.bracket=str_node::b_none;
if(nm!="\\sum") { // multipliers should sit on terms in a sum
prodnode->multiplier=it->multiplier;
one(it->multiplier);
}
it=prodnode;
}
bool Algorithm::prod_unwrap_single_term(iterator& it)
{
if((*it->name)=="\\prod") {
if(tr.number_of_children(it)==1) {
multiply(tr.begin(it)->multiplier, *it->multiplier);
tr.begin(it)->fl.bracket=it->fl.bracket;
tr.begin(it)->multiplier=it->multiplier;
tr.flatten(it);
it=tr.erase(it);
return true;
}
}
return false;
}
bool Algorithm::sum_unwrap_single_term(iterator& it)
{
if((*it->name)=="\\sum") {
if(tr.number_of_children(it)==1) {
multiply(tr.begin(it)->multiplier, *it->multiplier);
tr.begin(it)->fl.bracket=it->fl.bracket;
tr.begin(it)->multiplier=it->multiplier;
tr.flatten(it);
it=tr.erase(it);
return true;
}
}
return false;
}
bool Algorithm::separated_by_derivative(iterator i1, iterator i2, iterator check_dependence) const
{
iterator lca = tr.lowest_common_ancestor(i1, i2);
// Walk up the tree from the first node until the LCA, flag any derivatives
// with which we do not commute.
struct {
bool operator()(const Kernel& kernel, Ex& tr, iterator walk, iterator lca, iterator check_dependence)
{
const Properties& pr=kernel.properties;
do {
walk=Ex::parent(walk);
if(walk == lca) break;
const Derivative *der=pr.get<Derivative>(walk);
if(der) {
if(tr.is_valid(check_dependence) ) {
const DependsBase *dep = pr.get<DependsBase>(check_dependence);
if(dep) {
Ex deps=dep->dependencies(kernel, check_dependence);
sibling_iterator depobjs=deps.begin(deps.begin());
while(depobjs!=deps.end(deps.begin())) {
if(walk->name == depobjs->name) {
return true;
}
else {
// compare all indices
sibling_iterator indit=tr.begin(walk);
while(indit!=tr.end(walk)) {
if(indit->is_index()) {
if(subtree_exact_equal(&pr, indit, depobjs))
return true;
}
++indit;
}
}
++depobjs;
}
return false; // Dependence found but not relevant here.
}
else return false; // No dependence property found at all.
}
else return true; // Should not check for dependence.
}
}
while(walk != lca);
return false;
}
} one_run;
if(one_run(kernel, tr, i1, lca, check_dependence)) return true;
if(one_run(kernel, tr, i2, lca, check_dependence)) return true;
return false;
}
// bool Algorithm::cleanup_anomalous_products(Ex& tr, Ex::iterator& it)
// {
// if(*(it->name)=="\\prod") {
// if(tr.number_of_children(it)==0) {
// it->name=name_set.insert("1").first;
// return true;
// }
// else if(tr.number_of_children(it)==1) {
// tr.begin(it)->fl.bracket=it->fl.bracket;
// tr.begin(it)->multiplier=it->multiplier;
// tr.flatten(it);
// Ex::iterator tmp=tr.erase(it);
// // txtout << "HERRE?" << std::endl;
// pushup_multiplier(tmp);
// it=tmp;
// return true;
// }
// }
// return false;
// }
//
unsigned int Algorithm::locate_single_object(Ex::iterator obj_to_find,
Ex::iterator st, Ex::iterator nd,
std::vector<unsigned int>& store)
{
unsigned int count=0;
unsigned int index=0;
while(st!=nd) {
Ex::iterator it1=st;
it1.skip_children();
++it1;
if(tr.equal(st, it1, obj_to_find, Algorithm::compare_)) {
++count;
store.push_back(index);
}
++st;
++index;
}
return count;
}
bool Algorithm::locate_object_set(const Ex& objs,
Ex::iterator st, Ex::iterator nd,
std::vector<unsigned int>& store)
{
// Locate the objects in which to symmetrise. We use an integer
// index (offset wrt. 'st') rather than an iterator because the
// latter only apply to a single tree, not to its copies.
// We accept only a tree with a \comma node at the top.
Ex::iterator top=objs.begin();
if(*top->name!="\\comma")
top = objs.begin(objs.begin());
assert(*top->name=="\\comma");
Ex::sibling_iterator fst=objs.begin(top);
Ex::sibling_iterator fnd=objs.end(top);
while(fst!=fnd) {
Ex::iterator aim=fst;
if((*aim->name)=="\\comma") {
// Objects can themselves be lists of other objects (for instance
// when we want to symmetrise in index pairs).
if(locate_object_set(aim, st, nd, store)==false)
return false;
}
else {
if((*aim->name).size()==0 && tr.number_of_children(aim)==1)
aim=tr.begin(aim);
if(locate_single_object(aim, st, nd, store)!=1)
return false;
}
++fst;
}
return true;
}
namespace cadabra {
// static functions
unsigned int Algorithm::number_of_indices(const Properties& pr, iterator it)
{
unsigned int res=0;
index_iterator indit=index_iterator::begin(pr, it);
while(indit!=index_iterator::end(pr, it)) {
++res;
++indit;
}
return res;
}
unsigned int Algorithm::number_of_direct_indices(iterator it)
{
unsigned int res=0;
sibling_iterator sib=Ex::begin(it);
while(sib!=Ex::end(it)) {
if(sib->fl.parent_rel==str_node::p_sub || sib->fl.parent_rel==str_node::p_super)
++res;
++sib;
}
return res;
}
bool Algorithm::less_without_numbers(nset_t::iterator it1, nset_t::iterator it2)
{
std::string::const_iterator ch1=(*it1).begin();
std::string::const_iterator ch2=(*it2).begin();
while(ch1!=(*it1).end() && ch2!=(*it2).end()) {
if(isdigit(*ch1)) return true; // bla1 < blaq
if(isdigit(*ch2)) return false; // blaa !< bla1
if(*ch1>=*ch2) return false;
++ch1;
++ch2;
}
if(ch1==(*it1).end()) {
if(ch2==(*it2).end())
return false;
else
return true;
}
return false;
}
bool Algorithm::equal_without_numbers(nset_t::iterator it1, nset_t::iterator it2)
{
std::string::const_iterator ch1=(*it1).begin();
std::string::const_iterator ch2=(*it2).begin();
while(ch1!=(*it1).end() && ch2!=(*it2).end()) {
if(isdigit(*ch1)) {
if(isdigit(*ch2))
return true;
else
return false;
}
if(*ch1!=*ch2) return false;
++ch1;
++ch2;
}
if(ch1==(*it1).end()) {
if(ch2==(*it2).end())
return true;
else
return false;
}
return false;
}
bool Algorithm::compare_(const str_node& one, const str_node& two)
{
// If the obj->name is empty, this means that we look for a tree with
// anything as root, but the required index structure in obj. This
// requires a slightly different 'equal_to' (one that always matches
// an empty node with a non-empty node).
if(/* one.fl.bracket!=two.fl.bracket || */ one.fl.parent_rel!=two.fl.parent_rel)
return false;
if((*two.name).size()==0)
return true;
else if(one.name==two.name)
return true;
return false;
}
}
|