Changeset 1664
 Timestamp:
 Mar 17, 2010 10:32:45 AM (11 years ago)
 Location:
 stable/20100101
 Files:

 4 edited
Legend:
 Unmodified
 Added
 Removed

stable/20100101/cppad/local/optimize.hpp
r1647 r1664 996 996 { 997 997 mask = 1; 998 for(i = 2; i < 6; i++) if( arg[1] & mask ) 999 { CPPAD_ASSERT_UNKNOWN( arg[i] < i_var ); 1000 tape[arg[i]].connect = yes_connected; 998 for(i = 2; i < 6; i++) 999 { if( arg[1] & mask ) 1000 { CPPAD_ASSERT_UNKNOWN( arg[i] < i_var ); 1001 tape[arg[i]].connect = yes_connected; 1002 } 1001 1003 mask = mask << 1; 1002 1004 } 
stable/20100101/cppad/local/sparse_pack.hpp
r1614 r1664 3 3 4 4 /*  5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003 09Bradley M. Bell5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 200310 Bradley M. Bell 6 6 7 7 CppAD is distributed under multiple licenses. This distribution is under … … 180 180 // search for next element of the set 181 181 Pack check = data_[ next_index_ * n_pack_ + j ]; 182 while( next_element_ < end_)182 while( true ) 183 183 { if( check & (one << k) ) 184 184 { next_element_++; … … 186 186 } 187 187 next_element_++; 188 // check if no more elements in the set 189 if( next_element_ == end_ ) 190 return end_; 188 191 k++; 189 192 CPPAD_ASSERT_UNKNOWN( k <= n_bit_ ); … … 195 198 } 196 199 } 197 next_element_ = end_; 200 // should never get here 201 CPPAD_ASSERT_UNKNOWN(false); 198 202 return end_; 199 203 } 
stable/20100101/test_more/optimize.cpp
r1608 r1664 1 1 /* $Id$ */ 2 2 /*  3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003 09Bradley M. Bell3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 200310 Bradley M. Bell 4 4 5 5 CppAD is distributed under multiple licenses. This distribution is under … … 257 257 return ok; 258 258 } 259 // 259 // =================================================================== 260 260 // Test duplicate operation analysis 261 261 … … 420 420 421 421 // create f: X > Y and stop tape recording 422 // Y[ X[0] ] = X[1] and other components of Y are zero.423 422 CppAD::ADFun<double> F; 424 423 F.Dependent(X, Y); … … 485 484 486 485 // create f: X > Y and stop tape recording 487 // Y[ X[0] ] = X[1] and other components of Y are zero.488 486 CppAD::ADFun<double> F; 489 487 F.Dependent(X, Y); … … 512 510 } 513 511 //  512 bool duplicate_four(void) 513 { // Check that unary expression matching not only checks hash code, 514 // and operator, but also operand (old bug that has been fixed). 515 bool ok = true; 516 using CppAD::AD; 517 size_t j; 518 519 // domain space vector 520 size_t n = 1; 521 CPPAD_TEST_VECTOR< AD<double> > X(n); 522 X[0] = 1.; 523 524 // range space vector 525 size_t m = 1; 526 CPPAD_TEST_VECTOR< AD<double> > Y(m); 527 528 // declare independent variables and start tape recording 529 CppAD::Independent(X); 530 531 // check a huge number of same operation with different operands 532 size_t n_operations = size_t(CPPAD_HASH_TABLE_SIZE) + 5; 533 Y[0] = X[0]; 534 for(j = 0; j < n_operations; j++) 535 Y[0] = abs(Y[0]); 536 537 // create f: X > Y and stop tape recording 538 CppAD::ADFun<double> F; 539 F.Dependent(X, Y); 540 541 // check number of variables in original function 542 ok &= (F.size_var() == 1 + n + n_operations ); 543 544 CPPAD_TEST_VECTOR<double> x(n), y(m); 545 x[0] = 1.; 546 547 y = F.Forward(0, x); 548 ok &= ( y[0] == Value( Y[0] ) ); 549 550 F.optimize(); 551 552 // check same number of variables in optimized version 553 ok &= (F.size_var() == 1 + n + n_operations ); 554 555 y = F.Forward(0, x); 556 ok &= ( y[0] == Value( Y[0] ) ); 557 558 return ok; 559 } 560 // ==================================================================== 514 561 bool cummulative_sum(void) 515 562 { // test conversion of a sequence of additions and subtraction … … 847 894 return ok; 848 895 } 896 // check that CondExp properly detects dependencies 897 bool cond_exp_depend(void) 898 { bool ok = true; 899 using CppAD::AD; 900 901 AD<double> zero(0.), one(1.), two(2.), three(3.); 902 903 size_t n = 4; 904 CPPAD_TEST_VECTOR< AD<double> > X(n); 905 X[0] = zero; 906 X[1] = one; 907 X[2] = two; 908 X[3] = three; 909 CppAD::Independent(X); 910 911 size_t m = 4; 912 CPPAD_TEST_VECTOR< AD<double> > Y(m); 913 Y[0] = CondExpLt(X[0] + .5, one, two, three); 914 Y[1] = CondExpLt(zero, X[1] + .5, two, three); 915 Y[2] = CondExpLt(zero, one, X[2] + .5, three); 916 Y[3] = CondExpLt(zero, one, two, X[3] + .5); 917 918 CppAD::ADFun<double> f(X, Y); 919 f.optimize(); 920 921 CPPAD_TEST_VECTOR<double> x(n), y(m); 922 size_t i; 923 for(i = 0; i < n; i++) 924 x[i] = double(n  i); 925 y = f.Forward(0, x); 926 927 if( x[0] + .5 < 1. ) 928 ok &= y[0] == 2.; 929 else ok &= y[0] == 3.; 930 if( 0. < x[1] + .5 ) 931 ok &= y[1] == 2.; 932 else ok &= y[1] == 3.; 933 ok &= y[2] == x[2] + .5;; 934 ok &= y[3] == 2.; 935 936 return ok; 937 } 938 849 939 } 850 940 … … 859 949 ok &= duplicate_two(); 860 950 ok &= duplicate_three(); 951 ok &= duplicate_four(); 861 952 // convert sequence of additions to cummulative summation 862 953 ok &= cummulative_sum(); … … 866 957 ok &= reverse_sparse_jacobian_csum(); 867 958 ok &= reverse_sparse_hessian_csum(); 959 // check that CondExp properly detects dependencies 960 ok &= cond_exp_depend(); 961 868 962 return ok; 869 963 } 
stable/20100101/test_more/sparse_jacobian.cpp
r1558 r1664 1 1 /* $Id$ */ 2 2 /*  3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003 09Bradley M. Bell3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 200310 Bradley M. Bell 4 4 5 5 CppAD is distributed under multiple licenses. This distribution is under … … 259 259 return ok; 260 260 } 261 262 bool multiple_of_n_bit() 263 { bool ok = true; 264 using CppAD::AD; 265 using CppAD::vector; 266 size_t i, j; 267 268 // should be the same as the corresponding typedef in 269 // cppad/local/sparse_pack.hpp 270 typedef size_t Pack; 271 272 // number of bits per packed value 273 size_t n_bit = std::numeric_limits<Pack>::digits; 274 275 // check case where number of variables is equal to n_bit 276 vector< AD<double> > x(n_bit); 277 vector< AD<double> > y(n_bit); 278 279 // create an AD function with domain and range dimension equal to n_bit 280 CppAD::Independent(x); 281 for(i = 0; i < n_bit; i++) 282 y[i] = x[n_bit  i  1]; 283 CppAD::ADFun<double> f(x, y); 284 285 // Jacobian sparsity patterns 286 vector<bool> r(n_bit * n_bit); 287 vector<bool> s(n_bit * n_bit); 288 for(i = 0; i < n_bit; i++) 289 { for(j = 0; j < n_bit; j++) 290 r[ i * n_bit + j ] = (i == j); 291 } 292 s = f.ForSparseJac(n_bit, r); 293 294 // check the result 295 for(i = 0; i < n_bit; i++) 296 { for(j = 0; j < n_bit; j++) 297 { if( i == n_bit  j  1 ) 298 ok = ok & s[ i * n_bit + j ]; 299 else ok = ok & (! s[i * n_bit + j] ); 300 } 301 } 302 303 return ok; 304 } 261 305 } // End empty namespace 262 306 # include <vector> … … 291 335 // ok &= reverse_set< std::valarray<double>, std_valarray_set >(); 292 336 //  293 return ok; 294 } 337 // 338 ok &= multiple_of_n_bit(); 339 return ok; 340 }
Note: See TracChangeset
for help on using the changeset viewer.