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

Last change on this file since 1121 was 1121, checked in by Peter, 14 years ago

fixes #308

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1// $Id: VectorMutable.cc 1121 2008-02-22 15:29:56Z 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  double& VectorMutable::operator[](size_t i)
125  {
126    return this->operator()(i);
127  }
128
129
130  const VectorMutable& VectorMutable::operator+=(double d)
131  {
132    assert(vec_);
133    gsl_vector_add_constant(vec_, d);
134    return *this;
135  }
136
137
138  const VectorMutable& VectorMutable::operator-=(const VectorBase& other)
139  {
140    assert(vec_);
141    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
142    if (status)
143      throw utility::GSL_error(std::string("VectorMutable::sub", status));
144    return *this;
145  }
146
147
148  const VectorMutable& VectorMutable::operator-=(const double d)
149  {
150    assert(vec_);
151    gsl_vector_add_constant(vec_, -d);
152    return *this;
153  }
154
155
156  const VectorMutable& VectorMutable::operator*=(double d)
157  {
158    assert(vec_);
159    gsl_vector_scale(vec_, d);
160    return *this;
161  }
162
163
164  void shuffle(VectorMutable& invec)
165  {
166    random::random_shuffle(invec.begin(), invec.end());
167  }
168
169
170  void sort(VectorMutable& invec)
171  {
172    std::sort(invec.begin(), invec.end());
173  }
174
175
176  VectorMutable::operator proxy()
177  {
178    assert(vec_==const_vec_);
179    proxy p;
180    p.vec_ = vec_;
181    vec_ = NULL; // proxy takes ownership and delivers to its receiver
182    const_vec_ = NULL;
183    return p;
184  }
185
186}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.