linear_combination.h 27.6 KB
Newer Older
1
2
3
4
5
6
#ifndef _SPEL_LINEAR_COMBINATION_H_
#define _SPEL_LINEAR_COMBINATION_H_

#include <list>
#include "eigen.h"

Damien Leroux's avatar
Damien Leroux committed
7
8
#define _EPSILON 1.e-10

9
template <typename PARENT_TYPE, typename STATE_TYPE=size_t>
10
11
struct combination_type {
    static const size_t nopar = (size_t) -1;
12

13
    typedef combination_type<PARENT_TYPE, STATE_TYPE> this_type;
14
15
16
17
18

    typedef Eigen::Matrix<this_type, Eigen::Dynamic, 1> Vector;

    typedef std::map<PARENT_TYPE, Vector> expansion_map_type;

19
    struct key_type {
20
21
        /* TODO: parent MUST be const generation_rs* */
        PARENT_TYPE parent;
22
        STATE_TYPE state;
23

24
        key_type(PARENT_TYPE p, STATE_TYPE s) : parent(p), state(s) {}
25
26
        key_type() : parent(0), state(0) {}
        key_type& operator = (const key_type& k) { parent = k.parent; state = k.state; return *this; }
27
        bool operator == (const key_type& other) const { return parent == other.parent && state == other.state; }
28
        bool operator == (PARENT_TYPE x) const { return parent == x; }
29
        bool operator != (const key_type& other) const { return parent != other.parent || state != other.state; }
30
        bool operator != (PARENT_TYPE x) const { return parent != x; }
31
32
33
34
35
36
37
38
        bool operator < (const key_type& other) const { return parent < other.parent || (parent == other.parent && state < other.state); }

        friend std::ostream& operator << (std::ostream& os, const key_type& k)
        {
            return os << '{' << k.parent << ':' << k.state << '}';
        }
    };

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    struct key_list {
        /* not sure about vector. maybe multiset? multimap<generation_rs, size_t>? */
        std::vector<key_type> keys;
        typedef typename std::vector<key_type>::iterator iterator;
        typedef typename std::vector<key_type>::const_iterator const_iterator;
        key_list(key_type&& k) : keys({std::move(k)}) {}
        key_list(const key_type& k) : keys({k}) {}
        key_list(size_t n) : keys(n) {}
        key_list(key_type&& k1, key_type&& k2) : keys({std::move(k1), std::move(k2)}) { std::sort(keys.begin(), keys.end()); }
        key_list(std::initializer_list<key_type>&& kl) : keys(kl) { std::sort(keys.begin(), keys.end()); }
        key_list() : keys() {}

        iterator begin() { return keys.begin(); }
        iterator end() { return keys.end(); }
        const_iterator begin() const { return keys.begin(); }
        const_iterator end() const { return keys.end(); }

        size_t size() const { return keys.size(); }

        bool
            operator == (const key_list& other) const
            {
                auto ai = keys.begin();
                auto aj = keys.end();
                auto bi = other.keys.begin();
                auto bj = other.keys.end();
                if (keys.size() != other.keys.size()) {
                    return false;
                }
                while (ai != aj && bi != bj && *ai == *bi) { ++ai; ++bi; }
                return ai == aj && bi == bj;
            }

        bool
            operator != (const key_list& other) const { return !((*this) == other); }

        bool
            operator < (const key_list& other) const
            {
                auto ai = keys.begin();
                auto aj = keys.end();
                auto bi = other.keys.begin();
                auto bj = other.keys.end();
                while (ai != aj && bi != bj && *ai == *bi) { ++ai; ++bi; }
                if (ai == aj) {
                    return bi != bj;
                } else {
                    if (bi == bj) {
                        return false;
                    }
                    return *ai < *bi;
                }
            }

        friend
            std::ostream&
            operator << (std::ostream& os, const key_list& kl)
            {
                auto ai = kl.keys.begin();
                auto aj = kl.keys.end();
                if (ai != aj) {
                    os << (*ai);
                    for(++ai; ai != aj; ++ai) {
                        os << '*' << (*ai);
                    }
                }
                return os;
            }

        friend
            key_list
            operator + (const key_list& k1, const key_list& k2)
            {
                key_list ret(k1.keys.size() + k2.keys.size());
                auto it = std::set_union(k1.keys.begin(), k1.keys.end(), k2.keys.begin(), k2.keys.end(), ret.keys.begin());
                ret.keys.resize(it - ret.keys.begin());
                return ret;
            }

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        friend
            key_list
            operator - (const key_list& k1, const key_type& k_remove)
            {
                key_list ret;
                ret.keys.reserve(k1.keys.size() - 1);
                for (const auto& k: k1) {
                    if (k != k_remove) {
                        ret.keys.push_back(k);
                    }
                }
                return ret;
            }

        friend
            key_list
            operator % (const key_list& k1, const std::vector<PARENT_TYPE>& keep)
            {
                key_list ret;
                ret.keys.reserve(keep.size());
                for (const auto& k: k1) {
                    if (std::find(keep.begin(), keep.end(), k.parent) != keep.end()) {
                        ret.keys.push_back(k);
                    }
                }
                return ret;
            }

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        friend
            key_list
            operator / (const key_list& k1, const std::vector<PARENT_TYPE>& discard)
            {
                key_list ret;
                ret.keys.reserve(k1.keys.size() - discard.size());
                auto ki = k1.begin();
                auto kj = k1.end();
                auto di = discard.begin();
                auto dj = discard.end();
                for (; ki != kj && di != dj; ++ki) {
                    if (ki->parent == *di) {
                        ++di;
                    } else if (ki->parent > *di) {
                        ++di;
                    } else {
                        ret.keys.push_back(*ki);
                    }
                }
                ret.keys.insert(ret.keys.end(), ki, kj);
                return ret;
            }

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
        std::pair<key_list, key_list>
            extract(const std::vector<PARENT_TYPE>& parents) const
            {
                std::pair<key_list, key_list> ret;
                ret.first.keys.reserve(parents.size());
                ret.second.keys.reserve(keys.size() - parents.size());
                for (const auto& k: keys) {
                    if (std::find(parents.begin(), parents.end(), k.parent) != parents.end()) {
                        ret.first.keys.push_back(k);
                    } else {
                        ret.second.keys.push_back(k);
                    }
                }
                return ret;
            }

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        bool
            is_consistent() const
            {
                auto i1 = keys.begin(), i2 = i1, j = keys.end();
                for (; i1 != j; ++i1) {
                    i2 = i1;
                    for (++i2; i2 != j && i2->parent == i1->parent && i2->state == i1->state; ++i2);
                    if (i2 != j && i2->parent == i1->parent) {
                        MSG_DEBUG("INCONSISTENT KEYS " << keys);
                        return false;
                    }
                }
                return true;
            }
    };

201
    struct element_type {
202
        key_list keys;
203
        double coef;
204
205
206
207
208
209
210
        element_type(const key_type& k1, double c) : keys({k1}), coef(c) {}
        element_type(const key_list& k, double c) : keys(k), coef(c) {}
        element_type(key_list&& k, double c) : keys(std::move(k)), coef(c) {}
        element_type(const key_type& k1, const key_type& k2, double c) : keys({k1, k2}), coef(c) {}
        element_type(element_type&& e) : keys(std::move(e.keys)), coef(e.coef) {}
        element_type(const element_type& e) : keys(e.keys), coef(e.coef) {}
        element_type(double c) : keys(), coef(c) {}
211
        element_type() : keys(), coef(1) {}
212
213
        element_type& operator = (const element_type& e) { keys = e.keys; coef = e.coef; return *this; }
        element_type& operator = (element_type&& e) { keys = std::move(e.keys); coef = e.coef; return *this; }
Damien Leroux's avatar
Damien Leroux committed
214
215
        bool operator == (const element_type& other) const { return keys == other.keys && fabs(coef - other.coef) < _EPSILON; }
        bool operator != (const element_type& other) const { return keys != other.keys || coef != other.coef; }
216
217
218
219
220
221
222
223
224
225
226
        bool operator < (const element_type& other) const { return keys < other.keys; }

        friend
            std::ostream& operator << (std::ostream& os, const element_type& e)
            {
                os << e.coef;
                if (e.keys.keys.size()) {
                    os << '*' << e.keys;
                }
                return os;
            }
227

228
229
230
231
232
233
        friend
            element_type operator + (const element_type& e1, double coef)
            {
                return {e1.keys, e1.coef + coef};
            }

234
235
236
237
238
239
        friend
            element_type operator * (const element_type& e1, double coef)
            {
                return {e1.keys, e1.coef * coef};
            }

240
241
242
243
244
245
246
247
248
249
250
        friend
            element_type operator * (const element_type& e1, const element_type& e2)
            {
                element_type ret(e1.keys + e2.keys, e1.coef * e2.coef);
                /*if (!ret.keys.is_consistent()) {*/
                    /*ret.keys.keys.clear();*/
                    /*ret.coef = 0;*/
                /*}*/
                return ret;
            }

251
252
253
254
255
256
257
        std::pair<key_list, element_type>
            extract(const std::vector<PARENT_TYPE>& parents) const
            {
                auto kk = keys.extract(parents);
                return {kk.first, {kk.second, coef}};
            }

258
        bool is_consistent() const
259
        {
260
261
262
            return keys.is_consistent();
        }

263
        this_type operator | (const std::map<PARENT_TYPE, Vector>& expansions) const
264
265
266
267
268
269
270
271
272
        {
            MSG_DEBUG("================================================");
            MSG_DEBUG("expanding " << (*this));
            std::vector<key_type> common_parents;
            common_parents.reserve(keys.size());
            for (const auto& k: keys) {
                if (expansions.find(k.parent) != expansions.end()) {
                    common_parents.push_back(k);
                }
273
            }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
            MSG_DEBUG("common parents " << common_parents);
            key_list tmp_key(keys.size() - common_parents.size());
            /*auto it = keys.begin();*/
            /*for (const auto& k: common_parents) {*/
                /*while (it->parent != k.parent) {*/
                    /*tmp_key.keys.push_back(k);*/
                    /*++it;*/
                /*}*/
            /*}*/
            std::set_difference(keys.begin(), keys.end(), common_parents.begin(), common_parents.end(), tmp_key.begin());
            this_type ret;
            ret.m_combination.emplace_back(tmp_key, coef);
            MSG_DEBUG("tmp_key " << tmp_key);
            for (const auto& p: common_parents) {
                const auto& lc = expansions.find(p.parent)->second(p.state);
                MSG_DEBUG("lc = " << lc);
                ret *= lc;
                /*this_type tmp;*/
                /*for (const auto& e: lc) {*/
                    /*tmp.m_combination.emplace_back(tmp_key + e.keys, coef * e.coef);*/
                /*}*/
                /*ret *= tmp;*/
296
            }
297
298
299
            std::sort(ret.begin(), ret.end());
            MSG_DEBUG("expansion " << ret);
            return ret;
300
301
302
        }
    };

303
304
305
    typedef std::vector<element_type> sum_type;
    typedef typename std::vector<element_type>::iterator sum_iterator_type;
    typedef typename std::vector<element_type>::const_iterator sum_const_iterator_type;
306
307
308

    sum_type m_combination;

309
    friend std::ostream& operator << (std::ostream& os, const combination_type<PARENT_TYPE, STATE_TYPE>& c)
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    {
        auto i = c.begin();
        auto j = c.end();
        if (i != j) {
            os << (*i++);
            for (; i != j; ++i) {
                os << " + " << (*i);
            }
        }
        return os;
    }

    combination_type()
        : m_combination()
    {}

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
    combination_type(const this_type& other)
        : m_combination(other.m_combination)
    {}

    combination_type(this_type&& other)
        : m_combination(std::move(other.m_combination))
    {}

    this_type&
        operator = (const this_type& other)
        {
            m_combination = other.m_combination;
            return *this;
        }

    this_type&
        operator = (this_type&& other)
        {
            m_combination = std::move(other.m_combination);
            return *this;
        }

348
    combination_type(PARENT_TYPE par, size_t n_states)
349
350
351
        : m_combination()
    {
        for (size_t i = 0; i < n_states; ++i) {
352
            m_combination.emplace_back(key_type{par, i}, 1.);
353
354
355
356
357
358
        }
    }

    combination_type(double coef)
        : m_combination()
    {
359
        m_combination.emplace_back(coef);
360
361
    }

Damien Leroux's avatar
Damien Leroux committed
362
363
364
365
366
367
    element_type&
        operator [] (size_t i) { return m_combination[i]; }

    const element_type&
        operator [] (size_t i) const { return m_combination[i]; }

368
369
370
371
372
373
    sum_iterator_type begin() { return m_combination.begin(); }
    sum_iterator_type end() { return m_combination.end(); }
    sum_const_iterator_type begin() const { return m_combination.begin(); }
    sum_const_iterator_type end() const { return m_combination.end(); }

    size_t size() const { return m_combination.size(); }
374

375
376
377
    bool not_empty() const { return m_combination.size() > 0; }

    bool is_consistent() const
378
    {
379
380
381
382
383
384
385
386
387
388
389
        for (const auto& e: m_combination) {
            if (!e.is_consistent()) {
                return false;
            }
        }
        return true;
    }

    this_type operator + (const this_type& c) const
    {
        this_type ret;
390
391
392
393
394
395
        auto ai = m_combination.cbegin();
        auto aj = m_combination.cend();
        auto bi = c.m_combination.cbegin();
        auto bj = c.m_combination.cend();
        while (true) {
            if (bi == bj || ai == aj) { break; }
396
397
398
399
400
            /*MSG_DEBUG("a=" << (*ai) << " b=" << (*bi));*/
            /*if (ai->parent == bi->parent && ai->state != bi->state) {
                ret.m_combination = {*ai, *bi};
                break;
            } else*/
Damien Leroux's avatar
Damien Leroux committed
401
402
            if (*ai < *bi) { /*MSG_DEBUG("a<b");*/ if (ai->coef > _EPSILON || ai->coef < -_EPSILON) { ret.m_combination.push_back(*ai); } ++ai; }
            else if (*bi < *ai) { /*MSG_DEBUG("b<a");*/ if (bi->coef > _EPSILON || ai->coef < -_EPSILON) { ret.m_combination.push_back(*bi); } ++bi; }
403
            else {
404
                /*MSG_DEBUG("a=b");*/
Damien Leroux's avatar
Damien Leroux committed
405
406
407
408
                auto tmp = (*ai) + bi->coef;
                if (tmp.coef > _EPSILON || tmp.coef < -_EPSILON) {
                    ret.m_combination.emplace_back(tmp);
                }
409
410
                ++ai;
                ++bi;
411
412
413
414
415
416
417
            }
        }
        for (; ai != aj; ++ai) { if (ai->coef) { ret.m_combination.push_back(*ai); } }
        for (; bi != bj; ++bi) { if (bi->coef) { ret.m_combination.push_back(*bi); } }
        return ret;
    }

418
    this_type operator * (const this_type& c) const
419
    {
420
        this_type ret;
421
422
        for (const auto& e1: *this) {
            for (const auto& e2: c) {
423
424
425
                double c = e1.coef * e2.coef;
                if (!c) { continue; }
                ret.m_combination.emplace_back(e1 * e2);
426
427
428
429
430
            }
        }
        return ret;
    }

431
432
433
434
435
436
437
    this_type& operator += (const element_type& c)
    {
        this_type cc;
        cc.m_combination.emplace_back(c);
        return *this += cc;
    }

438
    this_type& operator += (const this_type& c)
439
    {
440
        this_type tmp = (*this) + c;
441
442
443
        m_combination.swap(tmp.m_combination);
        return *this;
    }
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

    this_type& operator *= (const this_type& c)
    {
        this_type tmp = (*this) * c;
        m_combination.swap(tmp.m_combination);
        return *this;
    }

    this_type
        operator | (const expansion_map_type& expansions) const
        {
            this_type ret;
            /*bool modified;*/
            /*do {*/
                /*modified = false;*/
                for (const auto& e: m_combination) {
                    this_type tmp = e | expansions;
                    /*modified |= (tmp.size() != 1 || tmp.m_combination.front() != e);*/
                    ret += tmp;
                    /*MSG_DEBUG("tmp=" << tmp);*/
                    /*MSG_DEBUG("e=" << e);*/
                    /*MSG_DEBUG("ret=" << ret);*/
                }
                /*MSG_DEBUG("modified=" << modified);*/
            /*} while (modified);*/
            return ret;
        }

    friend
        this_type operator * (const element_type& e, const this_type& lc)
        {
            this_type ret;
            ret.reserve(lc.m_combination.size());
            for (const auto& e2: lc.m_combination) {
                ret.emplace_back(e * e2);
            }
            return ret;
        }

    double apply(const std::map<PARENT_TYPE, VectorXd>& parent_states) const
    {
        double accum = 0;
        for (const auto& e: m_combination) {
            double tmp = e.coef;
            for (const key_type& k: e.keys) {
489
490
                MSG_DEBUG("k.parent=" << k.parent << " k.state=" << k.state);
                MSG_QUEUE_FLUSH();
491
492
493
494
495
496
                tmp *= parent_states.find(k.parent)->second(k.state);
            }
            accum += tmp;
        }
        return accum;
    }
497
498

    bool operator == (const combination_type<PARENT_TYPE, STATE_TYPE>& other) const { return m_combination == other.m_combination; }
499
500
};

501
502
503
504
505
506
507
508
509
510
511
512
template <typename PARENT_TYPE, typename STATE_TYPE>
typename combination_type<PARENT_TYPE, STATE_TYPE>::Vector
operator | (const typename combination_type<PARENT_TYPE, STATE_TYPE>::Vector& V,
            const std::map<PARENT_TYPE, typename combination_type<PARENT_TYPE, STATE_TYPE>::Vector>& expansions)
{
    typename combination_type<PARENT_TYPE, STATE_TYPE>::Vector ret(V.size());
    for (int i = 0; i < V.size(); ++i) {
        ret(i) = V(i) | expansions;
    }
    return ret;
}

513

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654

template <typename PARENT_TYPE, typename STATE_TYPE>
combination_type<PARENT_TYPE, STATE_TYPE>
kronecker(const combination_type<PARENT_TYPE, STATE_TYPE>& c1, const combination_type<PARENT_TYPE, STATE_TYPE>& c2)
{
    typedef typename combination_type<PARENT_TYPE, STATE_TYPE>::key_type key_type;
    typedef typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list key_list;
    typedef typename combination_type<PARENT_TYPE, STATE_TYPE>::element_type element_type;
    combination_type<PARENT_TYPE, STATE_TYPE> ret;

    std::vector<PARENT_TYPE> I1, I2, inter;

    I1.reserve(c1.m_combination.front().keys.size());
    I2.reserve(c2.m_combination.front().keys.size());
    inter.resize(std::min(c1.m_combination.front().keys.size(), c2.m_combination.front().keys.size()));

    /* compute I1, I2, and I1 n I2 */
    for (const auto& k: c1.m_combination.front().keys) { I1.push_back(k.parent); }
    for (const auto& k: c2.m_combination.front().keys) { I2.push_back(k.parent); }
    auto end = std::set_intersection(I1.begin(), I1.end(), I2.begin(), I2.end(), inter.begin());
    inter.resize(end - inter.begin());

    /* precompute kronecker_delta ... */
    std::map<key_list, std::pair<std::vector<element_type>, std::vector<element_type>>> sub_kron;

    /* ... on c1 */
    for (const auto& e: c1.m_combination) {
        key_list common_key;
        element_type sub(e.coef);
        sub.keys.keys.reserve(e.keys.keys.size() - inter.size());
        common_key.keys.reserve(inter.size());
        for (const auto& k: e.keys.keys) {
            if (std::find(inter.begin(), inter.end(), k.parent) != inter.end()) {
                common_key.keys.emplace_back(k);
            } else {
                sub.keys.keys.emplace_back(k);
            }
        }
        sub_kron[common_key].first.emplace_back(sub);
    }

    /* ... on c2 */
    for (const auto& e: c2.m_combination) {
        key_list common_key;
        element_type sub(e.coef);
        sub.keys.keys.reserve(e.keys.keys.size() - inter.size());
        common_key.keys.reserve(inter.size());
        for (const auto& k: e.keys.keys) {
            if (std::find(inter.begin(), inter.end(), k.parent) != inter.end()) {
                common_key.keys.emplace_back(k);
            } else {
                sub.keys.keys.emplace_back(k);
            }
        }
        sub_kron[common_key].second.emplace_back(sub);
    }

    size_t total_size = 0;

    for (const auto& kv: sub_kron) {
        total_size += kv.second.first.size() * kv.second.second.size();
    }

    /* perform kronecker per subset */

    ret.m_combination.reserve(total_size);
    for (const auto& kv: sub_kron) {
        element_type e0(1.);
        e0.keys = kv.first;
        for (const auto& e1: kv.second.first) {
            for (const auto& e2: kv.second.second) {
                /*ret.m_combination.emplace_back(e0 * e1 * e2);*/
                ret += e0 * e1 * e2;
            }
        }
    }

    std::sort(ret.begin(), ret.end());

    return ret;
}


template <typename PARENT_TYPE, typename STATE_TYPE>
std::vector<PARENT_TYPE>
get_parents(const combination_type<PARENT_TYPE, STATE_TYPE>& comb)
{
    std::vector<PARENT_TYPE> ret;
    for (const auto& k: comb.begin()->keys) {
        ret.push_back(k.parent);
    }
    return ret;
}


template <typename PARENT_TYPE, typename STATE_TYPE>
combination_type<PARENT_TYPE, STATE_TYPE>
state_to_combination(PARENT_TYPE parent, const std::vector<STATE_TYPE>& states)
{
    typedef typename combination_type<PARENT_TYPE, STATE_TYPE>::key_type key_type;
    combination_type<PARENT_TYPE, STATE_TYPE> ret;
    ret.m_combination.reserve(states.size());
    for (const auto& s: states) {
        ret.m_combination.emplace_back(key_type{parent, s}, 1.);
    }
    return ret;
}


template <typename PARENT_TYPE, typename STATE_TYPE>
combination_type<PARENT_TYPE, STATE_TYPE>
hadamard(const combination_type<PARENT_TYPE, STATE_TYPE>& c1, const combination_type<PARENT_TYPE, STATE_TYPE>& c2)
{
    auto i1 = c1.m_combination.begin();
    auto i2 = c2.m_combination.begin();
    auto j = c1.m_combination.end();
    /* assume c1.size() == c2.size() */
    combination_type<PARENT_TYPE, STATE_TYPE> ret;
    ret.m_combination.reserve(c1.m_combination.size());
    for (; i1 != j; ++i1, ++i2) {
        ret.m_combination.emplace_back((*i1) * (*i2));
    }
    return ret;
}


template <typename PARENT_TYPE, typename STATE_TYPE>
std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, combination_type<PARENT_TYPE, STATE_TYPE>>
sum_over(const combination_type<PARENT_TYPE, STATE_TYPE>& comb, const std::vector<PARENT_TYPE>& parents)
{
    std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, combination_type<PARENT_TYPE, STATE_TYPE>> ret;

    for (const auto& e: comb) {
        auto ke = e.extract(parents);
        ret[ke.first] += ke.second;
    }

    return ret;
}


Damien Leroux's avatar
Damien Leroux committed
655
656
657
658
659
660
661
662
663
664
665
template <typename PARENT_TYPE, typename STATE_TYPE>
combination_type<PARENT_TYPE, STATE_TYPE>
normalize_over(const combination_type<PARENT_TYPE, STATE_TYPE>& comb, const std::vector<PARENT_TYPE>& variables)
{
    typedef typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list key_list;
    combination_type<PARENT_TYPE, STATE_TYPE> ret;
    std::map<key_list, double> norm_coef;
    for (const auto& e: comb) {
        auto k = e.keys % variables;
        norm_coef[k] += e.coef;
    }
666
    /*MSG_DEBUG("NORM FACTORS: " << norm_coef);*/
Damien Leroux's avatar
Damien Leroux committed
667
668
669
670
671
672
673
674
675
676
    for (auto& kv: norm_coef) { kv.second = 1. / kv.second; }
    for (const auto& e: comb) {
        auto k = e.keys % variables;
        ret.m_combination.emplace_back(e.keys, e.coef * norm_coef[k]);
    }
    return ret;
}



677
678
679
680
681
682
683
684
template <typename PARENT_TYPE, typename STATE_TYPE>
std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, combination_type<PARENT_TYPE, STATE_TYPE>>
sum_over_dual(const combination_type<PARENT_TYPE, STATE_TYPE>& comb, const std::vector<PARENT_TYPE>& parents)
{
    std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, combination_type<PARENT_TYPE, STATE_TYPE>> ret;

    for (const auto& e: comb) {
        auto ke = e.extract(parents);
Damien Leroux's avatar
Damien Leroux committed
685
686
        /*ret[ke.first] += ke.second;*/
        ret[ke.second.keys] += {ke.first, ke.second.coef};
687
688
    }

689
    /*MSG_DEBUG("sum_over_dual(" << comb << ", " << parents << ") => " << ret);*/
Damien Leroux's avatar
Damien Leroux committed
690

691
692
693
694
    return ret;
}


Damien Leroux's avatar
Damien Leroux committed
695
#if 0
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
template <typename PARENT_TYPE, typename STATE_TYPE>
std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, combination_type<PARENT_TYPE, STATE_TYPE>>
sum_over_and_normalize(const combination_type<PARENT_TYPE, STATE_TYPE>& comb, const std::vector<PARENT_TYPE>& parents)
{
    std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, combination_type<PARENT_TYPE, STATE_TYPE>> ret;

    std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, double> norm;

    for (const auto& e: comb) {
        auto ke = e.extract(parents);
        norm[ke.second.keys] += e.coef;
    }

    for (auto& kv: norm) { kv.second = 1. / kv.second; }

    for (const auto& e: comb) {
        auto ke = e.extract(parents);
        ret[ke.first] += ke.second * norm[ke.second.keys];
    }

    return ret;
}
Damien Leroux's avatar
Damien Leroux committed
718
#endif
719
720
721
722
723
724
725
726
727
728
729
730

template <typename PARENT_TYPE, typename STATE_TYPE>
combination_type<PARENT_TYPE, STATE_TYPE>
fold(const std::map<typename combination_type<PARENT_TYPE, STATE_TYPE>::key_list, combination_type<PARENT_TYPE, STATE_TYPE>>& comb_map)
{
    combination_type<PARENT_TYPE, STATE_TYPE> ret;
    for (const auto& kv: comb_map) {
        ret += kv.second;
    }
    return ret;
}

Damien Leroux's avatar
Damien Leroux committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

template <typename PARENT_TYPE, typename STATE_TYPE>
combination_type<PARENT_TYPE, STATE_TYPE>
project(const combination_type<PARENT_TYPE, STATE_TYPE>& comb, const std::vector<PARENT_TYPE>& project_variables, const std::vector<PARENT_TYPE>& norm_variables)
{
    if (norm_variables.size()) {
        return fold(sum_over_dual(normalize_over(comb, norm_variables), project_variables));
    } else {
        return fold(sum_over_dual(comb, project_variables));
    }
}


template <typename PARENT_TYPE, typename STATE_TYPE>
combination_type<PARENT_TYPE, STATE_TYPE>
project_dual(const combination_type<PARENT_TYPE, STATE_TYPE>& comb, const std::vector<PARENT_TYPE>& project_variables, const std::vector<PARENT_TYPE>& norm_variables)
{
    return fold(sum_over_dual(normalize_over(comb, norm_variables), project_variables));
}


752
namespace Eigen {
753
754
755
756
757
    template<typename PARENT_TYPE>
    struct NumTraits<combination_type<PARENT_TYPE>> {
        typedef combination_type<PARENT_TYPE> Real;
        typedef combination_type<PARENT_TYPE> NonInteger;
        typedef combination_type<PARENT_TYPE> Nested;
758
759
760
761
762
763
764
765
766
767
768
769
        enum {
            IsComplex = 0,
            IsInteger = 1,
            ReadCost = 10,
            WriteCost = 10,
            MulCost = 100,
            AddCost = 1000,
            IsSigned = 0,
            RequireInitialization = 1
        };
        static Real epsilon() { return {0.}; }
        static Real dummy_precision() { return {0.}; }
770
771
        static combination_type<PARENT_TYPE> highest() { return {1.e308}; }
        static combination_type<PARENT_TYPE> lowest() { return {0.}; }
772
773
774
    };
}

775

776
/*namespace impl { struct generation_rs; }*/
777
778
779
780
781

typedef combination_type<size_t> gencomb_type;


typedef gencomb_type::Vector VectorLC;
782
783

inline
784
VectorLC
785
786
make_parent_comb(size_t par, size_t n_states)
{
787
    VectorLC ret(n_states);
788
    for (size_t i = 0; i < n_states; ++i) {
789
        ret(i, 0).m_combination.emplace_back(gencomb_type::key_type{par, i}, 1.);
790
    }
791
792
    /*MSG_DEBUG("make_parent_comb");*/
    /*MSG_DEBUG(ret);*/
793
794
795
    return ret;
}

796
797
798
799
800
801
802
803
804
805
806
807
808
inline
std::vector<gencomb_type>
make_parent_comb_vec(size_t par, size_t n_states)
{
    std::vector<gencomb_type> ret(n_states);
    for (size_t i = 0; i < n_states; ++i) {
        ret[i].m_combination.emplace_back(gencomb_type::key_type{par, i}, 1.);
    }
    /*MSG_DEBUG("make_parent_comb");*/
    /*MSG_DEBUG(ret);*/
    return ret;
}

809
810
811
812
/*inline*/
/*VectorLC*/
/*expand(const VectorLC& V, */

813
814
#endif