Prev Next start_adolc.cpp

Getting Started Using Adolc with f2cad Library

Prototype
int f2cad::dscal_(integer *n, doublereal *da, doublereal *dx, integer *incx);

Description
This is a simple example using Adolc and the libf2cadAdolc.a library routine dscal to compute the function  \[
f(a) = 

\left( \begin{array}{c}
     1 \\
     2 
\end{array} \right)
\] 
We check that the derivative of this function, calculated using the Adolc, satisfies  \[
f^{(1)} (a)
=
\left( \begin{array}{c}
     1 \\
     2    
\end{array} \right)
\] 
 

// standard input output library
# include <iostream>

// f2cad set up for adouble
# define F2CAD_USE_ADOLC

// prototype for dscal
# include <f2cad/dscal.hpp>

int main(void)
{	
	// turn on recording of adouble operations
	int tag  = 1;
	int keep = 1;
	trace_on(tag, keep);

	// independent variables
	int n     = 1;   // number of independent variables
	adouble  a[1];   // vector of independent variables
	a[0]   <<= 5.;   // first independent variables

	// create data structure expected by dscal
	integer  m = 2;
	adouble  f[2];
	f[0]     = 1.;
	f[1]     = 2.;
	integer incf = 1;

	// set f = a * f 
	f2cad::dscal_(&m, a, f, &incf );

	// dependent variables
	double fi;
	f[0] >>= fi;     // first dependent variable value
	f[1] >>= fi;     // second dependent variable value

	// turn off recording of adouble operations
	trace_off();

	// set differential for a 
	int d    = 1;    // order of the differential  
	double *daMemory = new double [2 * n];
	double **da      = new double * [n];
	int i, j;
	for(j = 0; j < n; j++)
		da[j] = daMemory + 2 * j;
	da[0][0] = .5;   // value of a
	da[0][1] = 1.;   // differential for a

	// compute differential for f
	double *dfMemory = new double [2 * m];
	double **df      = new double * [m];
	for(i = 0; i < m; i++)
		df[i] = dfMemory + 2 * i;
	forward(tag, int(m), n, d, keep, da, df);

	// print results
	std::cout << "f(a)  = a * (1, 2)^T" << std::endl;
	std::cout << "f'(a) =     (" 
                  << df[0][1] 
                  << ", " 
                  << df[1][1] 
                  << ")^T" 
                  << std::endl;
	bool ok = df[0][1] == 1. && df[1][1] == 2.;

	// free allocated memory
	delete [] daMemory;
	delete [] dfMemory;
	delete [] da;
	delete [] df;


	if( ok ) 
		return 0;    // no error
	return 1;         // error
}

Input File: get_started/adolc/run.cpp