Last change on this file was 152, checked in by bradbell, 4 years ago

[f2cad/trunk] Change test return values from bool to one of (test_none, test_pass, test_fail).

run.cpp: Change test return type and print None, Pass, or Fail.
example/*.cpp Change return type.
add2lib.sh: change return type in automatically created examples.

File size: 3.2 KB
Line
1/* ----------------------------------------------------------------------------
5-----------------------------------------------------------------------------*/
6
7/*
8$begin dgesl$$9spell 10 hpp 11 blas 12 df 13 Linpack 14 dgefa dgesl 15 bool 16 doublereal 17 ipvt 18 lda 19$$ 20 21$section dgesl$$22 23index blas, dgesl$$
24$index dgesl, blas$$25 26head Prototype$$ 27$include p.omh/dgefa.p.omh$$28include p.omh/dgesl.p.omh$$
29
30
31$children% 32 omh/dgesl.f.omh 33%$$34head Fortran Source$$ 35$cref dgesl.f$$36 37head Description$$
38This example uses the Linpack routines
39$codei%dgefa.f%$$and 40codei%dgesl.f%$$ 41to solve the linear equation 42$latex $43\left( \begin{array}{cc} 44 1 & 2 \\ 45 3 & 4 46\end{array} \right) 47\left( \begin{array}{c} 48 f_0 \\ 49 f_1 50\end{array} \right) 51= 52\left( \begin{array}{c} 53 b_0 \\ 54 b_1 55\end{array} \right) 56$ $$57Multiplying the top row by 3 and subtracting it from 58the bottom row we obtain the equivalent equation: 59latex $60\left( \begin{array}{cc} 61 1 & 2 \\ 62 0 & -2 63\end{array} \right) 64\left( \begin{array}{c} 65 f_0 \\ 66 f_1 67\end{array} \right) 68= 69\left( \begin{array}{c} 70 b_0 \\ 71 b_1 - 3 b_0 72\end{array} \right) 73$$$
74It follows that
75$latex $76\left( \begin{array}{c} 77 f_0 \\ 78 f_1 79\end{array} \right) 80= 81\left( \begin{array}{c} 82 b_1 - 2 b_0 \\ 83 3 b_0 / 2 - b_1/ 2 84\end{array} \right) 85$ $$86The code below uses 87the xref/f2cad_link/$$ routines 88$code f2cad::Independent$$and code f2cad::Dependent$$,
89to define the function
90$latex $91f(b) = 92\left( \begin{array}{c} 93 b_1 - 2 b_0 \\ 94 3 b_0 / 2 - b_1/ 2 95\end{array} \right) 96$ $$97We check that the derivative of this function, 98calculated using the code f2cad::Partial$$ routine, satisfies 99$latex $100f^{(1)} (b) 101= 102\left( \begin{array}{cc} 103 -2 & 1 \\ 104 3/2 & -1 / 2 105\end{array} \right) 106$ $$107 108codep */ 109 110# include <f2cad/dgefa.hpp> 111# include <f2cad/dgesl.hpp> 112 113test_result dgesl(void) 114{ bool ok = true; 115 116 // A is a 2 by 2 matrix in column major order 117 doublereal A[4]; 118 A[0] = doublereal(1); A[2] = doublereal(2); 119 A[1] = doublereal(3); A[3] = doublereal(4); 120 121 // b is a column vector of length 2 122 doublereal b[2]; 123 b[0] = doublereal(1); 124 b[1] = doublereal(2); 125 126 // declare independent variables 127 integer n = 2; 128 f2cad::Independent(n, b); 129 130 // f is a column vector of length 2 131 doublereal f[2]; 132 integer i; 133 for(i = 0; i < n; i++) 134 f[i] = b[i]; 135 136 // solve A * f = b 137 integer ipvt[2]; 138 integer lda = n; 139 integer job = 0; 140 integer info; 141 f2cad::dgefa_ (A, &lda, &n, ipvt, &info); 142 f2cad::dgesl_ (A, &lda, &n, ipvt, f, &job); 143 144 // check info flag 145 ok &= (info == 0); 146 147 // construct the AD function object F : b -> f 148 // f[0] = b[1] - 2 b[0] 149 // f[1] = 3 b[0] / 2 - b[1]/ 2 150 integer m = 2; 151 f2cad::Dependent(m, f); 152 153 double p; 154 p = f2cad::Partial<doublereal>(0, 0); 155 ok &= f2cad::near_equal(p, -2., 1e-10, 1e-10); 156 157 p = f2cad::Partial<doublereal>(0, 1); 158 ok &= f2cad::near_equal(p, 1., 1e-10, 1e-10); 159 160 p = f2cad::Partial<doublereal>(1, 0); 161 ok &= f2cad::near_equal(p, 3./2., 1e-10, 1e-10); 162 163 p = f2cad::Partial<doublereal>(1, 1); 164 ok &= f2cad::near_equal(p, -1./2., 1e-10, 1e-10); 165 166 if( ok ) 167 return test_pass; 168 return test_fail; 169} 170/*$$
171\$end
172*/
Note: See TracBrowser for help on using the repository browser.