Changeset 3889 for trunk/test_more
 Timestamp:
 Feb 18, 2017 12:14:05 PM (3 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test_more/atomic_sparsity.cpp
r3884 r3889 12 12 Atomic function 13 13 g( x ) = [ x_2, x_0 * x_1 ] 14 15 f(u) = g( u_0 + u_1, u_0 + u_1, u_2 )16 14 \] $$ 17 15 */ … … 197 195 } 198 196 }; // End of atomic_set_sparsity class 199 } // End empty namespace 200 201 bool atomic_sparsity(void) 197 198 199 // f(u) = g( u_0 + u_1 , u_0 + u_1 , u_2 ) 200 // = [ u_2 , (u_0 + u_1)^2 ] 201 bool test_one(void) 202 202 { bool ok = true; 203 203 using CppAD::AD; … … 252 252 ok &= s[i] == check_s[i]; 253 253 } 254 // correct Hessian result 254 // correct Hessian result for w_0 * f_0 (u) + w_1 * f_1(u) 255 255 set_vector check_h(n); 256 256 check_h[0].insert(0); … … 278 278 return ok; 279 279 } 280 281 // f(u) = g( u_0 + u_1 , u_1 + u_2 , u_2 + u_0 ) 282 // = [ u_2 + u_0 , (u_0 + u_1)*(u_1 + u_2) ] 283 bool test_two(void) 284 { bool ok = true; 285 using CppAD::AD; 286 using CppAD::NearEqual; 287 double eps = 10. * std::numeric_limits<double>::epsilon(); 288 // Create the atomic get_started object 289 atomic_set_sparsity afun("atomic_set_sparsity"); 290 size_t n = 3; 291 size_t m = 2; 292 vector< AD<double> > au(n), ay(m); 293 for(size_t j = 0; j < n; j++) 294 au[j] = double(j + 1); 295 296 // declare independent variables and start tape recording 297 CppAD::Independent(au); 298 299 // ax 300 vector< AD<double> > ax(n); 301 ax[0] = au[0] + au[1]; 302 ax[1] = au[1] + au[2]; 303 ax[2] = au[2] + au[0]; 304 305 // call user function 306 afun(ax, ay); 307 308 // create f: u > y and stop tape recording 309 CppAD::ADFun<double> f(au, ay); 310 311 // check function value 312 ok &= NearEqual(ay[0] , au[2] + au[0], eps, eps); 313 ok &= NearEqual(ay[1] , (au[0] + au[1]) * (au[1] + au[2]), eps, eps); 314 315 // correct Jacobian result 316 set_vector check_s(m); 317 check_s[0].insert(2); 318 check_s[0].insert(0); 319 check_s[1].insert(0); 320 check_s[1].insert(1); 321 check_s[1].insert(2); 322 // compute and test forward mode 323 { set_vector r(n), s(m); 324 for(size_t i = 0; i < n; i++) 325 r[i].insert(i); 326 s = f.ForSparseJac(n, r); 327 for(size_t i = 0; i < m; i++) 328 ok &= s[i] == check_s[i]; 329 } 330 // compute and test reverse mode 331 { set_vector r(m), s(m); 332 for(size_t i = 0; i < m; i++) 333 r[i].insert(i); 334 s = f.RevSparseJac(m, r); 335 for(size_t i = 0; i < m; i++) 336 ok &= s[i] == check_s[i]; 337 } 338 //  339 // correct Hessian result for f_0 (u) 340 set_vector check_h(n), s(1); 341 s[0].insert(0); 342 // compute and test forward mode 343 { set_vector r(1), h(n); 344 for(size_t j = 0; j < n; j++) 345 r[0].insert(j); 346 h = f.ForSparseHes(r, s); 347 for(size_t i = 0; i < n; i++) 348 ok &= h[i] == check_h[i]; 349 } 350 // compute and test reverse mode 351 { set_vector h(n); 352 h = f.RevSparseHes(n, s); 353 for(size_t i = 0; i < n; i++) 354 ok &= h[i] == check_h[i]; 355 } 356 //  357 // correct Hessian result for f_1 (u) 358 s[0].clear(); 359 s[0].insert(1); 360 check_h[0].insert(1); 361 check_h[0].insert(2); 362 check_h[1].insert(0); 363 check_h[1].insert(1); 364 check_h[1].insert(2); 365 check_h[2].insert(0); 366 check_h[2].insert(1); 367 // compute and test forward mode 368 { set_vector r(1), h(n); 369 for(size_t j = 0; j < n; j++) 370 r[0].insert(j); 371 h = f.ForSparseHes(r, s); 372 for(size_t i = 0; i < n; i++) 373 ok &= h[i] == check_h[i]; 374 } 375 // compute and test reverse mode 376 { set_vector h(n); 377 h = f.RevSparseHes(n, s); 378 for(size_t i = 0; i < n; i++) 379 ok &= h[i] == check_h[i]; 380 } 381 return ok; 382 } 383 384 } // End empty namespace 385 386 bool atomic_sparsity(void) 387 { bool ok = true; 388 ok &= test_one(); 389 ok &= test_two(); 390 return ok; 391 }
Note: See TracChangeset
for help on using the changeset viewer.