- 3.0.2 core module.
linspace.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 namespace ct {
9 namespace core {
10 
12 
22 template <typename TRAJECTORY_T>
23 TRAJECTORY_T linspace(const typename TRAJECTORY_T::value_type& a,
24  const typename TRAJECTORY_T::value_type& b,
25  const size_t N)
26 {
27  if (N < 1)
28  throw std::runtime_error("ERROR in CT_LINSPACE: N<1.");
29 
30  typename TRAJECTORY_T::value_type h = (b - a) / (N - 1);
31  TRAJECTORY_T traj(N);
32 
33  typename TRAJECTORY_T::iterator it;
34  typename TRAJECTORY_T::value_type val;
35 
36  for (it = traj.begin(), val = a; it != traj.end(); ++it, val += h)
37  *it = val;
38 
39  return traj;
40 }
41 
42 } // namespace core
43 } // namespace ct
TRAJECTORY_T linspace(const typename TRAJECTORY_T::value_type &a, const typename TRAJECTORY_T::value_type &b, const size_t N)
replicates the well-known linspace command from MATLAB in C++
Definition: linspace.h:23