phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
generics.h
Go to the documentation of this file.
1 #ifndef _GENERICS_H_
2 #define _GENERICS_H_
3 
4 #include "internal.h"
5 
6 template <class TYPE>
7 SEXP ndeme (TYPE& X) {
8  return ScalarInteger(X.ndeme());
9 }
10 
11 template <class TYPE>
12 SEXP nsample (TYPE& X) {
13  return ScalarInteger(X.nsample());
14 }
15 
16 template <class TYPE>
17 SEXP timezero (TYPE& X) {
18  return ScalarReal(X.timezero());
19 }
20 
21 template <class TYPE>
22 SEXP time (TYPE& X) {
23  return ScalarReal(X.time());
24 }
25 
27 template <class TYPE>
28 SEXP serial (const TYPE& X) {
29  SEXP out;
30  PROTECT(out = NEW_RAW(X.bytesize()));
31  X >> RAW(out);
32  UNPROTECT(1);
33  return out;
34 }
35 
37 template <class TYPE>
38 SEXP yaml (const TYPE& X) {
39  return mkString(X.yaml().c_str());
40 }
41 
43 template <class TYPE>
44 SEXP describe (const TYPE& X) {
45  return mkString(X.describe().c_str());
46 }
47 
49 template <class TYPE>
50 SEXP structure (const TYPE& X) {
51  return X.structure();
52 }
53 
55 template <class TYPE>
56 SEXP newick (const TYPE& X) {
57  return mkString(X.newick().c_str());
58 }
59 
61 template <class TYPE>
62 SEXP lineage_count (const TYPE& G) {
63  return G.lineage_count();
64 }
65 
67 template <class TYPE>
68 SEXP gendat (const TYPE& G) {
69  return G.gendat();
70 }
71 
73 template<class TYPE>
74 SEXP make (SEXP Params, SEXP IVPs, SEXP T0) {
75  SEXP o;
76  PROTECT(Params = AS_NUMERIC(Params));
77  PROTECT(IVPs = AS_NUMERIC(IVPs));
78  PROTECT(T0 = AS_NUMERIC(T0));
79  GetRNGstate();
80  TYPE X = *REAL(T0);
81  X.update_params(REAL(Params),LENGTH(Params));
82  X.update_IVPs(REAL(IVPs),LENGTH(IVPs));
83  X.rinit();
84  X.update_clocks();
85  PutRNGstate();
86  PROTECT(o = serial(X));
87  UNPROTECT(4);
88  return o;
89 }
90 
92 template<class TYPE>
93 SEXP revive (SEXP State, SEXP Params) {
94  SEXP o;
95  TYPE X = State;
96  PROTECT(Params = AS_NUMERIC(Params));
97  X.update_params(REAL(Params),LENGTH(Params));
98  PROTECT(o = serial(X));
99  UNPROTECT(2);
100  return o;
101 }
102 
104 template<class TYPE>
105 SEXP run (SEXP State, SEXP Tout) {
106  SEXP out;
107  TYPE X = State;
108  PROTECT(Tout = AS_NUMERIC(Tout));
109  GetRNGstate();
110  X.valid();
111  X.play(*REAL(Tout));
112  PutRNGstate();
113  PROTECT(out = serial(X));
114  UNPROTECT(2);
115  return out;
116 }
117 
119 template <class TYPE>
120 SEXP genealogy (SEXP State) {
121  TYPE A = State;
122  SEXP out;
123  PROTECT(out = serial(A.geneal));
124  SET_ATTR(out,install("class"),mkString("gpgen"));
125  UNPROTECT(1);
126  return out;
127 }
128 
129 #define MAKEFN(X,TYPE) SEXP make ## X (SEXP Params, SEXP IVPs, SEXP T0) { \
130  return make<TYPE>(Params,IVPs,T0); \
131  } \
132 
133 #define REVIVEFN(X,TYPE) SEXP revive ## X (SEXP State, SEXP Params) { \
134  return revive<TYPE>(State,Params); \
135  } \
136 
137 #define RUNFN(X,TYPE) SEXP run ## X (SEXP State, SEXP Times) { \
138  return run<TYPE>(State,Times); \
139  } \
140 
141 #define GENEALFN(X,TYPE) SEXP geneal ## X (SEXP State) { \
142  return genealogy<TYPE>(State); \
143  } \
144 
145 #define YAMLFN(X,TYPE) SEXP yaml ## X (SEXP State) { \
146  return yaml<TYPE>(State); \
147  } \
148 
149 #define GENERICS(X,TYPE) \
150  extern "C" { \
151  \
152  MAKEFN(X,TYPE) \
153  \
154  REVIVEFN(X,TYPE) \
155  \
156  RUNFN(X,TYPE) \
157  \
158  GENEALFN(X,TYPE) \
159  \
160  YAMLFN(X,TYPE) \
161  \
162  } \
163 
164 #endif
SEXP ndeme(TYPE &X)
Definition: generics.h:7
SEXP run(SEXP State, SEXP Tout)
run simulations
Definition: generics.h:105
SEXP revive(SEXP State, SEXP Params)
refresh parameters
Definition: generics.h:93
SEXP describe(const TYPE &X)
human readable output
Definition: generics.h:44
SEXP timezero(TYPE &X)
Definition: generics.h:17
SEXP time(TYPE &X)
Definition: generics.h:22
SEXP structure(const TYPE &X)
structure in R list format
Definition: generics.h:50
SEXP newick(const TYPE &X)
tree in newick format
Definition: generics.h:56
SEXP serial(const TYPE &X)
binary serialization
Definition: generics.h:28
SEXP genealogy(SEXP State)
extract the bare genealogy
Definition: generics.h:120
SEXP gendat(const TYPE &G)
data-frame format
Definition: generics.h:68
SEXP yaml(const TYPE &X)
human/machine readable output
Definition: generics.h:38
SEXP lineage_count(const TYPE &G)
number of lineages through time
Definition: generics.h:62
SEXP make(SEXP Params, SEXP IVPs, SEXP T0)
initialization
Definition: generics.h:74
SEXP nsample(TYPE &X)
Definition: generics.h:12