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

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

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

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