ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
BinaryOp.h
Go to the documentation of this file.
1// -*- c++ -*-
2/*
3 * Copyright (c) 2010-2012, Jim Bosch
4 * All rights reserved.
5 *
6 * ndarray is distributed under a simple BSD-like license;
7 * see the LICENSE file that should be present in the root
8 * of the source distribution, or alternately available at:
9 * https://github.com/ndarray/ndarray
10 */
11#ifndef NDARRAY_DETAIL_BinaryOp_h_INCLUDED
12#define NDARRAY_DETAIL_BinaryOp_h_INCLUDED
13
21#include "ndarray/vectorize.h"
22#include <boost/iterator/iterator_adaptor.hpp>
23#include <boost/iterator/zip_iterator.hpp>
24#include <boost/tuple/tuple.hpp>
25
26namespace ndarray {
27namespace detail {
28
36template <typename Operand1, typename Operand2, typename BinaryFunction>
37class BinaryOpIterator : public boost::iterator_adaptor<
38 BinaryOpIterator<Operand1,Operand2,BinaryFunction>,
39 boost::zip_iterator<
40 boost::tuple<
41 typename ExpressionTraits<Operand1>::Iterator,
42 typename ExpressionTraits<Operand2>::Iterator
43 >
44 >,
45 typename ExpressionTraits< BinaryOpExpression<Operand1,Operand2,BinaryFunction> >::Value,
46 boost::use_default,
47 typename ExpressionTraits< BinaryOpExpression<Operand1,Operand2,BinaryFunction> >::Reference
48 > {
50public:
51 typedef typename ExpressionTraits<Operand1>::Iterator BaseIterator1;
52 typedef typename ExpressionTraits<Operand2>::Iterator BaseIterator2;
53 typedef typename ExpressionTraits<Operation>::Value Value;
54 typedef typename ExpressionTraits<Operation>::Reference Reference;
55
56 BinaryOpIterator() : BinaryOpIterator::iterator_adaptor_(), _functor() {}
57
59 BaseIterator1 const & baseIter1,
60 BaseIterator2 const & baseIter2,
61 BinaryFunction const & functor
62 ) :
63 BinaryOpIterator::iterator_adaptor_(boost::make_tuple(baseIter1,baseIter2)),
64 _functor(functor) {}
65
66 BinaryOpIterator(BinaryOpIterator const & other) :
67 BinaryOpIterator::iterator_adaptor_(other), _functor(other._functor) {}
68
69private:
70 friend class boost::iterator_core_access;
71
72 Reference dereference() const {
73 return vectorize(
74 _functor,
75 this->base_reference()->template get<0>(),
76 this->base_reference()->template get<1>()
77 );
78 }
79
80 BinaryFunction _functor;
81};
82
90template <typename Operand1, typename Operand2, typename BinaryFunction, int N>
91class BinaryOpExpression : public ExpressionBase< BinaryOpExpression<Operand1,Operand2,BinaryFunction,N> > {
93public:
94 typedef typename ExpressionTraits<Self>::Element Element;
95 typedef typename ExpressionTraits<Self>::ND ND;
96 typedef typename ExpressionTraits<Self>::Iterator Iterator;
97 typedef typename ExpressionTraits<Self>::Value Value;
98 typedef typename ExpressionTraits<Self>::Reference Reference;
99 typedef Vector<Size,N> Index;
100
102 Operand1 const & operand1,
103 Operand2 const & operand2,
104 BinaryFunction const & functor
105 ) :
106 _operand1(operand1), _operand2(operand2), _functor(functor) {
107 NDARRAY_ASSERT(_operand1.getShape() == _operand2.getShape());
108 }
109
110 Reference operator[](Size n) const {
111 return Reference(_operand1[n],_operand2[n],_functor);
112 }
113
114 Iterator begin() const {
115 return Iterator(_operand1.begin(),_operand2.begin(),_functor);
116 }
117
118 Iterator end() const {
119 return Iterator(_operand1.end(),_operand2.end(),_functor);
120 }
121
122 template <int P> Size getSize() const {
123 return _operand1.template getSize<P>();
124 }
125
126 Index getShape() const {
127 return _operand1.getShape();
128 }
129
130 Operand1 _operand1;
131 Operand2 _operand2;
132 BinaryFunction _functor;
133};
134
135} // namespace detail
136} // namespace ndarray
137
138#endif // !NDARRAY_DETAIL_BinaryOp_h_INCLUDED
Definitions for ExpressionBase.
CRTP base class for all multidimensional expressions.
Definition ExpressionBase.h:40
Definition BinaryOp.h:91
Definition BinaryOp.h:48
boost::enable_if< typenameExpressionTraits< Scalar >::IsScalar, typenameUnaryFunction::result_type >::type vectorize(UnaryFunction const &functor, Scalar const &scalar)
Apply a non-mutating unary function object to a scalar.
Definition vectorize.h:73
Traits for expressions.
Definition ExpressionTraits.h:30
A fixed-size 1D array class.
Definition Vector.h:82
Code to apply arbitrary scalar functors to arrays.