source: trunk/src/vector.h @ 30

Last change on this file since 30 was 30, checked in by Peter, 19 years ago

adding SVM

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1// $Id: vector.h 30 2004-01-16 17:40:51Z peter $
2
3#ifndef _thep_gsl_api_vector_
4#define _thep_gsl_api_vector_
5
6#include <iostream>
7#include <fstream>
8#include <iomanip>
9#include <cmath>
10#include <cstdlib>
11#include <cassert>
12
13#include <gsl/gsl_math.h>
14#include <gsl/gsl_vector.h>
15#include <gsl/gsl_linalg.h>
16#include <gsl/gsl_blas.h>
17
18
19namespace thep_gsl_api
20{
21
22  /**
23     This is a vector interface to GSL for c++.
24     Important: vector has two dimenstions depending on
25     whether it's a "row" vector or a "column" vector.
26     A column vector is created by default.
27     Note that the namespace is "thep_gsl_api".
28  */
29
30  class vector
31  {
32  public:
33    typedef double Type;
34 
35    /**
36       Default constructor
37       Does not allocate any memory
38    */
39    vector();
40
41    /**
42       Constructor taking three argumens:
43       N = Number of elements
44       init_to_zero, if true vector will be initialized with zeros.
45    */
46    vector( const size_t&, bool is_col_vector = true, 
47      bool init_to_zero = true );
48
49
50
51    /**
52  Copy constructor
53    */
54    vector( const vector& other );
55
56
57    /**
58       Constructor taking as only argument a matrix of gsl type.
59       Be causious here! Do not delete this matrix outside of
60       class: NO COPY IS MADE!!!
61    */
62    vector( gsl_vector*, bool is_col = true );
63
64    /**
65       Constructor taking as only argument a an istream that should
66       contain the vector. Column vector should be sepated with '\\n'
67       and row vector should be separeted with whitespace (not '\\n').       
68
69    */
70    vector(std::istream &);
71
72
73    /**
74       Destructor will clear matrix's allocated memory
75    */
76    ~vector();
77
78
79    /**
80       The most important function when adding new classes to
81       c++_tools. This function will return a pointer to the
82       inner data of the class. Should be forbidden in a strictly
83       object-orientated environment.
84       Motivation: Effeciency! ( CAN IT BE MADE CONST? )
85       To be used when writting new classes using gsl-functions.
86    */
87    inline gsl_vector* get_gsl_vector() const; 
88
89
90    /**
91       Returns the number of rows in the vector.
92    */
93    size_t size() const { return v_->size; }
94
95
96    /**
97       Tells whether a vector is column or not
98     */
99    bool is_column() const { return is_col_; }
100
101
102    /**
103       get( i ) will return the vi element in vector v
104       where i = column (if row vector) or row (if column vector)
105    */
106    inline Type get( const size_t& i ) const;
107
108
109    /**
110       set( i, val ) will make vi = val in vector v
111       where i = column (if row vector) or row (if column vector)
112    */
113    inline void set( const size_t& i, const Type& val );
114
115    /**
116       set_all( val ) will make vi = val in vector v
117       for all "i"
118    */
119    inline void set_all( const Type& val );
120
121
122    /**
123       Assignment-operator. No requirements on dimensions,
124       i.e. if a = b, then a will be a copy of b with b:s
125       dimensions.
126     */
127    vector& operator=( const vector& other );
128
129
130    /**
131       Access element-operator
132    */
133    inline double &operator[](size_t i)  { return *gsl_vector_ptr( v_, i ); }
134    const double &operator[](size_t i) const { return *gsl_vector_ptr( v_, i ); }
135
136   
137    /**
138       operators for comparison are: \n
139       == equal \n
140       != not equal
141    */
142    bool operator==( const vector &other ) const;
143    bool operator!=( const vector &other ) const; 
144
145    /**
146       operators for basic arithmetics are: \n
147       \f$+\f$ vector-vector addition \n
148       \f$-\f$ vector-vector subtraction \n
149       \f$/=\f$ divide all elements with a constant
150    */
151    vector operator+( const vector& other ) const;
152    vector operator-( const vector& other ) const;
153    int operator/=( const double& c );
154    int operator/=( const vector& other ) const;
155    vector operator-() const;
156    /**
157       This operator is implemented as dot-product.
158       I case of discussions later on; Jari decided
159       this!
160    */
161    double operator*( const vector &other ) const;
162
163    /**
164       This function multiplies all elements in two vectors
165       and returns a third vector containing the result.
166       res = a*b; where size(a) = size(b) = size(res)
167    */
168    vector vector::mul_elements( const vector& other );
169   
170   
171    /**
172        standard output operator is defined
173    */
174    friend std::ostream& operator<< ( std::ostream&, const vector& );
175   
176
177  private:
178    gsl_vector* new_copy( const gsl_vector* );
179
180    gsl_vector* v_;
181    bool is_col_;
182  };
183#include "vector.icc"
184
185
186};
187
188
189
190
191#endif
Note: See TracBrowser for help on using the repository browser.