File: vector.h

package info (click to toggle)
rivet 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 22,900 kB
  • sloc: cpp: 47,222; sh: 10,328; python: 6,469; ansic: 1,710; makefile: 1,221
file content (384 lines) | stat: -rw-r--r-- 11,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2006-2007 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with Eigen; if not, write to the Free Software Foundation, Inc., 51
// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. This exception does not invalidate any other reasons why a work
// based on this file might be covered by the GNU General Public License.

/** \file vector.h
  * \brief Vector and VectorX class templates
  */

#ifndef EIGEN_VECTOR_H
#define EIGEN_VECTOR_H

#include "vectorbase.h"

namespace Eigen
{

/** \ingroup fixedsize
  *
  * \ingroup vectors
  *
  * \brief Fixed-size vector.
  *
  * A class for fixed-size vectors (for linear algebra).
  * Thus, a Vector<T,Size> is the same
  * as a T[Size] array, except that it has convenient operators and methods
  * for basic vector math.
  *
  * The template parameter T is the type of the coords of the vector.
  * It can be any type representing either real or complex numbers.
  * The template parameter Size is the size of the vector (number of coords).
  * The following typedefs are provided to cover the usual cases:
  * @code
    typedef Vector<double, 2>               Vector2d;
    typedef Vector<double, 3>               Vector3d;
    typedef Vector<double, 4>               Vector4d;
    typedef Vector<float,  2>               Vector2f;
    typedef Vector<float,  3>               Vector3f;
    typedef Vector<float,  4>               Vector4f;
    typedef Vector<std::complex<double>, 2> Vector2cd;
    typedef Vector<std::complex<double>, 3> Vector3cd;
    typedef Vector<std::complex<double>, 4> Vector4cd;
    typedef Vector<std::complex<float>,  2> Vector2cf;
    typedef Vector<std::complex<float>,  3> Vector3cf;
    typedef Vector<std::complex<float>,  4> Vector4cf;
  * @endcode
  *
  * If you prefer dynamic-size vectors (they are slower), see the VectorX
  * class template, which provides exactly the same functionality and API
  * in dynamic-size version.
  *
  * The Vector class template provides all the usual operators and methods
  * to manipulate vectors.
  *
  * Here are some examples of usage of Vector:
  * @code
    using namespace Eigen;
    using namespace std; // we'll use cout for outputting vectors

    Vector3d vec1( -1.1, 2.9, 4.3 ); // construct vector vec1 with given coords
    double array[3] = { 2.4, 3.1, -0.7 };
    Vector3d vec2( array ); // reads the coords of vec2 from array2

    vec1 += vec2; // computes the coord-wise sum vec1 + vec2, stores it in vec1
    vec1 = vec1 - vec2; // there are also non-assignment operators
    vec1 = 0.9 * vec1 + vec2 / 2.6; // you can also multiply/divide by numbers

    vec1.x() = vec2.y() // read-write access to the x,y,z,w coords
    vec1(2) = -1.4; // Stores the value -1.4 in coord 2 of vec1.
    vec1.z() = -1.4; // equivalent to the previous line

    cout << vec1 << endl; // outputs vec1
    cout << "norm of vec1: " << vec1.norm() << endl;
    cout << cross( vec1, vec2 ) << endl; // cross-product
  * @endcode
  */
template< typename T, int Size >
class Vector : public VectorBase< T, Vector<T, Size> >
{
    friend class VectorBase< T, Vector<T, Size> >;
    typedef class VectorBase< T, Vector<T, Size> > Base;

private:

    /** \internal
      * Returns false. A Vector<T,Size> doesn't have dynamic size.
      */
    bool _hasDynamicSize() const
    { return false; }

    /** \internal
      * Returns the size of the vector.
      */
    int _size() const
    { return Size; }

    /** \internal
      * Does nothing. A Vector<T,Size> can't be resized.
      *
      * if newsize != size(), a debug message is generated.
      */
    void _resize( int size ) const
    { assert( size == this->size() ); }

public:

    /**
      * Default constructor. Constructs a vector with uninitialized coords.
      */
    Vector() {}

    /**
      * Convenience constructor provided for API homogeneity with VectorX.
      * The unused_size argument is not used.
      */
    explicit Vector( int unused_size )
    { assert( unused_size == this->size() ); }

    /**
      * Copy constructor.
      */
    Vector( const Vector &v )
    {
        this->readArray( v.array() );
    }

    /**
      * Constructor reading the coords from an array.
      */
    Vector( const T *array )
    {
        readArray( array );
    }

    /**
      * Convenience constructor provided for API homogeneity with VectorX.
      * Constructor reading the coords from an array.
      * The unused_size argument is not used.
      */
    Vector( int unused_size, const T *array )
    {
        assert( unused_size == this->size() );
        readArray( array );
    }

    /**
      * Convenience constructor for vectors of size 2.
      */
    Vector( T x, T y )
    {
        assert( this->size() == 2 );
        this->x() = x;
        this->y() = y;
    }

    /**
      * Convenience constructor for vectors of size 3.
      */
    Vector( T x, T y, T z )
    {
        assert( this->size() == 3 );
        this->x() = x;
        this->y() = y;
        this->z() = z;
    }

    /**
      * Convenience constructor for vectors of size 4.
      */
    Vector( T x, T y, T z, T w )
    {
        assert( this->size() == 4 );
        this->x() = x;
        this->y() = y;
        this->z() = z;
        this->w() = w;
    }

    Vector & operator = ( const Vector & other )
    { return Base::operator = ( other ); }

    Vector & operator += ( const Vector & other )
    { return Base::operator += ( other ); }

    Vector & operator -= ( const Vector & other )
    { return Base::operator -= ( other ); }

    Vector & operator *=( const T & factor )
    { return Base::operator *= ( factor ); }

    Vector & operator /=( const T & factor )
    { return Base::operator /= ( factor ); }

protected:

    /**
      * The vector's array of coordinates.
      */
    T m_array[Size];
};

/** \ingroup dynamicsize
  *
  * \ingroup vectors
  *
  * \brief Dynamic-size vector
  *
  * A class for dynamic-size vectors (for linear algebra).
  *
  * The template parameter T is the type of the coords of the vector.
  * It can be any type representing either real or complex numbers.
  * The following typedefs are provided to cover the usual cases:
  * @code
    typedef VectorX<double>                 VectorXd;
    typedef VectorX<float>                  VectorXf;
    typedef VectorX< std::complex<double> > VectorXcd;
    typedef VectorX< std::complex<float> >  VectorXcf;
  * @endcode
  *
  * If you prefer fixed-size vectors (they are faster), see the Vector
  * class template, which provides exactly the same functionality and API
  * in fixed-size version.
  *
  * The VectorX class template provides all the usual operators and methods
  * to manipulate vectors.
  *
  * Here are some examples of usage of VectorX:
  * @code
    using namespace Eigen;
    using namespace std; // we'll use cout for outputting vectors

    double array1[3] = { -1.1, 2.9, 4.3 };
    VectorXd vec1( 3, array1 ); // construct vector vec1 from array array1

    VectorXd vec2( 3 ); // construct a new uninitialized vector of size 3
    double array2[3] = { 2.4, 3.1, -0.7 };
    vec2.readArray( array2); // reads the coords of vec2 from array2

    vec1 += vec2; // computes the coord-wise sum vec1 + vec2, stores it in vec1
    vec1 = vec1 - vec2; // there are also non-assignment operators
    vec1 = 0.9 * vec1 + vec2 / 2.6; // you can also multiply/divide by numbers

    VectorXd vec3(5); // construct a new uninitialized vector of size 5
    vec3 = vec1; // Resizes vec3 to size 3, copies vec1 into vec3

    vec1(2) = -1.4; // Stores the value -1.4 in coord 2 of vec1.
    cout << vec1 << endl;
    cout << "norm of vec1: " << vec1.norm() << endl;
  * @endcode
  */
template <typename T>
class VectorX : public VectorBase< T, VectorX<T> >
{
    friend class VectorBase< T, VectorX<T> >;
    typedef class VectorBase< T, VectorX<T> > Base;


private:

    /** \internal
      * Small helper function for the constructors and the _resize method
      */
    void init( int size )
    {
       assert( size >= 1 );
       m_size = size;
       m_array  = new T[_size()];
    }

    /** \internal
      * Returns true. A VectorX has dynamic size.
      */
    bool _hasDynamicSize() const
    { return true; }

    /** \internal
      * Returns the size of the vector.
      */
    int _size() const
    { return m_size; }

    /** \internal
      * Resizes the vector.
      */
    void _resize( int size );

public:

    /**
      * Copy constructor
      */
    VectorX( const VectorX & other )
    {
        init( other._size() );
        readArray( other.array() );
    }

    /**
      * Constructs a vector with given size and uninitialized coords.
      * The default value sor size is 1.
      */
    explicit VectorX( int size = 1 )
    { init( size ); }

    /**
      * Constructs a vector with given size and reads its coord from the array.
      */
    VectorX( int size, const T * array )
    {
        init( size );
        readArray( array );
    }

    ~VectorX()
    { delete[] m_array; }

    VectorX & operator = ( const VectorX & other )
    { return Base::operator = ( other ); }

    VectorX & operator += ( const VectorX & other )
    { return Base::operator += ( other ); }

    VectorX & operator -= ( const VectorX & other )
    { return Base::operator -= ( other ); }

    VectorX & operator *=( const T & factor )
    { return Base::operator *= ( factor ); }

    VectorX & operator /=( const T & factor )
    { return Base::operator /= ( factor ); }

protected:

    /**
      * The size (dimension) of the vector
      */
    int m_size;

    /**
      * The vector's array of coordinates.
      */
    T *m_array;

};

template<typename T>
void VectorX<T>::_resize( int size )
{
    assert( size >= 1 );
    if( size == _size() ) return;
    if( size > _size() )
    {
        delete[] m_array;
        m_array  = new T[size];
    }
    m_size = size;
}

EIGEN_MAKE_FIXEDSIZE_TYPEDEFS(Vector)
EIGEN_MAKE_DYNAMICSIZE_TYPEDEFS(VectorX)

}

#endif // EIGEN_VECTOR_H