phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
internal.h
Go to the documentation of this file.
1#ifndef _PHYLOPOMP_INTERNAL_H_
2#define _PHYLOPOMP_INTERNAL_H_
3
4#define R_NO_REMAP
5
6#include <R.h>
7#include <Rmath.h>
8#include <Rdefines.h>
9
10#ifdef __cplusplus
11#include <cassert>
12#else
13#include <assert.h>
14#endif
15
16#ifndef STANDALONE
17
18#define err(...) Rf_errorcall(R_NilValue,__VA_ARGS__)
19#define warn(...) Rf_warningcall(R_NilValue,__VA_ARGS__)
20#define rprint(S) Rprintf("%s\n",(S).c_str())
21
22#else
23
24#include <iostream>
25#include <cstdio>
26#define err(...) {printf(__VA_ARGS__); printf("\n"); exit(-1);}
27#define warn(...) {printf(__VA_ARGS__); printf("\n");}
28#define rprint(S) printf("%s\n",(S).c_str())
29
30#endif
31
32#ifdef __cplusplus
33
34#include <string>
35#include <cstring>
36
37typedef std::string string_t;
38
39#endif
40
41#ifdef __cplusplus
42
43#define mkChar Rf_mkChar
44#define mkString Rf_mkString
45#define ScalarInteger Rf_ScalarInteger
46#define ScalarReal Rf_ScalarReal
47#define install Rf_install
48#define isNull Rf_isNull
49
50#endif
51
52typedef Rbyte raw_t;
53typedef double slate_t;
54typedef size_t name_t;
55
56// interface with R's integer RNG
57static inline int random_integer (int n) {
58 return (int) floor(R_unif_index((double) n));
59}
60
61// select n of the first N integers at random
62static inline void random_sample_wo_repl (int *samples, int N, int n) {
63 int k = 0; // total input records dealt with
64 int m = 0; // number of items selected so far
65 while (m < n && k < N) {
66 int u = random_integer(N-k);
67 if (u < n-m) {
68 samples[m++] = k;
69 }
70 k++;
71 }
72}
73
74// helper function for filling a return list
75static inline int set_list_elem
76(
77 SEXP list, SEXP names, SEXP element,
78 const char *name, int pos
79 ) {
80 SET_ELEMENT(list,pos,element);
81 SET_STRING_ELT(names,pos,Rf_mkChar(name));
82 return ++pos;
83}
84
85static inline int rcateg (double erate, double *rate, int nrate) {
86 double u = erate*unif_rand();
87 int e = 0;
88 while (u > rate[e] && e < nrate) {
89 if (rate[e] < 0)
90 err("in '%s': invalid rate rate[%d]=%lg",__func__,e,rate[e]); // #nocov
91 u -= rate[e++];
92 }
93 assert(e!=nrate); // #nocov
94 return e;
95}
96
97#endif
Rbyte raw_t
Definition internal.h:52
static int rcateg(double erate, double *rate, int nrate)
Definition internal.h:85
static int set_list_elem(SEXP list, SEXP names, SEXP element, const char *name, int pos)
Definition internal.h:76
size_t name_t
Definition internal.h:54
static int random_integer(int n)
Definition internal.h:57
#define err(...)
Definition internal.h:18
static void random_sample_wo_repl(int *samples, int N, int n)
Definition internal.h:62
double slate_t
Definition internal.h:53
#define n
Definition lbdp_pomp.c:9
#define N
Definition seirs_pomp.c:36
static const int nrate
Definition seirs_pomp.c:7