ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tiny_vec.hpp
Go to the documentation of this file.
1 // Copyright (C) 2016 Kasper Kristensen
2 // License: GPL-2
3 
4 /* Simple vector class that can be used with tiny_ad */
5 template <class Type>
6 struct tiny_vec_ref {
7  Type *p;
8  size_t n;
9  tiny_vec_ref(Type *p_, size_t n_) : p(p_), n(n_) {}
10  template<class T>
11  tiny_vec_ref &operator=(const T &other) {
12  for(size_t i = 0; i < n; i++) p[i] = other[i];
13  return *this;
14  }
15 };
16 template <class Type, int n>
17 struct tiny_vec {
18  Type data[n];
20  tiny_vec(const tiny_vec &other) {
21  for(int i=0; i<n; i++) data[i] = other.data[i];
22  }
23  tiny_vec(const Type &other) {
24  for(int i=0; i<n; i++) data[i] = other;
25  }
26  void resize(size_t length){ /* Ignore - this is fixed size */ }
27  int size() const { return n; }
28  Type operator[] (size_t i) const { return data[i]; }
29  Type &operator[] (size_t i) { return data[i]; }
30  void setZero() {for(int i=0; i<n; i++) (*this)[i] = 0;}
31  tiny_vec_ref<Type> segment(size_t start, size_t length) {
32  tiny_vec_ref<Type> ans(&(data[start]), length);
33  return ans;
34  }
35 #define VBINARY_OPERATOR(OP) \
36  tiny_vec operator OP (const tiny_vec &other) const { \
37  tiny_vec ans; \
38  for(int i=0; i<n; i++) ans.data[i] = data[i] OP other.data[i]; \
39  return ans; \
40  } \
41  template<class Scalar> \
42  tiny_vec operator OP (const Scalar &other) const { \
43  tiny_vec ans; \
44  for(int i=0; i<n; i++) ans.data[i] = data[i] OP other; \
45  return ans; \
46  }
51 #define VUNARY_OPERATOR(OP) \
52  tiny_vec operator OP () const { \
53  tiny_vec ans; \
54  for(int i=0; i<n; i++) ans.data[i] = OP (*this).data[i]; \
55  return ans; \
56  }
59 #define COMPOUND_ASSIGNMENT_OPERATOR(OP) \
60  tiny_vec& operator OP (const Type &other) { \
61  for(int i=0; i<n; i++) (*this).data[i] OP other; \
62  return *this; \
63  } \
64  tiny_vec& operator OP (const tiny_vec &other) { \
65  for(int i=0; i<n; i++) (*this).data[i] OP other[i]; \
66  return *this; \
67  }
72 
73  #ifdef EIGEN_CORE_H
74  operator vector<Type>(){
75  vector<Type> ans(n);
76  for(int i=0; i<n; i++) ans[i] = (*this)[i];
77  return ans;
78  }
79  #endif
80 };
81 
82 template<class Type, int n>
84  return y.operator* (x);
85 }
86 
87 template<class Type, int n>
88 std::ostream &operator<<(std::ostream &os, tiny_vec<Type, n> const &x) {
89  os << "[ ";
90  for(int i=0; i < x.size(); i++) os << x[i] << " ";
91  os << "]";
92  return os;
93 }
tiny_vec(const tiny_vec &other)
Definition: tiny_vec.hpp:20
#define x
void setZero()
Definition: tiny_vec.hpp:30
Type operator[](size_t i) const
Definition: tiny_vec.hpp:28
int size() const
Definition: tiny_vec.hpp:27
#define VBINARY_OPERATOR(OP)
Definition: tiny_vec.hpp:35
tiny_vec_ref(Type *p_, size_t n_)
Definition: tiny_vec.hpp:9
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
Definition: d3arr12.cpp:17
tiny_vec_ref< Type > segment(size_t start, size_t length)
Definition: tiny_vec.hpp:31
Type data[n]
Definition: tiny_vec.hpp:18
tiny_vec_ref & operator=(const T &other)
Definition: tiny_vec.hpp:11
Type * p
Definition: tiny_vec.hpp:7
#define VUNARY_OPERATOR(OP)
Definition: tiny_vec.hpp:51
tiny_vec(const Type &other)
Definition: tiny_vec.hpp:23
size_t n
Definition: tiny_vec.hpp:8
void resize(size_t length)
Definition: tiny_vec.hpp:26
size_t length(const adstring &t)
Returns the size of adstr.
Definition: string1.cpp:228
tiny_vec()
Definition: tiny_vec.hpp:19
#define COMPOUND_ASSIGNMENT_OPERATOR(OP)
Definition: tiny_vec.hpp:59