/usr/include/polybori/routines/pbori_routines_misc.h is in libpolybori-dev 0.8.3-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // -*- c++ -*-
//*****************************************************************************
/** @file pbori_routines_misc.h
*
* @author Alexander Dreyer
* @date 2006-08-23
*
* This file includes files, which defines miscellaneous function templates.
*
* @par Copyright:
* (c) 2006 by The PolyBoRi Team
**/
//*****************************************************************************
#ifndef polybori_routines_pbori_routines_misc_h_
#define polybori_routines_pbori_routines_misc_h_
// include basic definitions
#include <polybori/pbori_defs.h>
// temprarily
#include <polybori/cache/CacheManager.h>
#include <polybori/diagram/CDDOperations.h>
BEGIN_NAMESPACE_PBORI
template<class Iterator>
typename Iterator::value_type
index_vector_hash(Iterator start, Iterator finish){
typedef typename Iterator::value_type value_type;
value_type vars = 0;
value_type sum = 0;
while (start != finish){
vars++;
sum += ((*start)+1) * ((*start)+1);
++start;
}
return sum * vars;
}
/// Function templates for determining the degree of a decision diagram
/// with the help of cache (e. g. CDegreeCache)
template <class DegreeCacher, class NaviType>
typename NaviType::deg_type
dd_cached_degree(const DegreeCacher& cache, NaviType navi) {
typedef typename NaviType::deg_type deg_type;
if (navi.isConstant()){ // No need for caching of constant nodes' degrees
if (navi.terminalValue())
return 0;
else
return -1;
}
// Look whether result was cached before
typename DegreeCacher::node_type result = cache.find(navi);
if (result.isValid())
return (*result);
// Get degree of then branch (contains at least one valid path)...
deg_type deg = dd_cached_degree(cache, navi.thenBranch()) + 1;
// ... combine with degree of else branch
deg = std::max(deg, dd_cached_degree(cache, navi.elseBranch()) );
// Write result to cache
cache.insert(navi, deg);
return deg;
}
/// Function templates for determining the degree of a decision diagram
/// with the help of cache (e. g. CDegreeCache)
/// Variant *with* given upper bound
/// Assumming that the bound is valid!
template <class DegreeCacher, class NaviType, class SizeType>
typename NaviType::deg_type
dd_cached_degree(const DegreeCacher& cache, NaviType navi, SizeType bound) {
typedef typename NaviType::deg_type deg_type;
if (bound < 0) {
return -1;
}
// No need for caching of constant nodes' degrees
if (bound == 0) {
while(!navi.isConstant())
navi.incrementElse();
}
if (navi.isConstant())
return (navi.terminalValue()? 0: -1);
// Look whether result was cached before
typename DegreeCacher::node_type result = cache.find(navi, bound);
if (result.isValid())
return *result;
// Get degree of then branch (contains at least one valid path)...
deg_type deg = dd_cached_degree(cache, navi.thenBranch(), bound - 1);
if (deg >= 0) ++deg;
// ... combine with degree of else branch
if (bound > deg) // if deg <= bound, we are already finished
deg = std::max(deg, dd_cached_degree(cache, navi.elseBranch(), bound) );
// Write result to cache
if (deg >= 0) {
PBORI_ASSERT(bound >= 0);
cache.insert(navi, bound, deg);
}
return deg;
}
template <class Iterator, class NameGenerator,
class Separator, class EmptySetType,
class OStreamType>
void
dd_print_term(Iterator start, Iterator finish, const NameGenerator& get_name,
const Separator& sep, const EmptySetType& emptyset,
OStreamType& os){
if (start != finish){
os << get_name(*start);
++start;
}
else
os << emptyset();
while (start != finish){
os << sep() << get_name(*start);
++start;
}
}
template <class TermType, class NameGenerator,
class Separator, class EmptySetType,
class OStreamType>
void
dd_print_term(const TermType& term, const NameGenerator& get_name,
const Separator& sep, const EmptySetType& emptyset,
OStreamType& os){
dd_print_term(term.begin(), term.end(), get_name, sep, emptyset, os);
}
template <class Iterator, class NameGenerator,
class Separator, class InnerSeparator,
class EmptySetType, class OStreamType>
void
dd_print_terms(Iterator start, Iterator finish, const NameGenerator& get_name,
const Separator& sep, const InnerSeparator& innersep,
const EmptySetType& emptyset, OStreamType& os) {
if (start != finish){
dd_print_term(*start, get_name, innersep, emptyset, os);
++start;
}
while (start != finish){
os << sep();
dd_print_term(*start, get_name, innersep, emptyset, os);
++start;
}
}
template <bool use_fast,
class CacheType, class NaviType, class PolyType>
PolyType
dd_multiply(const CacheType& cache_mgr,
NaviType firstNavi, NaviType secondNavi, PolyType init){
// Extract subtypes
typedef typename PolyType::dd_type dd_type;
typedef typename NaviType::idx_type idx_type;
typedef NaviType navigator;
if (firstNavi.isConstant()) {
if(firstNavi.terminalValue())
return cache_mgr.generate(secondNavi);
else
return cache_mgr.zero();
}
if (secondNavi.isConstant()) {
if(secondNavi.terminalValue())
return cache_mgr.generate(firstNavi);
else
return cache_mgr.zero();
}
if (firstNavi == secondNavi)
return cache_mgr.generate(firstNavi);
// Look up, whether operation was already used
navigator cached = cache_mgr.find(firstNavi, secondNavi);
PolyType result(cache_mgr.zero());
if (cached.isValid()) { // Cache lookup sucessful
return cache_mgr.generate(cached);
}
else { // Cache lookup not sucessful
// Get top variable's index
if (*secondNavi < *firstNavi)
std::swap(firstNavi, secondNavi);
idx_type index = *firstNavi;
// Get then- and else-branches wrt. current indexed variable
navigator as0 = firstNavi.elseBranch();
navigator as1 = firstNavi.thenBranch();
navigator bs0;
navigator bs1;
if (*secondNavi == index) {
bs0 = secondNavi.elseBranch();
bs1 = secondNavi.thenBranch();
}
else {
bs0 = secondNavi;
bs1 = cache_mgr.zero().navigation();
}
PolyType result0 = dd_multiply<use_fast>(cache_mgr, as0, bs0, init);
PolyType result1(cache_mgr.zero());
// use fast multiplication
if (use_fast && (*firstNavi == *secondNavi)) {
PolyType res10 = PolyType(cache_mgr.generate(as1)) +
PolyType(cache_mgr.generate(as0));
PolyType res01 = PolyType(cache_mgr.generate(bs0)) +
PolyType(cache_mgr.generate(bs1));
result1 = dd_multiply<use_fast>(cache_mgr, res10.navigation(),
res01.navigation(), init) - result0;
}
// not using fast multiplication
else if (as0 == as1) {
result1 = dd_multiply<use_fast>(cache_mgr, bs0, as1, init);
}
else {
result1 = dd_multiply<use_fast>(cache_mgr, as0, bs1, init);
if (bs0 != bs1){
PolyType bs01 = PolyType(cache_mgr.generate(bs0)) +
PolyType(cache_mgr.generate(bs1));
result1 +=
dd_multiply<use_fast>(cache_mgr, bs01.navigation(), as1, init);
}
}
result = dd_type(index, result1.diagram(), result0.diagram());
// Insert in cache
cache_mgr.insert(firstNavi, secondNavi, result.navigation());
} // end of Cache lookup not sucessful
return result;
}
template <class CacheType, class NaviType, class PolyType>
PolyType
dd_multiply_recursively(const CacheType& cache_mgr,
NaviType firstNavi, NaviType secondNavi, PolyType init){
enum { use_fast =
#ifdef PBORI_FAST_MULTIPLICATION
true
#else
false
#endif
};
return dd_multiply<use_fast>(cache_mgr, firstNavi, secondNavi, init);
}
template <class CacheType, class NaviType, class PolyType>
PolyType
dd_multiply_recursively_monom(const CacheType& cache_mgr,
NaviType monomNavi, NaviType navi, PolyType init){
// Extract subtypes
typedef typename PolyType::dd_type dd_type;
typedef typename NaviType::idx_type idx_type;
typedef NaviType navigator;
if (monomNavi.isConstant()) {
if(monomNavi.terminalValue())
return cache_mgr.generate(navi);
else
return cache_mgr.zero();
}
PBORI_ASSERT(monomNavi.elseBranch().isEmpty());
if (navi.isConstant()) {
if(navi.terminalValue())
return cache_mgr.generate(monomNavi);
else
return cache_mgr.zero();
}
if (monomNavi == navi)
return cache_mgr.generate(monomNavi);
// Look up, whether operation was already used
navigator cached = cache_mgr.find(monomNavi, navi);
if (cached.isValid()) { // Cache lookup sucessful
return cache_mgr.generate(cached);
}
// Cache lookup not sucessful
// Get top variables' index
idx_type index = *navi;
idx_type monomIndex = *monomNavi;
if (monomIndex < index) { // Case: index may occure within monom
init = dd_multiply_recursively_monom(cache_mgr, monomNavi.thenBranch(), navi,
init).diagram().change(monomIndex);
}
else if (monomIndex == index) { // Case: monom and poly start with same index
// Increment navigators
navigator monomThen = monomNavi.thenBranch();
navigator naviThen = navi.thenBranch();
navigator naviElse = navi.elseBranch();
if (naviThen != naviElse)
init = (dd_multiply_recursively_monom(cache_mgr, monomThen, naviThen, init)
+ dd_multiply_recursively_monom(cache_mgr, monomThen, naviElse,
init)).diagram().change(index);
}
else { // Case: var(index) not part of monomial
init =
dd_type(index,
dd_multiply_recursively_monom(cache_mgr, monomNavi, navi.thenBranch(),
init).diagram(),
dd_multiply_recursively_monom(cache_mgr, monomNavi, navi.elseBranch(),
init).diagram() );
}
// Insert in cache
cache_mgr.insert(monomNavi, navi, init.navigation());
return init;
}
template <class DDGenerator, class Iterator, class NaviType, class PolyType>
PolyType
dd_multiply_recursively_exp(const DDGenerator& ddgen,
Iterator start, Iterator finish,
NaviType navi, PolyType init){
// Extract subtypes
typedef typename NaviType::idx_type idx_type;
typedef typename PolyType::dd_type dd_type;
typedef NaviType navigator;
if (start == finish)
return ddgen.generate(navi);
PolyType result(ddgen.zero());
if (navi.isConstant()) {
if(navi.terminalValue()) {
std::reverse_iterator<Iterator> rstart(finish), rfinish(start);
result = ddgen.generate(navi);
while (rstart != rfinish) {
result = result.diagram().change(*rstart);
++rstart;
}
}
else
return ddgen.zero();
return result;
}
// Cache lookup not sucessful
// Get top variables' index
idx_type index = *navi;
idx_type monomIndex = *start;
if (monomIndex < index) { // Case: index may occure within monom
Iterator next(start);
while( (next != finish) && (*next < index) )
++next;
result = dd_multiply_recursively_exp(ddgen, next, finish, navi, init);
std::reverse_iterator<Iterator> rstart(next), rfinish(start);
while (rstart != rfinish) {
result = result.diagram().change(*rstart);
++rstart;
}
}
else if (monomIndex == index) { // Case: monom and poly start with same index
// Increment navigators
++start;
navigator naviThen = navi.thenBranch();
navigator naviElse = navi.elseBranch();
if (naviThen != naviElse)
result =(dd_multiply_recursively_exp(ddgen, start, finish, naviThen, init)
+ dd_multiply_recursively_exp(ddgen, start, finish, naviElse,
init)).diagram().change(index);
}
else { // Case: var(index) not part of monomial
result =
dd_type(index,
dd_multiply_recursively_exp(ddgen, start, finish,
navi.thenBranch(), init).diagram(),
dd_multiply_recursively_exp(ddgen, start, finish,
navi.elseBranch(), init).diagram() );
}
return result;
}
template<class DegCacheMgr, class NaviType, class SizeType>
bool max_degree_on_then(const DegCacheMgr& deg_mgr, NaviType navi,
SizeType degree, valid_tag is_descending) {
navi.incrementThen();
return ((dd_cached_degree(deg_mgr, navi, degree - 1) + 1) == degree);
}
template<class DegCacheMgr, class NaviType, class SizeType>
bool max_degree_on_then(const DegCacheMgr& deg_mgr, NaviType navi,
SizeType degree, invalid_tag non_descending) {
navi.incrementElse();
return (dd_cached_degree(deg_mgr, navi, degree) != degree);
}
template <class NaviType>
NaviType dd_get_constant(NaviType navi) {
while(!navi.isConstant()) {
navi.incrementElse();
}
return navi;
}
template <class T> class CIndexCacheHandle;
// with degree bound
template <class CacheType, class DegCacheMgr, class NaviType,
class TermType, class SizeType, class DescendingProperty>
TermType
dd_recursive_degree_lead(const CacheType& cache_mgr, const DegCacheMgr&
deg_mgr,
NaviType navi, TermType init, SizeType degree,
DescendingProperty prop) {
typedef CIndexCacheHandle<NaviType> deg2node;
if PBORI_UNLIKELY(degree < 0)
return cache_mgr.zero();
if (navi.isConstant())
return cache_mgr.generate(navi);
if (degree == 0)
return cache_mgr.generate(dd_get_constant(navi));
// Check cache for previous results
NaviType cached = cache_mgr.find(navi, deg2node(degree, cache_mgr.manager()));
if (cached.isValid())
return cache_mgr.generate(cached);
// Go to next branch
if ( max_degree_on_then(deg_mgr, navi, degree, prop) ) {
NaviType then_branch = navi.thenBranch();
init = dd_recursive_degree_lead(cache_mgr, deg_mgr, then_branch,
init, degree - 1, prop);
if ((navi.elseBranch().isEmpty()) && (init.navigation()==then_branch))
init = cache_mgr.generate(navi);
else
init = init.change(*navi);
}
else {
init = dd_recursive_degree_lead(cache_mgr, deg_mgr, navi.elseBranch(),
init, degree, prop);
}
/// caching deactivated - wrong results might pollute the cache @todo fix
NaviType resultNavi(init.navigation());
cache_mgr.insert(navi, deg2node(degree, cache_mgr.manager()), resultNavi);
// if (!cache_mgr.find(resultNavi).isValid())
// deg_mgr.insert(resultNavi, degree, degree);
return init;
}
template <class CacheType, class DegCacheMgr, class NaviType,
class TermType, class DescendingProperty>
TermType
dd_recursive_degree_lead(const CacheType& cache_mgr, const DegCacheMgr& deg_mgr,
NaviType navi, TermType init, DescendingProperty prop){
// if (navi.isConstant())
// return cache_mgr.generate(dd_get_constant(navi));
return dd_recursive_degree_lead(cache_mgr, deg_mgr, navi, init,
dd_cached_degree(deg_mgr, navi), prop);
}
template <class CacheType, class DegCacheMgr, class NaviType,
class TermType, class SizeType, class DescendingProperty>
TermType&
dd_recursive_degree_leadexp(const CacheType& cache_mgr,
const DegCacheMgr& deg_mgr,
NaviType navi, TermType& result,
SizeType degree,
DescendingProperty prop) {
typedef CIndexCacheHandle<NaviType> deg2node;
if PBORI_UNLIKELY(degree < 0) {
result.resize(0);
return result;
}
if (navi.isConstant())
return result;
if (degree == 0) {
while (!navi.isConstant())
navi.incrementElse();
if (!navi.terminalValue())
result.resize(0);
return result;
}
// Check cache for previous result
NaviType cached = cache_mgr.find(navi, deg2node(degree, cache_mgr.manager()));
if (cached.isValid())
return result = result.multiplyFirst(cache_mgr.generate(cached));
// Prepare next branch
if ( max_degree_on_then(deg_mgr, navi, degree, prop) ) {
result.push_back(*navi);
navi.incrementThen();
--degree;
}
else
navi.incrementElse();
return
dd_recursive_degree_leadexp(cache_mgr, deg_mgr, navi, result, degree, prop);
}
template <class CacheType, class DegCacheMgr, class NaviType,
class TermType, class DescendingProperty>
TermType&
dd_recursive_degree_leadexp(const CacheType& cache_mgr,
const DegCacheMgr& deg_mgr,
NaviType navi, TermType& result,
DescendingProperty prop) {
if (navi.isConstant())
return result;
return dd_recursive_degree_leadexp(cache_mgr, deg_mgr, navi, result,
dd_cached_degree(deg_mgr, navi), prop);
}
// Existential Abstraction. Given a ZDD F, and a set of variables
// S, remove all occurrences of s in S from any subset in F. This can
// be implemented by cofactoring F with respect to s = 1 and s = 0,
// then forming the union of these results.
template <class CacheType, class NaviType, class TermType>
TermType
dd_existential_abstraction(const CacheType& cache_mgr,
NaviType varsNavi, NaviType navi, TermType init){
typedef typename TermType::dd_type dd_type;
typedef typename TermType::idx_type idx_type;
if (navi.isConstant())
return cache_mgr.generate(navi);
idx_type index(*navi);
while (!varsNavi.isConstant() && ((*varsNavi) < index))
varsNavi.incrementThen();
if (varsNavi.isConstant())
return cache_mgr.generate(navi);
// Check cache for previous result
NaviType cached = cache_mgr.find(varsNavi, navi);
if (cached.isValid())
return cache_mgr.generate(cached);
NaviType thenNavi(navi.thenBranch()), elseNavi(navi.elseBranch());
TermType thenResult =
dd_existential_abstraction(cache_mgr, varsNavi, thenNavi, init);
TermType elseResult =
dd_existential_abstraction(cache_mgr, varsNavi, elseNavi, init);
if ((*varsNavi) == index)
init = thenResult.unite(elseResult);
else if ( (thenResult.navigation() == thenNavi) &&
(elseResult.navigation() == elseNavi) )
init = cache_mgr.generate(navi);
else
init = dd_type(index, thenResult, elseResult);
// Insert result to cache
cache_mgr.insert(varsNavi, navi, init.navigation());
return init;
}
template <class CacheType, class NaviType, class PolyType>
PolyType
dd_divide_recursively(const CacheType& cache_mgr,
NaviType navi, NaviType monomNavi, PolyType init){
// Extract subtypes
typedef typename NaviType::idx_type idx_type;
typedef NaviType navigator;
typedef typename PolyType::dd_type dd_type;
if (monomNavi.isConstant()) {
PBORI_ASSERT(monomNavi.terminalValue() == true);
return cache_mgr.generate(navi);
}
PBORI_ASSERT(monomNavi.elseBranch().isEmpty());
if (navi.isConstant())
return cache_mgr.zero();
if (monomNavi == navi)
return cache_mgr.one();
// Look up, whether operation was already used
navigator cached = cache_mgr.find(navi, monomNavi);
if (cached.isValid()) { // Cache lookup sucessful
return cache_mgr.generate(cached);
}
// Cache lookup not sucessful
// Get top variables' index
idx_type index = *navi;
idx_type monomIndex = *monomNavi;
if (monomIndex == index) { // Case: monom and poly start with same index
// Increment navigators
navigator monomThen = monomNavi.thenBranch();
navigator naviThen = navi.thenBranch();
init = dd_divide_recursively(cache_mgr, naviThen, monomThen, init);
}
else if (monomIndex > index) { // Case: monomIndex may occure within poly
init =
dd_type(index,
dd_divide_recursively(cache_mgr, navi.thenBranch(), monomNavi,
init).diagram(),
dd_divide_recursively(cache_mgr, navi.elseBranch(), monomNavi,
init).diagram() );
}
// Insert in cache
cache_mgr.insert(navi, monomNavi, init.navigation());
return init;
}
template <class DDGenerator, class Iterator, class NaviType, class PolyType>
PolyType
dd_divide_recursively_exp(const DDGenerator& ddgen,
NaviType navi, Iterator start, Iterator finish,
PolyType init){
// Extract subtypes
typedef typename NaviType::idx_type idx_type;
typedef typename PolyType::dd_type dd_type;
typedef NaviType navigator;
if (start == finish)
return ddgen.generate(navi);
if (navi.isConstant())
return ddgen.zero();
// Cache lookup not sucessful
// Get top variables' index
idx_type index = *navi;
idx_type monomIndex = *start;
PolyType result(ddgen.zero());
if (monomIndex == index) { // Case: monom and poly start with same index
// Increment navigators
++start;
navigator naviThen = navi.thenBranch();
result = dd_divide_recursively_exp(ddgen, naviThen, start, finish, init);
}
else if (monomIndex > index) { // Case: monomIndex may occure within poly
result =
dd_type(index,
dd_divide_recursively_exp(ddgen, navi.thenBranch(), start, finish,
init).diagram(),
dd_divide_recursively_exp(ddgen, navi.elseBranch(), start, finish,
init).diagram() );
}
else
result = ddgen.zero();
return result;
}
/// Function templates for determining the used variables of a decision diagram
/// with the help of cache
template <class CacheType, class NaviType, class MonomType>
MonomType
cached_used_vars(const CacheType& cache, NaviType navi, MonomType init) {
if (navi.isConstant()) // No need for caching of constant nodes' degrees
return init;
// Look whether result was cached before
NaviType cached_result = cache.find(navi);
typedef typename MonomType::poly_type poly_type;
if (cached_result.isValid())
return CDDOperations<typename MonomType::dd_type,
MonomType>().getMonomial(cache.generate(cached_result));
MonomType result = cached_used_vars(cache, navi.thenBranch(), init);
result *= cached_used_vars(cache, navi.elseBranch(), init);
result = result.change(*navi);
// Write result to cache
cache.insert(navi, result.diagram().navigation());
return result;
}
template <class NaviType, class Iterator>
bool
dd_owns(NaviType navi, Iterator start, Iterator finish) {
if (start == finish) {
while(!navi.isConstant())
navi.incrementElse();
return navi.terminalValue();
}
while(!navi.isConstant() && (*start > *navi) )
navi.incrementElse();
if (navi.isConstant() || (*start != *navi))
return false;
return dd_owns(navi.thenBranch(), ++start, finish);
}
/// Test whether a set of monomials contains all divisors of degree - 1 of a
/// given monomial
template <class NaviType, class MonomIterator>
bool
dd_contains_divs_of_dec_deg(NaviType navi,
MonomIterator start, MonomIterator finish) {
// Managing trivial cases
if (start == finish) // divisors of monomial 1 is the empty set,
// which is always contained
return true;
if (navi.isConstant()) { // set is empty or owns 1 only
if (navi.terminalValue()) // set == {1}
return (++start == finish); // whether monom is of degree one
else // empty set contains no divisors
return false;
}
// Actual computations
if (*navi < *start)
return dd_contains_divs_of_dec_deg(navi.elseBranch(), start, finish);
if (*navi > *start) {
if (++start == finish) // if monom is of degree one
return owns_one(navi);
else
return false;
}
// (*navi == *start) here
++start;
return dd_owns(navi.elseBranch(), start, finish) &&
dd_contains_divs_of_dec_deg(navi.thenBranch(), start, finish);
}
// determine the part of a polynomials of a given degree
template <class CacheType, class NaviType, class DegType, class SetType>
SetType
dd_graded_part(const CacheType& cache, NaviType navi, DegType deg,
SetType init) {
if (deg == 0) {
while(!navi.isConstant())
navi.incrementElse();
return cache.generate(navi);
}
if(navi.isConstant())
return cache.zero();
// Look whether result was cached before
NaviType cached = cache.find(navi, deg);
if (cached.isValid())
return cache.generate(cached);
SetType result =
SetType(*navi,
dd_graded_part(cache, navi.thenBranch(), deg - 1, init),
dd_graded_part(cache, navi.elseBranch(), deg, init)
);
// store result for later reuse
cache.insert(navi, deg, result.navigation());
return result;
}
/// Function templates extracting the terms of a given decision diagram contain
/// which contains only indices from first lexicographical path in
/// Note: Replacement for dd_intersect_some_index
template <class CacheManager, class NaviType, class SetType>
SetType
dd_first_divisors_of(CacheManager cache_mgr, NaviType navi,
NaviType rhsNavi, SetType init ) {
typedef typename SetType::dd_type dd_type;
while( (!navi.isConstant()) && (*rhsNavi != *navi) ) {
if ( (*rhsNavi < *navi) && (!rhsNavi.isConstant()) )
rhsNavi.incrementThen();
else
navi.incrementElse();
}
if (navi.isConstant()) // At end of path
return cache_mgr.generate(navi);
// Look up, whether operation was already used
NaviType result = cache_mgr.find(navi, rhsNavi);
if (result.isValid()) // Cache lookup sucessful
return cache_mgr.generate(result);
PBORI_ASSERT(*rhsNavi == *navi);
// Compute new result
init = dd_type(*rhsNavi,
dd_first_divisors_of(cache_mgr, navi.thenBranch(), rhsNavi,
init).diagram(),
dd_first_divisors_of(cache_mgr, navi.elseBranch(), rhsNavi,
init).diagram() );
// Insert result to cache
cache_mgr.insert(navi, rhsNavi, init.navigation());
return init;
}
template <class CacheType, class NaviType, class SetType>
SetType
dd_first_multiples_of(const CacheType& cache_mgr,
NaviType navi, NaviType rhsNavi, SetType init){
typedef typename SetType::dd_type dd_type;
if(rhsNavi.isConstant()) {
PBORI_ASSERT(rhsNavi.terminalValue() == true);
return cache_mgr.generate(navi);
}
if (navi.isConstant() || (*navi > *rhsNavi))
return cache_mgr.zero();
if (*navi == *rhsNavi)
return dd_first_multiples_of(cache_mgr, navi.thenBranch(),
rhsNavi.thenBranch(), init).change(*navi);
// Look up old result - if any
NaviType result = cache_mgr.find(navi, rhsNavi);
if (result.isValid())
return cache_mgr.generate(result);
// Compute new result
init = dd_type(*navi,
dd_first_multiples_of(cache_mgr, navi.thenBranch(),
rhsNavi, init).diagram(),
dd_first_multiples_of(cache_mgr, navi.elseBranch(),
rhsNavi, init).diagram() );
// Insert new result in cache
cache_mgr.insert(navi, rhsNavi, init.navigation());
return init;
}
/// Internal variant: Maps a polynomial from one ring to another, using a given
/// map old_index -> new_polynomial
template <class PolyType, class RingType, class MapType, class NaviType>
PolyType
substitute_variables__(const RingType& ring, const MapType& idx2poly, NaviType navi) {
if (navi.isConstant())
return ring.constant(navi.terminalValue());
//typename MapType::const_reference var(idx2poly[*navi]);
// PBORI_ASSERT(var.ring() == ring);
return (idx2poly[*navi] *
substitute_variables__<PolyType>(ring, idx2poly, navi.thenBranch())) +
substitute_variables__<PolyType>(ring, idx2poly, navi.elseBranch());
}
/// Maps a polynomial from one ring to another, using a given map
/// old_index -> new_polynomial
template <class RingType, class MapType, class PolyType>
PolyType
substitute_variables(const RingType& ring,
const MapType& idx2poly, const PolyType& poly) {
return substitute_variables__<PolyType>(ring, idx2poly, poly.navigation());
}
END_NAMESPACE_PBORI
#endif
|