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

Last change on this file since 1703 was 1680, checked in by Peter, 13 years ago

correcting includes

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