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
|
/*
Cadabra: a field-theory motivated computer algebra system.
Copyright (C) 2001-2014 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 "Storage.hh"
#include "Combinatorics.hh"
#include "Exceptions.hh"
#include <iomanip>
#include <sstream>
#include <locale>
#include <codecvt>
#include <boost/functional/hash.hpp>
//#define DEBUG 1
namespace cadabra {
nset_t name_set;
rset_t rat_set;
long to_long(multiplier_t mul)
{
return mul.get_num().get_si();
}
double to_double(multiplier_t mul)
{
return mul.get_d();
}
std::string to_string(long num)
{
std::ostringstream str;
str << num;
return str.str();
}
// Expression constructor/destructor members.
Ex::Ex()
: tree<str_node>(), state_(result_t::l_no_action)
{
}
Ex::Ex(tree<str_node>::iterator it)
: tree<str_node>(it), state_(result_t::l_no_action)
{
}
Ex::Ex(const str_node& x)
: tree<str_node>(x), state_(result_t::l_no_action)
{
}
Ex::Ex(const Ex& other)
: std::enable_shared_from_this<Ex>(other), tree<str_node>(other), state_(result_t::l_no_action)
{
// std::cout << "Ex copy constructor" << std::endl;
}
Ex::Ex(const std::string& str)
: state_(result_t::l_no_action)
{
set_head(str_node(str));
}
Ex::Ex(int val)
: state_(result_t::l_no_action)
{
set_head(str_node("1"));
multiply(begin()->multiplier, val);
}
Ex::result_t Ex::state() const
{
return state_;
}
void Ex::update_state(Ex::result_t newstate)
{
switch(newstate) {
case Ex::result_t::l_error:
state_=newstate;
break;
case Ex::result_t::l_applied:
if(state_!=Ex::result_t::l_error)
state_=newstate;
break;
default:
break;
}
}
void Ex::reset_state()
{
state_=Ex::result_t::l_checkpointed;
}
bool Ex::changed_state()
{
bool ret=false;
if(state_==result_t::l_checkpointed || state_==result_t::l_applied) ret=true;
state_=result_t::l_no_action;
return ret;
}
bool Ex::is_rational() const
{
if(begin()!=end())
if(begin()->is_rational())
return true;
return false;
}
multiplier_t Ex::to_rational() const
{
if(!is_rational())
throw InternalError("Called to_rational() on non-rational Ex");
return *(begin()->multiplier);
}
bool Ex::is_integer() const
{
if(begin()!=end())
if(begin()->is_integer())
return true;
return false;
}
long Ex::to_integer() const
{
if(!is_integer())
throw InternalError("Called to_integer() on non-integer Ex");
return to_long(*(begin()->multiplier));
}
std::ostream& Ex::print_python(std::ostream& str, Ex::iterator it)
{
std::string name(*(*it).name);
std::string res;
if(*it->multiplier!=1)
str << *it->multiplier;
for(unsigned int i=0; i<name.size(); ++i) {
if(name[i]=='#') res+="\\#";
// else if(name[i]=='\\') res+="\\backslash{}";
else res+=name[i];
}
str << res;
Ex::sibling_iterator beg=it.begin();
Ex::sibling_iterator fin=it.end();
str_node::bracket_t current_bracket=str_node::b_invalid;
str_node::parent_rel_t current_parent_rel=str_node::p_invalid;
while(beg!=fin) {
if(beg==it.begin() || current_parent_rel!=(*beg).fl.parent_rel) {
switch((*beg).fl.parent_rel) {
case str_node::p_super:
str << "^";
break;
case str_node::p_sub:
str << "_";
break;
case str_node::p_property:
str << "$";
break;
case str_node::p_exponent:
str << "&";
break;
default:
break;
}
current_parent_rel=(*beg).fl.parent_rel;
}
if(beg==it.begin() || current_bracket!=(*beg).fl.bracket || current_parent_rel!=(*beg).fl.parent_rel) {
switch((*beg).fl.bracket) {
case str_node::b_round:
str << "(";
break;
case str_node::b_square:
str << "[";
break;
case str_node::b_curly:
str << "{";
break;
case str_node::b_pointy:
str << "<";
break;
case str_node::b_none: {
if((*beg).fl.parent_rel==str_node::p_none) str << "(";
else str << "{";
}
break;
default:
break;
}
current_bracket=(*beg).fl.bracket;
}
print_python(str, beg);
auto nxt=beg;
++nxt;
if(nxt!=fin) {
if((*beg).fl.parent_rel!=str_node::p_none)
str << " ";
}
if(nxt==fin || (*nxt).fl.bracket!=(*beg).fl.bracket || (*beg).fl.parent_rel==str_node::p_none) {
current_bracket=str_node::b_invalid;
current_parent_rel=str_node::p_invalid;
switch((*beg).fl.bracket) {
case str_node::b_round:
str << ")";
break;
case str_node::b_square:
str << "]";
break;
case str_node::b_curly:
str << "}";
break;
case str_node::b_pointy:
str << ">";
break;
case str_node::b_none: {
if((*beg).fl.parent_rel==str_node::p_none) str << ")";
else str << "}";
}
break;
default:
break;
}
}
++beg;
}
return str;
}
std::ostream& Ex::print_repr(std::ostream& str, Ex::iterator it) const
{
str << *it->name;
sibling_iterator sib=it.begin();
while(sib!=it.end()) {
print_repr(str, sib);
++sib;
}
return str;
}
std::ostream& Ex::print_recursive_treeform(std::ostream& str, Ex::iterator it)
{
unsigned int num=1;
switch((*it).fl.parent_rel) {
case str_node::p_super:
str << "^";
break;
case str_node::p_sub:
str << "_";
break;
case str_node::p_property:
str << "$";
break;
case str_node::p_exponent:
str << "&";
break;
default:
break;
}
return print_recursive_treeform(str, it, num);
}
std::ostream& Ex::print_entire_tree(std::ostream& str) const
{
sibling_iterator sib=begin();
unsigned int num=1;
while(sib!=end()) {
print_recursive_treeform(str, sib, num);
++sib;
++num;
}
return str;
}
std::ostream& Ex::print_recursive_treeform(std::ostream& str, Ex::iterator it, unsigned int& num)
{
bool compact_tree=getenv("CDB_COMPACTTREE");
Ex::sibling_iterator beg=it.begin();
Ex::sibling_iterator fin=it.end();
if((*it).fl.bracket ==str_node::b_round) str << "(";
else if((*it).fl.bracket ==str_node::b_square) str << "[";
else if((*it).fl.bracket ==str_node::b_curly) str << "\\{";
else if((*it).fl.bracket ==str_node::b_pointy) str << "\\<";
else if((*it).fl.bracket ==str_node::b_none) str << "{";
// if((*it).fl.mark) str << "\033[1m";
str << *(*it).name;
// if((*it).fl.mark) str << "\033[0m";
if((*it).fl.bracket ==str_node::b_round) str << ")";
else if((*it).fl.bracket ==str_node::b_square) str << "]";
else if((*it).fl.bracket ==str_node::b_curly) str << "\\}";
else if((*it).fl.bracket ==str_node::b_pointy) str << "\\>";
else if((*it).fl.bracket ==str_node::b_none) str << "}";
if(*it->multiplier!=multiplier_t(1)) {
if(compact_tree)
str << "#" << *it->multiplier;
else
str << " " << *it->multiplier;
}
// str << " (" << calc_hash(it) << ")";
// str << " (" << depth(it) << ")";
// str << " (" << it->fl.bracket << " " << &(*it) << ")";
str << " (" << it->fl.bracket << " " << it.node << ")";
if(!compact_tree) str << std::endl;
while(beg!=fin) {
int offset=1;
if(num && !compact_tree) {
str << std::setw(3) << num << ":";
offset=1;
}
if(!compact_tree)
for(int i=offset; i<depth(beg); ++i)
str << " ";
switch((*beg).fl.parent_rel) {
case str_node::p_super:
str << "^";
break;
case str_node::p_sub:
str << "_";
break;
case str_node::p_property:
str << "$";
break;
case str_node::p_exponent:
str << "&";
break;
default:
break;
}
if(num) ++num;
print_recursive_treeform(str, beg, num);
++beg;
}
return str;
}
// std::ostream& operator<<(std::ostream& str, const Ex& tr)
// {
// unsigned int number=1;
// Ex::iterator it=tr.begin();
// while(it!=tr.end()) {
// tr.print_recursive_infix(str, it, number, true);
// it.skip_children();
// ++it;
// if(it!=tr.end())
// str << std::endl;
// }
// return str;
// }
Ex::iterator Ex::named_parent(Ex::iterator it, const std::string& nm) const
{
// std::cout << "!!" << *it->name << std::endl << std::flush;
assert(is_valid(it));
while(*it->name!=nm /* && it->is_command()==false */) {
it=parent(it);
assert(is_valid(it));
// std::cout << " !!" << *it->name << std::endl << std::flush;
}
// std::cout << " out" << std::endl;
return it;
}
Ex::iterator Ex::erase_expression(Ex::iterator it)
{
it=named_parent(it, "\\history");
return erase(it);
}
hashval_t Ex::calc_hash(iterator it) const
{
// Hash values do not contain info about the multiplier field,
// nor do they know about the type of the links (FIXME: is the latter
// the correct thing to do?)
//
// If this algorithm is changed, factorise::calc_restricted_hash in
// core/algorithms/factor_in.cc should also be modified!
iterator end=it;
end.skip_children();
++end;
it.skip_children(false);
hashval_t seed = 0;
while(it!=end) {
boost::hash_combine(seed, *it->name);
++it;
}
return seed;
}
Ex::sibling_iterator Ex::arg(iterator it, unsigned int num)
{
if(*it->name=="\\comma") {
assert(Ex::number_of_children(it)>num);
return Ex::child(it,num);
}
else return it;
}
unsigned int Ex::arg_size(sibling_iterator sib)
{
if(*sib->name=="\\comma") return Ex::number_of_children(sib);
else return 1;
}
multiplier_t Ex::arg_to_num(sibling_iterator sib, unsigned int num) const
{
sibling_iterator nod;
if(*sib->name=="\\comma") nod=child(sib,num);
else nod=sib;
return *nod->multiplier;
}
unsigned int Ex::equation_number(Ex::iterator it) const
{
iterator historynode=named_parent(it, "\\history");
unsigned int num=0;
iterator sit=begin();
// long totravel=0;
while(sit!=end()) {
// ++totravel;
if(*sit->name=="\\history") {
++num;
if(historynode==sit) {
// txtout << "had to travel " << totravel << std::endl;
return num;
}
}
sit.skip_children();
++sit;
}
return 0;
}
nset_t::iterator Ex::equation_label(Ex::iterator it) const
{
nset_t::iterator ret=name_set.end();
iterator sit=begin();
while(sit!=end()) {
if(*sit->name=="\\history") {
if(it==sit)
goto found;
iterator eit=begin(sit);
iterator endit=sit;
endit.skip_children();
++endit;
while(eit!=endit) {
if(it==eit)
goto found;
++eit;
}
sit.skip_children();
}
++sit;
}
found:
if(sit!=end()) {
sibling_iterator lit=begin(sit);
while(lit!=end(sit)) {
if(*lit->name=="\\label") {
ret=begin(lit)->name;
break;
}
++lit;
}
}
return ret;
}
// Always returns the \\history node of the equation (i.e. the top node).
Ex::iterator Ex::equation_by_number(unsigned int i) const
{
iterator it=begin();
unsigned int num=1;
while(it!=end()) {
if(*it->name=="\\history") {
if(num==i) return it;
else ++num;
}
it.skip_children();
++it;
}
return it;
// if(num==number_of_siblings(begin()))
// return end();
// return it;
}
Ex::iterator Ex::equation_by_name(nset_t::iterator nit) const
{
unsigned int tmp;
return equation_by_name(nit, tmp);
}
Ex::iterator Ex::equation_by_name(nset_t::iterator nit, unsigned int& tmp) const
{
unsigned int num=0;
iterator it=begin();
while(it!=end()) {
if(*it->name=="\\history") {
++num;
sibling_iterator lit=begin(it);
while(lit!=end(it)) {
if(*lit->name=="\\label") {
if(begin(lit)->name==nit) {
tmp=num;
return it;
}
}
++lit;
}
}
it.skip_children();
++it;
}
return end();
}
bool Ex::is_hidden(iterator it) const
{
do {
if(*it->name=="\\ldots") return true;
if(is_head(it)) break;
it=parent(it);
}
while(true);
return false;
}
Ex::iterator Ex::procedure_by_name(nset_t::iterator nit) const
{
iterator it=begin();
while(it!=end()) {
if(*it->name=="\\procedure") {
sibling_iterator lit=begin(it);
while(lit!=end(it)) {
if(*lit->name=="\\label") {
if(begin(lit)->name==nit)
return it;
}
++lit;
}
}
it.skip_children();
++it;
}
return end();
}
Ex::iterator Ex::replace_index(iterator pos, const iterator& from, bool keep_parent_rel)
{
// assert(pos->fl.parent_rel==str_node::p_sub || pos->fl.parent_rel==str_node::p_super);
str_node::bracket_t bt=pos->fl.bracket;
str_node::parent_rel_t pr=pos->fl.parent_rel;
iterator ret=replace(pos, from);
ret->fl.bracket=bt;
if(keep_parent_rel)
ret->fl.parent_rel=pr;
return ret;
}
Ex::iterator Ex::move_index(iterator pos, const iterator& from)
{
// assert(pos->fl.parent_rel==str_node::p_sub || pos->fl.parent_rel==str_node::p_super);
str_node::bracket_t bt=pos->fl.bracket;
str_node::parent_rel_t pr=pos->fl.parent_rel;
move_ontop(pos, from);
from->fl.bracket=bt;
from->fl.parent_rel=pr;
return from;
}
void Ex::list_wrap_single_element(iterator& it)
{
if(*it->name!="\\comma") {
iterator commanode=insert(it, str_node("\\comma"));
sibling_iterator fr=it, to=it;
++to;
reparent(commanode, fr, to);
it=commanode;
}
}
void Ex::list_unwrap_single_element(iterator& it)
{
if(*it->name=="\\comma") {
if(number_of_children(it)==1) {
flatten(it);
it=erase(it);
}
}
}
Ex::iterator Ex::flatten_and_erase(iterator pos)
{
// assert(number_of_children(pos)==1);
multiplier_t tmp=*pos->multiplier;
flatten(pos);
pos=erase(pos);
multiply(pos->multiplier, tmp);
return pos;
}
unsigned int Ex::number_of_equations() const
{
unsigned int last_eq=0;
iterator eq=begin();
while(eq!=end()) {
if(*eq->name=="\\history")
++last_eq;
eq.skip_children();
++eq;
}
return last_eq;
}
Ex::iterator Ex::equation_by_number_or_name(iterator it, unsigned int last_used_equation,
unsigned int& real_eqno) const
{
iterator ret;
if(it->is_rational()) {
int eqno=static_cast<int>(it->multiplier->get_d());
real_eqno=eqno;
ret=equation_by_number(eqno);
}
else {
if(*it->name=="%") {
ret=equation_by_number(last_used_equation);
real_eqno=last_used_equation;
}
else {
ret=equation_by_name(it->name, real_eqno);
}
}
return ret;
}
Ex::iterator Ex::equation_by_number_or_name(iterator it, unsigned int last_used_equation) const
{
unsigned int tmp;
return equation_by_number_or_name(it, last_used_equation, tmp);
}
std::string Ex::equation_number_or_name(iterator it, unsigned int last_used_equation) const
{
std::stringstream ss;
if(it->is_rational()) {
int eqno=static_cast<int>(it->multiplier->get_d());
ss << eqno;
}
else {
if(*it->name=="%") ss << last_used_equation;
else ss << *it->name;
}
return ss.str();
}
bool Ex::operator==(const Ex& other) const
{
return equal_subtree(begin(), other.begin());
}
// bool Ex::operator<(const Ex& other) const
// {
// Ex::iterator_base_less less;
// return less(begin(), other.begin());
// }
void Ex::push_history(const std::vector<Ex::path_t>& paths)
{
history.push_back(*this);
terms.push_back(paths);
}
std::vector<Ex::path_t> Ex::pop_history()
{
tree<str_node>::operator=(history.back());
history.pop_back();
auto ret(terms.back());
terms.pop_back();
return ret;
}
int Ex::history_size() const
{
return history.size();
}
str_node::str_node(void)
{
multiplier=rat_set.insert(1).first;
// fl.modifier=m_none;
fl.bracket=b_none;
fl.parent_rel=p_none;
// fl.mark=0;
}
str_node::str_node(nset_t::iterator nm, bracket_t br, parent_rel_t pr)
{
multiplier=rat_set.insert(1).first;
name=nm;
// fl.modifier=m_none;
fl.bracket=br;
fl.parent_rel=pr;
// fl.mark=0;
}
str_node::str_node(const std::u32string& nm, bracket_t br, parent_rel_t pr)
{
#ifdef _MSC_VER
std::wstring_convert<std::codecvt_utf8<int32_t>, int32_t> conv;
auto p = reinterpret_cast<const int32_t*>(nm.data());
std::string nm8 = conv.to_bytes(p, p + nm.size());
#else
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
std::string nm8=conv.to_bytes(nm);
#endif
#ifdef DEBUG
std::cerr << "str_node: " << nm8 << std::endl;
#endif
multiplier=rat_set.insert(1).first;
name=name_set.insert(nm8).first;
// fl.modifier=m_none;
fl.bracket=br;
fl.parent_rel=pr;
}
str_node::str_node(const std::string& nm, bracket_t br, parent_rel_t pr)
{
multiplier=rat_set.insert(1).first;
name=name_set.insert(nm).first;
// fl.modifier=m_none;
fl.bracket=br;
fl.parent_rel=pr;
// fl.mark=0;
}
void str_node::flip_parent_rel()
{
if(fl.parent_rel==p_super) fl.parent_rel=p_sub;
else if(fl.parent_rel==p_sub) fl.parent_rel=p_super;
else throw std::logic_error("flip_parent_rel called on non-index");
}
bool str_node::is_zero() const
{
if(*multiplier==0) return true;
return false;
}
bool str_node::is_identity() const
{
if(*name=="1" && *multiplier==1) return true;
return false;
}
bool str_node::is_rational() const
{
return (*name=="1");
}
bool str_node::is_integer() const
{
if(*name=="1") {
if(multiplier->get_den()==1)
return true;
}
return false;
}
bool str_node::is_unsimplified_rational() const
{
if((*name).size()==0) return false;
for(unsigned int i=0; i<(*name).size(); ++i) {
if(!isdigit((*name)[i]) && (*name)[i]!='/' && (*name)[i]!='-')
return false;
}
return true;
}
bool str_node::is_unsimplified_integer() const
{
if((*name).size()==0) return false;
for(unsigned int i=0; i<(*name).size(); ++i) {
if(!isdigit((*name)[i]) && (*name)[i]!='-')
return false;
}
return true;
}
bool str_node::is_index() const
{
if(fl.parent_rel==p_sub || fl.parent_rel==p_super) return true;
return false;
}
bool str_node::is_quoted_string() const
{
if((*name).size()<2) return false;
if((*name)[0]!='\"') return false;
if((*name)[(*name).size()-1]!='\"') return false;
return true;
}
bool str_node::is_command() const
{
if((*name).size()>0)
if((*name)[0]=='@') {
if((*name).size()>1) {
if((*name)[1]!='@')
return true;
}
else return true;
}
return false;
}
bool str_node::is_inert_command() const
{
if((*name).size()>1)
if((*name)[0]=='@')
if((*name)[1]=='@')
return true;
return false;
}
bool str_node::is_name_wildcard() const
{
if((*name).size()>0)
if((*name)[name->size()-1]=='?') {
if(name->size()>1) {
if((*name)[name->size()-2]!='?')
return true;
}
else return true;
}
return false;
}
bool str_node::is_object_wildcard() const
{
if((*name).size()>1)
if((*name)[name->size()-1]=='?')
if((*name)[name->size()-2]=='?')
return true;
return false;
}
bool str_node::is_range_wildcard() const
{
if(name->size()>0) {
if((*name)[0]=='#')
return true;
}
return false;
}
bool str_node::is_siblings_wildcard() const
{
if(name->size()>0) {
if((*name)[name->size()-1]=='@')
return true;
}
return false;
}
bool str_node::is_autodeclare_wildcard() const
{
if(name->size()>0)
if((*name)[name->size()-1]=='#')
return true;
return false;
}
bool str_node::is_indexstar_wildcard() const
{
if((*name).size()>1)
if((*name)[name->size()-1]=='?')
if((*name)[name->size()-2]=='*')
return true;
return false;
}
bool str_node::is_indexplus_wildcard() const
{
if((*name).size()>1)
if((*name)[name->size()-1]=='?')
if((*name)[name->size()-2]=='+')
return true;
return false;
}
bool str_node::is_numbered_symbol() const
{
int len=(*name).size();
if(len>1)
if(isdigit((*name)[len-1]))
return true;
return false;
}
nset_t::iterator str_node::name_only()
{
if(is_name_wildcard()) {
std::string tmp=(*name).substr(0, name->size()-1);
return name_set.insert(tmp).first;
}
else if(is_object_wildcard()) {
std::string tmp=(*name).substr(0, name->size()-2);
return name_set.insert(tmp).first;
}
else if(is_autodeclare_wildcard()) {
size_t pos=name->find('#');
std::string tmp=(*name).substr(0, pos);
return name_set.insert(tmp).first;
}
else if(is_numbered_symbol()) {
size_t pos=name->find_first_of("0123456789");
std::string tmp=(*name).substr(0, pos);
return name_set.insert(tmp).first;
}
return name;
}
bool str_node::operator==(const str_node& other) const
{
if(*name==*other.name &&
fl.bracket==other.fl.bracket &&
fl.parent_rel==other.fl.parent_rel &&
multiplier==other.multiplier)
return true;
else return false;
}
bool str_node::compare_names_only(const str_node& one, const str_node& two)
{
if(one.name==two.name) return true;
else return false;
}
bool str_node::compare_name_brack_par(const str_node& one, const str_node& two)
{
if(one.name==two.name &&
one.fl.bracket==two.fl.bracket &&
one.fl.parent_rel==two.fl.parent_rel) return true;
else return false;
}
bool str_node::compare_name_inverse_par(const str_node& one, const str_node& two)
{
if(one.name==two.name &&
( (one.fl.parent_rel==str_node::p_super && two.fl.parent_rel==str_node::p_sub) ||
(one.fl.parent_rel==str_node::p_sub && two.fl.parent_rel==str_node::p_super)))
return true;
return false;
}
bool nset_it_less::operator()(nset_t::iterator first, nset_t::iterator second) const
{
if(*first < *second)
return true;
return false;
}
void multiply(rset_t::iterator& num, multiplier_t fac)
{
fac*=*num;
fac.canonicalize();
num=rat_set.insert(fac).first;
}
void add(rset_t::iterator& num, multiplier_t fac)
{
fac+=*num;
fac.canonicalize();
num=rat_set.insert(fac).first;
}
void zero(rset_t::iterator& num)
{
num=rat_set.insert(0).first;
}
void one(rset_t::iterator& num)
{
num=rat_set.insert(1).first;
}
void flip_sign(rset_t::iterator& num)
{
multiplier_t fac=-(*num);
fac.canonicalize();
num=rat_set.insert(fac).first;
}
void half(rset_t::iterator& num)
{
multiplier_t fac=(*num)/2;
fac.canonicalize();
num=rat_set.insert(fac).first;
}
bool str_node::operator<(const cadabra::str_node& other) const
{
if(*name<*other.name) return true;
else return false;
}
}
// Keep operator overloading outside of the cadabra namespace.
std::ostream& operator<<(std::ostream& str, const cadabra::Ex& ex)
{
if(ex.begin()==ex.end()) return str;
ex.print_recursive_treeform(str, ex.begin());
// ex.print_python(str, ex.begin());
return str;
}
std::ostream& operator<<(std::ostream& str, cadabra::Ex::iterator it)
{
cadabra::Ex::print_recursive_treeform(str, it);
// ex.print_python(str, ex.begin());
return str;
}
|