1 | // $Id: VectorView.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 "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 | view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(), |
---|
66 | 0,other.size())); |
---|
67 | const_vec_ = vec_ = &(view_->vector); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride) |
---|
72 | : VectorMutable() |
---|
73 | { |
---|
74 | view_ = |
---|
75 | new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(), |
---|
76 | offset, stride,n)); |
---|
77 | const_vec_ = vec_ = &(view_->vector); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | VectorView::VectorView(matrix& m, size_t i, bool row) |
---|
82 | : VectorMutable() |
---|
83 | { |
---|
84 | view_=new gsl_vector_view(row ? |
---|
85 | gsl_matrix_row (m.gsl_matrix_p(),i) : |
---|
86 | gsl_matrix_column(m.gsl_matrix_p(),i) ); |
---|
87 | const_vec_ = vec_ = &(view_->vector); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | VectorView::VectorView(proxy p) |
---|
92 | : VectorMutable(p.vec_), view_(NULL) |
---|
93 | { |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | VectorView::~VectorView(void) |
---|
98 | { |
---|
99 | delete_allocated_memory(); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | const VectorView& VectorView::assign(const VectorBase& other ) |
---|
104 | { |
---|
105 | if (size()!=other.size()) |
---|
106 | throw utility::GSL_error("VectorView::dimension mis-match"); |
---|
107 | if (!other.size()) |
---|
108 | return *this; |
---|
109 | gsl_vector_memcpy(vec_, other.gsl_vector_p()); |
---|
110 | const_vec_ = vec_; |
---|
111 | return *this; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | bool VectorView::isview(void) const |
---|
116 | { |
---|
117 | return true; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | const VectorView& VectorView::operator=(const VectorView& other ) |
---|
123 | { |
---|
124 | return assign(other); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | const VectorView& VectorView::operator=(const VectorBase& other ) |
---|
129 | { |
---|
130 | return assign(other); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void VectorView::delete_allocated_memory(void) |
---|
135 | { |
---|
136 | if (view_){ |
---|
137 | delete view_; |
---|
138 | view_=NULL; |
---|
139 | } |
---|
140 | const_vec_ = vec_ = NULL; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | }}} // of namespace utility, yat, and thep |
---|