22 |
05 Aug 03 |
peter |
// $Id$ |
12 |
19 Jun 03 |
daniel |
2 |
|
570 |
05 Apr 06 |
jari |
3 |
/* |
570 |
05 Apr 06 |
jari |
Copyright (C) 2003 Daniel Dalevi, Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4207 |
26 Aug 22 |
peter |
Copyright (C) 2012, 2013, 2017, 2021, 2022 Peter Johansson |
570 |
05 Apr 06 |
jari |
9 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/trac/yat |
570 |
05 Apr 06 |
jari |
11 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
570 |
05 Apr 06 |
jari |
16 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
570 |
05 Apr 06 |
jari |
General Public License for more details. |
570 |
05 Apr 06 |
jari |
21 |
|
570 |
05 Apr 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
570 |
05 Apr 06 |
jari |
24 |
*/ |
570 |
05 Apr 06 |
jari |
25 |
|
2881 |
18 Nov 12 |
peter |
26 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
27 |
|
1015 |
01 Feb 08 |
peter |
28 |
#include "VectorView.h" |
1027 |
02 Feb 08 |
peter |
29 |
#include "VectorMutable.h" |
3623 |
09 Feb 17 |
peter |
30 |
#include "Vector.h" |
4119 |
07 Nov 21 |
peter |
31 |
#include "MatrixMutable.h" |
680 |
11 Oct 06 |
jari |
32 |
#include "utility.h" |
125 |
02 Aug 04 |
peter |
33 |
|
783 |
05 Mar 07 |
jari |
34 |
#include <cassert> |
42 |
26 Feb 04 |
jari |
35 |
|
42 |
26 Feb 04 |
jari |
36 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
37 |
namespace yat { |
616 |
31 Aug 06 |
jari |
38 |
namespace utility { |
12 |
19 Jun 03 |
daniel |
39 |
|
12 |
19 Jun 03 |
daniel |
40 |
|
1015 |
01 Feb 08 |
peter |
41 |
VectorView::VectorView(void) |
1027 |
02 Feb 08 |
peter |
42 |
: VectorMutable(), view_(NULL) |
686 |
16 Oct 06 |
jari |
43 |
{ |
686 |
16 Oct 06 |
jari |
44 |
} |
12 |
19 Jun 03 |
daniel |
45 |
|
686 |
16 Oct 06 |
jari |
46 |
|
1015 |
01 Feb 08 |
peter |
47 |
VectorView::VectorView(VectorView& other) |
1027 |
02 Feb 08 |
peter |
48 |
: VectorMutable() |
686 |
16 Oct 06 |
jari |
49 |
{ |
3102 |
02 Nov 13 |
peter |
50 |
copy(other.gsl_vector_p()); |
686 |
16 Oct 06 |
jari |
51 |
} |
686 |
16 Oct 06 |
jari |
52 |
|
686 |
16 Oct 06 |
jari |
53 |
|
1027 |
02 Feb 08 |
peter |
54 |
VectorView::VectorView(VectorMutable& other) |
1027 |
02 Feb 08 |
peter |
55 |
: VectorMutable() |
686 |
16 Oct 06 |
jari |
56 |
{ |
1118 |
21 Feb 08 |
peter |
57 |
copy(other.gsl_vector_p()); |
1008 |
01 Feb 08 |
peter |
58 |
} |
1008 |
01 Feb 08 |
peter |
59 |
|
1008 |
01 Feb 08 |
peter |
60 |
|
1027 |
02 Feb 08 |
peter |
61 |
VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride) |
1027 |
02 Feb 08 |
peter |
62 |
: VectorMutable() |
1008 |
01 Feb 08 |
peter |
63 |
{ |
2687 |
27 Feb 12 |
peter |
64 |
view_ = |
2687 |
27 Feb 12 |
peter |
65 |
new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(), |
995 |
30 Nov 07 |
peter |
66 |
offset, stride,n)); |
995 |
30 Nov 07 |
peter |
67 |
const_vec_ = vec_ = &(view_->vector); |
686 |
16 Oct 06 |
jari |
68 |
} |
686 |
16 Oct 06 |
jari |
69 |
|
686 |
16 Oct 06 |
jari |
70 |
|
4119 |
07 Nov 21 |
peter |
71 |
VectorView::VectorView(MatrixMutable& m, size_t i, bool row) |
1027 |
02 Feb 08 |
peter |
72 |
: VectorMutable() |
42 |
26 Feb 04 |
jari |
73 |
{ |
420 |
02 Dec 05 |
jari |
74 |
view_=new gsl_vector_view(row ? |
420 |
02 Dec 05 |
jari |
75 |
gsl_matrix_row (m.gsl_matrix_p(),i) : |
420 |
02 Dec 05 |
jari |
76 |
gsl_matrix_column(m.gsl_matrix_p(),i) ); |
995 |
30 Nov 07 |
peter |
77 |
const_vec_ = vec_ = &(view_->vector); |
257 |
04 Mar 05 |
jari |
78 |
} |
257 |
04 Mar 05 |
jari |
79 |
|
257 |
04 Mar 05 |
jari |
80 |
|
1027 |
02 Feb 08 |
peter |
81 |
VectorView::VectorView(proxy p) |
1118 |
21 Feb 08 |
peter |
82 |
: VectorMutable(), view_(NULL) |
1008 |
01 Feb 08 |
peter |
83 |
{ |
1118 |
21 Feb 08 |
peter |
84 |
copy(p.vec_); |
1008 |
01 Feb 08 |
peter |
85 |
} |
1008 |
01 Feb 08 |
peter |
86 |
|
1008 |
01 Feb 08 |
peter |
87 |
|
4141 |
01 Feb 22 |
peter |
88 |
VectorView::VectorView(gsl_vector* v) |
4141 |
01 Feb 22 |
peter |
89 |
{ |
4141 |
01 Feb 22 |
peter |
90 |
copy(v); |
4141 |
01 Feb 22 |
peter |
91 |
} |
4141 |
01 Feb 22 |
peter |
92 |
|
4141 |
01 Feb 22 |
peter |
93 |
|
4141 |
01 Feb 22 |
peter |
94 |
VectorView::VectorView(double* v, size_t size, size_t stride) |
4141 |
01 Feb 22 |
peter |
95 |
{ |
4141 |
01 Feb 22 |
peter |
96 |
assert(v); |
4141 |
01 Feb 22 |
peter |
97 |
assert(size); |
4141 |
01 Feb 22 |
peter |
98 |
assert(stride); |
4141 |
01 Feb 22 |
peter |
99 |
view_ = |
4141 |
01 Feb 22 |
peter |
100 |
new gsl_vector_view(gsl_vector_view_array_with_stride(v, stride, size)); |
4141 |
01 Feb 22 |
peter |
101 |
const_vec_ = vec_ = &(view_->vector); |
4141 |
01 Feb 22 |
peter |
102 |
} |
4141 |
01 Feb 22 |
peter |
103 |
|
4141 |
01 Feb 22 |
peter |
104 |
|
1015 |
01 Feb 08 |
peter |
105 |
VectorView::~VectorView(void) |
42 |
26 Feb 04 |
jari |
106 |
{ |
1008 |
01 Feb 08 |
peter |
107 |
delete_allocated_memory(); |
42 |
26 Feb 04 |
jari |
108 |
} |
22 |
05 Aug 03 |
peter |
109 |
|
42 |
26 Feb 04 |
jari |
110 |
|
3605 |
27 Jan 17 |
peter |
111 |
const VectorView& VectorView::assign(const gsl_vector* other) |
42 |
26 Feb 04 |
jari |
112 |
{ |
3605 |
27 Jan 17 |
peter |
113 |
if (other == NULL) { |
3605 |
27 Jan 17 |
peter |
114 |
if (size()) |
3605 |
27 Jan 17 |
peter |
115 |
throw utility::GSL_error("VectorView::assign dimension mis-match"); |
3605 |
27 Jan 17 |
peter |
116 |
return *this; |
3605 |
27 Jan 17 |
peter |
117 |
} |
3605 |
27 Jan 17 |
peter |
118 |
|
3605 |
27 Jan 17 |
peter |
119 |
if (size() != other->size) |
1130 |
23 Feb 08 |
peter |
120 |
throw utility::GSL_error("VectorView::assign dimension mis-match"); |
3623 |
09 Feb 17 |
peter |
121 |
|
3623 |
09 Feb 17 |
peter |
// indirect assignment |
3623 |
09 Feb 17 |
peter |
123 |
if (detail::serial_overlap(const_vec_, other)) { |
3623 |
09 Feb 17 |
peter |
124 |
gsl_vector* tmp = detail::create_gsl_vector_copy(other); |
3623 |
09 Feb 17 |
peter |
125 |
if (gsl_vector_memcpy(vec_, tmp)) { |
3623 |
09 Feb 17 |
peter |
126 |
gsl_vector_free(tmp); |
3623 |
09 Feb 17 |
peter |
127 |
throw utility::GSL_error("VectorView::assign memcpy failed."); |
3623 |
09 Feb 17 |
peter |
128 |
} |
3623 |
09 Feb 17 |
peter |
129 |
gsl_vector_free(tmp); |
3623 |
09 Feb 17 |
peter |
130 |
} |
3623 |
09 Feb 17 |
peter |
131 |
else if (gsl_vector_memcpy(vec_, other)) |
1130 |
23 Feb 08 |
peter |
132 |
throw utility::GSL_error("VectorView::assign memcpy failed."); |
1008 |
01 Feb 08 |
peter |
133 |
const_vec_ = vec_; |
995 |
30 Nov 07 |
peter |
134 |
return *this; |
2687 |
27 Feb 12 |
peter |
135 |
} |
12 |
19 Jun 03 |
daniel |
136 |
|
3605 |
27 Jan 17 |
peter |
137 |
const VectorView& VectorView::assign(const VectorBase& other ) |
3605 |
27 Jan 17 |
peter |
138 |
{ |
3605 |
27 Jan 17 |
peter |
139 |
assign(other.gsl_vector_p()); |
3605 |
27 Jan 17 |
peter |
140 |
return *this; |
3605 |
27 Jan 17 |
peter |
141 |
} |
12 |
19 Jun 03 |
daniel |
142 |
|
3605 |
27 Jan 17 |
peter |
143 |
|
1118 |
21 Feb 08 |
peter |
144 |
void VectorView::copy(gsl_vector* other ) |
1118 |
21 Feb 08 |
peter |
145 |
{ |
3104 |
02 Nov 13 |
peter |
146 |
if (!other) // nothing to do |
3104 |
02 Nov 13 |
peter |
147 |
return; |
1118 |
21 Feb 08 |
peter |
148 |
view_ = new gsl_vector_view(gsl_vector_subvector(other,0,other->size)); |
1118 |
21 Feb 08 |
peter |
149 |
const_vec_ = vec_ = &(view_->vector); |
1118 |
21 Feb 08 |
peter |
150 |
} |
1118 |
21 Feb 08 |
peter |
151 |
|
1118 |
21 Feb 08 |
peter |
152 |
|
1015 |
01 Feb 08 |
peter |
153 |
bool VectorView::isview(void) const |
880 |
21 Sep 07 |
peter |
154 |
{ |
1008 |
01 Feb 08 |
peter |
155 |
return true; |
1008 |
01 Feb 08 |
peter |
156 |
} |
1008 |
01 Feb 08 |
peter |
157 |
|
1008 |
01 Feb 08 |
peter |
158 |
|
1026 |
01 Feb 08 |
peter |
159 |
const VectorView& VectorView::operator=(const VectorView& other ) |
1008 |
01 Feb 08 |
peter |
160 |
{ |
1008 |
01 Feb 08 |
peter |
161 |
return assign(other); |
1008 |
01 Feb 08 |
peter |
162 |
} |
1008 |
01 Feb 08 |
peter |
163 |
|
2687 |
27 Feb 12 |
peter |
164 |
|
1026 |
01 Feb 08 |
peter |
165 |
const VectorView& VectorView::operator=(const VectorBase& other ) |
1008 |
01 Feb 08 |
peter |
166 |
{ |
1008 |
01 Feb 08 |
peter |
167 |
return assign(other); |
1008 |
01 Feb 08 |
peter |
168 |
} |
1008 |
01 Feb 08 |
peter |
169 |
|
2687 |
27 Feb 12 |
peter |
170 |
|
1015 |
01 Feb 08 |
peter |
171 |
void VectorView::delete_allocated_memory(void) |
791 |
11 Mar 07 |
jari |
172 |
{ |
1008 |
01 Feb 08 |
peter |
173 |
if (view_){ |
810 |
15 Mar 07 |
jari |
174 |
delete view_; |
1008 |
01 Feb 08 |
peter |
175 |
view_=NULL; |
1008 |
01 Feb 08 |
peter |
176 |
} |
1008 |
01 Feb 08 |
peter |
177 |
const_vec_ = vec_ = NULL; |
810 |
15 Mar 07 |
jari |
178 |
} |
810 |
15 Mar 07 |
jari |
179 |
|
810 |
15 Mar 07 |
jari |
180 |
|
1651 |
15 Dec 08 |
peter |
181 |
VectorView::operator proxy() |
1651 |
15 Dec 08 |
peter |
182 |
{ |
1651 |
15 Dec 08 |
peter |
183 |
assert(vec_==const_vec_); |
1651 |
15 Dec 08 |
peter |
184 |
proxy p; |
1651 |
15 Dec 08 |
peter |
185 |
p.vec_ = vec_; |
1651 |
15 Dec 08 |
peter |
186 |
vec_ = NULL; // proxy takes ownership and delivers to its receiver |
1651 |
15 Dec 08 |
peter |
187 |
const_vec_ = NULL; |
1651 |
15 Dec 08 |
peter |
188 |
return p; |
1651 |
15 Dec 08 |
peter |
189 |
} |
1651 |
15 Dec 08 |
peter |
190 |
|
1651 |
15 Dec 08 |
peter |
191 |
|
686 |
16 Oct 06 |
jari |
192 |
}}} // of namespace utility, yat, and thep |