Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53116 - in sandbox/numeric_adaptor: boost/numeric_adaptor libs/numeric_adaptor
From: barend.gehrels_at_[hidden]
Date: 2009-05-19 16:19:10


Author: barendgehrels
Date: 2009-05-19 16:19:09 EDT (Tue, 19 May 2009)
New Revision: 53116
URL: http://svn.boost.org/trac/boost/changeset/53116

Log:
Updated string-conversion by adding get
Added few functions (abs,hypot,tan,arctan)
Few style changes
Updated the sample
Text files modified:
   sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp | 10 +++++
   sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp | 40 +++++++++++++++------
   sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp | 10 +++++
   sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp | 72 +++++++++++++++++++++++++++++++++------
   sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp | 66 +++++++++++++++++++++++-------------
   5 files changed, 149 insertions(+), 49 deletions(-)

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp 2009-05-19 16:19:09 EDT (Tue, 19 May 2009)
@@ -46,6 +46,11 @@
         value = v.c_str();
     }
 
+ static inline void abs(type& r, type const& a)
+ {
+ r = cln::abs(a);
+ }
+
 
     static inline void sqrt(type& r, type const& a)
     {
@@ -62,6 +67,11 @@
         r = cln::sin(a);
     }
 
+ static inline void hypot(type& r, type const& a, type const& b)
+ {
+ r = cln::sqrt(a * a + b * b);
+ }
+
 
     template <typename OtherType>
     static inline OtherType big_numeric_cast(type const& b)

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp 2009-05-19 16:19:09 EDT (Tue, 19 May 2009)
@@ -45,53 +45,69 @@
     }
 
 
- static inline void copy(const type& source, type& dest)
+ static inline void copy(type const& source, type& dest)
     {
         mpf_set(dest, source);
     }
 
- static inline void add(type& r, const type& a, const type& b)
+ static inline void add(type& r, type const& a, type const& b)
     {
         mpf_add(r, a, b);
     }
 
- static inline void subtract(type& r, const type& a, const type& b)
+ static inline void subtract(type& r, type const& a, type const& b)
     {
         mpf_sub(r, a, b);
     }
 
- static inline void multiply(type& r, const type& a, const type& b)
+ static inline void multiply(type& r, type const& a, type const& b)
     {
         mpf_mul(r, a, b);
     }
 
- static inline void divide(type& r, const type& a, const type& b)
+ static inline void divide(type& r, type const& a, type const& b)
     {
         mpf_div(r, a, b);
     }
 
- static inline void sqrt(type& r, const type& a)
+ static inline void abs(type& r, type const& a)
+ {
+ mpf_abs(r, a);
+ }
+
+ static inline void sqrt(type& r, type const& a)
     {
         mpf_sqrt(r, a);
     }
 
- static inline void cos(type& r, const type& a)
+ static inline void cos(type& r, type const& a)
     {
- // COS is not available.
+ // COS is not available in GMP
         long double d = mpf_get_d(a);
         mpf_set_d(r, std::cos(d));
     }
 
- static inline void sin(type& r, const type& a)
+ static inline void sin(type& r, type const& a)
     {
- // SIN is not available.
+ // SIN is not available in GMP
         long double d = mpf_get_d(a);
         mpf_set_d(r, std::sin(d));
     }
 
+ static inline void hypot(type& r, type const& a, type const& b)
+ {
+ mpf_mul(r, a, a);
+ type t;
+ mpf_init(t);
+ mpf_mul(t, b, b);
+ mpf_add(t, r, t);
+ mpf_sqrt(r, t);
+ mpf_clear(t);
+ }
+
 
     template <typename OtherType>
- static inline OtherType big_numeric_cast(const type& b)
+ static inline OtherType big_numeric_cast(type const& b)
     {
         return mpf_get_d(b);
     }
@@ -121,7 +137,7 @@
     }
 
 
- static inline int compare(const type& a, const type& b)
+ static inline int compare(type const& a, type const& b)
     {
         return mpf_cmp(a, b);
     }

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp 2009-05-19 16:19:09 EDT (Tue, 19 May 2009)
@@ -17,10 +17,11 @@
 
 #include <boost/numeric_adaptor/default_policy.hpp>
 
-
 #include <boost/numeric/conversion/cast.hpp>
 #include <boost/lexical_cast.hpp>
 
+#include <boost/math/special_functions/hypot.hpp>
+
 
 template <typename T>
 struct ieee_policy : public default_policy<T>
@@ -35,6 +36,13 @@
     static inline void sqrt(type& r, type const& a) { r = std::sqrt(a); }
     static inline void cos(type& r, type const& a) { r = std::cos(a); }
     static inline void sin(type& r, type const& a) { r = std::sin(a); }
+ static inline void tan(type& r, type const& a) { r = std::tan(a); }
+ static inline void atan(type& r, type const& a) { r = std::atan(a); }
+ static inline void abs(type& r, type const& a) { r = std::abs(a); }
+ static inline void hypot(type& r, type const& a, type const& b)
+ {
+ r = boost::math::hypot(a, b);
+ }
 
     template <typename OtherType>
     static inline OtherType big_numeric_cast(type const& v)

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp 2009-05-19 16:19:09 EDT (Tue, 19 May 2009)
@@ -11,9 +11,10 @@
 #define NUMERIC_ADAPTOR_NUMERIC_ADAPTOR_HPP_
 
 
+#include <boost/static_assert.hpp>
 
 
-template <typename Policy, typename ToType>
+template <typename Policy, typename ToType, bool IsCasted>
 struct caster
 {
     static inline ToType cast(typename Policy::type const& value)
@@ -22,22 +23,22 @@
     }
 };
 
-// specialization for strings
-/*
+// specialization for strings: casting is not possible, getting is OK
 template <typename Policy>
-struct caster<Policy, std::string>
+struct caster<Policy, std::string, true>
 {
- static inline std::string cast(typename Policy::type const& value)
+ struct CAST_TO_STRING_IS_NOT_POSSIBLE {};
+
+ // PROHIBITED!
+ static inline CAST_TO_STRING_IS_NOT_POSSIBLE cast(typename Policy::type const& )
     {
- return Policy::as_string(value);
     }
 };
-*/
 
 template <typename Policy>
-struct caster<Policy, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >
+struct caster<Policy, std::string, false>
 {
- static inline std::basic_string<char, std::char_traits<char>, std::allocator<char> > cast(typename Policy::type const& value)
+ static inline std::string cast(typename Policy::type const& value)
     {
         return Policy::as_string(value);
     }
@@ -45,6 +46,7 @@
 
 
 
+
 template <typename Policy>
 struct numeric_adaptor
 {
@@ -67,6 +69,11 @@
         Policy::set(value, v);
     }
 
+ inline numeric_adaptor(const char* v)
+ {
+ Policy::init(value);
+ Policy::set(value, std::string(v));
+ }
 
     // Constructor with a normal IEEE type
     template <typename FromType>
@@ -97,11 +104,18 @@
         return *this;
     }
 
- // Cast to normal IEEE type or to std::string
+ // Cast to normal IEEE type but NOT to std::string because of compilation problems
     template <typename ToType>
     inline operator ToType() const
     {
- return caster<Policy, ToType>::cast(value);
+ return caster<Policy, ToType, true>::cast(value);
+ }
+
+ // tuple/fusion/variant-like get template function
+ template <typename ToType>
+ inline ToType get() const
+ {
+ return caster<Policy, ToType, false>::cast(value);
     }
 
 
@@ -163,6 +177,14 @@
     }
 
     // Functions
+ static inline numeric_adaptor<Policy> abs(numeric_adaptor<Policy> const& v)
+ {
+ typename Policy::type r;
+ Policy::init(r);
+ Policy::abs(r, v.value);
+ return numeric_adaptor<Policy>(r, true);
+ }
+
     static inline numeric_adaptor<Policy> sqrt(numeric_adaptor<Policy> const& v)
     {
         typename Policy::type r;
@@ -187,11 +209,37 @@
         return numeric_adaptor<Policy>(r, true);
     }
 
+ static inline numeric_adaptor<Policy> tan(numeric_adaptor<Policy> const& v)
+ {
+ typename Policy::type r;
+ Policy::init(r);
+ Policy::tan(r, v.value);
+ return numeric_adaptor<Policy>(r, true);
+ }
+
+ static inline numeric_adaptor<Policy> atan(numeric_adaptor<Policy> const& v)
+ {
+ typename Policy::type r;
+ Policy::init(r);
+ Policy::atan(r, v.value);
+ return numeric_adaptor<Policy>(r, true);
+ }
+
+
+ static inline numeric_adaptor<Policy> hypot(numeric_adaptor<Policy> const& a,
+ numeric_adaptor<Policy> const& b)
+ {
+ typename Policy::type r;
+ Policy::init(r);
+ Policy::hypot(r, a.value, b.value);
+ return numeric_adaptor<Policy>(r, true);
+ }
+
 
 private :
     typename Policy::type value;
 
- // Constructor with a type. Bool (or any other signature changing parameter)
+ // Construct from a policy-type. Bool (or any other signature changing parameter)
     // is necessary for cases where type == OtherType
     inline numeric_adaptor<Policy>(typename Policy::type const& v, bool)
     {

Modified: sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp
==============================================================================
--- sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp (original)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/sample.cpp 2009-05-19 16:19:09 EDT (Tue, 19 May 2009)
@@ -18,33 +18,36 @@
 # include <boost/numeric_adaptor/cln_policy.hpp>
 #endif
 
-
+template <typename T, typename N>
+inline T get(N const& bn)
+{
+ return bn.template get<T>();
+}
 
 template <typename T, typename Num>
-void sample1(const std::string& header, T const& c1, T const& c2, T const& c4)
+void sample1(const std::string& header, T const& t_a, T const& t_b, T const& t_c)
 {
     std::cout << std::endl << "---" << header << std::endl;
- Num v1 = c1;
- Num v2 = c2;
- Num v3 = v1 + v2;
-
- // NOTE: strings can NOT be casted / streamed in one line for GCC/Comeau (for MSVC/DMC it is OK)
- // See http://www.codeguru.com/forum/showthread.php?p=1842725
- T t3 = v3;
- std::cout << "a + b: " << t3 << std::endl;
-
- Num v4 = c4;
- Num v5 = (v1 + v2) * v4;
- T t5 = v5;
- std::cout << " " << t5;
- v5 = v1 + v2 * v4;
- T t5a = v5;
- std::cout << " " << t5a;
-
- Num v6 = Num::sqrt(v3);
- T t6 = v6;
- std::cout << " " << t6 << std::endl;
-
+ Num a = t_a;
+ Num b = t_b;
+ Num a_plus_b = a + b;
+
+ //T t3 = a_plus_b; // does NOT compile for strings
+ std::cout << "a + b: " << a_plus_b.template get<T>() << std::endl;
+
+ Num c = t_c;
+ Num par = (a + b) * c;
+ std::cout << "(a + b) c: " << par.template get<T>() << std::endl;
+ par = a + b * c;
+ std::cout << "a + bc: " << par.template get<T>() << std::endl;
+
+ Num sqrt_a_plus_b = Num::sqrt(a_plus_b);
+ std::cout << "sqrt(a+b): " << sqrt_a_plus_b.template get<T>() << std::endl;
+
+ // Calc the hypot function
+ Num hypot_b_c = Num::hypot(b, c);
+ std::cout << "hypot: " << hypot_b_c.template get<T>() << std::endl;
+ std::cout << "hypot2: " << get<T>(Num::sqrt(b * b + c * c)) << std::endl;
 }
 
 
@@ -81,6 +84,8 @@
 
     Num v7 = Num::cos(v1);
     std::cout << "cos: " << T(v7) << std::endl;
+ v7 = Num::abs(v7);
+ std::cout << "abs: " << T(v7) << std::endl;
     v7 = Num::sin(v1);
     std::cout << "sin: " << T(v7) << std::endl;
 
@@ -98,7 +103,7 @@
 int main()
 {
     mpf_set_default_prec(128);
- std::cout << std::setprecision(12);
+ std::cout << std::setprecision(18);
 
     long_double_issue();
 
@@ -119,11 +124,24 @@
     sample2<float, numeric_adaptor<ieee_policy<double> > >("use float, calculate with double", a, b, c);
     sample2<double, numeric_adaptor<ieee_policy<double> > >("use double, calculate with double", a, b, c);
 
+
 #if ! defined(_MSC_VER)
     sample2<double, numeric_adaptor<gmp_policy> >("use double, calculate with gmp", a, b, c);
     sample2<double, numeric_adaptor<cln_policy> >("use double, calculate with CLN", a, b, c);
 #endif
 
 
+ {
+ double a = -3;
+ double b = -4;
+ std::cout << hypot(a, b) << std::endl;
+ std::cout << boost::math::hypot(a, b) << std::endl;
+
+ double rat = a / b;
+ double h = b * sqrt(1 + rat*rat);
+ std::cout << h << std::endl;
+
+ }
+
     return 0;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk