complex.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file complex.hpp
  3. ///
  4. // Copyright 2005 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_NUMERIC_FUNCTIONAL_COMPLEX_HPP_EAN_01_17_2006
  8. #define BOOST_NUMERIC_FUNCTIONAL_COMPLEX_HPP_EAN_01_17_2006
  9. #ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
  10. # error Include this file before boost/accumulators/numeric/functional.hpp
  11. #endif
  12. #include <complex>
  13. #include <boost/mpl/or.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/utility/enable_if.hpp>
  16. #include <boost/typeof/std/complex.hpp>
  17. #include <boost/accumulators/numeric/functional_fwd.hpp>
  18. namespace boost { namespace numeric { namespace operators
  19. {
  20. // So that the stats compile when Sample type is std::complex
  21. template<typename T, typename U>
  22. typename
  23. disable_if<
  24. mpl::or_<is_same<T, U>, is_same<std::complex<T>, U> >
  25. , std::complex<T>
  26. >::type
  27. operator *(std::complex<T> ri, U const &u)
  28. {
  29. // BUGBUG promote result to typeof(T()*u) ?
  30. return ri *= static_cast<T>(u);
  31. }
  32. template<typename T, typename U>
  33. typename
  34. disable_if<
  35. mpl::or_<is_same<T, U>, is_same<std::complex<T>, U> >
  36. , std::complex<T>
  37. >::type
  38. operator /(std::complex<T> ri, U const &u)
  39. {
  40. // BUGBUG promote result to typeof(T()*u) ?
  41. return ri /= static_cast<T>(u);
  42. }
  43. }}} // namespace boost::numeric::operators
  44. namespace boost { namespace numeric
  45. {
  46. namespace detail
  47. {
  48. template<typename T>
  49. struct one_complex
  50. {
  51. static std::complex<T> const value;
  52. };
  53. template<typename T>
  54. std::complex<T> const one_complex<T>::value
  55. = std::complex<T>(numeric::one<T>::value, numeric::one<T>::value);
  56. }
  57. /// INTERNAL ONLY
  58. ///
  59. template<typename T>
  60. struct one<std::complex<T> >
  61. : detail::one_complex<T>
  62. {
  63. typedef one type;
  64. typedef std::complex<T> value_type;
  65. operator value_type const & () const
  66. {
  67. return detail::one_complex<T>::value;
  68. }
  69. };
  70. }} // namespace boost::numeric
  71. #endif