Fixed SWIG octave wrappers and added octave port of example_sprinkler
[libdai.git] / swig / dai.i
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2009 Patrick Pletscher [pletscher at inf dot ethz dot ch]
8 * 2009 Joris Mooij [joris dot mooij at libdai dot org]
9 */
10
11
12 %module dai
13
14 struct Neighbor {
15 size_t iter;
16 size_t node;
17 size_t dual;
18
19 Neighbor() {}
20 Neighbor( size_t iter, size_t node, size_t dual ) : iter(iter), node(node), dual(dual) {}
21
22 operator size_t () const { return node; }
23 };
24
25 %{
26 #include "../include/dai/var.h"
27 #include "../include/dai/smallset.h"
28 #include "../include/dai/varset.h"
29 #include "../include/dai/prob.h"
30 #include "../include/dai/factor.h"
31 #include "../include/dai/bipgraph.h"
32 #include "../include/dai/factorgraph.h"
33 #include "../include/dai/util.h"
34 %}
35
36 %ignore dai::TProb::operator[];
37 %ignore dai::TFactor::operator[];
38
39 %include "../include/dai/util.h"
40 %include "../include/dai/var.h"
41 %include "../include/dai/smallset.h"
42 %template(SmallSetVar) dai::SmallSet< dai::Var >;
43 %include "../include/dai/varset.h"
44 %extend dai::VarSet {
45 inline void append(const dai::Var &v) { (*self) |= v; } /* for python, octave */
46 };
47
48 %include "../include/dai/prob.h"
49 %template(Prob) dai::TProb<Real>;
50 %extend dai::TProb<Real> {
51 inline Real __getitem__(int i) const {return (*self)[i];} /* for python */
52 inline void __setitem__(int i,Real d) {(*self)[i] = d;} /* for python */
53 inline Real __paren(int i) const {return (*self)[i];} /* for octave */
54 inline void __paren_asgn(int i,Real d) {(*self)[i] = d;} /* for octave */
55 };
56 %include "../include/dai/factor.h"
57 %extend dai::TFactor<Real> {
58 inline Real __getitem__(int i) const {return (*self)[i];} /* for python */
59 inline void __setitem__(int i,Real d) {(*self)[i] = d;} /* for python */
60 inline Real __paren(int i) const {return (*self)[i];} /* for octave */
61 inline void __paren_asgn(int i,Real d) {(*self)[i] = d;} /* for octave */
62 };
63
64 %template(Factor) dai::TFactor<Real>;
65 %include "../include/dai/bipgraph.h"
66 %include "../include/dai/factorgraph.h"
67 %include "std_vector.i"
68 // TODO: typemaps for the vectors (input/output python arrays)
69 %inline{
70 typedef std::vector<dai::Factor> VecFactor;
71 typedef std::vector< VecFactor > VecVecFactor;
72 }
73 %template(VecFactor) std::vector< dai::Factor >;
74 %template(VecVecFactor) std::vector< VecFactor >;
75
76 %{
77 typedef dai::BipartiteGraph::Neighbor Neighbor;
78 %}
79
80 %include "../include/dai/index.h"
81 %extend dai::MultiFor {
82 inline size_t __getitem__(int i) const {
83 return (*self)[i];
84 }
85 inline void next() {
86 return (*self)++;
87 }
88 };