]> www.ginac.de Git - ginac.git/blobdiff - ginac/utils.cpp
Clean up combinatorial helpers.
[ginac.git] / ginac / utils.cpp
index 374e22637223864cd05e682112cba68af31c9acf..8085ba3bdeb38673b7d0d3fadf2282765a4901ee 100644 (file)
@@ -4,7 +4,7 @@
  *  but not of any interest to the user of the library. */
 
 /*
- *  GiNaC Copyright (C) 1999-2015 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2017 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -53,6 +53,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<unsigned> & p)
+{
+       numeric n = 0, d = 1;
+       for (auto & it : p) {
+               n = n.add(numeric(it));
+               d = d.mul(factorial(numeric(it)));
+       }
+       return factorial(n).div(d);
+}
 
 //////////
 // flyweight chest of numbers is initialized here: