/* * 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 static Matrix simplex(int n, mvtyp d) { Matrix ret(n,n+1); for(int i=0;i 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 > tuple; std::vector offsets; std::vector > choices; Matrix A;//one row for each potential inequality, to store entries with indices in chosen Vector tempA; Vector Abounds;// a negative bound for each row of A, bounding the absolute value of the rows; std::vector 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 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 const &target, int onlyK=-1) {// Cannot overflow int J=0; for(int i=0;ib]=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;i0; } } 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;J0) 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 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 const &target){replaceFirstOrSecond(true,subconfigurationIndex,newIndex,target);} void replaceSecond(int subconfigurationIndex, int newIndex, Vector const &target){replaceFirstOrSecond(false,subconfigurationIndex,newIndex,target);} InequalityTable(std::vector > 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(k+Flags::computeDotProductInMatrix,m); {int offset=0;for(int i=0;i(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 (i+0,i+1); for(int i=0;ii+1) for(int ii=i;ii 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::const_RowRef Ak=const_cast&>(A)[k]; gfan::Matrix::const_RowRef Ak=const_cast&>(A)[k]; int offsetsi=offsets[i]; int tupleiwidth=tuple[i].getWidth(); if(onlyK!=-1)if(i!=onlyK)continue; for(int j=0;j0); } void setChoicesFromEarlierHomotopy(InequalityTable const &parent, mvtyp degreeScaling, Vector 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) ? 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=0) A.UNCHECKEDACCESS(subconfigurationIndex,offsetNew+j).msubWithOverflowChecking(tuple[i].UNCHECKEDACCESS(b,j),denominator); } for(int a=0;a > choices; Vector target; bool useFirstChanged; bool useSecondChanged; std::vector stack; int eliminatedK; int eliminatedKOffset; std::vector > tuple; std::vector 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 > tempTuple;for(int i=0;i(tuple.size(),1)); InequalityTable tempTable(tempTuple,-1); tempTable.setChoicesInitially(); constructInequalityTableFromParent(tempTable,degreeScaling); } SingleTropicalHomotopyTraverser(std::vector > const &tuple_, int m_, std::vector > const &choices_, Vector 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 class TropicalRegenerationTraverser{ // The following class is an attempt to separate homotopy data from the traversal logic. class Data{ static mvtyp degree(Matrix const &m)//assumes entries of m are non-negative { mvtyp ret=0; for(int i=0;i > targets; std::vector > tuple; std::vector > > tuples; Vector degrees; bool isFiniteIndex(int level, int index) { return index>=tuple[0].getHeight()+1; } std::vector > produceIthTuple(int i) { int n=tuple[0].getHeight(); std::vector > ret; for(int j=0;j(n,degree(tuple[j])),tuple[j])); if(j>i)ret.push_back(simplex(n,1)); } return ret; } Data(std::vector > const &tuple_):tuple(tuple_) { int n=tuple[0].getHeight(); {//adjusting to positive orthant for(int i=0;i targ; for(int j=0;j::allOnes(n+1),Vector(tuple[i].getWidth()))); else targ=concatenation(targ,Vector(tuples[i][j].getWidth())); } targets.push_back(targ); } }; std::vector > firstIntersection() { std::vector > ret; for(int i=0;i(i+0,i+1)); return ret; } void castToNextLevel(std::vector > const &choices, int i, int S, std::vector > &ret) { assert(ret.size()==choices.size()); for(int j=0;j=S); assert(ret[i].second>=S); ret[i].first-=S; ret[i].second-=S; } }; static int cayleyConfigurationWidth(std::vector > const &tuple) { int m2=0; for(int i=0;i MySingleTropicalHomotopyTraverser; std::vector traversers; Data fullData; int level; bool deadEnd; bool isLevelLeaf; bool isSolutionVertex; std::vector isLevelLeafStack; TropicalRegenerationTraverser(std::vector > const &tuple_): fullData(tuple_),counter(0),depth(0) { assert(tuple_.size()); for(int i=0;i class SpecializedRTraverser: public Traverser { public: typedef TropicalRegenerationTraverser MyTropicalRegenerationTraverser; MyTropicalRegenerationTraverser T; mvtypDouble mixedVolume; int numberOfExpensiveSteps; SpecializedRTraverser(std::vector > 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_ */