ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
arange.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_arange_h_INCLUDED
12#define NDARRAY_arange_h_INCLUDED
13
20#include "ndarray/vectorize.h"
21
22#include <boost/iterator/counting_iterator.hpp>
23
24namespace ndarray {
25
31template <>
32struct ExpressionTraits<detail::CountingExpression> {
33 typedef int Element;
34 typedef boost::mpl::int_<1> ND;
35 typedef boost::counting_iterator<int> Iterator;
36 typedef int Value;
37 typedef int Reference;
38};
39
40namespace detail {
41
48class CountingExpression : public ExpressionBase<CountingExpression> {
49public:
55 typedef Vector<int,1> Index;
56
57 CountingExpression(int stop=0) : _stop(stop) { NDARRAY_ASSERT(stop >= 0); }
58
59 Reference operator[](int n) const {
60 return n;
61 }
62
63 Iterator begin() const {
64 return Iterator(0);
65 }
66
67 Iterator end() const {
68 return Iterator(_stop);
69 }
70
71 template <int P> int getSize() const {
72 BOOST_STATIC_ASSERT(P==0);
73 return _stop;
74 }
75
76 Index getShape() const {
77 return makeVector(_stop);
78 }
79
80private:
81 int _stop;
82};
83
84template <typename T>
86 T _offset;
87 T _scale;
88public:
89 typedef int argument_type;
90 typedef T result_type;
91
92 explicit RangeTransformer(T const & offset, T const & scale) : _offset(offset), _scale(scale) {}
93
94 T operator()(int n) const { return static_cast<T>(n) * _scale + _offset; }
95};
96
97} // namespace detail
98
101 return detail::CountingExpression(stop);
102}
103
105inline detail::UnaryOpExpression< detail::CountingExpression, detail::RangeTransformer<int> >
106arange(int start, int stop, int step = 1) {
107 NDARRAY_ASSERT(step != 0);
108 int size = stop - start;
109 if (step < -1) ++size;
110 if (step > 1) --size;
111 size /= step;
112 return vectorize(
115 );
116}
117
118} // namespace ndarray
119
120#endif // !NDARRAY_arange_h_INCLUDED
detail::CountingExpression arange(int stop)
Create 1D Expression that contains integer values in the range [0,stop).
Definition arange.h:100
CRTP base class for all multidimensional expressions.
Definition ExpressionBase.h:40
Definition arange.h:85
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.