TMB Documentation  v1.9.11
Vectorize.hpp
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 Kasper Kristensen
2 // License: GPL-2
3 
8 // Function body type declarations
9 // V=vector, T=scalar, I=integer, N=none
10 #define declareV(arg) const vector<Type> &arg
11 #define declareT(arg) Type arg
12 #define declareI(arg) int arg
13 #define declareN(arg)
14 // How to extract elementwise subset of the four types
15 #define elementV(arg,i) arg[i]
16 #define elementT(arg,i) arg
17 #define elementI(arg,i) arg
18 #define elementN(arg,i)
19 // How to place comma in front of the types
20 #define commaV ,
21 #define commaT ,
22 #define commaI ,
23 #define commaN
24 // Update output vector size
25 #define outputsizeV(n,arg) n = (arg.size()>n ? arg.size() : n)
26 #define outputsizeT(n,arg)
27 #define outputsizeI(n,arg)
28 #define outputsizeN(n,arg)
29 
36 #define GVECTORIZE(FUN,Type1,Type2,Type3,Type4,Type5,Type6) \
37 template <class Type> \
38 vector<Type> FUN( declare##Type1(arg1) comma##Type2 \
39  declare##Type2(arg2) comma##Type3 \
40  declare##Type3(arg3) comma##Type4 \
41  declare##Type4(arg4) comma##Type5 \
42  declare##Type5(arg5) comma##Type6 \
43  declare##Type6(arg6) ) \
44 { \
45  int n = 0; \
46  outputsize##Type1(n,arg1); \
47  outputsize##Type2(n,arg2); \
48  outputsize##Type3(n,arg3); \
49  outputsize##Type4(n,arg4); \
50  outputsize##Type5(n,arg5); \
51  outputsize##Type6(n,arg6); \
52  vector<Type> res(n); \
53  for(int i=0;i<n;i++) res[i] = FUN( element##Type1(arg1,i) comma##Type2 \
54  element##Type2(arg2,i) comma##Type3 \
55  element##Type3(arg3,i) comma##Type4 \
56  element##Type4(arg4,i) comma##Type5 \
57  element##Type5(arg5,i) comma##Type6 \
58  element##Type6(arg6,i) ); \
59  return res; \
60 }
61 
63 #define VECTORIZE1_t(FUN) \
64  GVECTORIZE(FUN,V,N,N,N,N,N)
65 
71 #define VECTORIZE2_tt(FUN) \
72  GVECTORIZE(FUN,V,T,N,N,N,N) \
73  GVECTORIZE(FUN,T,V,N,N,N,N) \
74  GVECTORIZE(FUN,V,V,N,N,N,N)
75 
81 #define VECTORIZE3_tti(FUN) \
82  GVECTORIZE(FUN,V,T,I,N,N,N) \
83  GVECTORIZE(FUN,T,V,I,N,N,N) \
84  GVECTORIZE(FUN,V,V,I,N,N,N)
85 
91 #define VECTORIZE3_ttt(FUN) \
92  GVECTORIZE(FUN,V,T,T,N,N,N) \
93  GVECTORIZE(FUN,T,V,T,N,N,N) \
94  GVECTORIZE(FUN,T,T,V,N,N,N) \
95  GVECTORIZE(FUN,V,V,T,N,N,N) \
96  GVECTORIZE(FUN,T,V,V,N,N,N) \
97  GVECTORIZE(FUN,V,T,V,N,N,N) \
98  GVECTORIZE(FUN,V,V,V,N,N,N)
99 
105 #define VECTORIZE4_ttti(FUN) \
106  GVECTORIZE(FUN,V,T,T,I,N,N) \
107  GVECTORIZE(FUN,T,V,T,I,N,N) \
108  GVECTORIZE(FUN,T,T,V,I,N,N) \
109  GVECTORIZE(FUN,V,V,T,I,N,N) \
110  GVECTORIZE(FUN,T,V,V,I,N,N) \
111  GVECTORIZE(FUN,V,T,V,I,N,N) \
112  GVECTORIZE(FUN,V,V,V,I,N,N)
113 
119 #define VECTORIZE5_tttti(FUN) \
120  GVECTORIZE(FUN,V,T,T,T,I,N) \
121  GVECTORIZE(FUN,T,V,T,T,I,N) \
122  GVECTORIZE(FUN,T,T,V,T,I,N) \
123  GVECTORIZE(FUN,V,V,T,T,I,N) \
124  GVECTORIZE(FUN,T,V,V,T,I,N) \
125  GVECTORIZE(FUN,V,T,V,T,I,N) \
126  GVECTORIZE(FUN,V,V,V,T,I,N)
127 
133 #define VECTORIZE6_ttttti(FUN) \
134  GVECTORIZE(FUN,V,T,T,T,T,I) \
135  GVECTORIZE(FUN,T,V,T,T,T,I) \
136  GVECTORIZE(FUN,T,T,V,T,T,I) \
137  GVECTORIZE(FUN,V,V,T,T,T,I) \
138  GVECTORIZE(FUN,T,V,V,T,T,I) \
139  GVECTORIZE(FUN,V,T,V,T,T,I) \
140  GVECTORIZE(FUN,V,V,V,T,T,I)
141 
144 #define VECTORIZE1_n(FUN) \
145 template<class Type> \
146 vector<Type> FUN(int n, Type arg1) { \
147  vector<Type> ans(n); \
148  for(int i=0; i<n; i++) ans(i) = FUN(arg1); \
149  return ans; \
150 }
151 
154 #define VECTORIZE2_n(FUN) \
155 template<class Type> \
156 vector<Type> FUN(int n, Type arg1, Type arg2) { \
157  vector<Type> ans(n); \
158  for(int i=0; i<n; i++) ans(i) = FUN(arg1, arg2); \
159  return ans; \
160 }
161 
164 #define VECTORIZE3_n(FUN) \
165 template<class Type> \
166 vector<Type> FUN(int n, Type arg1, Type arg2, Type arg3) { \
167  vector<Type> ans(n); \
168  for(int i=0; i<n; i++) ans(i) = FUN(arg1, arg2, arg3); \
169  return ans; \
170 }
171 
172 using CppAD::abs;
173 VECTORIZE1_t(abs)
174 VECTORIZE1_t(acos)
175 VECTORIZE1_t(asin)
176 VECTORIZE1_t(atan)
177 VECTORIZE1_t(cos)
178 VECTORIZE1_t(erf)
179 VECTORIZE1_t(exp)
180 VECTORIZE1_t(log)
181 VECTORIZE1_t(log10)
182 VECTORIZE1_t(sin)
183 VECTORIZE1_t(sqrt)
184 VECTORIZE2_tt(pow)
#define VECTORIZE2_tt(FUN)
Vectorize 2-argument functions.
Definition: Vectorize.hpp:71
#define VECTORIZE1_t(FUN)
Vectorize 1-argument functions.
Definition: Vectorize.hpp:63
License: GPL v2