source: trunk/yat/utility/VectorMutable.cc @ 1190

Last change on this file since 1190 was 1190, checked in by Peter, 16 years ago

adding missing implementation of operator += and added some throwing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1// $Id: VectorMutable.cc 1190 2008-02-29 18:17:25Z peter $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "VectorMutable.h"
28#include "utility.h"
29#include "yat/random/random.h"
30
31#include <algorithm>
32#include <cassert>
33#include <cmath>
34#include <iostream>
35#include <sstream>
36#include <utility>
37#include <vector>
38
39namespace theplu {
40namespace yat {
41namespace utility {
42
43
44  VectorMutable::VectorMutable(void)
45    : VectorBase(NULL), vec_(NULL)
46  {
47  }
48
49
50  VectorMutable::VectorMutable(gsl_vector* v)
51    : VectorBase(v), vec_(v)
52  {
53  }
54
55
56  VectorMutable::VectorMutable(const gsl_vector* v)
57    : VectorBase(v), vec_(NULL)
58  {
59  }
60
61
62  VectorMutable::~VectorMutable(void)
63  {
64  }
65
66
67  void VectorMutable::all(double value)
68  {
69    assert(vec_);
70    gsl_vector_set_all(vec_,value);
71  }
72
73
74  VectorMutable::iterator VectorMutable::begin(void)
75  {
76    if (vec_)
77      return iterator(&(this->operator()(0)), vec_->stride);
78    return iterator(NULL, 1);
79  }
80
81
82  void VectorMutable::div(const VectorBase& other)
83  {
84    assert(vec_);
85    int status=gsl_vector_div(vec_,other.gsl_vector_p());
86    if (status)
87      throw utility::GSL_error(std::string("VectorMutable::div",status));
88  }
89
90
91  VectorMutable::iterator VectorMutable::end(void)
92  {
93    if (vec_)
94      return iterator(&(this->operator()(0))+vec_->stride*size(), 
95                            const_vec_->stride);
96    return iterator(NULL, 1);
97  }
98
99
100  gsl_vector* VectorMutable::gsl_vector_p(void)
101  {
102    return vec_;
103  }
104
105
106  void VectorMutable::mul(const VectorBase& other)
107  {
108    assert(vec_);
109    int status=gsl_vector_mul(vec_,other.gsl_vector_p());
110    if (status)
111      throw utility::GSL_error(std::string("VectorMutable::div",status));
112  }
113
114
115  double& VectorMutable::operator()(size_t i)
116  {
117    double* d=gsl_vector_ptr(vec_, i);
118    if (!d)
119      throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL);
120    return *d;
121  }
122
123
124  const VectorMutable& VectorMutable::operator+=(const VectorBase& other)
125  {
126    if (size()!=other.size())
127      throw std::runtime_error("VectorMutable::operator+= size must be same.");
128    assert(vec_);
129    int status=gsl_vector_add(vec_, other.gsl_vector_p());
130    if (status)
131      throw utility::GSL_error(std::string("VectorMutable::sub", status));
132    return *this;
133  }
134
135
136  const VectorMutable& VectorMutable::operator+=(double d)
137  {
138    assert(vec_);
139    gsl_vector_add_constant(vec_, d);
140    return *this;
141  }
142
143
144  const VectorMutable& VectorMutable::operator-=(const VectorBase& other)
145  {
146    if (size()!=other.size())
147      throw std::runtime_error("VectorMutable::operator-= size must be same.");
148    assert(vec_);
149    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
150    if (status)
151      throw utility::GSL_error(std::string("VectorMutable::sub", status));
152    return *this;
153  }
154
155
156  const VectorMutable& VectorMutable::operator-=(const double d)
157  {
158    assert(vec_);
159    gsl_vector_add_constant(vec_, -d);
160    return *this;
161  }
162
163
164  const VectorMutable& VectorMutable::operator*=(double d)
165  {
166    assert(vec_);
167    gsl_vector_scale(vec_, d);
168    return *this;
169  }
170
171
172  void shuffle(VectorMutable& invec)
173  {
174    random::random_shuffle(invec.begin(), invec.end());
175  }
176
177
178  void sort(VectorMutable& invec)
179  {
180    std::sort(invec.begin(), invec.end());
181  }
182
183
184  VectorMutable::operator proxy()
185  {
186    assert(vec_==const_vec_);
187    proxy p;
188    p.vec_ = vec_;
189    vec_ = NULL; // proxy takes ownership and delivers to its receiver
190    const_vec_ = NULL;
191    return p;
192  }
193
194}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.