source: trunk/src/vector.cc @ 12

Last change on this file since 12 was 12, checked in by daniel, 19 years ago

Matrix and vector APIs for GSL.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1
2#include <iostream>
3#include "vector.h"
4
5using namespace thep_gsl_api;
6
7
8// Constructors and Destructors
9///////////////////////////////
10vector::vector() : v_( NULL )
11{
12  is_col_ = true;
13}
14
15
16vector::vector( const size_t& N, bool is_col_vector, 
17    bool init_to_zero ) :
18  is_col_( is_col_vector )
19{
20  if( init_to_zero )
21    { 
22     v_ = gsl_vector_calloc( N );
23    }
24  else     
25    {
26     v_ = gsl_vector_alloc ( N );
27    }
28}
29
30
31// Is this the way to do it? No copy ...
32// internal data ...
33vector::vector( gsl_vector* v, bool is_col ) : v_( v ), is_col_( is_col )
34{
35}
36
37
38// Copy constructor
39vector::vector( const vector& other )
40{
41  v_ = new_copy( other.get_gsl_vector() );
42  is_col_ = other.is_column();
43}
44
45
46vector::~vector()
47{
48  if( v_ ) 
49    {
50     gsl_vector_free( v_ );
51     v_ = NULL;
52    }
53}
54
55
56gsl_vector* vector::new_copy( const gsl_vector* p_other )
57{
58  // Get dimenstions
59  size_t N = p_other->size;
60   
61  // Create new empty vector
62  gsl_vector* p_res =  gsl_vector_alloc( N );
63
64  // Copy p_others elements into p_res
65  gsl_vector_memcpy( p_res, p_other );
66
67  return p_res;
68}
69
70
71// Operators on vectors
72////////////////////////
73vector& vector::operator=( const vector& other )
74{
75  if( this != &other ) 
76    {
77     gsl_vector* v_new = new_copy( other.get_gsl_vector() );
78     if( v_ ) gsl_vector_free( v_ );
79     v_ = v_new;
80    }
81  return *this;
82} 
83
84
85vector vector::operator+( const vector &other ) const
86{
87  assert( size() == other.size() );
88  vector res( *this );
89  gsl_vector_add( res.get_gsl_vector(), other.get_gsl_vector() );
90  return res;
91}
92
93
94vector vector::operator-( const vector &other ) const
95{
96  assert( size() == other.size() );
97  vector res( *this );
98  gsl_vector_sub( res.get_gsl_vector(), other.get_gsl_vector() );
99  return res;
100}
101
102
103int vector::operator/=( const vector& other ) const
104{
105  return gsl_vector_div( v_, other.get_gsl_vector() );
106}
107
108// Each element is divided by the "constant"
109int vector::operator/=( const double& constant ) 
110{
111  assert( constant != 0 );
112  return gsl_vector_scale( v_, 1 / constant );
113}
114
115// This is not the dot product, hence a matrix
116// is returned
117// matrix vector::operator*( const vector &other ) const
118// {
119//   assert( rows() == other.cols() );
120//   matrix res( rows(), other.cols() );
121//   gsl_linalg_matmult( v_, other.get_gsl_vector(),
122//          res.get_gsl_vector() );
123//   return res;
124// }
125
126
127std::ostream& thep_gsl_api::operator<< ( std::ostream& s_out, 
128           const vector& a )
129{
130  using namespace std;
131  s_out.setf( ios::fixed ); 
132
133  if( a.is_column() )
134    {
135     for ( size_t i = 0; i < a.size(); ++i ) 
136       {
137  s_out << a.get( i  ) << endl;
138       }
139    }
140  else
141    {
142     for ( size_t j = 0; j < a.size(); ++j ) 
143       {
144  s_out << a.get( j ) << " ";
145       }
146    }
147
148  return s_out;
149}
150
151
152// // Public functions (A-Z)
153// /////////////////////////
154
155
Note: See TracBrowser for help on using the repository browser.