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

Last change on this file since 3623 was 3623, checked in by Peter, 7 years ago

merge release 0.14.1 into trunk

move functions 'create_gsl_vector' and 'create_gsl_vector_copy' from
Vector private to free functions in namespace detail, i.e., they are
available for other classes/functions, but not part of the public
API. In the same region add functions 'overlap' and 'serial_overlap'
to check if two GSL vectors overlap or can be used safely as LHS and
RHS in a function.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 1026
File size: 4.7 KB
Line 
1// $Id: VectorMutable.cc 3623 2017-02-09 06:11:57Z 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, 2009 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010, 2012, 2016, 2017 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "VectorMutable.h"
29#include "Vector.h"
30#include "Exception.h"
31#include "stl_utility.h"
32#include "yat/random/random.h"
33
34#include <algorithm>
35#include <cassert>
36#include <cmath>
37#include <limits>
38#include <stdexcept>
39#include <string>
40
41namespace theplu {
42namespace yat {
43namespace utility {
44
45
46  VectorMutable::VectorMutable(void)
47    : VectorBase(NULL), vec_(NULL)
48  {
49  }
50
51
52  VectorMutable::VectorMutable(gsl_vector* v)
53    : VectorBase(v), vec_(v)
54  {
55  }
56
57
58  VectorMutable::VectorMutable(const gsl_vector* v)
59    : VectorBase(v), vec_(NULL)
60  {
61  }
62
63
64  VectorMutable::~VectorMutable(void)
65  {
66  }
67
68
69  void VectorMutable::all(double value)
70  {
71    assert(vec_);
72    gsl_vector_set_all(vec_,value);
73  }
74
75
76  VectorMutable::iterator VectorMutable::begin(void)
77  {
78    if (vec_)
79      return iterator(&(this->operator()(0)), vec_->stride);
80    return iterator(NULL, 1);
81  }
82
83
84  void VectorMutable::div(const VectorBase& other)
85  {
86    assert(vec_);
87    if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) {
88      Vector tmp(other);
89      div(tmp);
90      return;
91    }
92    int status=gsl_vector_div(vec_,other.gsl_vector_p());
93    if (status)
94      throw utility::GSL_error(std::string("VectorMutable::div",status));
95  }
96
97
98  VectorMutable::iterator VectorMutable::end(void)
99  {
100    if (vec_)
101      return iterator(&(this->operator()(0))+vec_->stride*size(),
102                            const_vec_->stride);
103    return iterator(NULL, 1);
104  }
105
106
107  gsl_vector* VectorMutable::gsl_vector_p(void)
108  {
109    return vec_;
110  }
111
112
113  void VectorMutable::mul(const VectorBase& other)
114  {
115    assert(vec_);
116    if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) {
117      Vector tmp(other);
118      mul(tmp);
119      return;
120    }
121    int status=gsl_vector_mul(vec_,other.gsl_vector_p());
122    if (status)
123      throw utility::GSL_error(std::string("VectorMutable::div",status));
124  }
125
126
127  double& VectorMutable::operator()(size_t i)
128  {
129    double* d=gsl_vector_ptr(vec_, i);
130    if (!d)
131      throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL);
132    return *d;
133  }
134
135
136  const VectorMutable& VectorMutable::operator+=(const VectorBase& other)
137  {
138    if (size()!=other.size())
139      throw runtime_error("VectorMutable::operator+= size must be same.");
140    assert(vec_);
141
142    // if ranges overlap be a bit careful
143    if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) {
144      Vector tmp(other);
145      *this += tmp;
146      return *this;
147    }
148    int status=gsl_vector_add(vec_, other.gsl_vector_p());
149    if (status)
150      throw utility::GSL_error(std::string("VectorMutable::add", status));
151    return *this;
152  }
153
154
155  const VectorMutable& VectorMutable::operator+=(double d)
156  {
157    assert(vec_);
158    gsl_vector_add_constant(vec_, d);
159    return *this;
160  }
161
162
163  const VectorMutable& VectorMutable::operator-=(const VectorBase& other)
164  {
165    if (size()!=other.size())
166      throw runtime_error("VectorMutable::operator-= size must be same.");
167    assert(vec_);
168    if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) {
169      Vector tmp(other);
170      *this -= tmp;
171      return *this;
172    }
173    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
174    if (status)
175      throw utility::GSL_error(std::string("VectorMutable::sub", status));
176    return *this;
177  }
178
179
180  const VectorMutable& VectorMutable::operator-=(const double d)
181  {
182    assert(vec_);
183    gsl_vector_add_constant(vec_, -d);
184    return *this;
185  }
186
187
188  const VectorMutable& VectorMutable::operator*=(double d)
189  {
190    assert(vec_);
191    gsl_vector_scale(vec_, d);
192    return *this;
193  }
194
195
196  const VectorMutable& VectorMutable::operator=(const VectorBase& rhs)
197  {
198    return assign(rhs);
199  }
200
201
202  const VectorMutable& VectorMutable::operator=(const VectorMutable& rhs)
203  {
204    return assign(rhs);
205  }
206
207
208  void shuffle(VectorMutable& invec)
209  {
210    random::random_shuffle(invec.begin(), invec.end());
211  }
212
213
214  void sort(VectorMutable& invec)
215  {
216    std::sort(invec.begin(), invec.end(), less_nan<double>());
217  }
218
219
220}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.