codac 1.5.6
Loading...
Searching...
No Matches
codac2_Vector.h
Go to the documentation of this file.
1
12#ifndef __CODAC2_VECTOR_H__
13#define __CODAC2_VECTOR_H__
14
15#include <ostream>
16#include <codac_Vector.h>
17#include <codac2_Matrix.h>
18
19namespace codac2
20{
21 using Eigen::Dynamic;
22
23 template<int N=Dynamic>
24 class Vector_ : public Matrix_<N,1>
25 {
26 public:
27
28 Vector_()
29 { }
30
31 Vector_(size_t n)
32 : Matrix_<N,1>(n,1)
33 {
34 assert(N == Dynamic || N == (int)n);
35 }
36
37 Vector_(size_t n, double x)
38 : Matrix_<N,1>(n,1,x)
39 {
40 assert(N == Dynamic || N == (int)n);
41 }
42
43 Vector_(std::initializer_list<double> l) : Matrix_<N,1>(l.size(),1)
44 {
45 assert(N == (int)l.size() || N == -1);
46 size_t i = 0;
47 for(double li : l)
48 (*this)(i++,0) = li;
49 }
50
51 template<int M>
52 Vector_(const Matrix_<M,1>& x)
53 : Matrix_<M,1>(x)
54 {
55 static_assert(M == Dynamic || M == N);
56 }
57
58 explicit Vector_(size_t n, const double values[])
59 : Matrix_<N,1>(n,1,values)
60 { }
61
62 explicit Vector_(const double values[])
63 : Matrix_<N,1>(N,1,values)
64 { }
65
66 template<typename T,size_t M>
67 explicit Vector_(const std::array<T,M>& array)
68 : Matrix_<N,1>(N,1)
69 {
70 static_assert(N == Dynamic || N == (int)M);
71 for(size_t i = 0 ; i < M ; i++)
72 *(this->data()+i) = array[i];
73 }
74
75 template<typename OtherDerived>
76 Vector_(const Eigen::MatrixBase<OtherDerived>& other)
77 : Matrix_<N,1>(other)
78 { }
79
80 // This method allows you to assign Eigen expressions to Vector_
81 template<typename OtherDerived>
82 Vector_& operator=(const Eigen::MatrixBase<OtherDerived>& other)
83 {
84 this->Eigen::Matrix<double,N,1>::operator=(other);
85 return *this;
86 }
87
88 Matrix_<N,N> as_diag() const
89 {
90 return Matrix_<N,N>(Eigen::Matrix<double,N,N>(this->asDiagonal()));
91 }
92
93 Matrix_<1,N> transpose() const
94 {
95 return Matrix_<1,N>(Eigen::Matrix<double,N,1>::transpose());
96 }
97
98 // todo: place this in common inheritance with IntervalVector_
99 template<size_t N1,size_t N2>
100 Vector_<N2-N1+1> subvector() const
101 {
102 assert(N1 >= 0 && N1 < N && N2 >= 0 && N2 < N && N1 <= N2);
103 return this->template block<N2-N1+1,1>(N1,0);
104 }
105 };
106
107 template<int N>
108 std::ostream& operator<<(std::ostream& os, const Vector_<N>& x)
109 {
110 os << "(";
111 for(size_t i = 0 ; i < x.size() ; i++)
112 os << x[i] << (i<x.size()-1 ? " ; " : "");
113 os << ")";
114 return os;
115 }
116
117 template<int N>
118 Matrix_<N,N> diag(const Vector_<N> v)
119 {
120 return v.as_diag();
121 }
122
123 template<int N>
124 codac::Vector to_codac1(const Vector_<N>& x)
125 {
126 ibex::Vector x_(x.size());
127 for(size_t i = 0 ; i < x.size() ; i++)
128 x_[i] = x[i];
129 return x_;
130 }
131
132 template<int N>
133 Vector_<N> to_codac2(const codac::Vector& x)
134 {
135 assert(x.size() == N);
136 Vector_<N> x_;
137 for(size_t i = 0 ; i < N ; i++)
138 x_[i] = x[i];
139 return x_;
140 }
141
142 class Vector : public Vector_<>
143 {
144 public:
145
146 explicit Vector(int n)
147 : Vector_<>(n)
148 { }
149
150 Vector(const Vector& x)
151 : Vector_<>(x)
152 { }
153
154 explicit Vector(std::initializer_list<double> l)
155 : Vector_<>(l)
156 { }
157
158 template<int N>
159 Vector(const Vector_<N>& v)
160 : Vector_<>(v)
161 { }
162
163 template<int N>
164 Vector(const Matrix_<N,1>& v)
165 : Vector_<>(v)
166 { }
167
168 };
169
170} // namespace codac
171
172#endif