<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>LuRatio: Example and Test</title> <meta name="description" id="description" content="LuRatio: Example and Test"/> <meta name="keywords" id="keywords" content=" example Luratio test "/> <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='_luratio.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="luratio.xml" target="_top">Prev</a> </td><td><a href="std_math_unary.xml" target="_top">Next</a> </td><td> <select onchange='choose_across0(this)'> <option>Index-></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-></option> <option>CppAD</option> <option>library</option> <option>LuRatio</option> <option>LuRatio.cpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>CppAD-></option> <option>Install</option> <option>Introduction</option> <option>AD</option> <option>ADFun</option> <option>library</option> <option>Example</option> <option>configure</option> <option>Appendix</option> </select> </td> <td> <select onchange='choose_down2(this)'> <option>library-></option> <option>ErrorHandler</option> <option>NearEqual</option> <option>speed_test</option> <option>SpeedTest</option> <option>NumericType</option> <option>CheckNumericType</option> <option>SimpleVector</option> <option>CheckSimpleVector</option> <option>nan</option> <option>pow_int</option> <option>Poly</option> <option>LuDetAndSolve</option> <option>RombergOne</option> <option>RombergMul</option> <option>Runge45</option> <option>Rosen34</option> <option>OdeErrControl</option> <option>OdeGear</option> <option>OdeGearControl</option> <option>BenderQuad</option> <option>LuRatio</option> <option>std_math_unary</option> <option>CppAD_vector</option> <option>TrackNewDel</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>LuRatio-></option> <option>LuRatio.cpp</option> </select> </td> <td>LuRatio.cpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>LuRatio: Example and Test</big></big></b></center> <code><font color="blue"><pre style='display:inline'> # include <cstdlib> // for rand function # include <cassert> # include <cppad/cppad.hpp> namespace { // Begin empty namespace CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> *NewFactor( size_t n , const <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> &x , bool &ok , <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><size_t> &ip , <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><size_t> &jp ) { using CppAD::AD; using CppAD::ADFun; size_t i, j, k; // values for independent and dependent variables <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > Y(n*n+1), X(n*n); // values for the LU factor <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > LU(n*n); // record the LU factorization corresponding to this value of x <a href="ad.xml" target="_top">AD</a><double> Ratio; for(k = 0; k < n*n; k++) X[k] = x[k]; <a href="independent.xml" target="_top">Independent</a>(X); for(k = 0; k < n*n; k++) LU[k] = X[k]; CppAD::LuRatio(ip, jp, LU, Ratio); for(k = 0; k < n*n; k++) Y[k] = LU[k]; Y[n*n] = Ratio; // use a function pointer so can return ADFun object <a href="funconstruct.xml" target="_top">ADFun</a><double> *FunPtr = new <a href="funconstruct.xml" target="_top">ADFun</a><double>(X, Y); // check value of ratio during recording ok &= (Ratio == 1.); // check that ip and jp are permutations of the indices 0, ... , n-1 for(i = 0; i < n; i++) { ok &= (ip[i] < n); ok &= (jp[i] < n); for(j = 0; j < n; j++) { if( i != j ) { ok &= (ip[i] != ip[j]); ok &= (jp[i] != jp[j]); } } } return FunPtr; } bool CheckLuFactor( size_t n , const <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> &x , const <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> &y , const <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><size_t> &ip , const <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><size_t> &jp ) { bool ok = true; double sum; // element of L * U double pij; // element of permuted x size_t i, j, k; // temporary indices // L and U factors <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> L(n*n), U(n*n); // Extract L from LU factorization for(i = 0; i < n; i++) { // elements along and below the diagonal for(j = 0; j <= i; j++) L[i * n + j] = y[ ip[i] * n + jp[j] ]; // elements above the diagonal for(j = i+1; j < n; j++) L[i * n + j] = 0.; } // Extract U from LU factorization for(i = 0; i < n; i++) { // elements below the diagonal for(j = 0; j < i; j++) U[i * n + j] = 0.; // elements along the diagonal U[i * n + i] = 1.; // elements above the diagonal for(j = i+1; j < n; j++) U[i * n + j] = y[ ip[i] * n + jp[j] ]; } // Compute L * U for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { // compute element (i,j) entry in L * U sum = 0.; for(k = 0; k < n; k++) sum += L[i * n + k] * U[k * n + j]; // element (i,j) in permuted version of A pij = x[ ip[i] * n + jp[j] ]; // compare ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(pij, sum, 1e-10, 1e-10); } } return ok; } } // end Empty namespace bool LuRatio(void) { bool ok = true; size_t n = 2; // number rows in A double ratio; // values for independent and dependent variables <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> x(n*n), y(n*n+1); // pivot vectors <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><size_t> ip(n), jp(n); // set x equal to the identity matrix x[0] = 1.; x[1] = 0; x[2] = 0.; x[3] = 1.; // create a fnction object corresponding to this value of x CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> *FunPtr = NewFactor(n, x, ok, ip, jp); // use function object to factor matrix y = FunPtr->Forward(0, x); ratio = y[n*n]; ok &= (ratio == 1.); ok &= CheckLuFactor(n, x, y, ip, jp); // set x so that the pivot ratio will be infinite x[0] = 0. ; x[1] = 1.; x[2] = 1. ; x[3] = 0.; // try to use old function pointer to factor matrix y = FunPtr->Forward(0, x); ratio = y[n*n]; // check to see if we need to refactor matrix ok &= (ratio > 10.); if( ratio > 10. ) { delete FunPtr; // to avoid a memory leak FunPtr = NewFactor(n, x, ok, ip, jp); } // now we can use the function object to factor matrix y = FunPtr->Forward(0, x); ratio = y[n*n]; ok &= (ratio == 1.); ok &= CheckLuFactor(n, x, y, ip, jp); delete FunPtr; // avoid memory leak return ok; }</pre> </font></code> <hr/>Input File: example/lu_ratio.cpp </body> </html>