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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// 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 check_numeric_type.cpp}
The CheckNumericType Function: Example and Test
###############################################
{xrst_literal
// BEGIN C++
// END C++
}
{xrst_end check_numeric_type.cpp}
*/
// BEGIN C++
# include <cppad/utility/check_numeric_type.hpp>
# include <cppad/utility/near_equal.hpp>
// Choosing a value between 1 and 10 selects a numeric class property to be
// omitted and result in an error message being generated
# define CppADMyTypeOmit 0
namespace { // Empty namespace
// -------------------------------------------------------------------
class MyType {
private:
double d;
public:
// constructor from void
MyType(void) : d(0.)
{ }
// constructor from an int
MyType(int d_) : d(d_)
{ }
// copy constructor
MyType(const MyType &x)
{ d = x.d; }
// assignment operator
void operator = (const MyType &x)
{ d = x.d; }
// member function that converts to double
double Double(void) const
{ return d; }
# if CppADMyTypeOmit != 1
// unary plus
MyType operator + (void) const
{ MyType x;
x.d = d;
return x;
}
# endif
# if CppADMyTypeOmit != 2
// unary plus
MyType operator - (void) const
{ MyType x;
x.d = - d;
return x;
}
# endif
# if CppADMyTypeOmit != 3
// binary addition
MyType operator + (const MyType &x) const
{ MyType y;
y.d = d + x.d ;
return y;
}
# endif
# if CppADMyTypeOmit != 4
// binary subtraction
MyType operator - (const MyType &x) const
{ MyType y;
y.d = d - x.d ;
return y;
}
# endif
# if CppADMyTypeOmit != 5
// binary multiplication
MyType operator * (const MyType &x) const
{ MyType y;
y.d = d * x.d ;
return y;
}
# endif
# if CppADMyTypeOmit != 6
// binary division
MyType operator / (const MyType &x) const
{ MyType y;
y.d = d / x.d ;
return y;
}
# endif
# if CppADMyTypeOmit != 7
// compound assignment addition
void operator += (const MyType &x)
{ d += x.d; }
# endif
# if CppADMyTypeOmit != 8
// compound assignment subtraction
void operator -= (const MyType &x)
{ d -= x.d; }
# endif
# if CppADMyTypeOmit != 9
// compound assignment multiplication
void operator *= (const MyType &x)
{ d *= x.d; }
# endif
# if CppADMyTypeOmit != 10
// compound assignment division
void operator /= (const MyType &x)
{ d /= x.d; }
# endif
};
// -------------------------------------------------------------------
/*
Solve: A[0] * x[0] + A[1] * x[1] = b[0]
A[2] * x[0] + A[3] * x[1] = b[1]
*/
template <class NumericType>
void Solve(NumericType *A, NumericType *x, NumericType *b)
{
// make sure NumericType satisfies its conditions
CppAD::CheckNumericType<NumericType>();
// copy b to x
x[0] = b[0];
x[1] = b[1];
// copy A to work space
NumericType W[4];
W[0] = A[0];
W[1] = A[1];
W[2] = A[2];
W[3] = A[3];
// divide first row by W(1,1)
W[1] /= W[0];
x[0] /= W[0];
W[0] = NumericType(1);
// subtract W(2,1) times first row from second row
W[3] -= W[2] * W[1];
x[1] -= W[2] * x[0];
W[2] = NumericType(0);
// divide second row by W(2, 2)
x[1] /= W[3];
W[3] = NumericType(1);
// use first row to solve for x[0]
x[0] -= W[1] * x[1];
}
} // End Empty namespace
bool CheckNumericType(void)
{ bool ok = true;
using CppAD::NearEqual;
double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
MyType A[4];
A[0] = MyType(1); A[1] = MyType(2);
A[2] = MyType(3); A[3] = MyType(4);
MyType b[2];
b[0] = MyType(1);
b[1] = MyType(2);
MyType x[2];
Solve(A, x, b);
MyType sum;
sum = A[0] * x[0] + A[1] * x[1];
ok &= NearEqual(sum.Double(), b[0].Double(), eps99, eps99);
sum = A[2] * x[0] + A[3] * x[1];
ok &= NearEqual(sum.Double(), b[1].Double(), eps99, eps99);
return ok;
}
// END C++
|