TMB Documentation  v1.9.11
tiny_valarray.hpp
1 // Copyright (C) 2016 Kasper Kristensen
2 // License: GPL-2
3 
4 #include <valarray>
5 #include <cmath>
6 template<class Type, int n>
7 struct tiny_vector : std::valarray<Type> {
8  tiny_vector() : std::valarray<Type>(n) {}
9  template<class T>
10  tiny_vector(T other) : std::valarray<Type>(other) {}
11  void setZero() {for(int i=0; i<n; i++) (*this)[i] = 0;}
12  std::slice_array<Type> segment(size_t start, size_t length) {
13  return (*this)[std::slice(start, length, 1)];
14  }
15 #ifdef EIGEN_CORE_H
16  operator vector<Type>(){
17  vector<Type> ans(n);
18  for(int i=0; i<n; i++) ans[i] = (*this)[i];
19  return ans;
20  }
21 #endif
22 };
23 template<class Type, int n, class T>
24 struct tiny_vector<Type, n> operator*(const T &x, const tiny_vector<Type, n> &y) {
25  return y * Type(x);
26 }
27 template<class Type, int n>
28 std::ostream &operator<<(std::ostream &os, tiny_vector<Type, n> const &x) {
29  os << "[ ";
30  for(int i=0; i < x.size(); i++) os << x[i] << " ";
31  os << "]";
32  return os;
33 }
Vector class used by TMB.
Definition: vector.hpp:17
License: GPL v2