1 | // $Id: VectorView.cc 1437 2008-08-25 17:55:00Z 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 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 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 "VectorView.h" |
---|
28 | #include "VectorMutable.h" |
---|
29 | #include "Matrix.h" |
---|
30 | #include "utility.h" |
---|
31 | #include "yat/random/random.h" |
---|
32 | |
---|
33 | #include <algorithm> |
---|
34 | #include <cassert> |
---|
35 | #include <cmath> |
---|
36 | #include <iostream> |
---|
37 | #include <sstream> |
---|
38 | #include <utility> |
---|
39 | #include <vector> |
---|
40 | |
---|
41 | namespace theplu { |
---|
42 | namespace yat { |
---|
43 | namespace utility { |
---|
44 | |
---|
45 | |
---|
46 | VectorView::VectorView(void) |
---|
47 | : VectorMutable(), view_(NULL) |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | VectorView::VectorView(VectorView& other) |
---|
53 | : VectorMutable() |
---|
54 | { |
---|
55 | view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(), |
---|
56 | 0, other.size())); |
---|
57 | const_vec_ = vec_ = &(view_->vector); |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | VectorView::VectorView(VectorMutable& other) |
---|
63 | : VectorMutable() |
---|
64 | { |
---|
65 | copy(other.gsl_vector_p()); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride) |
---|
70 | : VectorMutable() |
---|
71 | { |
---|
72 | view_ = |
---|
73 | new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(), |
---|
74 | offset, stride,n)); |
---|
75 | const_vec_ = vec_ = &(view_->vector); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | VectorView::VectorView(Matrix& m, size_t i, bool row) |
---|
80 | : VectorMutable() |
---|
81 | { |
---|
82 | view_=new gsl_vector_view(row ? |
---|
83 | gsl_matrix_row (m.gsl_matrix_p(),i) : |
---|
84 | gsl_matrix_column(m.gsl_matrix_p(),i) ); |
---|
85 | const_vec_ = vec_ = &(view_->vector); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | VectorView::VectorView(proxy p) |
---|
90 | : VectorMutable(), view_(NULL) |
---|
91 | { |
---|
92 | copy(p.vec_); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | VectorView::~VectorView(void) |
---|
97 | { |
---|
98 | delete_allocated_memory(); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | const VectorView& VectorView::assign(const VectorBase& other ) |
---|
103 | { |
---|
104 | if (size()!=other.size()) |
---|
105 | throw utility::GSL_error("VectorView::assign dimension mis-match"); |
---|
106 | if (!other.size()) |
---|
107 | return *this; |
---|
108 | if (gsl_vector_memcpy(vec_, other.gsl_vector_p())) |
---|
109 | throw utility::GSL_error("VectorView::assign memcpy failed."); |
---|
110 | const_vec_ = vec_; |
---|
111 | return *this; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void VectorView::copy(gsl_vector* other ) |
---|
116 | { |
---|
117 | view_ = new gsl_vector_view(gsl_vector_subvector(other,0,other->size)); |
---|
118 | const_vec_ = vec_ = &(view_->vector); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | bool VectorView::isview(void) const |
---|
123 | { |
---|
124 | return true; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | const VectorView& VectorView::operator=(const VectorView& other ) |
---|
130 | { |
---|
131 | return assign(other); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | const VectorView& VectorView::operator=(const VectorBase& other ) |
---|
136 | { |
---|
137 | return assign(other); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | void VectorView::delete_allocated_memory(void) |
---|
142 | { |
---|
143 | if (view_){ |
---|
144 | delete view_; |
---|
145 | view_=NULL; |
---|
146 | } |
---|
147 | const_vec_ = vec_ = NULL; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | }}} // of namespace utility, yat, and thep |
---|