X-Git-Url: https://ginac.de/ginac.git//ginac.git?a=blobdiff_plain;f=ginac%2Futils.h;h=7018bc9511a02584ffb885b81bf0cffb9d183a1b;hb=42ae6db8ba904362337098c508d168cffb9280eb;hp=fa6f54a589483a739600796a588541cf9252e42b;hpb=97af29c12bb3074cfb4e674d71000f0712c51ba2;p=ginac.git diff --git a/ginac/utils.h b/ginac/utils.h index fa6f54a5..7018bc95 100644 --- a/ginac/utils.h +++ b/ginac/utils.h @@ -138,6 +138,26 @@ OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1, } } +// Compute the sign of a permutation of a vector of things. +template +int permutation_sign(vector s) +{ + if (s.size() < 2) + return 0; + int sigma = 1; + for (typename vector::iterator i=s.begin(); i!=s.end()-1; ++i) { + for (typename vector::iterator j=i+1; j!=s.end(); ++j) { + if (*i == *j) + return 0; + if (*i > *j) { + iter_swap(i,j); + sigma = -sigma; + } + } + } + return sigma; +} + // Collection of `construct on first use' wrappers for safely avoiding // internal object replication without running into the `static // initialization order fiasco'. This chest of numbers helps speed up