File: compare.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (105 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
Check comparison operators between AD< AD<Base> > and Base, int
*/
# include <cppad/cppad.hpp>

namespace {
   // ---------------------------------------------------------------------
   bool compare_parameter(bool optimize)
   {  bool ok = true;
      using CppAD::AD;
      //
      // f
      CPPAD_TESTVECTOR( AD<double> ) ax(1), ap(1), ay(1);
      ax[0] = 1.0;
      ap[0] = 3.0;
      CppAD::Independent( ax, ap );
      if( ap[0] < 2.0 )
         ay[0] = ax[0] + ax[0];
      else
         ay[0] = ax[0] * ax[0];
      CppAD::ADFun<double> f(ax, ay);
      if( optimize )
         f.optimize();
      //
      // x, y, ok
      CPPAD_TESTVECTOR(double) x(1), y(1);
      x[0] = 4.0;
      y    = f.Forward(0, x);
      ok  &= y[0] == x[0] * x[0];
      ok &= f.compare_change_number() == 0;
      //
      // p, ok
      CPPAD_TESTVECTOR(double) p(1);
      p[0] = 1.0;
      f.new_dynamic(p);
      y    = f.Forward(0, x);
      ok  &= y[0] == x[0] * x[0];
      ok &= f.compare_change_number() == 1;
      //
      return ok;
   }
   // ---------------------------------------------------------------------
   template <class Type>
   bool compare_type(void)
   {  bool ok = true;
      using CppAD::AD;

      Type      middle = 4;
      AD<double> three = 3;
      AD<double> four  = 4;
      AD<double> five  = 5;

      // AD<double> > Type
      ok &= ! (three  >  middle);
      ok &= ! (four   >  middle);
      ok &=   (five   >  middle);
      // Type > AD<double>
      ok &=   (middle >  three );
      ok &= ! (middle >  four  );
      ok &= ! (middle >  five  );

      // AD<double> >= Type
      ok &= ! (three  >= middle);
      ok &=   (four   >= middle);
      ok &=   (five   >= middle);
      // Type > AD<double>
      ok &=   (middle >= three );
      ok &=   (middle >= four  );
      ok &= ! (middle >= five  );

      // AD<double> < Type
      ok &=   (three  <  middle);
      ok &= ! (four   <  middle);
      ok &= ! (five   <  middle);
      // Type > AD<double>
      ok &= ! (middle <  three );
      ok &= ! (middle <  four  );
      ok &=   (middle <  five  );

      // AD<double> <= Type
      ok &=   (three  <= middle);
      ok &=   (four   <= middle);
      ok &= ! (five   <= middle);
      // Type > AD<double>
      ok &= ! (middle <= three );
      ok &=   (middle <= four  );
      ok &=   (middle <= five  );

      return ok;
   }
}
bool Compare(void)
{  bool ok = true;
   ok  &= compare_type<int>();
   ok  &= compare_type<double>();
   ok  &= compare_type< CppAD::AD<double> >();
   ok  &= compare_parameter(true);
   ok  &= compare_parameter(false);
   return ok;
}