diff --git a/dune/common/Makefile.am b/dune/common/Makefile.am index fa163a6096d7ea5ca7b14ca8619e9a847bb8c79b..a081de8b1222014fda9133e832c6ed8559ab9986 100644 --- a/dune/common/Makefile.am +++ b/dune/common/Makefile.am @@ -27,6 +27,7 @@ commoninclude_HEADERS = \ debugstream.hh \ deprecated.hh \ densevector.hh \ + dynvector.hh \ enumset.hh \ exceptions.hh \ fassign.hh \ diff --git a/dune/common/dynvector.hh b/dune/common/dynvector.hh new file mode 100644 index 0000000000000000000000000000000000000000..1f35831113ae237eab606ebf88c14d2ccc9677bd --- /dev/null +++ b/dune/common/dynvector.hh @@ -0,0 +1,75 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +// $Id: fvector.hh 6105 2010-08-25 16:06:36Z christi $ +#ifndef DUNE_FVECTOR_HH +#define DUNE_FVECTOR_HH + +#include <cmath> +#include <cstddef> +#include <cstdlib> +#include <complex> +#include <cstring> +#include <limits> + +#include "exceptions.hh" +#include "genericiterator.hh" + +#include <vector> +#include "densevector.hh" + +namespace Dune { + + /** @addtogroup DenseMatVec + @{ + */ + + template< class K > + class DynamicVector : public DenseVector< std::vector<K> > + { + public: + typedef typename DenseVector< std::vector<K> >::size_type size_type; + + //! Constructor making uninitialized vector + DynamicVector() {} + + //! Constructor making vector with identical coordinates + DynamicVector (size_type n, const K& t) : + DenseVector< std::vector<K> > (n,t) + {} + + //! Constructor making vector with identical coordinates + DynamicVector (const DenseVector< std::vector<K> > & x) : + DenseVector< std::vector<K> > (x) + {} + + using DenseVector< std::vector<K> >::operator=; + }; + + /** \brief Read a DynamicVector from an input stream + * \relates DynamicVector + * + * \note This operator is STL compilant, i.e., the content of v is only + * changed if the read operation is successful. + * + * \param[in] in std :: istream to read from + * \param[out] v DynamicVector to be read + * + * \returns the input stream (in) + */ + template<class K> + inline std::istream &operator>> ( std::istream &in, + DynamicVector<K> &v ) + { + DynamicVector<K> w(v); + for( typename DynamicVector<K>::size_type i = 0; i < w.size(); ++i ) + in >> w[ i ]; + if(in) + v = w; + return in; + } + + /** @} end documentation */ + +} // end namespace + +#endif diff --git a/dune/common/test/Makefile.am b/dune/common/test/Makefile.am index 5a460ff933949bc16ef8c53e3543d9b594ce22cb..547040f46662ad090b2b5bef98d0767efde365f6 100644 --- a/dune/common/test/Makefile.am +++ b/dune/common/test/Makefile.am @@ -8,6 +8,7 @@ TESTPROGS = \ configparsertest \ conversiontest \ deprtuplestest \ + dynvectortest \ fassigntest \ float_cmp \ fmatrixtest \ @@ -109,6 +110,8 @@ iteratorfacadetest_SOURCES = iteratorfacadetest.cc iteratorfacadetest.hh \ iteratorfacadetest2_SOURCES = iteratorfacadetest2.cc +dynvectortest_SOURCES = dynvectortest.cc + fvectortest_SOURCES = fvectortest.cc fmatrixtest_SOURCES = fmatrixtest.cc diff --git a/dune/common/test/dynvectortest.cc b/dune/common/test/dynvectortest.cc new file mode 100644 index 0000000000000000000000000000000000000000..37d2fc568982db0c6166fa687137f9a9d12cb2d7 --- /dev/null +++ b/dune/common/test/dynvectortest.cc @@ -0,0 +1,78 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include <dune/common/dynvector.hh> +#include <dune/common/exceptions.hh> +#include <iostream> + +using Dune::DynamicVector; + +template<class ct> +void dynamicVectorTest(int d) { + ct a = 1; + DynamicVector<ct> v(d,1); + DynamicVector<ct> w(d,2); + DynamicVector<ct> z(d,2); + bool b; + + // Test whether the norm methods compile + (w+v).two_norm(); + (w+v).two_norm2(); + (w+v).one_norm(); + (w+v).one_norm_real(); + (w+v).infinity_norm(); + (w+v).infinity_norm_real(); + + // test op(vec,vec) + z = v + w; + z = v - w; + DynamicVector<ct> z2 = v + w; + w -= v; + w += v; + + // test op(vec,scalar) + w +=a; + w -= a; + w *= a; + w /= a; + + // test scalar product, axpy + a = v * w; + z = v.axpy(a,w); + + // test comparison + b = (w != v); + b = (w == v); + + + // test istream operator + std::stringstream s; + for (int i=0; i<d; i++) + { + s << i << " "; + v[i] = i; + } + s >> w; + assert(v == w); + +} + +int main() +{ + try { + for (int d=1; d<6; d++) + { + dynamicVectorTest<int>(d); + dynamicVectorTest<float>(d); + dynamicVectorTest<double>(d); + } + } catch (Dune::Exception& e) { + std::cerr << e << std::endl; + return 1; + } catch (...) { + std::cerr << "Generic exception!" << std::endl; + return 2; + } +}