common.h 3.97 KB
Newer Older
Laurent Belcour's avatar
Laurent Belcour committed
1 2 3 4
#pragma once

#include <vector>
#include <iostream>
5
#include <cassert>
6
#include <cmath>
Laurent Belcour's avatar
Laurent Belcour committed
7

8 9 10
/*! \brief A core implementation of a vector of double.
 *  \ingroup core
 *  \internal
11
 *
12 13 14 15
 *  \details
 *  This type is used for any transmission of vector data with unfixed
 *  dimension. It allows to have a generic fitter working for
 *  n-Dimensional data.
16
 */
Laurent Belcour's avatar
Laurent Belcour committed
17 18 19
class vec : public std::vector<double>
{
	public:
20
		// Constructor & Destructors
Laurent Belcour's avatar
Laurent Belcour committed
21
		//
22 23 24 25
		vec() : std::vector<double>()
		{
		}
		vec(int dim) : std::vector<double>(dim)
Laurent Belcour's avatar
Laurent Belcour committed
26
		{
27 28
			assign(dim, 0.0) ;
		}
Laurent Belcour's avatar
Laurent Belcour committed
29 30
		virtual ~vec() 
		{
31 32 33 34 35 36 37 38 39
        }

        //! \brief get a subpart of the vector
        vec subvector(int start, int n) const
        {
            vec res(n);
            memcpy(&res[0], &this->at(start), n*sizeof(double));
            return res;
        }
40 41 42 43 44 45 46 47 48 49 50 51

		//! \brief copy operator. It resize the left operand to the size of the 
		//! right operand.
		vec operator=(const vec& a)
		{
			this->resize(a.size());
			for(unsigned int i=0; i<a.size(); ++i)
			{
				this->at(i) = a[i];
			}
			return *this ;
		}
52
		
Laurent Belcour's avatar
Laurent Belcour committed
53 54 55 56
		// Mathematical operators
		//
		friend vec operator-(const vec& a)
		{
57 58
			vec b(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
59 60 61 62 63 64 65 66 67 68
			{
				b[i] = -a[i] ;
			}
			return b ;
		}
		friend vec operator-(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
69 70
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
71 72 73 74 75 76 77 78 79 80
			{
				c[i] = a[i] - b[i];
			}
			return c ;
		}
		friend vec operator+(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
81 82
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
83 84 85 86 87 88 89 90 91 92
			{
				c[i] = a[i] + b[i];
			}
			return c ;
		}
		friend vec operator*(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
93 94
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
95 96 97 98 99
			{
				c[i] = a[i] * b[i];
			}
			return c ;
		}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
		friend vec operator*(const vec& a, double b)
		{
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				c[i] = a[i] * b;
			}
			return c ;
		}
		friend vec operator*(double a, const vec& b)
		{
			vec c(b.size()) ;
			for(unsigned int i=0; i<b.size(); ++i)
			{
				c[i] = a * b[i];
			}
			return c ;
		}
		friend vec operator/(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size()) ;
#endif
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				c[i] = a[i] / b[i];
			}
			return c ;
		}
		friend vec operator/(const vec& a, double b)
		{
			vec c(a.size()) ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				c[i] = a[i] / b;
			}
			return c ;
		}
		friend vec operator/(double a, const vec& b)
		{
			vec c(b.size()) ;
			for(unsigned int i=0; i<b.size(); ++i)
			{
				c[i] = a / b[i];
			}
			return c ;
		}
		friend vec normalize(const vec& a)
		{
			vec b(a.size());
			double norm = 0.0 ;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				norm += a[i]*a[i];
			}
			norm = sqrt(norm);
			
			for(unsigned int i=0; i<a.size(); ++i)
			{
				b[i] = a[i]/norm;
			}
			return b;
		}
		friend double dot(const vec& a, const vec& b)
		{
#ifdef DEBUG
			assert(a.size() == b.size());
#endif
			double res = 0.0;
			for(unsigned int i=0; i<a.size(); ++i)
			{
				res += a[i]*b[i];
			}

			return res;
		}
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        friend bool operator<(const vec& a, const vec& b)
        {
            bool lessthan = true ;
            for(int i=0; i<a.size(); ++i)
                lessthan &= a[i] < b[i];
            return lessthan;
        }
        friend bool operator>(const vec& a, const vec& b)
        {
            bool greatthan = true ;
            for(int i=0; i<a.size(); ++i)
                greatthan &= a[i] > b[i];
            return greatthan;
        }

192
/*
Laurent Belcour's avatar
Laurent Belcour committed
193 194
		friend double norm(const vec& a)
		{
Laurent Belcour's avatar
Laurent Belcour committed
195

Laurent Belcour's avatar
Laurent Belcour committed
196 197
		}
*/
198
		//! \brief IO Functions
Laurent Belcour's avatar
Laurent Belcour committed
199
		//
Laurent Belcour's avatar
Laurent Belcour committed
200 201
		friend std::ostream& operator<< (std::ostream& out, const vec& v)
		{
Laurent Belcour's avatar
Laurent Belcour committed
202
			out << "[" ;
Laurent Belcour's avatar
Laurent Belcour committed
203
			for(unsigned int i=0; i<v.size(); ++i)
Laurent Belcour's avatar
Laurent Belcour committed
204
			{
Laurent Belcour's avatar
Laurent Belcour committed
205
				if(i > 0) out << ", " ;
Laurent Belcour's avatar
Laurent Belcour committed
206 207 208 209 210 211 212 213 214
				out << v[i] ;
			}
			out << "]" ;

			return out ;
		} ;


} ;