vector.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
35 #include <iomanip>
36 
37 using namespace std;
38 
40 
41 template<typename T>
43 )
44 {
45  unsigned oldDim = Base::rows();
46  unsigned argDim = _arg.rows();
47 
48  Base::conservativeResize(oldDim + argDim);
49  Base::block(oldDim, 0, argDim, 1) = _arg;
50 
51  return *this;
52 }
53 
54 template<typename T>
56 )
57 {
58  ASSERT( _idx < Base::rows() );
59 
60  Base::setZero( );
61  Base::operator()( _idx ) = T( 1 );
62 
63  return *this;
64 }
65 
66 template<typename T>
68 {
69  GenericVector scale( getDim() );
70  scale.setOnes();
71  return getNorm(_norm, scale);
72 }
73 
74 template<typename T>
76  const GenericVector< T >& _scale ) const
77 {
78  switch( _norm )
79  {
80  case VN_L1:
81  return GenericVector<T>(this->cwiseQuotient( _scale )).getAbsolute().sum();
82 
83  case VN_L2:
84  return Base::norm();
85 
86  case VN_LINF:
87  return GenericVector<T>(this->cwiseQuotient(_scale)).getAbsolute().getMax();
88 
89  default:
90  return T( -1 );
91  }
92 
93  return T( -1 );
94 }
95 
96 template<typename T>
97 returnValue GenericVector<T>::print( std::ostream& stream,
98  const std::string& name,
99  const std::string& startString,
100  const std::string& endString,
101  uint width,
102  uint precision,
103  const std::string& colSeparator,
104  const std::string& rowSeparator
105  ) const
106 {
107  if (name.size())
108  stream << name << " = ";
109 
110  stream << startString;
111 
112  for (unsigned i = 0; i < getDim(); ++i)
113  {
114  stream << Base::operator()( i );
115 
116  if (i < (getDim() - 1))
117  stream << rowSeparator;
118  }
119  stream << endString;
120 
121  return SUCCESSFUL_RETURN;
122 }
123 
124 template<>
125 returnValue GenericVector< double >::print( std::ostream& stream,
126  const std::string& name,
127  const std::string& startString,
128  const std::string& endString,
129  uint width,
130  uint precision,
131  const std::string& colSeparator,
132  const std::string& rowSeparator
133  ) const
134 {
135  IoFormatter iof( stream );
136 
137  if (name.size())
138  stream << name << " = ";
139 
140  stream << startString;
141 
142  iof.set(precision > 0 ? precision : iof.precision,
143  width,
144  precision > 0 ? ios::scientific | ios::right : ios::fixed);
145 
146  for (unsigned i = 0; i < getDim(); ++i)
147  {
148  stream << Base::operator()( i );
149 
150  if (i < (getDim() - 1))
151  stream << rowSeparator;
152  }
153  stream << endString;
154 
155  iof.reset();
156 
157  return SUCCESSFUL_RETURN;
158 }
159 
160 template<typename T>
161 returnValue GenericVector<T>::print( const std::string& filename,
162  const std::string& name,
163  const std::string& startString,
164  const std::string& endString,
165  uint width,
166  uint precision,
167  const std::string& colSeparator,
168  const std::string& rowSeparator
169  ) const
170 {
171  ofstream stream( filename.c_str() );
172 
173  if ( stream.is_open() )
174  return print(stream, name, startString, endString, width, precision,
175  colSeparator, rowSeparator);
176  else
178 
179  stream.close();
180 
181  return SUCCESSFUL_RETURN;
182 }
183 
184 template<typename T>
185 returnValue GenericVector<T>::print( std::ostream& stream,
186  const std::string& name,
187  PrintScheme printScheme
188  ) const
189 {
190  MatFile<T>* matFile = 0;
191 
192  switch ( printScheme )
193  {
194  case PS_MATLAB_BINARY:
195  matFile = new MatFile<T>;
196 
197  matFile->write(stream, *this, name.c_str());
198 
199  delete matFile;
200 
201  return SUCCESSFUL_RETURN;
202 
203  default:
204 
205  char* startString = 0;
206  char* endString = 0;
207  uint width = 0;
208  uint precision = 0;
209  char* colSeparator = 0;
210  char* rowSeparator = 0;
211 
212  returnValue ret = getGlobalStringDefinitions(printScheme, &startString,
213  &endString, width, precision, &colSeparator, &rowSeparator);
214  if (ret != SUCCESSFUL_RETURN)
215  return ret;
216 
217  returnValue status = print(stream, name, startString, endString, width,
218  precision, colSeparator, rowSeparator);
219 
220  if ( startString != 0 ) delete[] startString;
221  if ( endString != 0 ) delete[] endString;
222  if ( colSeparator != 0 ) delete[] colSeparator;
223  if ( rowSeparator != 0 ) delete[] rowSeparator;
224 
225  return status;
226  }
227 
228  return SUCCESSFUL_RETURN;
229 }
230 
231 template<typename T>
232 returnValue GenericVector<T>::print( const std::string& filename,
233  const std::string& name,
234  PrintScheme printScheme
235  ) const
236 {
237  ofstream stream( filename.c_str() );
239 
240  if ( stream.is_open() )
241  status = print(stream, name, printScheme);
242  else
244 
245  stream.close();
246 
247  return status;
248 }
249 
250 template<typename T>
251 returnValue GenericVector<T>::read( std::istream& stream )
252 {
253  vector< T > tmp;
254  stream >> tmp;
255 
256  Base::_set(GenericVector<T>( tmp ));
257 
258  return SUCCESSFUL_RETURN;
259 }
260 
261 template<typename T>
262 returnValue GenericVector<T>::read( const std::string& filename
263  )
264 {
265  ifstream stream( filename.c_str() );
267 
268  if (stream.is_open())
269  status = read( stream );
270  else
272 
273  stream.close();
274 
275  return status;
276 }
277 
278 //
279 // Explicit instantiation of templates
280 //
281 template class GenericVector<double>;
282 template class GenericVector<int>;
283 template class GenericVector<bool>;
284 
returnValue getGlobalStringDefinitions(PrintScheme _printScheme, char **_startString, char **_endString, uint &_width, uint &_precision, char **_colSeparator, char **_rowSeparator)
T getMax() const
Definition: vector.hpp:180
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
Allows to pass back messages to the calling function.
GenericVector & setUnitVector(unsigned _idx)
Definition: vector.cpp:55
Block< Derived > block(Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: BlockMethods.h:56
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
Simple class for writing binary data file that are compatible with Matlab.
#define CLOSE_NAMESPACE_ACADO
EIGEN_STRONG_INLINE Index rows() const
VectorNorm
Definition: vector.hpp:45
PrintScheme
virtual returnValue print(std::ostream &stream=std::cout, const std::string &name=DEFAULT_LABEL, const std::string &startString=DEFAULT_START_STRING, const std::string &endString=DEFAULT_END_STRING, uint width=DEFAULT_WIDTH, uint precision=DEFAULT_PRECISION, const std::string &colSeparator=DEFAULT_COL_SEPARATOR, const std::string &rowSeparator=DEFAULT_ROW_SEPARATOR) const
Definition: vector.cpp:97
Definition: vector.hpp:48
void set(streamsize _precision, streamsize _width, ios_base::fmtflags _flags)
Derived & setOnes(Index size)
#define ASSERT(x)
GenericVector & append(const GenericVector &_arg)
Definition: vector.cpp:42
EIGEN_STRONG_INLINE const CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const Derived, const OtherDerived > cwiseQuotient(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
BEGIN_NAMESPACE_QPOASES returnValue print(const real_t *const v, int n)
#define BEGIN_NAMESPACE_ACADO
void write(std::ostream &stream, const GenericMatrix< T > &mat, const char *name)
#define ACADOERROR(retval)
T getNorm(VectorNorm _norm) const
Definition: vector.cpp:67
virtual returnValue read(std::istream &stream)
Definition: vector.cpp:251
Definition: vector.hpp:47


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:16