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

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

fixing memory bug in VectorView? - fixes #305

  • 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 1118 2008-02-21 22:08:23Z 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 "matrix.h"
29#include "utility.h"
30#include "yat/random/random.h"
31
32#include <algorithm>
33#include <cassert>
34#include <cmath>
35#include <iostream>
36#include <sstream>
37#include <utility>
38#include <vector>
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44
45  VectorMutable::VectorMutable(void)
46    : VectorBase(NULL), vec_(NULL)
47  {
48  }
49
50
51  VectorMutable::VectorMutable(gsl_vector* v)
52    : VectorBase(v), vec_(v)
53  {
54  }
55
56
57  VectorMutable::VectorMutable(const gsl_vector* v)
58    : VectorBase(v), vec_(NULL)
59  {
60  }
61
62
63  VectorMutable::~VectorMutable(void)
64  {
65  }
66
67
68  void VectorMutable::all(double value)
69  {
70    assert(vec_);
71    gsl_vector_set_all(vec_,value);
72  }
73
74
75  VectorMutable::iterator VectorMutable::begin(void)
76  {
77    if (vec_)
78      return iterator(&(this->operator()(0)), vec_->stride);
79    return iterator(NULL, 1);
80  }
81
82
83  void VectorMutable::div(const VectorBase& other)
84  {
85    assert(vec_);
86    int status=gsl_vector_div(vec_,other.gsl_vector_p());
87    if (status)
88      throw utility::GSL_error(std::string("VectorMutable::div",status));
89  }
90
91
92  VectorMutable::iterator VectorMutable::end(void)
93  {
94    if (vec_)
95      return iterator(&(this->operator()(0))+vec_->stride*size(), 
96                            const_vec_->stride);
97    return iterator(NULL, 1);
98  }
99
100
101  gsl_vector* VectorMutable::gsl_vector_p(void)
102  {
103    return vec_;
104  }
105
106
107  void VectorMutable::mul(const VectorBase& other)
108  {
109    assert(vec_);
110    int status=gsl_vector_mul(vec_,other.gsl_vector_p());
111    if (status)
112      throw utility::GSL_error(std::string("VectorMutable::div",status));
113  }
114
115
116  double& VectorMutable::operator()(size_t i)
117  {
118    double* d=gsl_vector_ptr(vec_, i);
119    if (!d)
120      throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL);
121    return *d;
122  }
123
124
125  double& VectorMutable::operator[](size_t i)
126  {
127    return this->operator()(i);
128  }
129
130
131  const VectorMutable& VectorMutable::operator+=(double d)
132  {
133    assert(vec_);
134    gsl_vector_add_constant(vec_, d);
135    return *this;
136  }
137
138
139  const VectorMutable& VectorMutable::operator-=(const VectorBase& other)
140  {
141    assert(vec_);
142    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
143    if (status)
144      throw utility::GSL_error(std::string("VectorMutable::sub", status));
145    return *this;
146  }
147
148
149  const VectorMutable& VectorMutable::operator-=(const double d)
150  {
151    assert(vec_);
152    gsl_vector_add_constant(vec_, -d);
153    return *this;
154  }
155
156
157  const VectorMutable& VectorMutable::operator*=(double d)
158  {
159    assert(vec_);
160    gsl_vector_scale(vec_, d);
161    return *this;
162  }
163
164
165  void shuffle(VectorMutable& invec)
166  {
167    random::random_shuffle(invec.begin(), invec.end());
168  }
169
170
171  void sort(VectorMutable& invec)
172  {
173    std::sort(invec.begin(), invec.end());
174  }
175
176
177  VectorMutable::operator proxy()
178  {
179    assert(vec_==const_vec_);
180    proxy p;
181    p.vec_ = vec_;
182    vec_ = NULL; // proxy takes ownership and delivers to its receiver
183    const_vec_ = NULL;
184    return p;
185  }
186
187}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.