blob: 2acf97723347322b5d838c1a95a3b9fa3741cdcf [file] [log] [blame]
Narayan Kamathc981c482012-11-02 10:59:05 +00001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -07004// Copyright (C) 2010-2012 Gael Guennebaud <gael.guennebaud@inria.fr>
Narayan Kamathc981c482012-11-02 10:59:05 +00005// Copyright (C) 2010 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_GLOBAL_FUNCTIONS_H
12#define EIGEN_GLOBAL_FUNCTIONS_H
13
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070014#define EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(NAME,FUNCTOR) \
Narayan Kamathc981c482012-11-02 10:59:05 +000015 template<typename Derived> \
16 inline const Eigen::CwiseUnaryOp<Eigen::internal::FUNCTOR<typename Derived::Scalar>, const Derived> \
17 NAME(const Eigen::ArrayBase<Derived>& x) { \
18 return x.derived(); \
19 }
20
21#define EIGEN_ARRAY_DECLARE_GLOBAL_EIGEN_UNARY(NAME,FUNCTOR) \
22 \
23 template<typename Derived> \
24 struct NAME##_retval<ArrayBase<Derived> > \
25 { \
26 typedef const Eigen::CwiseUnaryOp<Eigen::internal::FUNCTOR<typename Derived::Scalar>, const Derived> type; \
27 }; \
28 template<typename Derived> \
29 struct NAME##_impl<ArrayBase<Derived> > \
30 { \
31 static inline typename NAME##_retval<ArrayBase<Derived> >::type run(const Eigen::ArrayBase<Derived>& x) \
32 { \
33 return x.derived(); \
34 } \
35 };
36
37
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070038namespace Eigen
Narayan Kamathc981c482012-11-02 10:59:05 +000039{
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070040 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(real,scalar_real_op)
41 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(imag,scalar_imag_op)
42 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(conj,scalar_conjugate_op)
43 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(sin,scalar_sin_op)
44 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(cos,scalar_cos_op)
45 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(asin,scalar_asin_op)
46 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(acos,scalar_acos_op)
47 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(tan,scalar_tan_op)
48 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(exp,scalar_exp_op)
49 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(log,scalar_log_op)
50 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(abs,scalar_abs_op)
51 EIGEN_ARRAY_DECLARE_GLOBAL_UNARY(sqrt,scalar_sqrt_op)
52
Narayan Kamathc981c482012-11-02 10:59:05 +000053 template<typename Derived>
54 inline const Eigen::CwiseUnaryOp<Eigen::internal::scalar_pow_op<typename Derived::Scalar>, const Derived>
55 pow(const Eigen::ArrayBase<Derived>& x, const typename Derived::Scalar& exponent) {
56 return x.derived().pow(exponent);
57 }
58
59 template<typename Derived>
60 inline const Eigen::CwiseBinaryOp<Eigen::internal::scalar_binary_pow_op<typename Derived::Scalar, typename Derived::Scalar>, const Derived, const Derived>
61 pow(const Eigen::ArrayBase<Derived>& x, const Eigen::ArrayBase<Derived>& exponents)
62 {
63 return Eigen::CwiseBinaryOp<Eigen::internal::scalar_binary_pow_op<typename Derived::Scalar, typename Derived::Scalar>, const Derived, const Derived>(
64 x.derived(),
65 exponents.derived()
66 );
67 }
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070068
Narayan Kamathc981c482012-11-02 10:59:05 +000069 /**
70 * \brief Component-wise division of a scalar by array elements.
71 **/
72 template <typename Derived>
73 inline const Eigen::CwiseUnaryOp<Eigen::internal::scalar_inverse_mult_op<typename Derived::Scalar>, const Derived>
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070074 operator/(const typename Derived::Scalar& s, const Eigen::ArrayBase<Derived>& a)
Narayan Kamathc981c482012-11-02 10:59:05 +000075 {
76 return Eigen::CwiseUnaryOp<Eigen::internal::scalar_inverse_mult_op<typename Derived::Scalar>, const Derived>(
77 a.derived(),
78 Eigen::internal::scalar_inverse_mult_op<typename Derived::Scalar>(s)
79 );
80 }
81
82 namespace internal
83 {
84 EIGEN_ARRAY_DECLARE_GLOBAL_EIGEN_UNARY(real,scalar_real_op)
85 EIGEN_ARRAY_DECLARE_GLOBAL_EIGEN_UNARY(imag,scalar_imag_op)
Narayan Kamathc981c482012-11-02 10:59:05 +000086 EIGEN_ARRAY_DECLARE_GLOBAL_EIGEN_UNARY(abs2,scalar_abs2_op)
Narayan Kamathc981c482012-11-02 10:59:05 +000087 }
88}
89
Carlos Hernandez7faaa9f2014-08-05 17:53:32 -070090// TODO: cleanly disable those functions that are not supported on Array (numext::real_ref, internal::random, internal::isApprox...)
Narayan Kamathc981c482012-11-02 10:59:05 +000091
92#endif // EIGEN_GLOBAL_FUNCTIONS_H