]> www.ginac.de Git - ginac.git/blobdiff - ginac/utils.cpp
Move combinatorial helpers from power.cpp to utils.h.
[ginac.git] / ginac / utils.cpp
index 1900e1b30ca20188eceb3d78b6e6dbed718493e1..89217a93cbf1d05a444509f7f38cc572053d9147 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "ex.h"
 #include "numeric.h"
+#include "operators.h"
 #include "utils.h"
 #include "version.h"
 
@@ -53,6 +54,19 @@ unsigned log2(unsigned n)
        return k;
 }
 
+/** Compute the multinomial coefficient n!/(p1!*p2!*...*pk!) where
+ *  n = p1+p2+...+pk, i.e. p is a partition of n.
+ */
+const numeric
+multinomial_coefficient(const std::vector<int> & p)
+{
+       numeric n = 0, d = 1;
+       for (auto & it : p) {
+               n += numeric(it);
+               d *= factorial(numeric(it));
+       }
+       return factorial(n) / d;
+}
 
 //////////
 // flyweight chest of numbers is initialized here: