Codebase list cppad / upstream/2015.00.00.7 doc / ad_in_c.cpp.xml
upstream/2015.00.00.7

Tree @upstream/2015.00.00.7 (Download .tar.gz)

ad_in_c.cpp.xml @upstream/2015.00.00.7raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Example and Test Linking CppAD to Languages Other than C++</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Example and Test Linking CppAD to Languages Other than C++"/>
<meta name="keywords" id="keywords" content=" example and test linking cppad to languages other than c++ C Ad algorithmic differentiation automatic link Cppad language "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_ad_in_c.cpp_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="ad_fun.cpp.xml" target="_top">Prev</a>
</td><td><a href="conj_grad.cpp.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>Example</option>
<option>General</option>
<option>ad_in_c.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>Example-&gt;</option>
<option>General</option>
<option>ExampleUtility</option>
<option>ListAllExamples</option>
<option>testvector</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>General-&gt;</option>
<option>ad_fun.cpp</option>
<option>ad_in_c.cpp</option>
<option>conj_grad.cpp</option>
<option>cppad_eigen.hpp</option>
<option>hes_minor_det.cpp</option>
<option>hes_lu_det.cpp</option>
<option>interface2c.cpp</option>
<option>jac_minor_det.cpp</option>
<option>jac_lu_det.cpp</option>
<option>mul_level</option>
<option>ode_stiff.cpp</option>
<option>mul_level_ode.cpp</option>
<option>mul_level_adolc_ode.cpp</option>
<option>ode_taylor.cpp</option>
<option>stack_machine.cpp</option>
</select>
</td>
<td>ad_in_c.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Example and Test Linking CppAD to Languages Other than C++</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cstdio&gt;
# include &lt;cppad/cppad.hpp&gt;
# include &lt;list&gt;

namespace { // Begin empty namespace *****************************************

/*
void debug_print(const char *label, double d)
{	using std::printf;

	unsigned char *byte = reinterpret_cast&lt;unsigned char *&gt;(&amp;d);
	size_t n_byte = sizeof(d);
	printf(&quot;%s&quot;, label);
	for(size_t i = 0; i &lt; n_byte; i++)
		printf(&quot;%x&quot;, byte[i]);
	printf(&quot;\n&quot;);
}
*/

// type in C corresponding to an <a href="ad.xml" target="_top">AD</a>&lt;double&gt; object 
typedef struct { void*  p_void; } cad;

// type in C corresponding to a an <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; 
typedef struct { void* p_void; } cad_fun;

// type in C corresponding to a C AD binary operator
typedef enum { op_add, op_sub, op_mul, op_div } cad_binary_op;

// type in C corresponding to a C AD unary operator
typedef enum { 
	op_abs, op_acos, op_asin, op_atan, op_cos, op_cosh, 
	op_exp, op_log,  op_sin,  op_sinh, op_sqrt
} cad_unary_op;

// --------------------------------------------------------------------------
// helper code not intended for use by C code  ------------------------------
using CppAD::AD;
using CppAD::ADFun;
using CppAD::vector;
using CppAD::NearEqual;

void cad2vector(size_t n, cad* p_cad, vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt;&amp; v)
{	assert( n == v.size() );
	for(size_t j = 0; j &lt; n; j++)
	{	<a href="ad.xml" target="_top">AD</a>&lt;double&gt;* p_ad = 
			reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_cad[j].p_void);
		v[j] = *p_ad;
	}
}

void vector2cad(size_t n, vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt;&amp; v, cad* p_cad)
{	assert( n == v.size() );
	for(size_t j = 0; j &lt; n; j++)
	{	<a href="ad.xml" target="_top">AD</a>&lt;double&gt;* p_ad = 
			reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_cad[j].p_void);
		*p_ad = v[j];
	}
}

void double2vector(size_t n, double* p_dbl, vector&lt;double&gt;&amp; v)
{	assert( n == v.size() );
	for(size_t j = 0; j &lt; n; j++)
		v[j] = p_dbl[j];
}

void vector2double(size_t n, vector&lt;double&gt;&amp; v, double *p_dbl)
{	assert( n == v.size() );
	for(size_t j = 0; j &lt; n; j++)
		p_dbl[j] = v[j];
}

std::list&lt;void*&gt; allocated;
# ifdef NDEBUG
inline void push_allocated(void *p)
{ }
inline void pop_allocated(void *p)
{ }
# else
inline void push_allocated(void *p)
{	assert( p != 0 );
	allocated.push_front(p);
}
inline void pop_allocated(void *p)
{	std::list&lt;void*&gt;::iterator i;
	for(i = allocated.begin(); i != allocated.end(); ++i)
	{	if( *i == p )
		{	allocated.erase(i);
			return;
		}
	}
	assert( 0 );
}

# endif
// --------------------------------------------------------------------------
// Here is the code that links C to CppAD. You will have to add more
// functions and operators to make a complete language link.
//
extern &quot;C&quot;
bool cad_near_equal(double x, double y)
{	double eps = 10. * std::numeric_limits&lt;double&gt;::epsilon();
	return <a href="nearequal.xml" target="_top">NearEqual</a>(x, y, eps, 0.);
}

// create a C++ AD object
// value is the value that the C++ AD object will have
// p_cad-&gt;p_void: on input is 0, on output points to C++ AD object
extern &quot;C&quot; 
void cad_new_ad(cad *p_cad, double value)
{	// make sure pointer is not currently allocated
	assert( p_cad-&gt;p_void == 0 );

	<a href="ad.xml" target="_top">AD</a>&lt;double&gt;* p_ad   = new <a href="ad.xml" target="_top">AD</a>&lt;double&gt;(value); 
	p_cad-&gt;p_void      = reinterpret_cast&lt;void*&gt;(p_ad);

	// put in list of allocate pointers
	push_allocated( p_cad-&gt;p_void );
}

// delete a C++ AD object
// p_cad-&gt;value: not used
// p_cad-&gt;p_void: on input points to C++ AD object, on output is 0
extern &quot;C&quot; 
void cad_del_ad(cad* p_cad)
{	// make sure that p_cad has been allocated
	pop_allocated( p_cad-&gt;p_void );

	<a href="ad.xml" target="_top">AD</a>&lt;double&gt;* p_ad   = reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt;( p_cad-&gt;p_void );
	delete p_ad;

	// special value for pointers that are not allocated
	p_cad-&gt;p_void = 0;
}

// extract the value from a C++ AD object
// extern &quot;C&quot;
double cad_value(cad* p_cad)
{	<a href="ad.xml" target="_top">AD</a>&lt;double&gt;* p_ad = reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_cad-&gt;p_void);
	return Value( Var2Par(*p_ad) );
}

// preform a C AD unary operation
extern &quot;C&quot;
void cad_unary(cad_unary_op op, cad* p_operand, cad* p_result) 
{	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; *operand, *result;
	result  = reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_result-&gt;p_void);
	operand = reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_operand-&gt;p_void);
	switch(op)
	{
		case op_abs:
		*result = abs( *operand );
		break;

		case op_acos:
		*result = acos( *operand );
		break;

		case op_asin:
		*result = asin( *operand );
		break;

		case op_atan:
		*result = atan( *operand ); 
		break;

		case op_cos:
		*result = cos( *operand );
		break;

		case op_cosh:
		*result = cosh( *operand );
		break;

		case op_exp:
		*result = exp( *operand );
		break;

		case op_log:
		*result = log( *operand );
		break;

		case op_sin:
		*result = sin( *operand );
		break;

		case op_sinh:
		*result = sinh( *operand );
		break;

		case op_sqrt:
		*result = sqrt( *operand );
		break;

		default:
		// not a unary operator
		assert(0);
		break;

	}
	return;
}

// perform a C AD binary operation
extern &quot;C&quot;
void cad_binary(cad_binary_op op, cad* p_left, cad* p_right, cad* p_result)
{	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; *result, *left, *right; 
	result = reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_result-&gt;p_void);
	left   = reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_left-&gt;p_void);
	right  = reinterpret_cast&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;* &gt; (p_right-&gt;p_void);
	assert( result != 0 );
	assert( left != 0 );
	assert( right != 0 );
	
	switch(op)
	{	case op_add:
		*result         = *left + (*right);
		break;

		case op_sub:
		*result         = *left - (*right);
		break;

		case op_mul:
		*result         = *left * (*right);
		break;

		case op_div:
		*result         = *left / (*right);
		break;

		default:
		// not a binary operator
		assert(0);
	}
	return;
}

// declare the independent variables in C++
extern &quot;C&quot;
void cad_independent(size_t n, cad* px_cad)
{	vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; x(n);
	cad2vector(n, px_cad, x);
	CppAD::<a href="independent.xml" target="_top">Independent</a>(x);
	vector2cad(n, x, px_cad);
}

// create an ADFun object in C++
extern &quot;C&quot;
cad_fun cad_new_fun(size_t n, size_t m, cad* px_cad, cad* py_cad)
{	cad_fun fun;

	<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;* p_adfun = new <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;;
	vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; x(n);
	vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; y(m);
	cad2vector(n, px_cad, x);
	cad2vector(m, py_cad, y);
	p_adfun-&gt;Dependent(x, y);

	fun.p_void = reinterpret_cast&lt;void*&gt;( p_adfun ); 

	// put in list of allocate pointers
	push_allocated( fun.p_void );

	return fun;
}

// delete an AD function object in C
extern &quot;C&quot;
void cad_del_fun(cad_fun *fun)
{	// make sure this pointer has been allocated
	pop_allocated( fun-&gt;p_void );

	<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;* p_adfun 
		= reinterpret_cast&lt; <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;* &gt; (fun-&gt;p_void);
	delete p_adfun;

	// special value for pointers that are not allocated
	fun-&gt;p_void = 0;
}

// evaluate the Jacobian corresponding to a function object
extern &quot;C&quot;
void cad_jacobian(cad_fun fun, 
	size_t n, size_t m, double* px, double* pjac )
{	assert( fun.p_void != 0 );

	<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;* p_adfun = 
		reinterpret_cast&lt; <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;* &gt;(fun.p_void);
	vector&lt;double&gt; x(n), jac(n * m);

	double2vector(n, px, x);
	jac = p_adfun-&gt;Jacobian(x);
	vector2double(n * m, jac, pjac);
}

// forward mode
extern &quot;C&quot;
void cad_forward(cad_fun fun, 
	size_t order, size_t n, size_t m, double* px, double* py )
{	assert( fun.p_void != 0 );

	<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;* p_adfun = 
		reinterpret_cast&lt; <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;* &gt;(fun.p_void);
	vector&lt;double&gt; x(n), y(m);

	double2vector(n, px, x);
	y = p_adfun-&gt;Forward(order, x);
	vector2double(m, y, py);
}

// check that allocated list has been completely freed
extern &quot;C&quot;
bool cad_allocated_empty(void)
{	return allocated.empty();
}

} // End empty namespace ****************************************************

# include &lt;math.h&gt; // used to check results in c code below

# define N 2       // number of independent variables in example
# define M 5       // number of dependent variables in example

// -------------------------------------------------------------------------
// Here is the C code that uses the CppAD link above
bool ad_in_c(void)
{	// This routine is intentionally coded as if it were written in C
	// as an example of how you can link C, and other languages to CppAD
	bool ok = true;

	// x vector of AD objects in C
	double value;
	size_t j, n = N;
	cad X[N];
	for(j = 0; j &lt; n; j++)
	{	value       = (double) (j+1) / (double) n;
		X[j].p_void = 0;
		cad_new_ad(X + j, value);
	}

	// y vector of AD objects in C
	size_t i, m = M;
	cad Y[M];
	for(i = 0; i &lt; m; i++)
	{	value       = 0.; // required, but not used
		Y[i].p_void = 0;
		cad_new_ad(Y + i, value);
	}

	// declare X as the independent variable vector 
	cad_independent(n, X);

	// y[0] = x[0] + x[1]
	cad_binary(op_add, X+0, X+1, Y+0);
	ok &amp;= cad_near_equal( cad_value(Y+0), cad_value(X+0)+cad_value(X+1) );

	// y[1] = x[0] - x[1]
	cad_binary(op_sub, X+0, X+1, Y+1);
	ok &amp;= cad_near_equal( cad_value(Y+1), cad_value(X+0)-cad_value(X+1) );

	// y[2] = x[0] * x[1]
	cad_binary(op_mul, X+0, X+1, Y+2);
	ok &amp;= cad_near_equal( cad_value(Y+2), cad_value(X+0)*cad_value(X+1) );

	// y[3] = x[0] * x[1]
	cad_binary(op_div, X+0, X+1, Y+3);
	ok &amp;= cad_near_equal( cad_value(Y+3), cad_value(X+0)/cad_value(X+1) );

	// y[4] = sin(x[0]) + asin(sin(x[0]))
	cad sin_x0 = { 0 };       // initialize p_void as zero
	cad_new_ad( &amp;sin_x0, 0.);
	cad_unary(op_sin, X+0, &amp;sin_x0);
	ok &amp;= cad_near_equal(cad_value(&amp;sin_x0), sin(cad_value(X+0)) );

	cad asin_sin_x0 = { 0 };  // initialize p_void as zero
	cad_new_ad( &amp;asin_sin_x0, 0.);
	cad_unary(op_asin, &amp;sin_x0, &amp;asin_sin_x0);
	ok &amp;= cad_near_equal( 
		cad_value(&amp;asin_sin_x0), 
		asin( cad_value(&amp;sin_x0) )
	);

	cad_binary(op_add, &amp;sin_x0, &amp;asin_sin_x0, Y+4);
	ok &amp;= cad_near_equal(
		cad_value(Y+4),
		cad_value(&amp;sin_x0) + cad_value(&amp;asin_sin_x0)
	);
	
	// declare y as the dependent variable vector and stop recording
	// and store function object in f
	cad_fun f = cad_new_fun(n, m, X, Y);

	// now use the function object
	double x[N], jac[N * M];
	x[0] = 1.;
	x[1] = .5;

	// compute the Jacobian
	cad_jacobian(f, n, m, x, jac);

	// check the Jacobian values
	size_t k = 0;
	// partial y[0] w.r.t. x[0]
	ok &amp;= cad_near_equal(jac[k++], 1.);
	// partial y[0] w.r.t. x[1]
	ok &amp;= cad_near_equal(jac[k++], 1.);
	// partial y[1] w.r.t. x[0]
	ok &amp;= cad_near_equal(jac[k++], 1.);
	// partial y[1] w.r.t. x[1]
	ok &amp;= cad_near_equal(jac[k++], -1.);
	// partial y[2] w.r.t. x[0]
	ok &amp;= cad_near_equal(jac[k++], x[1]);
	// partial y[2] w.r.t. x[1]
	ok &amp;= cad_near_equal(jac[k++], x[0]);
	// partial y[3] w.r.t. x[0]
	ok &amp;= cad_near_equal(jac[k++], 1./x[1]);
	// partial y[3] w.r.t. x[1]
	ok &amp;= cad_near_equal(jac[k++], -x[0]/(x[1]*x[1]));
	// partial y[4] w.r.t x[0]
	ok &amp;= cad_near_equal(jac[k++],  cos(x[0]) + 1.);
	// partial y[4] w.r.t x[1]
	ok &amp;= cad_near_equal(jac[k++],  0.);

	// evaluate the function f at a different x
	size_t order = 0;
	double y[M];
	x[0] = .5;
	x[1] = 1.;
	cad_forward(f, order, n, m, x, y); 

	// check the function values
	ok &amp;= cad_near_equal(y[0] , x[0] + x[1] );
	ok &amp;= cad_near_equal(y[1] , x[0] - x[1] );
	ok &amp;= cad_near_equal(y[2] , x[0] * x[1] );
	ok &amp;= cad_near_equal(y[3] , x[0] / x[1] );
	ok &amp;= cad_near_equal(y[4] , sin(x[0]) + asin(sin(x[0])) );

	// delete All C++ copies of the AD objects
	cad_del_fun( &amp;f );
	cad_del_ad( &amp;sin_x0 );
	cad_del_ad( &amp;asin_sin_x0 );
	for(j = 0; j &lt; n; j++)
		cad_del_ad(X + j); 
	for(i = 0; i &lt; m; i++)
		cad_del_ad(Y + i);

	ok     &amp;= cad_allocated_empty();
	return ok;
}

</pre>

</font></code>


<hr/>Input File: example/ad_in_c.cpp

</body>
</html>