1 | // $Id: VectorMutable.cc 1151 2008-02-25 22:32:04Z 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 | |
---|
39 | namespace theplu { |
---|
40 | namespace yat { |
---|
41 | namespace 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+=(double d) |
---|
125 | { |
---|
126 | assert(vec_); |
---|
127 | gsl_vector_add_constant(vec_, d); |
---|
128 | return *this; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | const VectorMutable& VectorMutable::operator-=(const VectorBase& other) |
---|
133 | { |
---|
134 | assert(vec_); |
---|
135 | int status=gsl_vector_sub(vec_, other.gsl_vector_p()); |
---|
136 | if (status) |
---|
137 | throw utility::GSL_error(std::string("VectorMutable::sub", status)); |
---|
138 | return *this; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | const VectorMutable& VectorMutable::operator-=(const double d) |
---|
143 | { |
---|
144 | assert(vec_); |
---|
145 | gsl_vector_add_constant(vec_, -d); |
---|
146 | return *this; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | const VectorMutable& VectorMutable::operator*=(double d) |
---|
151 | { |
---|
152 | assert(vec_); |
---|
153 | gsl_vector_scale(vec_, d); |
---|
154 | return *this; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | void shuffle(VectorMutable& invec) |
---|
159 | { |
---|
160 | random::random_shuffle(invec.begin(), invec.end()); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | void sort(VectorMutable& invec) |
---|
165 | { |
---|
166 | std::sort(invec.begin(), invec.end()); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | VectorMutable::operator proxy() |
---|
171 | { |
---|
172 | assert(vec_==const_vec_); |
---|
173 | proxy p; |
---|
174 | p.vec_ = vec_; |
---|
175 | vec_ = NULL; // proxy takes ownership and delivers to its receiver |
---|
176 | const_vec_ = NULL; |
---|
177 | return p; |
---|
178 | } |
---|
179 | |
---|
180 | }}} // of namespace utility, yat, and thep |
---|