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

Last change on this file since 1566 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1// $Id: VectorMutable.cc 1487 2008-09-10 08:41:36Z jari $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "VectorMutable.h"
26#include "utility.h"
27#include "yat/random/random.h"
28
29#include <algorithm>
30#include <cassert>
31#include <cmath>
32#include <iostream>
33#include <sstream>
34#include <utility>
35#include <vector>
36
37namespace theplu {
38namespace yat {
39namespace utility {
40
41
42  VectorMutable::VectorMutable(void)
43    : VectorBase(NULL), vec_(NULL)
44  {
45  }
46
47
48  VectorMutable::VectorMutable(gsl_vector* v)
49    : VectorBase(v), vec_(v)
50  {
51  }
52
53
54  VectorMutable::VectorMutable(const gsl_vector* v)
55    : VectorBase(v), vec_(NULL)
56  {
57  }
58
59
60  VectorMutable::~VectorMutable(void)
61  {
62  }
63
64
65  void VectorMutable::all(double value)
66  {
67    assert(vec_);
68    gsl_vector_set_all(vec_,value);
69  }
70
71
72  VectorMutable::iterator VectorMutable::begin(void)
73  {
74    if (vec_)
75      return iterator(&(this->operator()(0)), vec_->stride);
76    return iterator(NULL, 1);
77  }
78
79
80  void VectorMutable::div(const VectorBase& other)
81  {
82    assert(vec_);
83    int status=gsl_vector_div(vec_,other.gsl_vector_p());
84    if (status)
85      throw utility::GSL_error(std::string("VectorMutable::div",status));
86  }
87
88
89  VectorMutable::iterator VectorMutable::end(void)
90  {
91    if (vec_)
92      return iterator(&(this->operator()(0))+vec_->stride*size(), 
93                            const_vec_->stride);
94    return iterator(NULL, 1);
95  }
96
97
98  gsl_vector* VectorMutable::gsl_vector_p(void)
99  {
100    return vec_;
101  }
102
103
104  void VectorMutable::mul(const VectorBase& other)
105  {
106    assert(vec_);
107    int status=gsl_vector_mul(vec_,other.gsl_vector_p());
108    if (status)
109      throw utility::GSL_error(std::string("VectorMutable::div",status));
110  }
111
112
113  double& VectorMutable::operator()(size_t i)
114  {
115    double* d=gsl_vector_ptr(vec_, i);
116    if (!d)
117      throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL);
118    return *d;
119  }
120
121
122  const VectorMutable& VectorMutable::operator+=(const VectorBase& other)
123  {
124    if (size()!=other.size())
125      throw std::runtime_error("VectorMutable::operator+= size must be same.");
126    assert(vec_);
127    int status=gsl_vector_add(vec_, other.gsl_vector_p());
128    if (status)
129      throw utility::GSL_error(std::string("VectorMutable::sub", status));
130    return *this;
131  }
132
133
134  const VectorMutable& VectorMutable::operator+=(double d)
135  {
136    assert(vec_);
137    gsl_vector_add_constant(vec_, d);
138    return *this;
139  }
140
141
142  const VectorMutable& VectorMutable::operator-=(const VectorBase& other)
143  {
144    if (size()!=other.size())
145      throw std::runtime_error("VectorMutable::operator-= size must be same.");
146    assert(vec_);
147    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
148    if (status)
149      throw utility::GSL_error(std::string("VectorMutable::sub", status));
150    return *this;
151  }
152
153
154  const VectorMutable& VectorMutable::operator-=(const double d)
155  {
156    assert(vec_);
157    gsl_vector_add_constant(vec_, -d);
158    return *this;
159  }
160
161
162  const VectorMutable& VectorMutable::operator*=(double d)
163  {
164    assert(vec_);
165    gsl_vector_scale(vec_, d);
166    return *this;
167  }
168
169
170  void shuffle(VectorMutable& invec)
171  {
172    random::random_shuffle(invec.begin(), invec.end());
173  }
174
175
176  void sort(VectorMutable& invec)
177  {
178    std::sort(invec.begin(), invec.end());
179  }
180
181
182  VectorMutable::operator proxy()
183  {
184    assert(vec_==const_vec_);
185    proxy p;
186    p.vec_ = vec_;
187    vec_ = NULL; // proxy takes ownership and delivers to its receiver
188    const_vec_ = NULL;
189    return p;
190  }
191
192}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.