-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathvectorize.h
More file actions
144 lines (126 loc) · 3.81 KB
/
vectorize.h
File metadata and controls
144 lines (126 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// -*- c++ -*-
/*
* Copyright (c) 2010-2012, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#ifndef NDARRAY_vectorize_h_INCLUDED
#define NDARRAY_vectorize_h_INCLUDED
/**
* \file ndarray/vectorize.h @brief Code to apply arbitrary scalar functors to arrays.
*/
#include "ndarray_fwd.h"
#include "ndarray/detail/UnaryOp.h"
#include <boost/mpl/and.hpp>
#include <boost/utility/enable_if.hpp>
namespace ndarray {
namespace result_of {
template <typename T1, typename T2, typename T3=void>
struct vectorize {
typedef T1 BinaryFunction;
typedef T2 Argument1;
typedef T3 Argument2;
typename boost::mpl::if_<
boost::mpl::and_<
typename ExpressionTraits<Argument1>::IsScalar,
typename ExpressionTraits<Argument2>::IsScalar
>,
typename BinaryFunction::result_type,
detail::BinaryOpExpression<Argument1,Argument2,BinaryFunction>
>::type type;
};
template <typename T1, typename T2>
struct vectorize<T1,T2,void> {
typedef T1 UnaryFunction;
typedef T2 Argument;
typedef typename boost::mpl::if_<
typename ExpressionTraits<Argument>::IsScalar,
typename UnaryFunction::result_type,
detail::UnaryOpExpression<Argument,UnaryFunction>
>::type type;
};
} // namespace result_of
/// @addtogroup ndarrayMainGroup
/// @{
/**
* @brief Apply a non-mutating unary function object to a scalar.
*
* This overload exists to allow recursive usage of the Array-argument vectorize functions.
*/
template <typename Scalar, typename UnaryFunction>
#ifndef DOXYGEN
typename boost::enable_if<typename ExpressionTraits<Scalar>::IsScalar,
typename UnaryFunction::result_type>::type
#else
typename UnaryFunction::result_type
#endif
vectorize(
UnaryFunction const & functor,
Scalar const & scalar
) {
return functor(scalar);
}
/**
* @brief Apply a non-mutating unary function object to each element of a multidimensional Expression.
*
* Evaluation is lazy.
*/
template <typename Derived, typename UnaryFunction>
detail::UnaryOpExpression<Derived,UnaryFunction>
vectorize(
UnaryFunction const & functor,
ExpressionBase<Derived> const & operand
) {
return detail::UnaryOpExpression<Derived,UnaryFunction>(
static_cast<Derived const &>(operand),
functor
);
}
/**
* @brief Apply a non-mutating binary function object to a pair of scalars.
*
* This overload exists to allow recursive usage of the Array-argument vectorize functions.
*/
template <typename Scalar1, typename Scalar2, typename BinaryFunction>
#ifndef DOXYGEN
typename boost::enable_if_c<
(ExpressionTraits<Scalar1>::IsScalar::value
&& ExpressionTraits<Scalar2>::IsScalar::value),
typename BinaryFunction::result_type
>::type
#else
typename BinaryFunction::result_type
#endif
vectorize(
BinaryFunction const & functor,
Scalar1 const & scalar1,
Scalar2 const & scalar2
) {
return functor(scalar1,scalar2);
}
/**
* @brief Apply a non-mutating binary function object pairwise to
* the elements of two multidimensional Expressions.
*
* Evaluation is lazy.
*/
template <typename Derived1, typename Derived2, typename BinaryFunction>
detail::BinaryOpExpression<Derived1,Derived2,BinaryFunction>
vectorize(
BinaryFunction const & functor,
ExpressionBase<Derived1> const & operand1,
ExpressionBase<Derived2> const & operand2
) {
return detail::BinaryOpExpression<Derived1,Derived2,BinaryFunction>(
static_cast<Derived1 const &>(operand1),
static_cast<Derived2 const &>(operand2),
functor
);
}
/// @}
} // namespace ndarray
#endif // !NDARRAY_vectorize_h_INCLUDED