phylopomp
Phylodynamics for POMPs
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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#define mkChar Rf_mkChar
35#define mkString Rf_mkString
36#define ScalarInteger Rf_ScalarInteger
37#define ScalarReal Rf_ScalarReal
38#define install Rf_install
39#define isNull Rf_isNull
40
41#endif
42
43typedef Rbyte raw_t;
44typedef double slate_t;
45typedef size_t name_t;
46
47// interface with R's integer RNG
48static inline int random_integer (int n) {
49 return (int) floor(R_unif_index((double) n));
50}
51
52// select n of the first N integers at random
53static inline void random_sample_wo_repl (int *samples, int N, int n) {
54 int k = 0; // total input records dealt with
55 int m = 0; // number of items selected so far
56 while (m < n && k < N) {
57 int u = random_integer(N-k);
58 if (u < n-m) {
59 samples[m++] = k;
60 }
61 k++;
62 }
63}
64
65// helper function for filling a return list
66static inline int set_list_elem
67(
68 SEXP list, SEXP names, SEXP element,
69 const char *name, int pos
70 ) {
71 SET_ELEMENT(list,pos,element);
72 SET_STRING_ELT(names,pos,Rf_mkChar(name));
73 return ++pos;
74}
75
76static inline int rcateg (double erate, double *rate, int nrate) {
77 double u = erate*unif_rand();
78 int e = 0;
79 while (u > rate[e] && e < nrate) {
80 if (rate[e] < 0)
81 err("in '%s': invalid rate rate[%d]=%lg",__func__,e,rate[e]); // #nocov
82 u -= rate[e++];
83 }
84 assert(e!=nrate); // #nocov
85 return e;
86}
87
88#endif
Rbyte raw_t
Definition internal.h:43
static int rcateg(double erate, double *rate, int nrate)
Definition internal.h:76
static int set_list_elem(SEXP list, SEXP names, SEXP element, const char *name, int pos)
Definition internal.h:67
size_t name_t
Definition internal.h:45
static int random_integer(int n)
Definition internal.h:48
#define err(...)
Definition internal.h:18
static void random_sample_wo_repl(int *samples, int N, int n)
Definition internal.h:53
double slate_t
Definition internal.h:44
#define n
Definition lbdp_pomp.c:8
#define N
Definition seirs_pomp.c:32
static const int nrate
Definition seirs_pomp.c:4