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

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

going back to previous design in which view and const_view are in different classes. Having them in same didnt work as expected. There is a problem converting vector::iterator to vector::const_iterator. I'll open a separate ticket for this issue.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1// $Id: VectorMutable.cc 1027 2008-02-02 21:29:29Z 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    return iterator(*this, 0);
78  }
79
80
81  void VectorMutable::div(const VectorBase& other)
82  {
83    assert(vec_);
84    int status=gsl_vector_div(vec_,other.gsl_vector_p());
85    if (status)
86      throw utility::GSL_error(std::string("VectorMutable::div",status));
87  }
88
89
90  VectorMutable::iterator VectorMutable::end(void)
91  {
92    return iterator(*this, size());
93  }
94
95
96  gsl_vector* VectorMutable::gsl_vector_p(void)
97  {
98    return vec_;
99  }
100
101
102  void VectorMutable::mul(const VectorBase& other)
103  {
104    assert(vec_);
105    int status=gsl_vector_mul(vec_,other.gsl_vector_p());
106    if (status)
107      throw utility::GSL_error(std::string("VectorMutable::div",status));
108  }
109
110
111  double& VectorMutable::operator()(size_t i)
112  {
113    double* d=gsl_vector_ptr(vec_, i);
114    if (!d)
115      throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL);
116    return *d;
117  }
118
119
120  double& VectorMutable::operator[](size_t i)
121  {
122    return this->operator()(i);
123  }
124
125
126  const VectorMutable& VectorMutable::operator+=(double d)
127  {
128    assert(vec_);
129    gsl_vector_add_constant(vec_, d);
130    return *this;
131  }
132
133
134  const VectorMutable& VectorMutable::operator-=(const VectorBase& other)
135  {
136    assert(vec_);
137    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
138    if (status)
139      throw utility::GSL_error(std::string("VectorMutable::sub", status));
140    return *this;
141  }
142
143
144  const VectorMutable& VectorMutable::operator-=(const double d)
145  {
146    assert(vec_);
147    gsl_vector_add_constant(vec_, -d);
148    return *this;
149  }
150
151
152  const VectorMutable& VectorMutable::operator*=(double d)
153  {
154    assert(vec_);
155    gsl_vector_scale(vec_, d);
156    return *this;
157  }
158
159
160  void shuffle(VectorMutable& invec)
161  {
162    random::random_shuffle(invec.begin(), invec.end());
163  }
164
165
166  void sort(VectorMutable& invec)
167  {
168    std::sort(invec.begin(), invec.end());
169  }
170
171
172  VectorMutable::operator proxy()
173  {
174    proxy p;
175    p.vec_ = vec_;
176    vec_ = NULL; // proxy takes ownership and delivers to its receiver
177    return p;
178  }
179
180}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.