Prev Next

daxpy

Prototype
int f2cad::daxpy_(integer *n, doublereal *da, doublereal *dx, integer *incx, doublereal *dy, integer *incy);

Fortran Source
daxpy.f

Description
This example uses daxpy.f to compute  \[
\left( \begin{array}{c}
     f_0 \\
     f_1 
\end{array} \right)
=

\left( \begin{array}{c}
     x_0 \\
     x_1 
\end{array} \right)
+
\left( \begin{array}{c}
     3 \\
     4 
\end{array} \right)
\] 
Using the f2cad_link routines f2cad::Independent and f2cad::Dependent, this defines the function  \[
f(x) = 
\left( \begin{array}{c}
     5 x_0 + 3 \\
     5 x_1 + 4 
\end{array} \right)
\] 
We check that the derivative of this function, calculated using the f2cad::Partial routine, satisfies  \[
f^{(1)} (x)
=
\left( \begin{array}{cc}
     5 & 0 \\
     0 & 5
\end{array} \right)
\] 
 

# include <f2cad/daxpy.hpp>

test_result daxpy(void)
{	bool ok = true;

	// Input values for daxpy
	doublereal a = 5.;

	integer n    = 2;
	doublereal  x[2];
	x[0]         = 1.;
	x[1]         = 2.;
	integer incx = 1;

	integer m    = 2;
	doublereal  f[2];
	f[0]         = 3.;
	f[1]         = 4.;
	integer incf = 1;

	// declare independent variables
	f2cad::Independent(n, x);

	// set f = a * x + f
	f2cad::daxpy_(&n, &a, x, &incx, f, &incf);

	// declare dependent variables
	f2cad::Dependent(m, f);

	double p;
	p   = f2cad::Partial<doublereal>(0, 0);
	ok &= f2cad::near_equal(p, 5., 1e-10, 1e-10);

	p   = f2cad::Partial<doublereal>(1, 0);
	ok &= f2cad::near_equal(p, 0., 1e-10, 1e-10);

	p   = f2cad::Partial<doublereal>(0, 1);
	ok &= f2cad::near_equal(p, 0., 1e-10, 1e-10);

	p   = f2cad::Partial<doublereal>(1, 1);
	ok &= f2cad::near_equal(p, 5., 1e-10, 1e-10);

	if( ok )
		return test_pass;
	return test_fail;
}

Input File: example/daxpy.cpp