diff --git a/src/Makefile.am b/src/Makefile.am index eef5e05..00497a5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,7 @@ CLEANFILES = .license.stamp *.gcda *.gcno h_sources = masa.h masa_internal.h smasa.h ad_masa.h \ - nsctpl_fwd.hpp nsctpl.hpp \ - dualnumber.h numberarray.h dualnumberarray.h compare_types.h \ - raw_type.h shadownumber.h dualshadowarray.h dualshadow.h \ - testable.h + nsctpl_fwd.hpp nsctpl.hpp cc_sources = masa_core.cpp masa_class.cpp masa_map.cpp heat.cpp euler.cpp \ cns.cpp sod.cpp cmasa.cpp axi_euler.cpp axi_cns.cpp rans_sa.cpp \ diff --git a/src/compare_types.h b/src/compare_types.h deleted file mode 100644 index ed83f31..0000000 --- a/src/compare_types.h +++ /dev/null @@ -1,337 +0,0 @@ - -// $Id$ - -// The libMesh Finite Element Library. -// Copyright (C) 2002-2008 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner - -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. - -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - -#ifndef __compare_types_h__ -#define __compare_types_h__ - -// System includes -#include - -// Compile-time assertions: -// Use of ctassert, where E is a constant expression, -// will cause a compile-time error unless E evaulates to -// a nonzero integral value. - -template -struct ctassert { - enum { N = 1 - 2 * int(!t) }; - // 1 if t is true, -1 if t is false. - static char A[N]; - - static void apply() {} -}; - -template -struct ctprint -{ - typedef typename T9::compiler_should_print_types force_an_error; - force_an_error f; - static void apply() {} -}; - -template <> -struct ctprint<> -{ - static void apply() {} -}; - -template -char ctassert::A[N]; - -// Copy of boost's enable_if_c - -namespace boostcopy { - template - struct enable_if_c { - typedef T type; - }; - - template - struct enable_if_c {}; - - template - struct enable_if : public enable_if_c {}; - - template - struct enable_if_class { - typedef T type; - }; - -} - -// We can pass commas for template arguments through one level of -// macro expansion by "escaping" them this way: -#define MacroComma , - - -// List of scalar and builtin classes, useful for disambiguation -template -struct ScalarTraits { - static const bool value = false; -}; - -template -struct BuiltinTraits { - static const bool value = false; -}; - -#define ScalarBuiltin_true(type) \ -template<> \ -struct ScalarTraits { static const bool value = true; }; \ -template<> \ -struct BuiltinTraits { static const bool value = true; } - -ScalarBuiltin_true(char); -ScalarBuiltin_true(short); -ScalarBuiltin_true(int); -ScalarBuiltin_true(long); -ScalarBuiltin_true(unsigned char); -ScalarBuiltin_true(unsigned short); -ScalarBuiltin_true(unsigned int); -ScalarBuiltin_true(unsigned long); -ScalarBuiltin_true(float); -ScalarBuiltin_true(double); -ScalarBuiltin_true(long double); - -template -struct ScalarTraits > { static const bool value = ScalarTraits::value; }; - -template -struct BuiltinTraits > { static const bool value = BuiltinTraits::value; }; - - - -// Operators using different but compatible types need a return value -// based on whichever type the other can be upconverted into. For -// instance, the proper return type for -// TypeVector::operator*(double) is TypeVector. In -// general, an operation using types S and T should return a value -// based on CompareTypes::supertype - -template -struct CompareTypes { - -// All specializations need to define supertype. But compilers give -// better error messages for forgot-to-specialize-CompareTypes bugs if -// we leave supertype undefined in the unspecialized template -// definition. - -// typedef void supertype; - -// typedef S nosupertype; -}; - - -// A tricky SFINAE idiom for testing whether a particular CompareTypes -// combination has been properly specialized: - -template -struct DefinesSupertype -{ -private: - typedef char yes; - typedef struct { char array[2]; } no; - - template static yes test(typename C::supertype*); - template static no test(...); -public: - static const bool value = (sizeof(test(0)) == sizeof(yes)); -}; - - -// PlusType, MinusType, MultipliesType, and DividesType are usually just the -// same as CompareTypes, but user types may want to specialize further for -// efficiency. -template -struct PlusType { -}; - -template -struct MinusType { -}; - -template -struct MultipliesType { -}; - -template -struct DividesType { -}; - - -// typenames may need a MacroComma... -#define CompareTypes_default_Type(functor, typenames, typename1, typename2, enabletype) \ -template \ -struct functor##Type \ -{ \ - typedef typename CompareTypes::supertype supertype; \ -} - -#define CompareTypes_default_Types(typenames,typename1,typename2, enabletype) \ -CompareTypes_default_Type(Plus,typenames,typename1,typename2, enabletype); \ -CompareTypes_default_Type(Minus,typenames,typename1,typename2, enabletype); \ -CompareTypes_default_Type(Multiplies,typenames,typename1,typename2, enabletype); \ -CompareTypes_default_Type(Divides,typenames,typename1,typename2, enabletype) \ - - -template -struct CompareTypes { - typedef void supertype; -}; - -template -struct CompareTypes { - typedef T supertype; -}; - -template -struct CompareTypes { - typedef T supertype; -}; - -template -struct CompareTypes, reverseorder> { - typedef std::complex supertype; -}; - -template -struct CompareTypes, T, reverseorder> { - typedef std::complex supertype; -}; - -// There's got to be some magic template way to do these better - but the best -// thing on the net requires a bunch of Alexandrescu's code and doesn't work -// with older compilers - -#define CompareTypes_super(a,b,super) \ - template \ - struct CompareTypes { \ - typedef super supertype; \ - }; \ - CompareTypes_default_Types(,a,b,void) - -#define CompareTypes_all(mysub,mysuper) \ - CompareTypes_super(mysub, mysuper, mysuper); \ - CompareTypes_super(mysuper, mysub, mysuper); \ - CompareTypes_super(std::complex, mysuper, std::complex); \ - CompareTypes_super(mysuper, std::complex, std::complex); \ - CompareTypes_super(mysub, std::complex, std::complex); \ - CompareTypes_super(std::complex, mysub, std::complex); \ - CompareTypes_super(std::complex, std::complex, std::complex); \ - CompareTypes_super(std::complex, std::complex, std::complex) - -#define CompareTypes_single(mytype) \ - CompareTypes_super(mytype, mytype, mytype) - -CompareTypes_single(unsigned char); -CompareTypes_single(unsigned short); -CompareTypes_single(unsigned int); -CompareTypes_single(char); -CompareTypes_single(short); -CompareTypes_single(int); -CompareTypes_single(float); -CompareTypes_single(double); -CompareTypes_single(long double); - -CompareTypes_all(unsigned char, short); -CompareTypes_all(unsigned char, int); -CompareTypes_all(unsigned char, float); -CompareTypes_all(unsigned char, double); -CompareTypes_all(unsigned char, long double); -CompareTypes_all(unsigned short, int); -CompareTypes_all(unsigned short, float); -CompareTypes_all(unsigned short, double); -CompareTypes_all(unsigned short, long double); -CompareTypes_all(unsigned int, float); -CompareTypes_all(unsigned int, double); -CompareTypes_all(unsigned int, long double); -CompareTypes_all(char, short); -CompareTypes_all(char, int); -CompareTypes_all(char, float); -CompareTypes_all(char, double); -CompareTypes_all(char, long double); -CompareTypes_all(short, int); -CompareTypes_all(short, float); -CompareTypes_all(short, double); -CompareTypes_all(short, long double); -CompareTypes_all(int, float); -CompareTypes_all(int, double); -CompareTypes_all(int, long double); -CompareTypes_all(float, double); -CompareTypes_all(float, long double); -CompareTypes_all(double, long double); - -// gcc can't tell which of the following is the most specialized? Weak. -/* -template -struct CompareTypes, std::complex > { - typedef std::complex::supertype> supertype; -}; - -template -struct CompareTypes, T> { - typedef std::complex::supertype> supertype; -}; - -template -struct CompareTypes > { - typedef std::complex::supertype> supertype; -}; -*/ - - -// We can define CompareTypes template specializations with user types -// asymmetrically, to assist in disambiguation of templated functions -// or classes. But sometimes we just need the supertype: - -// FIXME: this won't work yet for cases where CompareTypes depends on -// reverseorder - -#define Symmetric_definition(templatename) \ -template >::value> \ -struct Symmetric##templatename { \ - typedef typename templatename::supertype supertype; \ -}; \ - \ -template \ -struct Symmetric##templatename \ -{ \ - typedef typename templatename::supertype supertype; \ -} - - - -Symmetric_definition(CompareTypes); -Symmetric_definition(PlusType); -Symmetric_definition(MinusType); -Symmetric_definition(MultipliesType); -Symmetric_definition(DividesType); - -#endif // __compare_types_h__ diff --git a/src/dualnumber.h b/src/dualnumber.h deleted file mode 100644 index 53823ef..0000000 --- a/src/dualnumber.h +++ /dev/null @@ -1,570 +0,0 @@ - -#ifndef __dualnumber_h__ -#define __dualnumber_h__ - -#include - -#include "compare_types.h" -#include "raw_type.h" -#include "testable.h" - -template -class DualNumber : public safe_bool > -{ -public: - typedef T value_type; - - typedef D derivatives_type; - - DualNumber(); - - template - DualNumber(const T2& val); - - template - DualNumber(const T2& val, const D2& deriv); - - T& value() { return _val; } - - const T& value() const { return _val; } - - D& derivatives() { return _deriv; } - - const D& derivatives() const { return _deriv; } - - bool boolean_test() const { return _val; } - - DualNumber operator- () const { return DualNumber(-_val, -_deriv); } - - template - DualNumber& operator+= (const DualNumber& a); - - template - DualNumber& operator+= (const T2& a); - - template - DualNumber& operator-= (const DualNumber& a); - - template - DualNumber& operator-= (const T2& a); - - template - DualNumber& operator*= (const DualNumber& a); - - template - DualNumber& operator*= (const T2& a); - - template - DualNumber& operator/= (const DualNumber& a); - - template - DualNumber& operator/= (const T2& a); - -private: - T _val; - D _deriv; -}; - - - -// Helper class to handle partial specialization for DualNumber -// constructors - -template -struct DualNumberConstructor -{ - static T value(const DualNumber& v) { return v.value(); } - - template - static T value(const T2& v) { return v; } - - template - static T value(const T2& v, const D2&) { return v; } - - template - static T value(const DualNumber& v) { - return DualNumberConstructor::value(v.value()); - } - - template - static D deriv(const T2&) { return 0.; } - - template - static D deriv(const DualNumber& v) { return v.derivatives(); } - - template - static D deriv(const T2&, const D2& d) { return d; } -}; - -template -struct DualNumberConstructor, DD> -{ - template - static DualNumber value(const DualNumber, D3>& v) { return v.value(); } - - template - static DualNumber value(const T2& v) { return v; } - - template - static DualNumber value(const T2& v, const D2& d) { return DualNumber(v,d); } - - template - static DualNumber value(const DualNumber& v, const D2&) { return v; } - - template - static DD deriv(const T2&) { return 0; } - - template - static DD deriv(const DualNumber& v) { return v.derivatives(); } - - template - static DD deriv(const T2&, const D2& d) { return d; } -}; - - -// -// Member function definitions -// - -template -inline -DualNumber::DualNumber() : - _val(), _deriv() {} - -template -template -inline -DualNumber::DualNumber(const T2& val) : - _val (DualNumberConstructor::value(val)), - _deriv(DualNumberConstructor::deriv(val)) {} - -template -template -inline -DualNumber::DualNumber(const T2& val, - const D2& deriv) : - _val (DualNumberConstructor::value(val,deriv)), - _deriv(DualNumberConstructor::deriv(val,deriv)) {} - - -// FIXME: these operators currently do automatic type promotion when -// encountering DualNumbers of differing levels of recursion and -// differentiability. But what we really want is automatic type -// *demotion*, to avoid pretending we have accurate derivatives which -// we don't have. If we could do that right then some potential -// subtle run-time user errors would turn into compile-time user -// errors. - -#define DualNumber_op(opname, functorname, simplecalc, dualcalc) \ -template \ -template \ -inline \ -DualNumber& \ -DualNumber::operator opname##= (const T2& in) \ -{ \ - simplecalc; \ - this->value() opname##= in; \ - return *this; \ -} \ - \ -template \ -template \ -inline \ -DualNumber& \ -DualNumber::operator opname##= (const DualNumber& in) \ -{ \ - dualcalc; \ - this->value() opname##= in.value(); \ - return *this; \ -} \ - \ -template \ -inline \ -typename functorname##Type,DualNumber >::supertype \ -operator opname (const DualNumber& a, const DualNumber& b) \ -{ \ - typedef typename \ - functorname##Type,DualNumber >::supertype \ - DS; \ - DS returnval = a; \ - returnval opname##= b; \ - return returnval; \ -} \ - \ -template \ -inline \ -typename functorname##Type,T,true>::supertype \ -operator opname (const T& a, const DualNumber& b) \ -{ \ - typedef typename \ - functorname##Type,T,true>::supertype DS; \ - DS returnval = a; \ - returnval opname##= b; \ - return returnval; \ -} \ - \ -template \ -inline \ -typename functorname##Type,T2,false>::supertype \ -operator opname (const DualNumber& a, const T2& b) \ -{ \ - typedef typename \ - functorname##Type,T2,false>::supertype DS; \ - DS returnval = a; \ - returnval opname##= b; \ - return returnval; \ -} - - - -DualNumber_op(+, Plus, , this->derivatives() += in.derivatives()) - -DualNumber_op(-, Minus, , this->derivatives() -= in.derivatives()) - -DualNumber_op(*, Multiplies, this->derivatives() *= in, - this->derivatives() *= in.value(); - this->derivatives() += this->value() * in.derivatives();) - -DualNumber_op(/, Divides, this->derivatives() /= in, - this->derivatives() /= in.value(); - this->derivatives() -= this->value()/(in.value()*in.value()) * in.derivatives(); -) - - - -namespace std { - -// Some forward declarations necessary for recursive DualNumbers - -template -inline DualNumber cos (const DualNumber& a); - -template -inline DualNumber cosh (const DualNumber& a); - -// Now just combined declaration/definitions - -#define DualNumber_std_unary(funcname, derivative, precalc) \ -template \ -inline \ -DualNumber funcname (const DualNumber& in) \ -{ \ - T funcval = std::funcname(in.value()); \ - precalc; \ - return DualNumber(funcval, (derivative) * in.derivatives()); \ -} - -DualNumber_std_unary(sqrt, 1 / (2 * funcval),) -DualNumber_std_unary(exp, funcval,) -DualNumber_std_unary(log, 1 / in.value(),) -DualNumber_std_unary(log10, 1 / in.value() * (1/std::log(T(10.))),) -DualNumber_std_unary(sin, std::cos(in.value()),) -DualNumber_std_unary(cos, -std::sin(in.value()),) -DualNumber_std_unary(tan, sec_in * sec_in, T sec_in = 1 / std::cos(in.value())) -DualNumber_std_unary(asin, 1 / std::sqrt(1 - in.value()*in.value()),) -DualNumber_std_unary(acos, -1 / std::sqrt(1 - in.value()*in.value()),) -DualNumber_std_unary(atan, 1 / (1 + in.value()*in.value()),) -DualNumber_std_unary(sinh, std::cosh(in.value()),) -DualNumber_std_unary(cosh, std::sinh(in.value()),) -DualNumber_std_unary(tanh, sech_in * sech_in, T sech_in = 1 / std::cos(in.value())) -DualNumber_std_unary(abs, (in.value() > 0) - (in.value() < 0),) // std < and > return 0 or 1 -DualNumber_std_unary(ceil, 0,) -DualNumber_std_unary(floor, 0,) - -#define DualNumber_std_binary(funcname, derivative) \ -template \ -inline \ -typename CompareTypes,DualNumber >::supertype \ -funcname (const DualNumber& a, const DualNumber& b) \ -{ \ - typedef typename CompareTypes::supertype TS; \ - typedef typename CompareTypes,DualNumber >::supertype type; \ - \ - TS funcval = std::funcname(a.value(), b.value()); \ - return type(funcval, derivative); \ -} \ - \ -template \ -inline \ -typename CompareTypes,T,true>::supertype \ -funcname (const T& a, const DualNumber& b) \ -{ \ - typedef typename CompareTypes,T,true>::supertype type; \ - type newa(a); \ - return std::funcname(newa, b); \ -} \ - \ -template \ -inline \ -typename CompareTypes,T2>::supertype \ -funcname (const DualNumber& a, const T2& b) \ -{ \ - typedef typename CompareTypes,T2>::supertype type; \ - type newb(b); \ - return std::funcname(a, newb); \ -} - -DualNumber_std_binary(pow, - funcval * (b.value() * a.derivatives() / a.value() + b.derivatives() * std::log(a.value()))) -DualNumber_std_binary(atan2, - (b.value() * a.derivatives() - a.value() * b.derivatives()) / - (b.value() * b.value() + a.value() * a.value())) -DualNumber_std_binary(max, - (a.value() > b.value()) ? a : b) -DualNumber_std_binary(min, - (a.value() > b.value()) ? b : a) -DualNumber_std_binary(fmod, a.derivatives()) - -template -class numeric_limits > : - public raw_numeric_limits, T> {}; - -} // namespace std - -#define DualNumber_compare(opname) \ -template \ -inline \ -bool \ -operator opname (const DualNumber& a, const DualNumber& b) \ -{ \ - return (a.value() opname b.value()); \ -} \ - \ -template \ -inline \ -typename boostcopy::enable_if_class< \ - typename CompareTypes,T>::supertype, \ - bool \ ->::type \ -operator opname (const T& a, const DualNumber& b) \ -{ \ - return (a opname b.value()); \ -} \ - \ -template \ -inline \ -typename boostcopy::enable_if_class< \ - typename CompareTypes,T2>::supertype, \ - bool \ ->::type \ -operator opname (const DualNumber& a, const T2& b) \ -{ \ - return (a.value() opname b); \ -} - -DualNumber_compare(>) -DualNumber_compare(>=) -DualNumber_compare(<) -DualNumber_compare(<=) -DualNumber_compare(==) -DualNumber_compare(!=) - -template -inline -std::ostream& -operator<< (std::ostream& output, const DualNumber& a) -{ - return output << '(' << a.value() << ',' << a.derivatives() << ')'; -} - - -// ScalarTraits, RawType, CompareTypes specializations - -template -struct ScalarTraits > -{ - static const bool value = ScalarTraits::value; -}; - -template -struct RawType > -{ - typedef typename RawType::value_type value_type; - - static value_type value(const DualNumber& a) { return raw_value(a.value()); } -}; - -template -struct PlusType, T2, reverseorder, - typename boostcopy::enable_if >::type> { - typedef DualNumber::supertype, D> supertype; -}; - -template -struct PlusType, DualNumber, reverseorder> { - typedef DualNumber::supertype, - typename SymmetricPlusType::supertype> supertype; -}; - -template -struct PlusType, DualNumber > { - typedef DualNumber::supertype, - typename SymmetricPlusType::supertype> supertype; -}; - - -template -struct MinusType, T2, reverseorder, - typename boostcopy::enable_if >::type> { - typedef DualNumber::supertype, D> supertype; -}; - -template -struct MinusType, DualNumber, reverseorder> { - typedef DualNumber::supertype, - typename SymmetricMinusType::supertype> supertype; -}; - -template -struct MinusType, DualNumber, reverseorder> { - typedef DualNumber::supertype, - typename SymmetricMinusType::supertype> supertype; -}; - - -template -struct MultipliesType, T2, reverseorder, - typename boostcopy::enable_if >::type> { - typedef DualNumber::supertype, - typename SymmetricMultipliesType::supertype> supertype; -}; - -template -struct MultipliesType, DualNumber, reverseorder> { - typedef DualNumber::supertype, - typename SymmetricPlusType< - typename SymmetricMultipliesType::supertype, - typename SymmetricMultipliesType::supertype>::supertype - > supertype; -}; - -template -struct MultipliesType, DualNumber, reverseorder> { - typedef DualNumber::supertype, - typename SymmetricMultipliesType::supertype - > supertype; -}; - - -template -struct DividesType, T2, false, - typename boostcopy::enable_if >::type> { - typedef DualNumber::supertype, - typename SymmetricDividesType::supertype> supertype; -}; - -template -struct DividesType, T2, true, - typename boostcopy::enable_if >::type> { - typedef DualNumber::supertype, - typename SymmetricDividesType< - typename SymmetricMultipliesType::supertype, - T - >::supertype - > supertype; -}; - - -template -struct DividesType, DualNumber, false> { - typedef DualNumber::supertype, - typename SymmetricMinusType< - typename SymmetricDividesType::supertype, - typename SymmetricDividesType< - typename SymmetricMultipliesType::supertype, - T2 - >::supertype - >::supertype - > supertype; -}; - -template -struct DividesType, DualNumber, true> { - typedef typename DividesType, DualNumber, false>::supertype supertype; -}; - -template -struct DividesType, DualNumber, false> { - typedef DualNumber::supertype, - typename SymmetricDividesType< - typename SymmetricMultipliesType::supertype, - T - >::supertype - >::supertype - > supertype; -}; - -template -struct DividesType, DualNumber, true> { - typedef typename DividesType, DualNumber, false>::supertype supertype; -}; - - - -template -struct CompareTypes, T2, reverseorder, - typename boostcopy::enable_if >::type> { - typedef DualNumber::supertype, - typename SymmetricCompareTypes< - typename SymmetricCompareTypes::supertype, - T - >::supertype> supertype; -}; - -template -struct CompareTypes, DualNumber > { - typedef DualNumber::supertype, - typename SymmetricCompareTypes< - typename SymmetricCompareTypes::supertype, - typename SymmetricCompareTypes::supertype - >::supertype - > supertype; -}; - -template -struct CompareTypes, DualNumber > { - typedef DualNumber::supertype> supertype; -}; - - -template -struct DerivativeType -{ - typedef typename T::derivatives_type::value_type type; -}; - - -template -struct DerivativesOf { - static - typename DerivativeType::type - derivative(const T& a, unsigned int derivativeindex) - { - return a.derivatives()[derivativeindex]; - } -}; - - -template -struct DerivativeOf { - static - typename DerivativeType::type - derivative(const T& a) - { - return a.derivatives().template get(); - } -}; - - -template -inline -D gradient(const DualNumber& a) -{ - return a.derivatives(); -} - - - -#endif // __dualnumber_h__ diff --git a/src/dualnumberarray.h b/src/dualnumberarray.h deleted file mode 100644 index 9870749..0000000 --- a/src/dualnumberarray.h +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef __dualnumberarray_h__ -#define __dualnumberarray_h__ - - -#include "dualnumber.h" -#include "numberarray.h" - - -template -struct DerivativeType > -{ - typedef NumberArray::type> type; -}; - - -template -struct DerivativesOf > -{ - static - typename DerivativeType >::type - derivative(const NumberArray& a, unsigned int derivativeindex) - { - typename DerivativeType >::type returnval; - for (unsigned int i=0; i != size; ++i) - returnval[i] = DerivativesOf::derivative(a[i], derivativeindex); - - return returnval; - } -}; - - -template -struct DerivativeOf, derivativeindex> -{ - static - typename DerivativeType >::type - derivative(const NumberArray& a) - { - typename DerivativeType >::type returnval; - for (unsigned int i=0; i != size; ++i) - returnval[i] = DerivativeOf::derivative(a[i]); - - return returnval; - } -}; - - -// For a vector of values a[i] each of which has a defined gradient, -// the divergence is the sum of derivative_wrt_xi(a[i]) - -// For a tensor of values, we take the divergence with respect to the -// first index. -template -inline -typename DerivativeType::type -divergence(const NumberArray& a) -{ - typename DerivativeType::type returnval = 0; - - for (unsigned int i=0; i != size; ++i) - returnval += DerivativesOf::derivative(a[i], i); - - return returnval; -} - - -// For a vector of values, the gradient is going to be a tensor -template -inline -NumberArray -gradient(const NumberArray& a) -{ - NumberArray returnval; - - for (unsigned int i=0; i != size; ++i) - returnval[i] = gradient(a[i]); - - return returnval; -} - -// DualNumber is subordinate to NumberArray - -#define DualNumberArray_comparisons(templatename) \ -template \ -struct templatename, DualNumber, reverseorder> { \ - typedef NumberArray, T2, reverseorder>::supertype> supertype; \ -} - -DualNumberArray_comparisons(CompareTypes); -DualNumberArray_comparisons(PlusType); -DualNumberArray_comparisons(MinusType); -DualNumberArray_comparisons(MultipliesType); -DualNumberArray_comparisons(DividesType); - -#endif // __dualnumberarray_h__ diff --git a/src/dualshadow.h b/src/dualshadow.h deleted file mode 100644 index 1e43bfb..0000000 --- a/src/dualshadow.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __dualshadow_h__ -#define __dualshadow_h__ - -// Order of declarations is important here? -#include "shadownumber.h" -#include "dualnumber.h" - -// ShadowNumber is subordinate to DualNumber: - -#define DualShadow_comparisons(templatename) \ -template \ -struct templatename, ShadowNumber, reverseorder> { \ - typedef DualNumber, reverseorder>::supertype, \ - typename Symmetric##templatename, reverseorder>::supertype> supertype; \ -} - -DualShadow_comparisons(CompareTypes); -DualShadow_comparisons(PlusType); -DualShadow_comparisons(MinusType); -DualShadow_comparisons(MultipliesType); -DualShadow_comparisons(DividesType); - -#endif // __dualshadow_h__ - diff --git a/src/dualshadowarray.h b/src/dualshadowarray.h deleted file mode 100644 index 265db24..0000000 --- a/src/dualshadowarray.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __dualshadowarray_h__ -#define __dualshadowarray_h__ - - -// Order of declarations is important here? -#include "dualshadow.h" -#include "dualnumberarray.h" - -// ShadowNumber is subordinate to NumberArray: - -#define DualShadowArray_comparisons(templatename) \ -template \ -struct templatename, ShadowNumber, reverseorder> { \ - typedef NumberArray, reverseorder>::supertype> supertype; \ -} - -DualShadowArray_comparisons(CompareTypes); -DualShadowArray_comparisons(PlusType); -DualShadowArray_comparisons(MinusType); -DualShadowArray_comparisons(MultipliesType); -DualShadowArray_comparisons(DividesType); - -#endif // __dualshadowarray_h__ diff --git a/src/numberarray.h b/src/numberarray.h deleted file mode 100644 index 2c7b475..0000000 --- a/src/numberarray.h +++ /dev/null @@ -1,387 +0,0 @@ - -#ifndef __numberarray_h__ -#define __numberarray_h__ - -#include -#include - -#include "compare_types.h" -#include "raw_type.h" - -template -class NumberArray -{ -public: - typedef T value_type; - - template - struct entry_type { - typedef value_type type; - }; - - template - struct rebind { - typedef NumberArray other; - }; - - NumberArray() {} - - NumberArray(const T& val) - { std::fill(_data, _data+size, val); } - - NumberArray(const T* vals) - { std::copy(vals, vals+size, _data); } - - template - NumberArray(NumberArray src) - { if (size) std::copy(&src[0], &src[0]+size, _data); } - - template - NumberArray(const T2& val) - { std::fill(_data, _data+size, T(val)); } - - T& operator[](unsigned int i) - { return _data[i]; } - - const T& operator[](unsigned int i) const - { return _data[i]; } - - template - typename entry_type::type& get() - { return _data[i]; } - - template - const typename entry_type::type& get() const - { return _data[i]; } - - NumberArray operator- () const { - NumberArray returnval; - for (unsigned int i=0; i != size; ++i) returnval[i] = -_data[i]; - return returnval; - } - - template - NumberArray& operator+= (const NumberArray& a) - { for (unsigned int i=0; i != size; ++i) _data[i] += a[i]; return *this; } - - template - NumberArray& operator+= (const T2& a) - { for (unsigned int i=0; i != size; ++i) _data[i] += a; return *this; } - - template - NumberArray& operator-= (const NumberArray& a) - { for (unsigned int i=0; i != size; ++i) _data[i] -= a[i]; return *this; } - - template - NumberArray& operator-= (const T2& a) - { for (unsigned int i=0; i != size; ++i) _data[i] -= a; return *this; } - - template - NumberArray& operator*= (const NumberArray& a) - { for (unsigned int i=0; i != size; ++i) _data[i] *= a[i]; return *this; } - - template - NumberArray& operator*= (const T2& a) - { for (unsigned int i=0; i != size; ++i) _data[i] *= a; return *this; } - - template - NumberArray& operator/= (const NumberArray& a) - { for (unsigned int i=0; i != size; ++i) _data[i] /= a[i]; return *this; } - - template - NumberArray& operator/= (const T2& a) - { for (unsigned int i=0; i != size; ++i) _data[i] /= a; return *this; } - - template - typename SymmetricMultipliesType::supertype - dot (const NumberArray& a) - { - typename SymmetricMultipliesType::supertype returnval = 0; - for (unsigned int i=0; i != size; ++i) - returnval += _data[i] * a[i]; - return returnval; - } - - template - NumberArray::supertype> > - outerproduct (const NumberArray& a) - { - NumberArray::supertype> > returnval; - - for (unsigned int i=0; i != size; ++i) - for (unsigned int j=0; j != size; ++j) - returnval[i][j] = _data[i] * a[j]; - - return returnval; - } - - static NumberArray > identity() - { - NumberArray > returnval(0); - - for (unsigned int i=0; i != size; ++i) - returnval[i][i] = 1; - - return returnval; - } - -private: - T _data[size]; -}; - - - -// -// Non-member functions -// - -template -struct NumberArrayOf -{ - typedef - typename CompareTypes::supertype - >::supertype - >::supertype - >::supertype - >::supertype - >::supertype - >::supertype supertype; - - typedef NumberArray type; -}; - - - -template -struct NumberArrayUnitVector -{ - typedef NumberArray type; - - static const type value() { - type returnval = 0; - returnval[index] = 1; - return returnval; - } -}; - - -template -struct NumberArrayFullVector -{ - typedef NumberArray type; - - static const type value() { - type returnval; - for (unsigned int i=0; i != size; ++i) - returnval[i] = 1; - return returnval; - } -}; - - - -template -inline -NumberArray > -transpose(const NumberArray >& a) -{ - NumberArray > returnval = a; - - for (unsigned int i=0; i != size; ++i) - for (unsigned int j=i+1; j != size; ++j) - std::swap(returnval[i][j], returnval[j][i]); - - return returnval; -} - - - -#define NumberArray_op_ab(opname, atype, btype, newtype) \ -template \ -inline \ -typename newtype::supertype \ -operator opname (const atype& a, const btype& b) \ -{ \ - typedef typename newtype::supertype TS; \ - TS returnval(a); \ - returnval opname##= b; \ - return returnval; \ -} - -#define NumberArray_op(opname, typecomparison) \ -NumberArray_op_ab(opname, NumberArray, NumberArray, \ - typecomparison##Type MacroComma NumberArray >) \ -NumberArray_op_ab(opname, T , NumberArray, \ - typecomparison##Type MacroComma T MacroComma true>) \ -NumberArray_op_ab(opname, NumberArray, T2 , \ - typecomparison##Type MacroComma T2>) - -NumberArray_op(+,Plus) -NumberArray_op(-,Minus) -NumberArray_op(*,Multiplies) -NumberArray_op(/,Divides) - -namespace std { - -#define NumberArray_std_unary(funcname) \ -template \ -inline \ -NumberArray \ -funcname (const NumberArray& a) \ -{ \ - NumberArray returnval; \ - \ - for (unsigned int i=0; i != size; ++i) \ - returnval[i] = std::funcname(a[i]); \ - \ - return returnval; \ -} - - -#define NumberArray_std_binary_abab(funcname, atype, btype, abtypes, aarg, barg) \ -template \ -inline \ -typename CompareTypes::supertype \ -funcname (const atype& a, const btype& b) \ -{ \ - typedef typename CompareTypes::supertype TS; \ - TS returnval; \ - \ - for (unsigned int i=0; i != size; ++i) \ - returnval[i] = std::funcname(aarg, barg); \ - \ - return returnval; \ -} - -#define NumberArray_std_binary(funcname) \ -NumberArray_std_binary_abab(funcname, NumberArray, NumberArray, \ - NumberArray MacroComma NumberArray, a[i], b[i]) \ -NumberArray_std_binary_abab(funcname, T , NumberArray, \ - NumberArray MacroComma T, a, b[i]) \ -NumberArray_std_binary_abab(funcname, NumberArray, T2 , \ - NumberArray MacroComma T2, a[i], b) - -NumberArray_std_binary(pow) -NumberArray_std_unary(exp) -NumberArray_std_unary(log) -NumberArray_std_unary(log10) -NumberArray_std_unary(sin) -NumberArray_std_unary(cos) -NumberArray_std_unary(tan) -NumberArray_std_unary(asin) -NumberArray_std_unary(acos) -NumberArray_std_unary(atan) -NumberArray_std_binary(atan2) -NumberArray_std_unary(sinh) -NumberArray_std_unary(cosh) -NumberArray_std_unary(tanh) -NumberArray_std_unary(sqrt) -NumberArray_std_unary(abs) -NumberArray_std_binary(max) -NumberArray_std_binary(min) -NumberArray_std_unary(ceil) -NumberArray_std_unary(floor) -NumberArray_std_binary(fmod) - - -template -class numeric_limits > : - public raw_numeric_limits, T> {}; - -} // namespace std - -#define NumberArray_operator_binary_abab(opname, atype, btype, aarg, barg) \ -template \ -inline \ -NumberArray \ -operator opname (const atype& a, const btype& b) \ -{ \ - NumberArray returnval; \ - \ - for (unsigned int i=0; i != size; ++i) \ - returnval[i] = (aarg opname barg); \ - \ - return returnval; \ -} - -#define NumberArray_operator_binary(opname) \ -NumberArray_operator_binary_abab(opname, NumberArray, NumberArray, a[i], b[i]) \ -NumberArray_operator_binary_abab(opname, T , NumberArray, a, b[i]) \ -NumberArray_operator_binary_abab(opname, NumberArray, T2 , a[i], b) - -NumberArray_operator_binary(<) -NumberArray_operator_binary(<=) -NumberArray_operator_binary(>) -NumberArray_operator_binary(>=) -NumberArray_operator_binary(==) -NumberArray_operator_binary(!=) - -template -inline -std::ostream& -operator<< (std::ostream& output, const NumberArray& a) -{ - output << '{'; - if (size) - output << a[0]; - for (unsigned int i=1; i \ -struct templatename, NumberArray, reverseorder> { \ - typedef NumberArray supertype; \ -}; \ - \ -template \ -struct templatename, NumberArray, reverseorder> { \ - typedef NumberArray::supertype> supertype; \ -}; \ - \ -template \ -struct templatename, T2, reverseorder, \ - typename boostcopy::enable_if >::type> { \ - typedef NumberArray::supertype> supertype; \ -} - -NumberArray_comparisons(CompareTypes); -NumberArray_comparisons(PlusType); -NumberArray_comparisons(MinusType); -NumberArray_comparisons(MultipliesType); -NumberArray_comparisons(DividesType); - -template -struct RawType > -{ - typedef NumberArray::value_type> value_type; - - static value_type value(const NumberArray& a) - { - value_type returnval; - for (unsigned int i=0; i != size; ++i) - returnval[i] = RawType::value(a[i]); - return returnval; - } -}; - -#endif // __numberarray_h__ diff --git a/src/raw_type.h b/src/raw_type.h deleted file mode 100644 index 70eb6ab..0000000 --- a/src/raw_type.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef __raw_type_h__ -#define __raw_type_h__ - -#include - - -template -struct RawType -{ - typedef T value_type; - - static value_type value(const T& a) { return a; } -}; - -// Make the user syntax slightly nicer -template -inline -typename RawType::value_type -raw_value(const T& a) { return RawType::value(a); } - - -template -class raw_numeric_limits -{ -public: - static const bool is_specialized = true; - static OldType min() throw() { return NewType(std::numeric_limits::min()); } - static OldType max() throw() { return NewType(std::numeric_limits::max()); } - static const int digits = std::numeric_limits::digits; - static const int digits10 = std::numeric_limits::digits10; - static const bool is_signed = std::numeric_limits::is_signed; - static const bool is_integer = std::numeric_limits::is_integer; - static const bool is_exact = std::numeric_limits::is_exact; - static const int radix = std::numeric_limits::radix; - static OldType epsilon() throw() {return NewType(std::numeric_limits::epsilon()); } - static OldType round_error() throw() {return NewType(std::numeric_limits::round_error()); } - - static const int min_exponent = std::numeric_limits::min_exponent; - static const int min_exponent10 = std::numeric_limits::min_exponent10; - static const int max_exponent = std::numeric_limits::max_exponent; - static const int max_exponent10 = std::numeric_limits::max_exponent10; - - static const bool has_infinity = std::numeric_limits::has_infinity; - static const bool has_quiet_NaN = std::numeric_limits::has_quiet_NaN; - static const bool has_signaling_NaN = std::numeric_limits::has_signaling_NaN; - static const std::float_denorm_style has_denorm = std::numeric_limits::has_denorm; - static const bool has_denorm_loss = std::numeric_limits::has_denorm_loss; - static OldType infinity() throw() {return NewType(std::numeric_limits::infinity()); } - static OldType quiet_NaN() throw() {return NewType(std::numeric_limits::quiet_NaN()); } - static OldType signaling_NaN() throw() {return NewType(std::numeric_limits::signaling_NaN()); } - static OldType denorm_min() throw() {return NewType(std::numeric_limits::denorm_min()); } - - static const bool is_iec559 = std::numeric_limits::is_iec559; - static const bool is_bounded = std::numeric_limits::is_bounded; - static const bool is_modulo = std::numeric_limits::is_modulo; - - static const bool traps = std::numeric_limits::traps; - static const bool tinyness_before = std::numeric_limits::tinyness_before; - static const std::float_round_style round_style = std::numeric_limits::round_style; -}; - - -#endif // __raw_type_h__ diff --git a/src/shadownumber.h b/src/shadownumber.h deleted file mode 100644 index 70b8136..0000000 --- a/src/shadownumber.h +++ /dev/null @@ -1,296 +0,0 @@ - -#ifndef __shadownumber_h__ -#define __shadownumber_h__ - -#include -#include - -#include "compare_types.h" -#include "raw_type.h" -#include "testable.h" - -template -class ShadowNumber : public safe_bool > -{ -public: - typedef T value_type; - - typedef S shadow_type; - - ShadowNumber() {} - - template - ShadowNumber(const T2& v, const S2& s) : _val(v), _shadow(s) {} - - template - ShadowNumber(const T2& val) : _val(val), _shadow(val) {} - - template - ShadowNumber(ShadowNumber& other) : _val(other._val), _shadow(other._shadow) {} - - T& value() { return _val; } - - const T& value() const { return _val; } - - S& shadow() { return _shadow; } - - const S& shadow() const { return _shadow; } - - bool boolean_test() const { return _val; } - - ShadowNumber operator- () const { return ShadowNumber (-_val, -_shadow); } - - template - ShadowNumber& operator+= (const ShadowNumber& a) - { _val += a.value(); _shadow += a.shadow(); return *this; } - - template - ShadowNumber& operator+= (const T2& a) - { _val += a; _shadow += a; return *this; } - - template - ShadowNumber& operator-= (const ShadowNumber& a) - { _val -= a.value(); _shadow -= a.shadow(); return *this; } - - template - ShadowNumber& operator-= (const T2& a) - { _val -= a; _shadow -= a; return *this; } - - template - ShadowNumber& operator*= (const ShadowNumber& a) - { _val *= a.value(); _shadow *= a.shadow(); return *this; } - - template - ShadowNumber& operator*= (const T2& a) - { _val *= a; _shadow *= a; return *this; } - - template - ShadowNumber& operator/= (const ShadowNumber& a) - { _val /= a.value(); _shadow /= a.shadow(); return *this; } - - template - ShadowNumber& operator/= (const T2& a) - { _val /= a; _shadow /= a; return *this; } - -private: - T _val; - S _shadow; -}; - -// -// Non-member functions -// - -#define ShadowNumber_op(opname) \ -template \ -inline \ -typename CompareTypes,ShadowNumber >::supertype \ -operator opname (const ShadowNumber& a, const ShadowNumber& b) \ -{ \ - typedef typename CompareTypes,ShadowNumber >::supertype TS; \ - TS returnval(a); \ - returnval opname##= b; \ - return returnval; \ -} \ - \ -template \ -inline \ -typename CompareTypes,T2>::supertype \ -operator opname (const ShadowNumber& a, const T2& b) \ -{ \ - typedef typename CompareTypes,T2>::supertype TS; \ - TS returnval(a); \ - returnval opname##= b; \ - return returnval; \ - \ -} \ -template \ -inline \ -typename CompareTypes,T>::supertype \ -operator opname (const T& a, const ShadowNumber& b) \ -{ \ - typedef typename CompareTypes,T>::supertype TS; \ - TS returnval(a); \ - returnval opname##= b; \ - return returnval; \ -} - -ShadowNumber_op(+) -ShadowNumber_op(-) -ShadowNumber_op(*) -ShadowNumber_op(/) - -namespace std { - -// Now just combined declaration/definitions - -#define ShadowNumber_std_unary(funcname) \ -template \ -inline \ -ShadowNumber \ -funcname (const ShadowNumber& a) \ -{ \ - return ShadowNumber(std::funcname(a.value()), std::funcname(a.shadow())); \ -} - - -#define ShadowNumber_std_binary(funcname) \ -template \ -inline \ -typename CompareTypes,ShadowNumber >::supertype \ -funcname (const ShadowNumber& a, const ShadowNumber& b) \ -{ \ - typedef typename CompareTypes,ShadowNumber >::supertype TS; \ - return TS (std::funcname(a.value(), b.value()), \ - std::funcname(a.shadow(), b.shadow())); \ -} \ - \ -template \ -inline \ -typename CompareTypes,T2>::supertype \ -funcname (const ShadowNumber& a, const T2& b) \ -{ \ - typedef typename CompareTypes,T2>::supertype TS; \ - return TS (std::funcname(a.value(), b), \ - std::funcname(a.shadow(), b)); \ -} \ - \ -template \ -inline \ -typename CompareTypes,T>::supertype \ -funcname (const T& a, const ShadowNumber& b) \ -{ \ - typedef typename CompareTypes,T>::supertype TS; \ - return TS (std::funcname(a, b.value()), \ - std::funcname(a, b.shadow())); \ -} - - -ShadowNumber_std_binary(pow) -ShadowNumber_std_unary(exp) -ShadowNumber_std_unary(log) -ShadowNumber_std_unary(log10) -ShadowNumber_std_unary(sin) -ShadowNumber_std_unary(cos) -ShadowNumber_std_unary(tan) -ShadowNumber_std_unary(asin) -ShadowNumber_std_unary(acos) -ShadowNumber_std_unary(atan) -ShadowNumber_std_binary(atan2) -ShadowNumber_std_unary(sinh) -ShadowNumber_std_unary(cosh) -ShadowNumber_std_unary(tanh) -ShadowNumber_std_unary(sqrt) -ShadowNumber_std_unary(abs) -ShadowNumber_std_binary(max) -ShadowNumber_std_binary(min) -ShadowNumber_std_unary(ceil) -ShadowNumber_std_unary(floor) -ShadowNumber_std_binary(fmod) - -template -class numeric_limits > : - public raw_numeric_limits, T> {}; - -} // namespace std - -#define ShadowNumber_operator_binary(opname) \ -template \ -inline \ -ShadowNumber \ -operator opname (const ShadowNumber& a, const ShadowNumber& b) \ -{ \ - return ShadowNumber (a.value() opname b.value(), a.shadow() opname b.shadow()); \ -} \ - \ -template \ -inline \ -typename boostcopy::enable_if_class< \ - typename CompareTypes,T2>::supertype, \ - ShadowNumber \ ->::type \ -operator opname (const ShadowNumber& a, const T2& b) \ -{ \ - return ShadowNumber (a.value() opname b, a.shadow() opname b); \ -} \ - \ -template \ -inline \ -typename boostcopy::enable_if_class< \ - typename CompareTypes,T>::supertype, \ - ShadowNumber \ ->::type \ -operator opname (const T& a, const ShadowNumber& b) \ -{ \ - return ShadowNumber (a opname b.value(), a opname b.shadow()); \ -} - - -ShadowNumber_operator_binary(<) -ShadowNumber_operator_binary(<=) -ShadowNumber_operator_binary(>) -ShadowNumber_operator_binary(>=) -ShadowNumber_operator_binary(==) -ShadowNumber_operator_binary(!=) - -template -inline -std::ostream& -operator<< (std::ostream& output, const ShadowNumber& a) -{ - return output << '(' << a.value() << ',' << a.shadow() << ')'; -} - - -// ScalarTraits, RawType, CompareTypes specializations - -template -struct ScalarTraits > -{ - static const bool value = ScalarTraits::value; -}; - -#define ShadowNumber_comparisons(templatename) \ -template \ -struct templatename, ShadowNumber, reverseorder> { \ - typedef ShadowNumber supertype; \ -}; \ - \ -template \ -struct templatename, ShadowNumber, reverseorder> { \ - typedef ShadowNumber::supertype, \ - typename Symmetric##templatename::supertype> supertype; \ -}; \ - \ -template \ -struct templatename, T2, reverseorder, \ - typename boostcopy::enable_if >::type> { \ - typedef ShadowNumber::supertype, \ - typename Symmetric##templatename::supertype> supertype; \ -} - -ShadowNumber_comparisons(CompareTypes); -ShadowNumber_comparisons(PlusType); -ShadowNumber_comparisons(MinusType); -ShadowNumber_comparisons(MultipliesType); -ShadowNumber_comparisons(DividesType); - - - -template -struct RawType > -{ - typedef typename RawType::value_type value_type; - - static value_type value(const ShadowNumber& a) { - const S max_value = std::max(S(a.value()), a.shadow()); - if (max_value) { - const S relative_error = (a.value() - a.shadow()) / max_value; - if (relative_error > 10*std::numeric_limits::epsilon()) - std::cerr << "Shadow relative error = " << relative_error << std::endl; - } - return a.value(); - } -}; - -#endif // __shadownumber_h__ diff --git a/src/testable.h b/src/testable.h deleted file mode 100644 index 0e075b4..0000000 --- a/src/testable.h +++ /dev/null @@ -1,91 +0,0 @@ - - -#ifndef __testable_h__ -#define __testable_h__ - -class safe_bool_base { - public: - typedef void (safe_bool_base::*bool_type)() const; - void this_type_does_not_support_comparisons() const {} - protected: - - safe_bool_base() {} - safe_bool_base(const safe_bool_base&) {} - safe_bool_base& operator=(const safe_bool_base&) {return *this;} - ~safe_bool_base() {} -}; - -// For testability without virtual function. -template -class safe_bool : private safe_bool_base { - // private or protected inheritance is very important here as it - // triggers the - // access control violation in main. - public: - operator bool_type() const { - return (static_cast(this))->boolean_test() - ? &safe_bool_base::this_type_does_not_support_comparisons : 0; - } - protected: - ~safe_bool() {} -}; - - -// For testability with a virtual function. -template<> -class safe_bool : private safe_bool_base { - // private or protected inheritance is very important here as it - // triggers the - // access control violation in main. - public: - operator bool_type() const { - safe_bool_base::this_type_does_not_support_comparisons(); - - return boolean_test() - ? &safe_bool_base::this_type_does_not_support_comparisons : 0; - } - protected: - virtual bool boolean_test() const=0; - virtual ~safe_bool() {} -}; - -template - inline bool operator==(const safe_bool& lhs, bool b) { - if (b) - { - if (lhs) return true; - else return false; - } - else - { - if (lhs) return false; - else return true; - } - } - -template - inline bool operator==(bool b, const safe_bool& rhs) { - if (b) - { - if (rhs) return true; - else return false; - } - else - { - if (rhs) return false; - else return true; - } - } - - -template - inline void operator==(const safe_bool& lhs,const safe_bool& rhs) { - lhs.this_type_does_not_support_comparisons(); - } - -template - inline void operator!=(const safe_bool& lhs,const safe_bool& rhs) { - lhs.this_type_does_not_support_comparisons(); - } - -#endif //__testable_h__