sciplot  0.3.1
A modern C++ plotting library powered by gnuplot
Vec.hpp
1 // sciplot - a modern C++ scientific plotting library powered by gnuplot
2 // https://github.com/sciplot/sciplot
3 //
4 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
5 //
6 // Copyright (c) 2018-2021 Allan Leal
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 // C++ includes
29 #include <algorithm>
30 #include <functional>
31 #include <string>
32 #include <valarray>
33 #include <vector>
34 
35 namespace sciplot {
36 
38 using Vec = std::valarray<double>;
39 
41 using Strings = std::vector<std::string>;
42 
44 template <typename T0, typename T1, typename U = double>
45 auto linspace(T0 x0, T1 x1, std::size_t numintervals) -> std::valarray<U>
46 {
47  std::valarray<U> result(numintervals + 1);
48  for(std::size_t i = 0; i <= numintervals; ++i)
49  result[i] = x0 + i * (x1 - x0) / static_cast<U>(numintervals);
50  return result;
51 }
52 
54 template <typename U = double>
55 auto range(int x0, int x1) -> Vec
56 {
57  const auto incr = (x1 > x0) ? +1 : -1;
58  std::valarray<U> result(x1 - x0 + 1);
59  for(std::size_t i = 0; i < result.size(); ++i)
60  result[i] = x0 + i * incr;
61  return result;
62 }
63 
64 } // namespace sciplot