TMB Documentation  v1.9.11
vector.hpp
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 Kasper Kristensen
2 // License: GPL-2
3 
8 using namespace Eigen;
9 
16 template <class Type>
17 struct vector : Array<Type,Dynamic,1>
18 {
19  typedef Type value_type;
20  typedef Array<Type,Dynamic,1> Base;
21  vector(void):Base() {}
22 
23  template<class T1>
24  vector(T1 x):Base(x) {}
25 
26  template<class T1, class T2>
27  vector(T1 x, T2 y):Base(x,y) {}
28 
29 
30  template<class T1>
31  vector & operator= (const T1 & other)
32  {
33  this->Base::operator=(other);
34  return *this;
35  }
36 
37  // /* index-vector subset */
38  // template <class T>
39  // Type & operator()(T ind){
40  // return this->Base::operator()(ind);
41  // }
42  using Base::operator();
43 
44  // kasper: would be better with references, i.e. allow x(indvec)=y;
45  vector<Type> operator()(vector<int> ind){
46  vector<Type> ans(ind.size());
47  for(int i=0;i<ind.size();i++)ans[i]=this->operator[](ind[i]);
48  return ans;
49  }
50 
51  /* Convert to _other_ vector class
52  Examples
53  1. vector<Type> to CppAD::vector<Type>
54  2. vector<int> to CppAD::vector<double>
55  */
56  template<class T>
57  operator CppAD::vector<T>(){
58  int n = this->size();
59  CppAD::vector<T> x(n);
60  for(int i=0; i<n; i++) x[i] = T(this->operator[](i));
61  return x;
62  }
63  /* Convert to _this_ vector class
64  Examples:
65  2. CppAD::vector<Type> to vector<Type>
66  3. CppAD::vector<int> to vector<double>
67 
68  FIXME:
69  Which one takes care of: vector<int> to vector<double> ?
70  */
71  template<class T>
72  vector(CppAD::vector<T> x):Base(){
73  int n = x.size();
74  this->resize(n);
75  for(int i=0; i<n; i++) this->operator[](i) = Type(x[i]);
76  }
77 
78  // std::vector
79  operator std::vector<Type>(){
80  int n = this->size();
81  std::vector<Type> x(n);
82  for(int i=0; i<n; i++) x[i] = (*this)(i);
83  return x;
84  }
85  vector(const std::vector<Type> &x) : Base(){
86  int n = x.size();
87  this->resize(n);
88  for(int i=0; i<n; i++) (*this)[i] = x[i];
89  }
90 
91  vector<Type> vec() { return *this; }
92 };
93 
100 template <class Type>
101 struct matrix : Matrix<Type,Dynamic,Dynamic>
102 {
103  typedef Matrix<Type,Dynamic,Dynamic> Base;
104  matrix(void):Base() {}
105  template<class T1>
106  matrix(T1 x):Base(x) {}
107  template<class T1, class T2>
108  matrix(T1 x, T2 y):Base(x,y) {}
109 
110  template<class T1>
111  matrix & operator= (const T1 & other)
112  {
113  this->Base::operator=(other);
114  return *this;
115  }
116 
120  vector<Type> vec(){
121  Array<Type,Dynamic,Dynamic> a = this->array();
122  a.resize(a.size(), 1);
123  return a;
124  }
125 };
Matrix class used by TMB.
Definition: vector.hpp:101
Array class used by TMB.
Definition: array.hpp:22
vector< Type > vec()
Definition: vector.hpp:120
License: GPL v2