Qucs-core  0.0.19
tvector.h
Go to the documentation of this file.
00001 /*
00002  * tvector.h - simple vector template class definitions
00003  *
00004  * Copyright (C) 2004, 2005, 2006 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #ifndef __TVECTOR_H__
00026 #define __TVECTOR_H__
00027 
00028 #include <vector>
00029 #include <assert.h>
00030 
00031 #include <limits>
00032 
00033 #include "precision.h"
00034 
00035 namespace qucs {
00036 
00037 template <class nr_type_t>
00038 class tvector;
00039 
00040 // Forward declarations of friend functions.
00041 template <class nr_type_t>
00042 nr_type_t   scalar (tvector<nr_type_t>, tvector<nr_type_t>);
00043 template <class nr_type_t>
00044 nr_double_t maxnorm (tvector<nr_type_t>);
00045 template <class nr_type_t>
00046 nr_double_t norm (tvector<nr_type_t>);
00047 template <class nr_type_t>
00048 nr_type_t   sum (tvector<nr_type_t>);
00049 template <class nr_type_t>
00050 tvector<nr_type_t> conj (tvector<nr_type_t>);
00051 template <class nr_type_t>
00052 tvector<nr_type_t> operator + (tvector<nr_type_t>, tvector<nr_type_t>);
00053 template <class nr_type_t>
00054 tvector<nr_type_t> operator + (tvector<nr_type_t>, nr_type_t);
00055 template <class nr_type_t>
00056 tvector<nr_type_t> operator + (nr_type_t, tvector<nr_type_t>);
00057 template <class nr_type_t>
00058 tvector<nr_type_t> operator - (tvector<nr_type_t>, tvector<nr_type_t>);
00059 template <class nr_type_t>
00060 tvector<nr_type_t> operator * (tvector<nr_type_t>, nr_double_t);
00061 template <class nr_type_t>
00062 tvector<nr_type_t> operator * (nr_double_t, tvector<nr_type_t>);
00063 template <class nr_type_t>
00064 tvector<nr_type_t> operator * (tvector<nr_type_t>, tvector<nr_type_t>);
00065 template <class nr_type_t>
00066 tvector<nr_type_t> operator - (tvector<nr_type_t>);
00067 template <class nr_type_t>
00068 bool operator < (tvector<nr_type_t>, tvector<nr_type_t>);
00069 template <class nr_type_t>
00070 bool operator > (tvector<nr_type_t>, tvector<nr_type_t>);
00071 
00072 template <class nr_type_t>
00073 class tvector
00074 {
00075  public:
00076   tvector () = default;
00077   tvector (const std::size_t i) : data(i) {};
00078   tvector (const tvector &) = default;
00079   ~tvector () = default;
00080   nr_type_t get (int);
00081   void set (int, nr_type_t);
00082   void set (nr_type_t);
00083   void set (nr_type_t, int, int);
00084   void set (tvector, int, int);
00085   std::size_t  size (void) const { return data.size (); }
00086   nr_type_t * getData (void) { return data.data(); }
00087   void clear (void);
00088   void exchangeRows (int, int);
00089   int  isFinite (void);
00090   void print (void);
00091   void reorder (int *);
00092   int  contains (nr_type_t, nr_double_t eps = std::numeric_limits<nr_double_t>::epsilon());
00093 
00094   // some basic vector operations
00095 #ifndef _MSC_VER
00096   friend tvector operator +<> (tvector, tvector);
00097   friend tvector operator -<> (tvector, tvector);
00098   friend tvector operator *<> (tvector, nr_double_t);
00099   friend tvector operator *<> (nr_double_t, tvector);
00100   friend tvector operator *<> (tvector, tvector);
00101   friend tvector operator -<> (tvector);
00102   friend tvector operator +<> (tvector, nr_type_t);
00103   friend tvector operator +<> (nr_type_t, tvector);
00104 #endif
00105 
00106   // other operations
00107 #ifndef _MSC_VER
00108   friend nr_double_t norm<> (tvector);
00109   friend nr_double_t maxnorm<> (tvector);
00110   friend nr_type_t   sum<> (tvector);
00111   friend nr_type_t   scalar<> (tvector, tvector);
00112   friend tvector     conj<> (tvector);
00113 #endif
00114 
00115   // comparisons
00116 #ifndef _MSC_VER
00117   friend bool operator < <> (tvector, tvector);
00118   friend bool operator > <> (tvector, tvector);
00119 #endif
00120 
00121   // intrinsic operators
00122   tvector operator += (tvector);
00123   tvector operator -= (tvector);
00124   tvector operator *= (nr_double_t);
00125   tvector operator /= (nr_double_t);
00126 
00127   // assignment operators
00128   tvector operator = (const nr_type_t);
00129 
00130   // easy accessor operators
00131   nr_type_t  operator () (int i) const {
00132     return data.at(i);
00133   }
00134   nr_type_t& operator () (int i) {
00135     return data.at(i); }
00136    nr_type_t  operator [] (int i) const {
00137     return data[i];
00138   }
00139   nr_type_t& operator [] (int i) {
00140     return data[i];
00141   }
00142 
00143  protected:
00144   std::vector<nr_type_t> data;
00145 
00146 };
00147 
00148   /*
00149   int  contains (nr_type_t val, nr_double_t eps = std::numeric_limits<nr_double_t>::epsilon()) {
00150     int count = 0;
00151     for (int i = 0; i < (int)data.size (); i++) if (abs ((data)[i] - val) <= eps) count++;
00152     return count;
00153     }*/
00154 
00155 
00156   
00157 } // namespace qucs
00158   
00159 #include "tvector.cpp"
00160 
00161 #endif /* __TVECTOR_H__ */