partition result into subgroups

Discussion of chess software programming and technical issues.

Moderator: Ras

petero2
Posts: 733
Joined: Mon Apr 19, 2010 7:07 pm
Location: Sweden
Full name: Peter Osterlund

Re: partition result into subgroups

Post by petero2 »

The hyper function was just a one-line wrapper that called rhyper and casted the result to int. Here is a new version that calls rhyper directly and also handles the N not divisible by k case:

Code: Select all

int main(int argc, char* argv[]) {
    int nBins;
    str2Num(argv[1], nBins);
    std::vector<int> rk;
    int total = 0;
    int nPerBin = 0;
    for (int i = 2; i < argc; i++) {
        int tmp;
        str2Num(argv[i], tmp);
        rk.push_back(tmp);
        total += tmp;
    }
    nPerBin = total / nBins;
    const int T = rk.size();
    for (int b = 0; b < nBins; b++) {
        int nSelect = (b < nBins - 1) ? nPerBin : total;
        int nSelectFrom = total;
        for (int t = 0; t < T; t++) {
            int tmp = (int)rhyper(rk[t], nSelectFrom-rk[t], nSelect);
            cout << setw(10) << tmp;
            nSelect -= tmp;
            nSelectFrom -= rk[t];
            rk[t] -= tmp;
            total -= tmp;
        }
        cout << endl;
    }
}
This code can handle any number of categories, for example:

Code: Select all

$ ./test 11 1000 2000 3000 4000 5000 6000
        90       166       297       363       417       576
       105       187       253       399       457       508
        84       192       295       363       449       526
        73       193       267       359       463       554
       108       177       275       355       440       554
        93       173       241       381       452       569
        99       173       267       395       449       526
        89       191       250       355       464       560
        84       196       276       361       465       527
        82       164       281       343       492       547
        93       188       298       326       452       553
It still requires some work to make the rhyper code compile standalone. It may be better to use GNU GSL instead, see http://www.gnu.org/software/gsl/manual/ ... ution.html.
Daniel Shawul
Posts: 4186
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: partition result into subgroups

Post by Daniel Shawul »

Thanks a lot. I see the basic hypergeom function is enough for any number of variables. This is a good addition to the collection of utility functions that I may need again sometime in the future. The brute force way is surely slower than this and had also some problems with short period PRNG.
Daniel