source: trunk/yat/utility/VectorView.cc @ 1598

Last change on this file since 1598 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1// $Id: VectorView.cc 1487 2008-09-10 08:41:36Z jari $
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 "VectorView.h"
26#include "VectorMutable.h"
27#include "Matrix.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
39namespace theplu {
40namespace yat {
41namespace utility {
42
43
44  VectorView::VectorView(void)
45    : VectorMutable(), view_(NULL)
46  {
47  }
48
49
50  VectorView::VectorView(VectorView& other)
51    : VectorMutable()
52  {
53    view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(), 
54                                                     0, other.size()));
55    const_vec_ = vec_ = &(view_->vector);
56   
57  }
58
59
60  VectorView::VectorView(VectorMutable& other)
61    : VectorMutable()
62  {
63    copy(other.gsl_vector_p());
64  }
65
66
67  VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride)
68    : VectorMutable()
69  {
70    view_ = 
71      new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(), 
72                                                           offset, stride,n));
73    const_vec_ = vec_ = &(view_->vector);
74  }
75
76
77  VectorView::VectorView(Matrix& m, size_t i, bool row)
78    : VectorMutable()
79  {
80    view_=new gsl_vector_view(row ?
81                              gsl_matrix_row   (m.gsl_matrix_p(),i) :
82                              gsl_matrix_column(m.gsl_matrix_p(),i)  );
83    const_vec_ = vec_ = &(view_->vector);
84  }
85
86
87  VectorView::VectorView(proxy p)
88    : VectorMutable(), view_(NULL)
89  {
90    copy(p.vec_);
91  }
92
93
94  VectorView::~VectorView(void)
95  {
96    delete_allocated_memory();
97  }
98
99
100  const VectorView& VectorView::assign(const VectorBase& other )
101  {
102    if (size()!=other.size())
103      throw utility::GSL_error("VectorView::assign dimension mis-match");
104    if (!other.size())
105      return *this;
106    if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
107      throw utility::GSL_error("VectorView::assign memcpy failed.");
108    const_vec_ = vec_;
109    return *this;
110  } 
111
112
113  void VectorView::copy(gsl_vector* other )
114  {
115    view_ = new gsl_vector_view(gsl_vector_subvector(other,0,other->size));
116    const_vec_ = vec_ = &(view_->vector);
117  }
118
119
120  bool VectorView::isview(void) const
121  {
122    return true;
123  }
124
125
126
127  const VectorView& VectorView::operator=(const VectorView& other )
128  {
129    return assign(other);
130  }
131
132 
133  const VectorView& VectorView::operator=(const VectorBase& other )
134  {
135    return assign(other);
136  }
137
138 
139  void VectorView::delete_allocated_memory(void)
140  {
141    if (view_){
142      delete view_;
143      view_=NULL;
144    }
145    const_vec_ = vec_ = NULL;
146  }
147
148
149}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.