File: bool_fun.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (62 lines) | stat: -rw-r--r-- 1,511 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
// 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
// ----------------------------------------------------------------------------

/*
{xrst_begin bool_fun.cpp}

AD Boolean Functions: Example and Test
######################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end bool_fun.cpp}
*/
// BEGIN C++

# include <cppad/cppad.hpp>
# include <complex>


// define abbreviation for double precision complex
typedef std::complex<double> Complex;

namespace {
   // a unary bool function with Complex argument
   static bool IsReal(const Complex &x)
   {  return x.imag() == 0.; }

   // a binary bool function with Complex arguments
   static bool AbsGeq(const Complex &x, const Complex &y)
   {  double axsq = x.real() * x.real() + x.imag() * x.imag();
      double aysq = y.real() * y.real() + y.imag() * y.imag();

      return axsq >= aysq;
   }

   // Create version of IsReal with AD<Complex> argument
   // inside of namespace and outside of any other function.
   CPPAD_BOOL_UNARY(Complex, IsReal)

   // Create version of AbsGeq with AD<Complex> arguments
   // inside of namespace and outside of any other function.
   CPPAD_BOOL_BINARY(Complex, AbsGeq)

}
bool BoolFun(void)
{  bool ok = true;

   CppAD::AD<Complex> x = Complex(1.,  0.);
   CppAD::AD<Complex> y = Complex(1.,  1.);

   ok &= IsReal(x);
   ok &= ! AbsGeq(x, y);

   return ok;
}

// END C++