1 | /* |
---|
2 | * Copyright(C) 2008 by Lingyun Yang |
---|
3 | * lingyun(.dot.]yang@gmail.com |
---|
4 | * http://www.lingyunyang.com |
---|
5 | * |
---|
6 | * Please get permission from Lingyun Yang before you redistribute this file. |
---|
7 | * |
---|
8 | */ |
---|
9 | //! \brief Automatic Differentiation |
---|
10 | //! \file tpsa.h |
---|
11 | //! \version $Id: tpsa.h,v 1.4 2009-04-17 17:32:23 frs Exp $ |
---|
12 | //! \author Lingyun Yang, http://www.lingyunyang.com/ |
---|
13 | |
---|
14 | #ifndef WIN32 |
---|
15 | |
---|
16 | /* should work unchanged on _win32 using Lahey */ |
---|
17 | #define ad_print ad_print_ |
---|
18 | #define ad_elem ad_elem_ |
---|
19 | #define ad_fill_ran ad_fill_ran_ |
---|
20 | #define ad_tra ad_tra_ |
---|
21 | #define ad_shift ad_shift_ |
---|
22 | #define ad_save_block ad_save_block_ |
---|
23 | #define ad_read_block ad_read_block_ |
---|
24 | #define ad_nvar ad_nvar_ |
---|
25 | #define ad_length ad_length_ |
---|
26 | #define ad_derivative ad_derivative_ |
---|
27 | #define ad_subst ad_subst_ |
---|
28 | #define ad_cos ad_cos_ |
---|
29 | #define ad_sin ad_sin_ |
---|
30 | #define ad_log ad_log_ |
---|
31 | #define ad_exp ad_exp_ |
---|
32 | #define ad_sqrt ad_sqrt_ |
---|
33 | #define ad_abs ad_abs_ |
---|
34 | #define ad_div_c ad_div_c_ |
---|
35 | #define ad_c_div ad_c_div_ |
---|
36 | #define ad_mult_const ad_mult_const_ |
---|
37 | #define ad_add_const ad_add_const_ |
---|
38 | #define ad_div ad_div_ |
---|
39 | #define ad_mult ad_mult_ |
---|
40 | #define ad_sub ad_sub_ |
---|
41 | #define ad_reset ad_reset_ |
---|
42 | #define ad_resetvars ad_resetvars_ |
---|
43 | #define ad_pok ad_pok_ |
---|
44 | #define ad_pek ad_pek_ |
---|
45 | #define ad_truncate ad_truncate_ |
---|
46 | #define ad_var ad_var_ |
---|
47 | #define ad_count ad_count_ |
---|
48 | #define ad_const ad_const_ |
---|
49 | #define ad_free ad_free_ |
---|
50 | #define ad_add ad_add_ |
---|
51 | #define ad_copy ad_copy_ |
---|
52 | #define ad_clean ad_clean_ |
---|
53 | #define ad_alloc ad_alloc_ |
---|
54 | #define ad_reserve ad_reserve_ |
---|
55 | #define ad_init ad_init_ |
---|
56 | #else |
---|
57 | #define ad_print AD_PRINT |
---|
58 | #define ad_elem AD_ELEM |
---|
59 | #define ad_fill_ran AD_FILL_RAN |
---|
60 | #define ad_tra AD_TRA |
---|
61 | #define ad_shift AD_SHIFT |
---|
62 | #define ad_save_block AD_SAVE_BLOCK |
---|
63 | #define ad_read_block AD_READ_BLOCK |
---|
64 | #define ad_nvar AD_NVAR |
---|
65 | #define ad_length AD_LENGTH |
---|
66 | #define ad_derivative AD_DERIVATIVE |
---|
67 | #define ad_subst AD_SUBST |
---|
68 | #define ad_cos AD_COS |
---|
69 | #define ad_sin AD_SIN |
---|
70 | #define ad_log AD_LOG |
---|
71 | #define ad_exp AD_EXP |
---|
72 | #define ad_sqrt AD_SQRT |
---|
73 | #define ad_abs AD_ABS |
---|
74 | #define ad_div_c AD_DIV_C |
---|
75 | #define ad_c_div AD_C_DIV |
---|
76 | #define ad_mult_const AD_MULT_CONST |
---|
77 | #define ad_add_const AD_ADD_CONST |
---|
78 | #define ad_div AD_DIV |
---|
79 | #define ad_mult AD_MULT |
---|
80 | #define ad_sub AD_SUB |
---|
81 | #define ad_reset AD_RESET |
---|
82 | #define ad_resetvars AD_RESETVARS_ |
---|
83 | #define ad_pok AD_POK |
---|
84 | #define ad_pek AD_PEK |
---|
85 | #define ad_truncate AD_TRUNCATE |
---|
86 | #define ad_var AD_VAR |
---|
87 | #define ad_count AD_COUNT |
---|
88 | #define ad_const AD_CONST |
---|
89 | #define ad_free AD_FREE |
---|
90 | #define ad_add AD_ADD |
---|
91 | #define ad_copy AD_COPY |
---|
92 | #define ad_clean AD_CLEAN |
---|
93 | #define ad_alloc AD_ALLOC |
---|
94 | #define ad_reserve AD_RESERVE |
---|
95 | #define ad_init AD_INIT |
---|
96 | #endif |
---|
97 | |
---|
98 | |
---|
99 | #include <vector> |
---|
100 | #include <limits> |
---|
101 | #include <cmath> |
---|
102 | #include <iostream> |
---|
103 | #include <iomanip> |
---|
104 | #include <cstdlib> |
---|
105 | |
---|
106 | #ifndef AD_HH |
---|
107 | #define AD_HH |
---|
108 | //! Type of order and number of variables. |
---|
109 | //typedef unsigned char TNVND; |
---|
110 | typedef unsigned int TNVND; |
---|
111 | typedef unsigned int TVEC; |
---|
112 | #ifdef __cplusplus |
---|
113 | extern "C" { |
---|
114 | #endif |
---|
115 | |
---|
116 | #ifdef MSVC_DLL |
---|
117 | _declspec(dllexport) void _stdcall ad_reserve(const TVEC* n); |
---|
118 | _declspec(dllexport) void _stdcall ad_init(const TNVND* nv, const TNVND* nd); |
---|
119 | _declspec(dllexport) void _stdcall ad_resetvars(const TNVND* nv); |
---|
120 | _declspec(dllexport) void _stdcall ad_alloc(TVEC* i); |
---|
121 | _declspec(dllexport) void _stdcall ad_free(const TVEC* i); |
---|
122 | _declspec(dllexport) void _stdcall ad_poolsize(size_t* n); |
---|
123 | |
---|
124 | _declspec(dllexport) void _stdcall ad_count(TVEC* n); |
---|
125 | _declspec(dllexport) void _stdcall ad_nvar(TVEC* n); |
---|
126 | _declspec(dllexport) void _stdcall ad_length(const TVEC* iv, unsigned int* n); |
---|
127 | _declspec(dllexport) void _stdcall ad_copy(const TVEC* i, const TVEC* j); |
---|
128 | _declspec(dllexport) void _stdcall ad_elem(const TVEC* ivec, unsigned int* idx, unsigned int* c, double* x); |
---|
129 | _declspec(dllexport) void _stdcall ad_pek(const TVEC* ivec, int* c, size_t* n, double* x); |
---|
130 | _declspec(dllexport) void _stdcall ad_pok(const TVEC* ivec, int* c, size_t* n, double* x); |
---|
131 | _declspec(dllexport) void _stdcall ad_var(const TVEC* ii, const double* x, unsigned int* iv); |
---|
132 | _declspec(dllexport) void _stdcall ad_abs(const TVEC* iv, double* r); |
---|
133 | _declspec(dllexport) void _stdcall ad_truncate(const TVEC* iv, const TNVND* d); |
---|
134 | |
---|
135 | _declspec(dllexport) void _stdcall ad_clean(const TVEC* iv, const double* eps); |
---|
136 | _declspec(dllexport) void _stdcall ad_reset(const TVEC* iv); |
---|
137 | _declspec(dllexport) void _stdcall ad_const(const TVEC* ii, const double* r); |
---|
138 | _declspec(dllexport) void _stdcall ad_fill_ran(const TVEC* iv, const double* ratio, const double* xm); |
---|
139 | |
---|
140 | _declspec(dllexport) void _stdcall ad_add(const TVEC* i, const TVEC* j); |
---|
141 | _declspec(dllexport) void _stdcall ad_add_const(const TVEC* i, double *r); |
---|
142 | |
---|
143 | _declspec(dllexport) void _stdcall ad_sub(const TVEC* i, const TVEC* j); |
---|
144 | //! internal multiplication, dst should be different from lhs and rhs. |
---|
145 | _declspec(dllexport) void _stdcall ad_mult(const TVEC* ivlhs, const TVEC* ivrhs, TVEC* ivdst); |
---|
146 | _declspec(dllexport) void _stdcall ad_mult_const(const TVEC* iv, double* c); |
---|
147 | |
---|
148 | _declspec(dllexport) void _stdcall ad_div_c(const TVEC* iv, const double* c); |
---|
149 | _declspec(dllexport) void _stdcall ad_c_div(const TVEC* iv, const double* c, TVEC* ivret); |
---|
150 | _declspec(dllexport) void _stdcall ad_div(const TVEC* ilhs, const TVEC* irhs, TVEC* idst); |
---|
151 | |
---|
152 | _declspec(dllexport) void _stdcall ad_sqrt(const TVEC* iv, const TVEC* iret); |
---|
153 | _declspec(dllexport) void _stdcall ad_exp(const TVEC* iv, const TVEC* iret); |
---|
154 | _declspec(dllexport) void _stdcall ad_log(const TVEC* iv, const TVEC* iret); |
---|
155 | _declspec(dllexport) void _stdcall ad_sin(const TVEC* iv, const TVEC* iret); |
---|
156 | _declspec(dllexport) void _stdcall ad_cos(const TVEC* iv, const TVEC* iret); |
---|
157 | |
---|
158 | _declspec(dllexport) void _stdcall ad_derivative(const TVEC* iv, unsigned int* expo, const TVEC* iret); |
---|
159 | _declspec(dllexport) void _stdcall ad_tra(const TVEC* iv, unsigned int* expo, const TVEC* iret); |
---|
160 | _declspec(dllexport) void _stdcall ad_shift(const TVEC* iv, unsigned int* ishift, const TVEC* iret, const double* eps); |
---|
161 | |
---|
162 | _declspec(dllexport) void _stdcall ad_read_block(const TVEC* iv, double* v, TNVND* J, const unsigned int* N); |
---|
163 | _declspec(dllexport) void _stdcall ad_save_block(const TVEC* iv, const double* v, const TNVND* J, const unsigned int* N); |
---|
164 | |
---|
165 | _declspec(dllexport) void _stdcall ad_rev(const TVEC* iv); |
---|
166 | |
---|
167 | _declspec(dllexport) void _stdcall ad_subst(const TVEC* iv, const TVEC* ibv, const TNVND* nbv, const TVEC* iret); |
---|
168 | //void ad_inverse(const TVEC* iv, const TNVND* nbv, const TVEC* iret, const TNVND* nret); |
---|
169 | _declspec(dllexport) void _stdcall ad_inverse(const TVEC* iva, const TNVND* nva, const TVEC* iret, const TNVND* nret); |
---|
170 | |
---|
171 | _declspec(dllexport) void _stdcall print_index(std::ostream& os); |
---|
172 | _declspec(dllexport) void _stdcall ad_print(const TVEC* iv); |
---|
173 | _declspec(dllexport) void _stdcall ad_print_array(const TVEC* iv, const TVEC* nv); |
---|
174 | #else |
---|
175 | void ad_reserve(const TVEC* n); |
---|
176 | void ad_init(const TNVND* nv, const TNVND* nd); |
---|
177 | void ad_resetvars(const TNVND* nv); |
---|
178 | void ad_alloc(TVEC* i); |
---|
179 | void ad_free(const TVEC* i); |
---|
180 | void ad_poolsize(size_t* n); |
---|
181 | |
---|
182 | void ad_count(TVEC* n); |
---|
183 | void ad_nvar(TVEC* n); |
---|
184 | void ad_length(const TVEC* iv, unsigned int* n); |
---|
185 | void ad_copy(const TVEC* i, const TVEC* j); |
---|
186 | void ad_elem(const TVEC* ivec, unsigned int* idx, unsigned int* c, double* x); |
---|
187 | void ad_pek(const TVEC* ivec, int* c, size_t* n, double* x); |
---|
188 | void ad_pok(const TVEC* ivec, int* c, size_t* n, double* x); |
---|
189 | void ad_var(const TVEC* ii, const double* x, unsigned int* iv); |
---|
190 | void ad_abs(const TVEC* iv, double* r); |
---|
191 | void ad_truncate(const TVEC* iv, const TNVND* d); |
---|
192 | |
---|
193 | void ad_clean(const TVEC* iv, const double* eps); |
---|
194 | void ad_reset(const TVEC* iv); |
---|
195 | void ad_const(const TVEC* ii, const double* r); |
---|
196 | void ad_fill_ran(const TVEC* iv, const double* ratio, const double* xm); |
---|
197 | |
---|
198 | void ad_add(const TVEC* i, const TVEC* j); |
---|
199 | void ad_add_const(const TVEC* i, double *r); |
---|
200 | |
---|
201 | void ad_sub(const TVEC* i, const TVEC* j); |
---|
202 | //! internal multiplication, dst should be different from lhs and rhs. |
---|
203 | void ad_mult(const TVEC* ivlhs, const TVEC* ivrhs, TVEC* ivdst); |
---|
204 | void ad_mult_const(const TVEC* iv, double* c); |
---|
205 | |
---|
206 | void ad_div_c(const TVEC* iv, const double* c); |
---|
207 | void ad_c_div(const TVEC* iv, const double* c, TVEC* ivret); |
---|
208 | void ad_div(const TVEC* ilhs, const TVEC* irhs, TVEC* idst); |
---|
209 | |
---|
210 | void ad_sqrt(const TVEC* iv, const TVEC* iret); |
---|
211 | void ad_exp(const TVEC* iv, const TVEC* iret); |
---|
212 | void ad_log(const TVEC* iv, const TVEC* iret); |
---|
213 | void ad_sin(const TVEC* iv, const TVEC* iret); |
---|
214 | void ad_cos(const TVEC* iv, const TVEC* iret); |
---|
215 | |
---|
216 | void ad_derivative(const TVEC* iv, unsigned int* expo, const TVEC* iret); |
---|
217 | void ad_tra(const TVEC* iv, unsigned int* expo, const TVEC* iret); |
---|
218 | void ad_shift(const TVEC* iv, unsigned int* ishift, const TVEC* iret, const double* eps); |
---|
219 | |
---|
220 | void ad_read_block(const TVEC* iv, double* v, TNVND* J, const unsigned int* N); |
---|
221 | void ad_save_block(const TVEC* iv, const double* v, const TNVND* J, const unsigned int* N); |
---|
222 | |
---|
223 | void ad_rev(const TVEC* iv); |
---|
224 | |
---|
225 | void ad_subst(const TVEC* iv, const TVEC* ibv, const TNVND* nbv, const TVEC* iret); |
---|
226 | void ad_inverse(const TVEC* iva, const TNVND* nva, const TVEC* iret, const TNVND* nret); |
---|
227 | |
---|
228 | void print_index(std::ostream& os); |
---|
229 | void ad_print(const TVEC* iv); |
---|
230 | void ad_print_array(const TVEC* iv, const TVEC* nv); |
---|
231 | #endif |
---|
232 | |
---|
233 | #ifdef __cplusplus |
---|
234 | } |
---|
235 | #endif |
---|
236 | |
---|
237 | #endif |
---|