1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_MATHFUNCTIONS_H
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_MATHFUNCTIONS_H
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \internal \struct global_math_functions_filtering_base
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * What it does:
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Defines a typedef 'type' as follows:
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * - if type T has a member typedef Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl, then
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *   global_math_functions_filtering_base<T>::type is a typedef for it.
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * - otherwise, global_math_functions_filtering_base<T>::type is a typedef for T.
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * How it's used:
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * To allow to defined the global math functions (like sin...) in certain cases, like the Array expressions.
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * When you do sin(array1+array2), the object array1+array2 has a complicated expression type, all what you want to know
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * is that it inherits ArrayBase. So we implement a partial specialization of sin_impl for ArrayBase<Derived>.
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * So we must make sure to use sin_impl<ArrayBase<Derived> > and not sin_impl<Derived>, otherwise our partial specialization
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * won't be used. How does sin know that? That's exactly what global_math_functions_filtering_base tells it.
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * How it's implemented:
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * SFINAE in the style of enable_if. Highly susceptible of breaking compilers. With GCC, it sure does work, but if you replace
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * the typename dummy by an integer template parameter, it doesn't work anymore!
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T, typename dummy = void>
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct global_math_functions_filtering_base
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef T type;
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T> struct always_void { typedef void type; };
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T>
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct global_math_functions_filtering_base
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  <T,
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath   typename always_void<typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl>::type
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  >
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl type;
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_MATHFUNC_IMPL(func, scalar) func##_impl<typename global_math_functions_filtering_base<scalar>::type>
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_MATHFUNC_RETVAL(func, scalar) typename func##_retval<typename global_math_functions_filtering_base<scalar>::type>::type
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of real                                                 *
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct real_impl
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const Scalar& x)
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x;
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename RealScalar>
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct real_impl<std::complex<RealScalar> >
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const std::complex<RealScalar>& x)
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::real;
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return real(x);
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct real_retval
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real type;
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(real, Scalar) real(const Scalar& x)
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(real, Scalar)::run(x);
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of imag                                                 *
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct imag_impl
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const Scalar&)
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return RealScalar(0);
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename RealScalar>
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct imag_impl<std::complex<RealScalar> >
110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const std::complex<RealScalar>& x)
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::imag;
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return imag(x);
115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct imag_retval
120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real type;
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(imag, Scalar) imag(const Scalar& x)
126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(imag, Scalar)::run(x);
128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of real_ref                                             *
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct real_ref_impl
136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar& run(Scalar& x)
139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return reinterpret_cast<RealScalar*>(&x)[0];
141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline const RealScalar& run(const Scalar& x)
143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return reinterpret_cast<const RealScalar*>(&x)[0];
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct real_ref_retval
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real & type;
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) >::type real_ref(const Scalar& x)
156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return real_ref_impl<Scalar>::run(x);
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) real_ref(Scalar& x)
162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(real_ref, Scalar)::run(x);
164c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
165c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
167c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of imag_ref                                             *
168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
169c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar, bool IsComplex>
171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct imag_ref_default_impl
172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
174c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar& run(Scalar& x)
175c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
176c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return reinterpret_cast<RealScalar*>(&x)[1];
177c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
178c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline const RealScalar& run(const Scalar& x)
179c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
180c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return reinterpret_cast<RealScalar*>(&x)[1];
181c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
182c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
183c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
184c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
185c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct imag_ref_default_impl<Scalar, false>
186c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
187c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(Scalar&)
188c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
189c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return Scalar(0);
190c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
191c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline const Scalar run(const Scalar&)
192c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
193c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return Scalar(0);
194c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
195c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
196c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
197c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
198c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct imag_ref_impl : imag_ref_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
199c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
200c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
201c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct imag_ref_retval
202c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
203c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real & type;
204c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
205c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
206c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
207c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) >::type imag_ref(const Scalar& x)
208c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
209c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return imag_ref_impl<Scalar>::run(x);
210c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
211c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
212c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
213c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) imag_ref(Scalar& x)
214c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
215c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(imag_ref, Scalar)::run(x);
216c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
217c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
218c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
219c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of conj                                                 *
220c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
221c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
222c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
223c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct conj_impl
224c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
225c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x)
226c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
227c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x;
228c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
229c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
230c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
231c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename RealScalar>
232c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct conj_impl<std::complex<RealScalar> >
233c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
234c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline std::complex<RealScalar> run(const std::complex<RealScalar>& x)
235c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
236c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::conj;
237c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return conj(x);
238c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
239c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
240c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
241c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
242c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct conj_retval
243c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
244c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Scalar type;
245c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
246c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
247c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
248c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(conj, Scalar) conj(const Scalar& x)
249c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
250c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
251c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
252c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
253c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
254c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of abs                                                  *
255c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
256c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
257c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
258c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct abs_impl
259c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
260c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
261c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const Scalar& x)
262c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
263c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::abs;
264c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return abs(x);
265c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
266c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
267c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
268c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
269c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct abs_retval
270c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
271c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real type;
272c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
273c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
274c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
275c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(abs, Scalar) abs(const Scalar& x)
276c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
277c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(abs, Scalar)::run(x);
278c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
279c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
280c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
281c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of abs2                                                 *
282c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
283c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
284c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
285c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct abs2_impl
286c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
287c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
288c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const Scalar& x)
289c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
290c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x*x;
291c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
292c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
293c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
294c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename RealScalar>
295c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct abs2_impl<std::complex<RealScalar> >
296c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
297c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const std::complex<RealScalar>& x)
298c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
299c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return real(x)*real(x) + imag(x)*imag(x);
300c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
301c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
302c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
303c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
304c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct abs2_retval
305c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
306c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real type;
307c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
308c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
309c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
310c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(abs2, Scalar) abs2(const Scalar& x)
311c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
312c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
313c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
314c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
315c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
316c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of norm1                                                *
317c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
318c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
319c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar, bool IsComplex>
320c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct norm1_default_impl
321c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
322c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
323c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const Scalar& x)
324c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
325c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return abs(real(x)) + abs(imag(x));
326c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
327c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
328c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
329c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
330c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct norm1_default_impl<Scalar, false>
331c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
332c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x)
333c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
334c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return abs(x);
335c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
336c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
337c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
338c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
339c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct norm1_impl : norm1_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
340c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
341c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
342c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct norm1_retval
343c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
344c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real type;
345c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
346c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
347c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
348c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(norm1, Scalar) norm1(const Scalar& x)
349c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
350c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
351c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
352c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
353c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
354c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of hypot                                                *
355c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
356c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
357c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
358c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct hypot_impl
359c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
360c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
361c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline RealScalar run(const Scalar& x, const Scalar& y)
362c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
363c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::max;
364c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::min;
365c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    RealScalar _x = abs(x);
366c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    RealScalar _y = abs(y);
367c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    RealScalar p = (max)(_x, _y);
368c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    RealScalar q = (min)(_x, _y);
369c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    RealScalar qp = q/p;
370c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return p * sqrt(RealScalar(1) + qp*qp);
371c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
372c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
373c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
374c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
375c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct hypot_retval
376c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
377c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real type;
378c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
379c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
380c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
381c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(hypot, Scalar) hypot(const Scalar& x, const Scalar& y)
382c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
383c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
384c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
385c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
386c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
387c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of cast                                                 *
388c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
389c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
390c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename OldType, typename NewType>
391c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct cast_impl
392c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
393c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline NewType run(const OldType& x)
394c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
395c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return static_cast<NewType>(x);
396c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
397c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
398c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
399c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// here, for once, we're plainly returning NewType: we don't want cast to do weird things.
400c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
401c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename OldType, typename NewType>
402c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline NewType cast(const OldType& x)
403c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
404c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return cast_impl<OldType, NewType>::run(x);
405c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
406c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
407c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
408c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of sqrt                                                 *
409c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
410c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
411c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar, bool IsInteger>
412c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct sqrt_default_impl
413c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
414c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x)
415c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
416c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::sqrt;
417c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return sqrt(x);
418c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
419c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
420c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
421c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
422c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct sqrt_default_impl<Scalar, true>
423c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
424c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar&)
425c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
426c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifdef EIGEN2_SUPPORT
427c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    eigen_assert(!NumTraits<Scalar>::IsInteger);
428c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#else
429c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
430c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
431c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return Scalar(0);
432c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
433c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
434c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
435c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
436c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct sqrt_impl : sqrt_default_impl<Scalar, NumTraits<Scalar>::IsInteger> {};
437c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
438c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
439c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct sqrt_retval
440c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
441c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Scalar type;
442c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
443c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
444c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
445c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(sqrt, Scalar) sqrt(const Scalar& x)
446c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
447c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(sqrt, Scalar)::run(x);
448c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
449c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
450c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
451c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of standard unary real functions (exp, log, sin, cos, ...  *
452c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
453c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
454c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This macro instanciate all the necessary template mechanism which is common to all unary real functions.
455c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_MATHFUNC_STANDARD_REAL_UNARY(NAME) \
456c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename Scalar, bool IsInteger> struct NAME##_default_impl {            \
457c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline Scalar run(const Scalar& x) { using std::NAME; return NAME(x); }  \
458c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  };                                                                                \
459c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename Scalar> struct NAME##_default_impl<Scalar, true> {              \
460c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline Scalar run(const Scalar&) {                                       \
461c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)                                       \
462c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return Scalar(0);                                                             \
463c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }                                                                               \
464c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  };                                                                                \
465c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename Scalar> struct NAME##_impl                                      \
466c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    : NAME##_default_impl<Scalar, NumTraits<Scalar>::IsInteger>                     \
467c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {};                                                                               \
468c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename Scalar> struct NAME##_retval { typedef Scalar type; };          \
469c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename Scalar>                                                         \
470c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline EIGEN_MATHFUNC_RETVAL(NAME, Scalar) NAME(const Scalar& x) {                \
471c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return EIGEN_MATHFUNC_IMPL(NAME, Scalar)::run(x);                               \
472c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
473c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
474c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_MATHFUNC_STANDARD_REAL_UNARY(exp)
475c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_MATHFUNC_STANDARD_REAL_UNARY(log)
476c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_MATHFUNC_STANDARD_REAL_UNARY(sin)
477c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_MATHFUNC_STANDARD_REAL_UNARY(cos)
478c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_MATHFUNC_STANDARD_REAL_UNARY(tan)
479c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_MATHFUNC_STANDARD_REAL_UNARY(asin)
480c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_MATHFUNC_STANDARD_REAL_UNARY(acos)
481c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
482c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
483c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of atan2                                                *
484c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
485c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
486c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar, bool IsInteger>
487c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct atan2_default_impl
488c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
489c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Scalar retval;
490c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x, const Scalar& y)
491c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
492c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::atan2;
493c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return atan2(x, y);
494c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
495c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
496c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
497c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
498c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct atan2_default_impl<Scalar, true>
499c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
500c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar&, const Scalar&)
501c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
502c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
503c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return Scalar(0);
504c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
505c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
506c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
507c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
508c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct atan2_impl : atan2_default_impl<Scalar, NumTraits<Scalar>::IsInteger> {};
509c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
510c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
511c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct atan2_retval
512c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
513c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Scalar type;
514c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
515c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
516c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
517c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(atan2, Scalar) atan2(const Scalar& x, const Scalar& y)
518c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
519c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(atan2, Scalar)::run(x, y);
520c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
521c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
522c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
523c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of pow                                                  *
524c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
525c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
526c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar, bool IsInteger>
527c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct pow_default_impl
528c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
529c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Scalar retval;
530c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x, const Scalar& y)
531c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
532c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::pow;
533c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return pow(x, y);
534c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
535c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
536c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
537c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
538c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct pow_default_impl<Scalar, true>
539c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
540c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(Scalar x, Scalar y)
541c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
542c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar res(1);
543c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    eigen_assert(!NumTraits<Scalar>::IsSigned || y >= 0);
544c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if(y & 1) res *= x;
545c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    y >>= 1;
546c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    while(y)
547c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
548c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      x *= x;
549c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(y&1) res *= x;
550c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      y >>= 1;
551c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
552c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return res;
553c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
554c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
555c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
556c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
557c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct pow_impl : pow_default_impl<Scalar, NumTraits<Scalar>::IsInteger> {};
558c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
559c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
560c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct pow_retval
561c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
562c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Scalar type;
563c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
564c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
565c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
566c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(pow, Scalar) pow(const Scalar& x, const Scalar& y)
567c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
568c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(pow, Scalar)::run(x, y);
569c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
570c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
571c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
572c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of random                                               *
573c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
574c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
575c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar,
576c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         bool IsComplex,
577c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         bool IsInteger>
578c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct random_default_impl {};
579c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
580c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
581c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct random_impl : random_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
582c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
583c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
584c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct random_retval
585c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
586c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Scalar type;
587c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
588c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
589c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y);
590c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random();
591c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
592c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
593c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct random_default_impl<Scalar, false, false>
594c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
595c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x, const Scalar& y)
596c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
597c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x + (y-x) * Scalar(std::rand()) / Scalar(RAND_MAX);
598c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
599c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run()
600c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
601c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return run(Scalar(NumTraits<Scalar>::IsSigned ? -1 : 0), Scalar(1));
602c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
603c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
604c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
605c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathenum {
606c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  floor_log2_terminate,
607c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  floor_log2_move_up,
608c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  floor_log2_move_down,
609c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  floor_log2_bogus
610c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
611c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
612c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<unsigned int n, int lower, int upper> struct floor_log2_selector
613c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
614c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  enum { middle = (lower + upper) / 2,
615c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         value = (upper <= lower + 1) ? int(floor_log2_terminate)
616c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath               : (n < (1 << middle)) ? int(floor_log2_move_down)
617c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath               : (n==0) ? int(floor_log2_bogus)
618c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath               : int(floor_log2_move_up)
619c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  };
620c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
621c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
622c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<unsigned int n,
623c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         int lower = 0,
624c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         int upper = sizeof(unsigned int) * CHAR_BIT - 1,
625c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         int selector = floor_log2_selector<n, lower, upper>::value>
626c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct floor_log2 {};
627c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
628c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<unsigned int n, int lower, int upper>
629c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct floor_log2<n, lower, upper, floor_log2_move_down>
630c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
631c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  enum { value = floor_log2<n, lower, floor_log2_selector<n, lower, upper>::middle>::value };
632c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
633c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
634c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<unsigned int n, int lower, int upper>
635c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct floor_log2<n, lower, upper, floor_log2_move_up>
636c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
637c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  enum { value = floor_log2<n, floor_log2_selector<n, lower, upper>::middle, upper>::value };
638c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
639c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
640c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<unsigned int n, int lower, int upper>
641c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct floor_log2<n, lower, upper, floor_log2_terminate>
642c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
643c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  enum { value = (n >= ((unsigned int)(1) << (lower+1))) ? lower+1 : lower };
644c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
645c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
646c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<unsigned int n, int lower, int upper>
647c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct floor_log2<n, lower, upper, floor_log2_bogus>
648c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
649c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // no value, error at compile time
650c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
651c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
652c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
653c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct random_default_impl<Scalar, false, true>
654c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
655c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::NonInteger NonInteger;
656c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
657c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x, const Scalar& y)
658c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
659c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x + Scalar((NonInteger(y)-x+1) * std::rand() / (RAND_MAX + NonInteger(1)));
660c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
661c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
662c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run()
663c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
664c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifdef EIGEN_MAKING_DOCS
665c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return run(Scalar(NumTraits<Scalar>::IsSigned ? -10 : 0), Scalar(10));
666c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#else
667c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    enum { rand_bits = floor_log2<(unsigned int)(RAND_MAX)+1>::value,
668c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath           scalar_bits = sizeof(Scalar) * CHAR_BIT,
669c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath           shift = EIGEN_PLAIN_ENUM_MAX(0, int(rand_bits) - int(scalar_bits))
670c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    };
671c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar x = Scalar(std::rand() >> shift);
672c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar offset = NumTraits<Scalar>::IsSigned ? Scalar(1 << (rand_bits-1)) : Scalar(0);
673c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x - offset;
674c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
675c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
676c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
677c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
678c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
679c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct random_default_impl<Scalar, true, false>
680c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
681c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run(const Scalar& x, const Scalar& y)
682c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
683c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return Scalar(random(real(x), real(y)),
684c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                  random(imag(x), imag(y)));
685c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
686c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Scalar run()
687c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
688c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename NumTraits<Scalar>::Real RealScalar;
689c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return Scalar(random<RealScalar>(), random<RealScalar>());
690c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
691c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
692c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
693c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
694c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y)
695c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
696c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(random, Scalar)::run(x, y);
697c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
698c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
699c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
700c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline EIGEN_MATHFUNC_RETVAL(random, Scalar) random()
701c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
702c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return EIGEN_MATHFUNC_IMPL(random, Scalar)::run();
703c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
704c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
705c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
706c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Implementation of fuzzy comparisons                                       *
707c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
708c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
709c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar,
710c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         bool IsComplex,
711c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         bool IsInteger>
712c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct scalar_fuzzy_default_impl {};
713c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
714c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
715c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct scalar_fuzzy_default_impl<Scalar, false, false>
716c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
717c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
718c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename OtherScalar>
719c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
720c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
721c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return abs(x) <= abs(y) * prec;
722c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
723c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
724c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
725c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::min;
726c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return abs(x - y) <= (min)(abs(x), abs(y)) * prec;
727c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
728c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar& prec)
729c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
730c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x <= y || isApprox(x, y, prec);
731c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
732c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
733c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
734c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
735c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct scalar_fuzzy_default_impl<Scalar, false, true>
736c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
737c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
738c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename OtherScalar>
739c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isMuchSmallerThan(const Scalar& x, const Scalar&, const RealScalar&)
740c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
741c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x == Scalar(0);
742c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
743c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar&)
744c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
745c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x == y;
746c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
747c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar&)
748c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
749c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x <= y;
750c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
751c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
752c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
753c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
754c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct scalar_fuzzy_default_impl<Scalar, true, false>
755c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
756c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
757c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename OtherScalar>
758c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
759c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
760c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return abs2(x) <= abs2(y) * prec * prec;
761c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
762c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
763c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
764c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using std::min;
765c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return abs2(x - y) <= (min)(abs2(x), abs2(y)) * prec * prec;
766c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
767c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
768c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
769c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
770c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct scalar_fuzzy_impl : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
771c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
772c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar, typename OtherScalar>
773c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y,
774c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                                   typename NumTraits<Scalar>::Real precision = NumTraits<Scalar>::dummy_precision())
775c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
776c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return scalar_fuzzy_impl<Scalar>::template isMuchSmallerThan<OtherScalar>(x, y, precision);
777c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
778c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
779c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
780c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline bool isApprox(const Scalar& x, const Scalar& y,
781c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                          typename NumTraits<Scalar>::Real precision = NumTraits<Scalar>::dummy_precision())
782c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
783c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
784c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
785c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
786c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Scalar>
787c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline bool isApproxOrLessThan(const Scalar& x, const Scalar& y,
788c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                                    typename NumTraits<Scalar>::Real precision = NumTraits<Scalar>::dummy_precision())
789c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
790c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
791c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
792c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
793c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/******************************************
794c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath***  The special case of the  bool type ***
795c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath******************************************/
796c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
797c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<> struct random_impl<bool>
798c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
799c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool run()
800c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
801c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return random<int>(0,1)==0 ? false : true;
802c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
803c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
804c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
805c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<> struct scalar_fuzzy_impl<bool>
806c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
807c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef bool RealScalar;
808c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
809c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename OtherScalar>
810c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isMuchSmallerThan(const bool& x, const bool&, const bool&)
811c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
812c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return !x;
813c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
814c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
815c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isApprox(bool x, bool y, bool)
816c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
817c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return x == y;
818c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
819c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
820c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline bool isApproxOrLessThan(const bool& x, const bool& y, const bool&)
821c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
822c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return (!x) || y;
823c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
824c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
825c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
826c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
827c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/****************************************************************************
828c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* Special functions                                                          *
829c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath****************************************************************************/
830c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
831c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// std::isfinite is non standard, so let's define our own version,
832c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// even though it is not very efficient.
833c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T> bool (isfinite)(const T& x)
834c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
835c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return x<NumTraits<T>::highest() && x>NumTraits<T>::lowest();
836c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
837c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
838c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace internal
839c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
840c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
841c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
842c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif // EIGEN_MATHFUNCTIONS_H
843