ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
make_lin_space.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef UTILS_MAKE_LIN_SPACE_HPP
21#define UTILS_MAKE_LIN_SPACE_HPP
22
23#include <boost/iterator/counting_iterator.hpp>
24#include <boost/iterator/transform_iterator.hpp>
25#include <boost/range/iterator_range.hpp>
26
27#include <cstddef>
28
29namespace Utils {
30/**
31 * @brief Equally spaced values in interval
32 *
33 * Returns a range of equally spaced values in
34 * the range of start and stop, like numpy.linspace.
35 *
36 * @tparam T floating point type
37 * @param start Start value of the interval
38 * @param stop End value of the interval
39 * @param number Number of partition points
40 * @param endpoint If true, the last point is
41 * stop, otherwise one less.
42 * @return Range of equally spaced values
43 */
44template <class T>
45auto make_lin_space(T start, T stop, std::size_t number, bool endpoint = true) {
46 using boost::make_counting_iterator;
47 using boost::make_iterator_range;
48 using boost::make_transform_iterator;
49
50 auto const dx = (stop - start) / T(number - endpoint);
51 auto x = [dx, start](std::size_t i) { return start + T(i) * dx; };
52
53 return make_iterator_range(
54 make_transform_iterator(make_counting_iterator(std::size_t(0)), x),
55 make_transform_iterator(make_counting_iterator(number), x));
56}
57} // namespace Utils
58
59#endif
auto make_lin_space(T start, T stop, std::size_t number, bool endpoint=true)
Equally spaced values in interval.