/usr/include/singular/gfanlib/gfanlib_tropicalhomotopy.h is in libsingular4-dev-common 1:4.1.0-p3+ds-2build1.
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 980 981 982 983 984 985 986 987 988 989 990 991 992 993 | /*
* gfanlib_tropicalhomotopy.h
*
* Created on: Apr 10, 2016
* Author: anders
*/
#ifndef GFANLIB_TROPICALHOMOTOPY_H_
#define GFANLIB_TROPICALHOMOTOPY_H_
#include "gfanlib_paralleltraverser.h"
#include "gfanlib_matrix.h"
namespace gfan{
class Flags{
public:
static const bool computeDotProductInMatrix=true;
};
/**
* We identify six possibly different types needed with possibly varying precission:
* 1) The entries of the circuits (or possibly their packed representation)
* 2) The mixed volume contribution of a single cell. This is obtained from an entry of a circuit and therefore can be represented by the above type.
* 3) The accumulated mixed volume. This will exceed the bound of the above type in many cases. Overflows are easily checked.
* 4) The type that dotVector uses as a result when dotting with the target. (Also used in campareInequalities)
* 5) The intermediate type for dotVector.
* 6) The type used in compareRevLexInverted
*
*
* Type 1 and 2 are the same.
* Type 3 is typically different.
*
* To simplify our design:
* we assume that type 4 is the same as 1 and 2. This is reasonable, as we need some bound to make type 6 efficient.
* we use a special (longer) type for 5, as that allows to do overflow checks at the end, assuming some bound on the target.
* In 6, we observe that there is no accumulation taking place. Moreover, with the assumption that 4 and 1 are the same, we only need a type with double precission to do the comparisons here, and now overflow check will be required.
*
*
* To conclude, we make two types. A single precision type for 1,2,4 and a double precision type for 3,5,6
* We further need to make assumptions on the absolute value of the entries of the target vector and the number of entries in combination to ensure that dot product computations do not overflow.
* Overflow checks are then only needed:
* when casting the return value of dotVector
* when doing the dotDivVector operation. But since bounds are known, in most cases checks are not needed
* when accumulating the mixed volume
*
*
* Suggested implementations:
* a pair of 32/64 bit integers with only the overflow checking listed above
* a pair of gmp integers for exact precision.
*/
template<class mvtyp>
static Matrix<mvtyp> simplex(int n, mvtyp d)
{
Matrix<mvtyp> ret(n,n+1);
for(int i=0;i<n;i++)ret[i][i+1]=d;
return ret;
}
template<class mvtyp, class mvtypDouble, class mvtypDivisor>
class SingleTropicalHomotopyTraverser{
class InequalityComparisonResult{//actual comparison functions were moved to the InequalityTable
public:
bool empty;
int configurationIndex;//used for finding best
int columnIndex;//used for finding best
};
class InequalityTable //circuit table // This table has been moved inside the IntegersectionTraverser simply because it is used nowhere else and is specific to mixed cells in Cayley configurations.
{
/* All methods are marked to show if they can overflow without throwing/asserting.
* Overflowing methods at the moment are:
* replaceFirstOrSecond: subroutine calls may overflow (dotDivVector)
* compareInequalities: only if target entries are too big
* dotVector: only if target entries are too big
* setChoicesFromEarlierHomotopy: only if tuple entries are too big
*/
std::vector<Matrix<mvtyp> > tuple;
std::vector<int> offsets;
std::vector<std::pair<int,int> > choices;
Matrix<mvtyp> A;//one row for each potential inequality, to store entries with indices in chosen
Vector<mvtyp> tempA;
Vector<mvtyp> Abounds;// a negative bound for each row of A, bounding the absolute value of the rows;
std::vector<mvtyp> svec;//used locally
int subconfigurationIndex;
mvtyp denominator;
int m;
int k;
bool isLegalIndex(int subconfigurationIndex, int columnIndex)const
{// Cannot overflow
return choices[subconfigurationIndex].first!=columnIndex && choices[subconfigurationIndex].second!=columnIndex;
}
mvtyp dotVector(int subconfigurationIndex, int columnIndex, Vector<mvtyp> const &target, int onlyK=-1)const
{ // May overflow if entries of target are too big.
//if onlyK!=-1 then only the onlyKth subconfiguration is considered
mvtypDouble ret;
if(onlyK!=-1)
{
if(onlyK==subconfigurationIndex)
{
int i=subconfigurationIndex;
ret+=extendedMultiplication(A.UNCHECKEDACCESS(i,columnIndex+offsets[subconfigurationIndex]),target.UNCHECKEDACCESS((choices)[i].second+offsets[i]));
ret-=extendedMultiplication(A.UNCHECKEDACCESS(i,columnIndex+offsets[subconfigurationIndex]),target.UNCHECKEDACCESS((choices)[i].first+offsets[i]));
ret-=extendedMultiplication(denominator,target.UNCHECKEDACCESS((choices)[i].first+offsets[i]));// the multiplication can be merged with multiplication above except that that could cause and overflow.
ret+=extendedMultiplication(denominator,target.UNCHECKEDACCESS(columnIndex+offsets[i]));
return ret.castToSingle();
}
else
{
int i=onlyK;
if(target.UNCHECKEDACCESS((choices)[i].first+offsets[i]).isNonZero())
{
ret+=extendedMultiplication(A.UNCHECKEDACCESS(i,columnIndex+offsets[subconfigurationIndex]),target.UNCHECKEDACCESS((choices)[i].second+offsets[i]));
ret-=extendedMultiplication(A.UNCHECKEDACCESS(i,columnIndex+offsets[subconfigurationIndex]),target.UNCHECKEDACCESS((choices)[i].first+offsets[i]));
}
return ret.castToSingle();
}
}
for(int i=0;i<tuple.size();i++)
{
ret+=extendedMultiplication(A.UNCHECKEDACCESS(i,columnIndex+offsets[subconfigurationIndex]),target.UNCHECKEDACCESS((choices)[i].second+offsets[i]));
if(i==subconfigurationIndex)
{
ret-=extendedMultiplication(A.UNCHECKEDACCESS(i,columnIndex+offsets[subconfigurationIndex]),target.UNCHECKEDACCESS((choices)[i].first+offsets[i]));
ret-=extendedMultiplication(denominator,target.UNCHECKEDACCESS((choices)[i].first+offsets[i]));// the multiplication can be merged with multiplication above except that that could cause and overflow.
ret+=extendedMultiplication(denominator,target.UNCHECKEDACCESS(columnIndex+offsets[i]));
}
else
{
ret-=extendedMultiplication(A.UNCHECKEDACCESS(i,columnIndex+offsets[subconfigurationIndex]),target.UNCHECKEDACCESS((choices)[i].first+offsets[i]));
}
}
return ret.castToSingle();
}
void assignDotProducts(Vector<mvtyp> const &target, int onlyK=-1)
{// Cannot overflow
int J=0;
for(int i=0;i<k;i++)
for(int j=0;j<tuple[i].getWidth();j++,J++)
A[k][J]=dotVector(i,j,target,onlyK);
}
bool isReverseLexInvertedLessThanZero(int subconfigurationIndex, int columnIndex)const __attribute__ ((always_inline))//As in ReverseLexicographicInvertedTermOrder. Compare against zero
{// Cannot overflow
int i;
int index=columnIndex+offsets[subconfigurationIndex];
for(i=0;i<subconfigurationIndex;i++)
if(A.UNCHECKEDACCESS(i,index).isNonZero())
{
if(choices[i].first<choices[i].second)
return A.UNCHECKEDACCESS(i,index).isNegative();
else
return A.UNCHECKEDACCESS(i,index).isPositive();
}
mvtyp a=A.UNCHECKEDACCESS(i,index);
{
int firstIndex=choices[i].first;
int secondIndex=choices[i].second;
int thirdIndex=columnIndex;
mvtyp firstValue=-a-denominator;
mvtyp secondValue=a;
mvtyp thirdValue=denominator;
// Bubble sort
if(secondIndex<firstIndex)
{
std::swap(secondIndex,firstIndex);
std::swap(secondValue,firstValue);
}
if(thirdIndex<secondIndex)
{
std::swap(secondIndex,thirdIndex);
std::swap(secondValue,thirdValue);
}
if(secondIndex<firstIndex)
{
std::swap(secondIndex,firstIndex);
std::swap(secondValue,firstValue);
}
if(firstValue.isNonZero())
return firstValue.isPositive();
if(secondValue.isNonZero())
return secondValue.isPositive();
if(thirdValue.isNonZero())
return thirdValue.isPositive();
}
i++;
for(;i<k;i++)
if(A.UNCHECKEDACCESS(i,index).isNonZero())
{
if(choices[i].first<choices[i].second)
return A.UNCHECKEDACCESS(i,index).isNegative();
else
return A.UNCHECKEDACCESS(i,index).isPositive();
}
return false;
}
public:
void computeABounds()
{//Cannot overflow
for(int i=0;i<A.getHeight();i++)
Abounds[i]=mvtyp::computeNegativeBound(&(A[i][0]),A.getWidth());
}
void checkABounds()const//remove?
{//Cannot overflow
for(int i=0;i<A.getHeight();i++)
{
mvtyp M=0;
mvtyp m=0;
for(int j=0;j<A.getWidth();j++)
{
if(M<=A[i][j])M=A[i][j];
if(A[i][j]<=m)m=A[i][j];
}
assert(Abounds[i]<=m);
assert(Abounds[i]<=-M);
}
}
mvtypDouble getCoordinateOfInequality(int subconfigurationIndex, int columnIndex, int i, int j)const
{// Cannot overflows
//get (i,j)th coordinate of (subconfigurationIndex,columnIndex)th inequality
if(i==subconfigurationIndex)
{
if(choices[i].first==j)return -A.UNCHECKEDACCESS(i,offsets[subconfigurationIndex]+columnIndex).extend()-denominator.extend();
else if(choices[i].second==j)return A.UNCHECKEDACCESS(i,offsets[subconfigurationIndex]+columnIndex).extend();
else if(j==columnIndex)return denominator.extend();
else return mvtypDouble(0);
}
else
if(choices[i].first==j)return -A.UNCHECKEDACCESS(i,offsets[subconfigurationIndex]+columnIndex).extend();
else if(choices[i].second==j)return A.UNCHECKEDACCESS(i,offsets[subconfigurationIndex]+columnIndex).extend();
else return mvtypDouble(0);
}
int sort2uniquely(int *v, int a, int b)const//a and b different
{// Cannot overflow
v[a>b]=a;
v[b>a]=b;
return 2;
}
int sort3uniquely(int *v, int a, int b, int c)const//a and b and c different
{// Cannot overflow
v[(a>b)+int(a>c)]=a;
v[(b>a)+int(b>c)]=b;
v[(c>a)+int(c>b)]=c;
return 3;
}
int sort4uniquely(int *v, int a, int b, int c, int d)const// a and b different and different from c and d, but c may equal d
{// Cannot overflow
if(c!=d)
{
v[(a>b)+int(a>c)+int(a>d)]=a;
v[(b>a)+int(b>c)+int(b>d)]=b;
v[(c>a)+int(c>b)+int(c>d)]=c;
v[(d>a)+int(d>b)+int(d>c)]=d;
return 4;
}
else return sort3uniquely(v,a,b,c);
}
bool compareReverseLexicographicInverted(int i1, int j1, int i2, int j2, mvtyp s1, mvtyp s2)const//s1 and s2 are always negative
{// cannot overflow
for(int i=0;i<k;i++)
{
if(i1!=i && i2!=i)
{
int temp=determinantSign1(A.UNCHECKEDACCESS(i,offsets[i1]+j1),s1,A.UNCHECKEDACCESS(i,offsets[i2]+j2),s2);
if(temp)
{
if(choices[i].first<choices[i].second)
return temp<0;
else
return temp>0;
}
}
int indices[4];
int F=choices[i].first;
int S=choices[i].second;
int toCheck;
if(i1==i)
if(i2==i)
toCheck=sort4uniquely(indices,F,S,j1,j2);
else
toCheck=sort3uniquely(indices,F,S,j1);
else
if(i2==i)
toCheck=sort3uniquely(indices,F,S,j2);
else
toCheck=sort2uniquely(indices,F,S);
for(int J=0;J<toCheck;J++)
{
int j=indices[J];
int temp=determinantSign2(getCoordinateOfInequality(i1,j1,i,j),s1,getCoordinateOfInequality(i2,j2,i,j),s2);
if(temp>0)
return true;
else if(temp<0)
return false;
}
}
return false;
}
mvtyp getVolume()
{// Cannot overflow
return denominator;
}
void replaceFirstOrSecond(bool first, int subconfigurationIndex, int newIndex, Vector<mvtyp> const &target)__attribute__ ((always_inline))//updates the inequality table according to replacing first or second by newIndex in the subconfigurationIndex'th configuration
{// Cannot overflow
int newIndex2=newIndex;for(int i=0;i<subconfigurationIndex;i++)newIndex2+=tuple[i].getWidth();
int oldIndex=first?choices[subconfigurationIndex].first:choices[subconfigurationIndex].second;
(first?choices[subconfigurationIndex].first:choices[subconfigurationIndex].second)=newIndex;
mvtyp nextDenominator=(first?A[subconfigurationIndex][newIndex2].extend()+denominator.extend():-A[subconfigurationIndex][newIndex2].extend()).castToSingle();
mvtyp t=nextDenominator;
mvtypDivisor denominatorDivisor(denominator);
for(int c=0;c<k+Flags::computeDotProductInMatrix;c++)tempA.UNCHECKEDACCESS(c)=A.UNCHECKEDACCESS(c,newIndex2);
(-Abounds[subconfigurationIndex].extend()).castToSingle();//This conversion will fail if the following fails
for(int u=0;u<m;u++)
svec[u]=first?-A.UNCHECKEDACCESS(subconfigurationIndex,u):A.UNCHECKEDACCESS(subconfigurationIndex,u);
mvtyp svecBound=Abounds[subconfigurationIndex];
if(first)
for(int j=0;j<tuple[subconfigurationIndex].getWidth();j++)
svec[offsets[subconfigurationIndex]+j].subWithOverflowChecking(denominator);
svecBound.subWithOverflowChecking(denominator);
for(int a=0;a<k+Flags::computeDotProductInMatrix;a++)
if(first||a!=subconfigurationIndex)
Abounds.UNCHECKEDACCESS(a)=mvtyp::dotDivVector(&A.UNCHECKEDACCESS(a,0),&(svec[0]),t,tempA.UNCHECKEDACCESS(a),denominatorDivisor,m,Abounds[a],svecBound);
// for(int u=0;u<m;u++)
// *A.UNCHECKEDACCESSRESTRICT(a,u)=dotDiv(svec[u],tempA.UNCHECKEDACCESS(a),t,A.UNCHECKEDACCESS(a,u),denominatorDivisor);
{
for(int a=0;a<k+Flags::computeDotProductInMatrix;a++)
{
A[a][offsets[subconfigurationIndex]+oldIndex]=tempA[a].negatedWithOverflowChecking();
Abounds[a]=min(Abounds[a],negabs(tempA[a]));
}
if(!first)
{
A[subconfigurationIndex][offsets[subconfigurationIndex]+oldIndex]=denominator.negatedWithOverflowChecking();
Abounds[subconfigurationIndex].subWithOverflowChecking(denominator);
}
}
denominator=nextDenominator;
// We clear these unused entries of A to ensure that these columns are not chosen when comparing inequalities
for(int i=0;i<k+Flags::computeDotProductInMatrix;i++)
{
A.UNCHECKEDACCESS(i,offsets[subconfigurationIndex]+choices[subconfigurationIndex].first)=0;
A.UNCHECKEDACCESS(i,offsets[subconfigurationIndex]+choices[subconfigurationIndex].second)=0;
}
}
void replaceFirst(int subconfigurationIndex, int newIndex, Vector<mvtyp> const &target){replaceFirstOrSecond(true,subconfigurationIndex,newIndex,target);}
void replaceSecond(int subconfigurationIndex, int newIndex, Vector<mvtyp> const &target){replaceFirstOrSecond(false,subconfigurationIndex,newIndex,target);}
InequalityTable(std::vector<Matrix<mvtyp> > const &tuple_, int subconfigurationIndex_):
tempA(tuple_.size()+Flags::computeDotProductInMatrix),
tuple(tuple_),
choices(tuple_.size()),
subconfigurationIndex(subconfigurationIndex_),
offsets(tuple_.size())
{// Cannot overflow
k=tuple.size();
m=0;
for(int i=0;i<tuple.size();i++)m+=tuple[i].getWidth();
svec.resize(m);
A=Matrix<mvtyp>(k+Flags::computeDotProductInMatrix,m);
{int offset=0;for(int i=0;i<tuple.size();i++){offsets[i]=offset;offset+=tuple[i].getWidth();}}
Abounds=Vector<mvtyp>(k+Flags::computeDotProductInMatrix);
}
void setChoicesInitially()
{// Cannot overflow
//THIS WILL ONLY WORK FOR THE STARTING CONFIGURATION
//sets denominators,A and choices (these must have been initialized to the right sizes)
for(int i=0;i<k;i++)
choices[i]=std::pair<int, int> (i+0,i+1);
for(int i=0;i<m;i++)
for(int j=0;j<k;j++)
A[j][i]=0;
//we follow the lemma in the article. Why does the proof of the lemma split into 3 cases and not just 2?
int a=0;
for(int i=0;i<k;i++)
for(int gamma=0;gamma<tuple[i].getWidth();gamma++,a++)
if(gamma>i+1)
for(int ii=i;ii<gamma;ii++)
A[ii][a]=-1;
else
for(int ii=gamma;ii<i;ii++)
A[ii][a]=1;
denominator=1;
for(int i=0;i<k;i++)Abounds[i]=-1;
// assignDotProducts(target);
}
void compareInequalities(InequalityComparisonResult &result, Vector<mvtyp> const &target, int onlyK=-1)
{// Can only overflow if target entries are too big. Actually it seems that it is not this function that will overflow but dotVector.
bool empty=true;
int bestConfigurationIndex=-1;
int bestColumnIndex=-1;
mvtyp targetDotBest=0;
for(int i=0;i<k;i++)
{
// TO DO: exchange the line with the template parameter version:
// gfan::Matrix<mvtyp>::const_RowRef Ak=const_cast<const gfan::Matrix<mvtyp>&>(A)[k];
gfan::Matrix<CircuitTableInt32>::const_RowRef Ak=const_cast<const gfan::Matrix<mvtyp>&>(A)[k];
int offsetsi=offsets[i];
int tupleiwidth=tuple[i].getWidth();
if(onlyK!=-1)if(i!=onlyK)continue;
for(int j=0;j<tupleiwidth;j++)
if(Flags::computeDotProductInMatrix || isLegalIndex(i,j))//unused inequalities will have value 0. Therefore isLegalIndex(i,j) is not required if values are stored.
{
mvtyp ineqDotTarget=Flags::computeDotProductInMatrix?Ak.UNCHECKEDACCESS(offsetsi+j):dotVector(i,j,target,onlyK);
if(ineqDotTarget.isNegative())
{
if(!isReverseLexInvertedLessThanZero(i,j))
{
if(empty||compareReverseLexicographicInverted(bestConfigurationIndex,bestColumnIndex,i,j,ineqDotTarget,targetDotBest))
{
targetDotBest=ineqDotTarget;
empty=false;
bestConfigurationIndex=i;
bestColumnIndex=j;
}
}
// assert(!ineq.isZero());
}
}
}
result.empty=empty;
result.configurationIndex=bestConfigurationIndex;
result.columnIndex=bestColumnIndex;
// assert(denominator>0);
}
void setChoicesFromEarlierHomotopy(InequalityTable const &parent, mvtyp degreeScaling, Vector<mvtyp> const &target)
{ // Overflows may happen, but only if the tuple entries are too big.
// Notice that the code below has overflow checks everywhere, except in the loop marked with a comment.
//sets denominators,A and choices (these must have been initialized to the right sizes
//columns have been added to configuration this->subconfigurationIndex
//for that reason we need to introduce new circuits. Old circuits are preserved.
//chioices are "relative" so no update is needed.
choices=parent.choices;
int numberToDrop=(subconfigurationIndex!=0) ? numberToDrop=k+1 : 0;
choices[subconfigurationIndex-1].first-=numberToDrop;
choices[subconfigurationIndex-1].second-=numberToDrop;
denominator=parent.denominator;
int offsetOld=0;
int offsetNew=0;
for(int i=0;i<k;i++)
{
int localNumberToDrop=0;
if(i==subconfigurationIndex-1)
localNumberToDrop=numberToDrop;
for(int a=0;a<A.getHeight()-Flags::computeDotProductInMatrix;a++)
if(a==subconfigurationIndex)
for(int j=0;j<parent.tuple[i].getWidth()-localNumberToDrop;j++)
A.UNCHECKEDACCESS(a,offsetNew+j)=parent.A.UNCHECKEDACCESS(a,offsetOld+j+localNumberToDrop);
else
{
// We can get an estimated 3 percent speed up by using known bounds to check if any of the multiplications below can overflow.
// Moreover, these multiplications can be moved outside the outer loop, to give better vectorisation.
for(int j=0;j<parent.tuple[i].getWidth()-localNumberToDrop;j++)
A.UNCHECKEDACCESS(a,offsetNew+j)=extendedMultiplication(degreeScaling,parent.A.UNCHECKEDACCESS(a,offsetOld+j+localNumberToDrop)).castToSingle();
}
if(i==subconfigurationIndex)
{
for(int j=parent.tuple[i].getWidth();j<tuple[i].getWidth();j++)
{
for(int a=0;a<A.getHeight()-Flags::computeDotProductInMatrix;a++)
A.UNCHECKEDACCESS(a,offsetNew+j)=0;
{
int b=choices[subconfigurationIndex].second-1;
if(b>=0)
A.UNCHECKEDACCESS(subconfigurationIndex,offsetNew+j).msubWithOverflowChecking(tuple[i].UNCHECKEDACCESS(b,j),denominator);
}
for(int a=0;a<k+Flags::computeDotProductInMatrix;a++)
{ // If tuple entries are not bounded, overflow can happen in the following loop
mvtypDouble tempDouble=A.UNCHECKEDACCESS(a,offsetNew+j).extend();
for(int b=0;b<k;b++)
if(choices[subconfigurationIndex].second!=b+1 &&choices[subconfigurationIndex].first!=b+1)
tempDouble+=extendedMultiplication(tuple[i].UNCHECKEDACCESS(b,j),A.UNCHECKEDACCESS(a,offsetNew+b+1));
A.UNCHECKEDACCESS(a,offsetNew+j)=tempDouble.castToSingle();
}
mvtypDouble left=degreeScaling.extend();
for(int b=0;b<k;b++)
left-=mvtyp(tuple[i].UNCHECKEDACCESS(b,j)).extend();
mvtyp leftS=left.castToSingle();
if(choices[subconfigurationIndex].second==0)
A.UNCHECKEDACCESS(subconfigurationIndex,offsetNew+j).msubWithOverflowChecking(leftS,denominator);
else if(choices[subconfigurationIndex].first!=0)
for(int a=0;a<k+Flags::computeDotProductInMatrix;a++)
A.UNCHECKEDACCESS(a,offsetNew+j).maddWithOverflowChecking(leftS,mvtyp(A.UNCHECKEDACCESS(a,offsetNew)));
}
for(int j=0;j<parent.tuple[i].getWidth();j++)// <-- this loop has side effects on addExpressionOfCeb() above. Therefore it is placed after the loop above.
for(int a=0;a<A.getHeight()-Flags::computeDotProductInMatrix;a++)
A.UNCHECKEDACCESS(a,offsetNew+j)=extendedMultiplication(A.UNCHECKEDACCESS(a,offsetNew+j),degreeScaling).castToSingle();
}
offsetOld+=parent.tuple[i].getWidth();
offsetNew+=tuple[i].getWidth();
}
denominator=extendedMultiplication(denominator,degreeScaling).castToSingle();
if(Flags::computeDotProductInMatrix)assignDotProducts(target,subconfigurationIndex);
computeABounds();
for(int a=0;a<k;a++)
for(int i=0;i<k+Flags::computeDotProductInMatrix;i++)
{
A[i][offsets[a]+choices[a].first]=0;
A[i][offsets[a]+choices[a].second]=0;
}
}
};
struct StackItem{
int columnIndex;
int configurationIndex;
bool b;
int choice;
bool useFirstChanged,useSecondChanged;
StackItem(int columnIndex_, int configurationIndex_, bool b_, int choice_, bool useFirstChanged_, bool useSecondChanged_):
columnIndex(columnIndex_),
configurationIndex(configurationIndex_),
b(b_),
choice(choice_),
useFirstChanged(useFirstChanged_),
useSecondChanged(useSecondChanged_)
{
}
};
public:
std::vector<std::pair<int,int> > choices;
Vector<mvtyp> target;
bool useFirstChanged;
bool useSecondChanged;
std::vector<StackItem> stack;
int eliminatedK;
int eliminatedKOffset;
std::vector<Matrix<mvtyp> > tuple;
std::vector<int> offsets;
int m;
InequalityComparisonResult result;
InequalityTable inequalityTable;
void constructInequalityTableFromParent(InequalityTable const &parentTable, mvtyp degreeScaling)
{
inequalityTable.setChoicesFromEarlierHomotopy(parentTable, degreeScaling, target);
}
void constructInequalityTableInitially(mvtyp degreeScaling)
{
std::vector<Matrix<mvtyp> > tempTuple;for(int i=0;i<tuple.size();i++)tempTuple.push_back(simplex<mvtyp>(tuple.size(),1));
InequalityTable tempTable(tempTuple,-1);
tempTable.setChoicesInitially();
constructInequalityTableFromParent(tempTable,degreeScaling);
}
SingleTropicalHomotopyTraverser(std::vector<Matrix<mvtyp> > const &tuple_, int m_, std::vector<std::pair<int,int> > const &choices_, Vector<mvtyp> const &target_, int eliminatedK_):
choices(choices_),
target(target_),
eliminatedK(eliminatedK_),
tuple(tuple_),
m(m_),
inequalityTable(tuple,eliminatedK),
offsets(tuple_.size())
{
eliminatedKOffset=0;for(int i=0;i<eliminatedK;i++)eliminatedKOffset+=tuple_[i].getWidth();
{int offset=0;for(int i=0;i<tuple.size();i++){offsets[i]=offset;offset+=tuple[i].getWidth();}}
/* We also need to check that the input is within the required bounds
* This is important to not have overflows in:
* dotVector
* setChoicesFromEarlierHomotopy
* The number of summands is bounded by 3k+4*/
bool isOK=mvtyp::isSuitableSummationNumber(3*tuple.size()+4);
for(int i=0;i<target.size();i++)isOK&=mvtyp(target[i]).fitsInHalf();
for(int i=0;i<tuple.size();i++)
for(int j=0;j<tuple[i].getHeight();j++)
for(int k=0;k<tuple[i].getWidth();k++)
isOK&=mvtyp(tuple[i][j][k]).fitsInHalf();
if(!isOK)throw MVMachineIntegerOverflow;
}
virtual void process()
{
}
bool findOutgoingAndProcess(bool doProcess)//sets up useFirstChanged and useSecondChanged and processes if process is true
{//returns true if we are at a leaf
// inequalityTable.checkABounds();
useFirstChanged=false;
useSecondChanged=false;
int onlyK=-1;
#if 1
if(eliminatedK!=-1)
if(target[choices[eliminatedK].first+eliminatedKOffset]==target[choices[eliminatedK].second+eliminatedKOffset])
onlyK=eliminatedK;
#endif
inequalityTable.compareInequalities(result,target,onlyK);
if(result.empty)
{
if(doProcess)process();
return true;
}
// reverse search rule:
mvtypDouble bestAtFirst=inequalityTable.getCoordinateOfInequality(result.configurationIndex,result.columnIndex,result.configurationIndex,choices[result.configurationIndex].first);
mvtypDouble bestAtSecond=inequalityTable.getCoordinateOfInequality(result.configurationIndex,result.columnIndex,result.configurationIndex,choices[result.configurationIndex].second);
if(bestAtFirst.isNegative())
{
if(bestAtSecond.isNegative())
{
useFirstChanged=true;
useSecondChanged=true;
}
else
{
if(bestAtSecond.isZero())
useFirstChanged=true;
else
if(choices[result.configurationIndex].second<result.columnIndex)
useFirstChanged=true;
}
}
else
{
if(bestAtSecond.isNegative())
{
if(bestAtFirst.isZero())
useSecondChanged=true;
else
if(choices[result.configurationIndex].first<result.columnIndex)
useSecondChanged=true;
}
else
{
assert(0);
}
}
return false;
}
void goToFirstChild()
{
// debug<<"First: configuration index:"<<data.configurationIndex<<" column index:"<<data.columnIndex<<"\n";
assert(useFirstChanged);
{
stack.push_back(StackItem(
result.columnIndex,
result.configurationIndex,
false,
choices[result.configurationIndex].first,
useFirstChanged,
useSecondChanged));
choices[result.configurationIndex].first=result.columnIndex;
inequalityTable.replaceFirst(result.configurationIndex,result.columnIndex,target);
}
}
void goToSecondChild()
{
// debug<<"Second: configuration index:"<<data.configurationIndex<<" column index:"<<data.columnIndex<<"\n";
assert(useSecondChanged);
{
stack.emplace_back(StackItem(
result.columnIndex,
result.configurationIndex,
true,
choices[result.configurationIndex].second,
useFirstChanged,
useSecondChanged));
choices[result.configurationIndex].second=result.columnIndex;
inequalityTable.replaceSecond(result.configurationIndex,result.columnIndex,target);
}
}
int numberOfChildren()
{
return int(useFirstChanged)+int(useSecondChanged);
}
void goToNthChild(int n)
{
if(n==0)
if(useFirstChanged)
goToFirstChild();
else
goToSecondChild();
else
goToSecondChild();
}
void goBack()
{
StackItem &B=stack.back();
result.columnIndex=B.columnIndex;
result.configurationIndex=B.configurationIndex;
if(B.b)
{
choices[result.configurationIndex].second=B.choice;
inequalityTable.replaceSecond(result.configurationIndex,B.choice,target);
}
else
{
choices[result.configurationIndex].first=B.choice;
inequalityTable.replaceFirst(result.configurationIndex,B.choice,target);
}
useFirstChanged=B.useFirstChanged;
useSecondChanged=B.useSecondChanged;
stack.pop_back();
}
bool atRoot()
{
return stack.empty();
}
};
/*
* This class concatenates several homotopies to a single tree.
*/
template<class mvtyp, class mvtypDouble, class mvtypDivisor>
class TropicalRegenerationTraverser{
// The following class is an attempt to separate homotopy data from the traversal logic.
class Data{
static mvtyp degree(Matrix<mvtyp> const &m)//assumes entries of m are non-negative
{
mvtyp ret=0;
for(int i=0;i<m.getWidth();i++)
{
mvtyp s(0);
for(int j=0;j<m.getHeight();j++)
s.addWithOverflowChecking(m[j][i]);
ret=max(s,ret);
}
return ret;
}
public:
std::vector<Vector<mvtyp> > targets;
std::vector<Matrix<mvtyp> > tuple;
std::vector<std::vector<Matrix<mvtyp> > > tuples;
Vector<mvtyp> degrees;
bool isFiniteIndex(int level, int index)
{
return index>=tuple[0].getHeight()+1;
}
std::vector<Matrix<mvtyp> > produceIthTuple(int i)
{
int n=tuple[0].getHeight();
std::vector<Matrix<mvtyp> > ret;
for(int j=0;j<tuple.size();j++)
{
if(j<i)ret.push_back(tuple[j]);
if(j==i)ret.push_back(combineLeftRight(simplex<mvtyp>(n,degree(tuple[j])),tuple[j]));
if(j>i)ret.push_back(simplex<mvtyp>(n,1));
}
return ret;
}
Data(std::vector<Matrix<mvtyp> > const &tuple_):tuple(tuple_)
{
int n=tuple[0].getHeight();
{//adjusting to positive orthant
for(int i=0;i<tuple.size();i++)
for(int j=0;j<tuple[i].getHeight();j++)
{
mvtyp m;
if(tuple[i].getWidth()==0)
m=0;
else
m=tuple[i][j][0];
for(int k=0;k<tuple[i].getWidth();k++)m=min(m,tuple[i][j][k]);
for(int k=0;k<tuple[i].getWidth();k++)tuple[i][j][k].subWithOverflowChecking(m);
}
}
for(int i=0;i<tuple.size();i++)
degrees.push_back(degree(tuple[i]));
for(int i=0;i<tuple.size();i++)
tuples.push_back(produceIthTuple(i));
for(int i=0;i<tuple.size();i++)
{
Vector<mvtyp> targ;
for(int j=0;j<tuple.size();j++)
{
if(j==i)
targ=concatenation(targ,concatenation(Vector<mvtyp>::allOnes(n+1),Vector<mvtyp>(tuple[i].getWidth())));
else
targ=concatenation(targ,Vector<mvtyp>(tuples[i][j].getWidth()));
}
targets.push_back(targ);
}
};
std::vector<std::pair<int,int> > firstIntersection()
{
std::vector<std::pair<int,int> > ret;
for(int i=0;i<tuple.size();i++)
ret.push_back(std::pair<int,int>(i+0,i+1));
return ret;
}
void castToNextLevel(std::vector<std::pair<int,int> > const &choices, int i, int S, std::vector<std::pair<int,int> > &ret)
{
assert(ret.size()==choices.size());
for(int j=0;j<choices.size();j++)
ret[j]=choices[j];
assert(ret[i].first>=S);
assert(ret[i].second>=S);
ret[i].first-=S;
ret[i].second-=S;
}
};
static int cayleyConfigurationWidth(std::vector<Matrix<mvtyp> > const &tuple)
{
int m2=0;
for(int i=0;i<tuple.size();i++)
m2+=tuple[i].getWidth();
return m2;
}
public:
int depth;
int counter;
typedef SingleTropicalHomotopyTraverser<mvtyp,mvtypDouble,mvtypDivisor> MySingleTropicalHomotopyTraverser;
std::vector<MySingleTropicalHomotopyTraverser> traversers;
Data fullData;
int level;
bool deadEnd;
bool isLevelLeaf;
bool isSolutionVertex;
std::vector<bool> isLevelLeafStack;
TropicalRegenerationTraverser(std::vector<Matrix<mvtyp> > const &tuple_):
fullData(tuple_),counter(0),depth(0)
{
assert(tuple_.size());
for(int i=0;i<tuple_.size();i++)
traversers.push_back(MySingleTropicalHomotopyTraverser(fullData.tuples[i],cayleyConfigurationWidth(fullData.tuples[i]),fullData.firstIntersection(),fullData.targets[i],i));
traversers[0].constructInequalityTableInitially(fullData.degrees[0]);
level=0;
}
virtual void process()
{
}
bool findOutgoingAndProcess(bool doProcess)
{
isSolutionVertex=false;
deadEnd=false;
isLevelLeaf=traversers[level].findOutgoingAndProcess(false);
if(isLevelLeaf)
{//leaf
bool isFinite=fullData.isFiniteIndex(level,traversers[level].choices[level].first)&&fullData.isFiniteIndex(level,traversers[level].choices[level].second);
deadEnd=!isFinite;
if(isFinite && (level==fullData.tuple.size()-1))
{
isSolutionVertex=true;
if(doProcess){process();}
return true;
}
}
return false;
}
int numberOfChildren()
{
if(isLevelLeaf&&(level==fullData.tuple.size()-1))return 0;
if(!isLevelLeaf)
return traversers[level].numberOfChildren();
else
return 1-deadEnd;
}
void goToNthChild(int n)
{
depth++;
isLevelLeafStack.push_back(isLevelLeaf);
if(!isLevelLeaf)
traversers[level].goToNthChild(n);
else
{
fullData.castToNextLevel(traversers[level].choices,level,fullData.tuples[level][level].getWidth()-fullData.tuples[level+1][level].getWidth(),traversers[level+1].choices);
traversers[level+1].constructInequalityTableFromParent(traversers[level].inequalityTable,fullData.degrees[level+1]);
level++;
}
}
void print()
{
}
void goBack()
{
depth--;
counter++;
deadEnd=false;
if(traversers[level].atRoot())
level--;
else
traversers[level].goBack();
isLevelLeaf=isLevelLeafStack.back();
isLevelLeafStack.pop_back();
}
};
/*
* This class glues Bjarne Knudsen's parallel traverser to our MultiLevelIntersectionTraverser.
* This class should eventually be written so that it works on any homotopy.
*
* Since we are not sure about how exceptions would be handled by the parallel traverser,
* we write ugly aborting code, which may actually work.
*/
template<class mvtyp, class mvtypDouble, class mvtypDivisor>
class SpecializedRTraverser: public Traverser
{
public:
typedef TropicalRegenerationTraverser<mvtyp,mvtypDouble,mvtypDivisor> MyTropicalRegenerationTraverser;
MyTropicalRegenerationTraverser T;
mvtypDouble mixedVolume;
int numberOfExpensiveSteps;
SpecializedRTraverser(std::vector<Matrix<mvtyp> > const &tuple_):
mixedVolume(),
numberOfExpensiveSteps(0),
T(tuple_) //Constructor my throw if input is too big.
{
numberOfExpensiveSteps++;
T.findOutgoingAndProcess(false);
}
int getEdgeCountNext( void )
{
if(!aborting)
{
try{
return T.numberOfChildren();
}
catch(...){abort();}
}
return 0;
}
int moveToNext( int index,
bool collect_info )
{
if(!aborting)
{
try{
T.goToNthChild(index);
numberOfExpensiveSteps++;
T.findOutgoingAndProcess(false);
}
catch(...){abort();}
}
return 0;
}
void moveToPrev( int index )
{
if(!aborting)
{
try{
T.goBack(); //index ignored
}
catch(...){abort();}
}
}
void collectInfo( void )
{
if(!aborting)
{
try{
if(T.isSolutionVertex)
mixedVolume.addWithOverflowCheck(T.traversers[T.level].inequalityTable.getVolume().extend());
}
catch(...){abort();}
}
}
void printState( void )
{
T.print();
}
};
}
#endif /* GFANLIB_TROPICALHOMOTOPY_H_ */
|