multi_chkpoint_one_algo

View page source

chkpoint_one Algorithm that Computes Square Root

Syntax

checkpoint_algo ( au , ay )

Purpose

This algorithm computes a square root using Newton’s method. It is meant to be very inefficient in order to demonstrate timing results.

au

This argument has prototype

const ADvector & au

where ADvector is a simple vector class with elements of type AD<double> . The size of au is three.

y_initial

We use the notation

y_initial = au [0]

for the initial value of the Newton iterate.

y_squared

We use the notation

y_squared = au [1]

for the value we are taking the square root of.

ay

This argument has prototype

ADvector & ay

The size of ay is one and ay [0] is the square root of y_squared .

Source

// includes used by all source code in multi_chkpoint_one.cpp file
# include <cppad/cppad.hpp>
# include "multi_chkpoint_one.hpp"
# include "team_thread.hpp"
//
namespace {
    using CppAD::thread_alloc; // fast multi-threading memory allocator
    using CppAD::vector;       // uses thread_alloc
    //
    typedef CppAD::AD<double> a_double;

    void checkpoint_algo(const vector<a_double>& au , vector<a_double>& ay)
    {
        // extract components of argument vector
        a_double y_initial  = au[0];
        a_double y_squared  = au[1];

        // Use Newton's method to solve f(y) = y^2 = y_squared
        a_double y_itr = y_initial;
        for(size_t itr = 0; itr < 20; itr++)
        {  // solve (y - y_itr) * f'(y_itr) = y_squared - y_itr^2
            a_double fp_itr = 2.0 * y_itr;
            y_itr           = y_itr + (y_squared - y_itr * y_itr) / fp_itr;
        }

        // return the Newton approximation for f(y) = y_squared
        ay[0] = y_itr;
    }
}