tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
linspace.hpp
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef TENSOR_LINSPACE_HPP
21 #define TENSOR_LINSPACE_HPP
22 
23 #include <tensor/gen.h>
24 
25 namespace tensor {
26 
27  template<typename elt_t>
28  static inline const Tensor<elt_t>
29  do_linspace(const Tensor<elt_t> &min, const Tensor<elt_t> &max, index n = 100)
30  {
31  index d = min.size();
32  Tensor<elt_t> output(d, n);
33  if (n == 1) {
34  output = min;
35  } else if (n) {
36  const Tensor<elt_t> base = reshape(max, d);
37  const Tensor<elt_t> delta = reshape((max - min) / (n - 1.0), d);
38  for (index i = 0; i < n; i++) {
39  output.at(range(), range(i)) = delta * (double)i + min;
40  }
41  }
42  if (d == 1)
43  return reshape(output, n);
44  else
45  return reshape(output, min.dimensions() << (igen << n));
46  }
47 
48 
49 } // namespace tensor
50 
51 #endif // TENSOR_LINSPACE_HPP
const RTensor reshape(const RTensor &t, const Indices &new_dims)
Return a RTensor with same data and given dimensions.