comparison armadillo-2.4.4/include/armadillo_bits/diagmat_proxy.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup diagmat_proxy
15 //! @{
16
17
18
19 template<typename T1>
20 class diagmat_proxy
21 {
22 public:
23
24 typedef typename T1::elem_type elem_type;
25 typedef typename get_pod_type<elem_type>::result pod_type;
26
27 inline diagmat_proxy(const Base<typename T1::elem_type,T1>& X)
28 : P ( X.get_ref() )
29 , P_is_vec( (P.get_n_rows() == 1) || (P.get_n_cols() == 1) )
30 , n_elem ( P_is_vec ? P.get_n_elem() : (std::min)(P.get_n_elem(), P.get_n_rows()) )
31 {
32 arma_extra_debug_sigprint();
33
34 arma_debug_check
35 (
36 (P_is_vec == false) && (P.get_n_rows() != P.get_n_cols()),
37 "diagmat(): only vectors and square matrices are accepted"
38 );
39 }
40
41
42 arma_inline
43 elem_type
44 operator[](const uword i) const
45 {
46 if( (Proxy<T1>::prefer_at_accessor == true) || (P_is_vec == false) )
47 {
48 return P.at(i,i);
49 }
50 else
51 {
52 return P[i];
53 }
54 }
55
56
57 arma_inline
58 elem_type
59 at(const uword row, const uword col) const
60 {
61 if(row == col)
62 {
63 if( (Proxy<T1>::prefer_at_accessor == true) || (P_is_vec == false) )
64 {
65 return P.at(row,row);
66 }
67 else
68 {
69 return P[row];
70 }
71 }
72 else
73 {
74 return elem_type(0);
75 }
76 }
77
78
79 const Proxy<T1> P;
80 const bool P_is_vec;
81 const uword n_elem;
82 };
83
84
85
86 template<typename eT>
87 class diagmat_proxy< Mat<eT> >
88 {
89 public:
90
91 typedef eT elem_type;
92 typedef typename get_pod_type<elem_type>::result pod_type;
93
94
95 inline diagmat_proxy(const Mat<eT>& X)
96 : P(X)
97 , P_is_vec( (P.n_rows == 1) || (P.n_cols == 1) )
98 , n_elem( P_is_vec ? P.n_elem : (std::min)(P.n_elem, P.n_rows) )
99 {
100 arma_extra_debug_sigprint();
101
102 arma_debug_check
103 (
104 (P_is_vec == false) && (P.n_rows != P.n_cols),
105 "diagmat(): only vectors and square matrices are accepted"
106 );
107 }
108
109
110 arma_inline elem_type operator[] (const uword i) const { return P_is_vec ? P[i] : P.at(i,i); }
111 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? ( P_is_vec ? P[row] : P.at(row,row) ) : elem_type(0); }
112
113 const Mat<eT>& P;
114 const bool P_is_vec;
115 const uword n_elem;
116 };
117
118
119
120 template<typename eT>
121 class diagmat_proxy< Row<eT> >
122 {
123 public:
124
125 typedef eT elem_type;
126 typedef typename get_pod_type<elem_type>::result pod_type;
127
128
129 inline diagmat_proxy(const Row<eT>& X)
130 : P(X)
131 , P_is_vec(true)
132 , n_elem(P.n_elem)
133 {
134 arma_extra_debug_sigprint();
135 }
136
137
138 arma_inline elem_type operator[] (const uword i) const { return P[i]; }
139 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); }
140
141
142 const Row<eT>& P;
143 const bool P_is_vec;
144 const uword n_elem;
145 };
146
147
148
149 template<typename eT>
150 class diagmat_proxy< Col<eT> >
151 {
152 public:
153
154 typedef eT elem_type;
155 typedef typename get_pod_type<elem_type>::result pod_type;
156
157
158 inline diagmat_proxy(const Col<eT>& X)
159 : P(X)
160 , P_is_vec(true)
161 , n_elem(P.n_elem)
162 {
163 arma_extra_debug_sigprint();
164 }
165
166
167 arma_inline elem_type operator[] (const uword i) const { return P[i]; }
168 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); }
169
170
171 const Col<eT>& P;
172 const bool P_is_vec;
173 const uword n_elem;
174 };
175
176
177
178 template<typename T1>
179 class diagmat_proxy_check
180 {
181 public:
182
183 typedef typename T1::elem_type elem_type;
184 typedef typename get_pod_type<elem_type>::result pod_type;
185
186 inline diagmat_proxy_check(const Base<typename T1::elem_type,T1>& X, const Mat<typename T1::elem_type>& out)
187 : P(X.get_ref())
188 , P_is_vec( (P.n_rows == 1) || (P.n_cols == 1) )
189 , n_elem( P_is_vec ? P.n_elem : (std::min)(P.n_elem, P.n_rows) )
190 {
191 arma_extra_debug_sigprint();
192 arma_ignore(out);
193
194 arma_debug_check
195 (
196 (P_is_vec == false) && (P.n_rows != P.n_cols),
197 "diagmat(): only vectors and square matrices are accepted"
198 );
199 }
200
201
202 arma_inline elem_type operator[] (const uword i) const { return P_is_vec ? P[i] : P.at(i,i); }
203 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? ( P_is_vec ? P[row] : P.at(row,row) ) : elem_type(0); }
204
205
206 const Mat<elem_type> P;
207 const bool P_is_vec;
208 const uword n_elem;
209 };
210
211
212
213 template<typename eT>
214 class diagmat_proxy_check< Mat<eT> >
215 {
216 public:
217
218 typedef eT elem_type;
219 typedef typename get_pod_type<elem_type>::result pod_type;
220
221
222 inline diagmat_proxy_check(const Mat<eT>& X, const Mat<eT>& out)
223 : P_local ( (&X == &out) ? new Mat<eT>(X) : 0 )
224 , P ( (&X == &out) ? (*P_local) : X )
225 , P_is_vec( (P.n_rows == 1) || (P.n_cols == 1) )
226 , n_elem ( P_is_vec ? P.n_elem : (std::min)(P.n_elem, P.n_rows) )
227 {
228 arma_extra_debug_sigprint();
229
230 arma_debug_check
231 (
232 (P_is_vec == false) && (P.n_rows != P.n_cols),
233 "diagmat(): only vectors and square matrices are accepted"
234 );
235 }
236
237 inline ~diagmat_proxy_check()
238 {
239 if(P_local)
240 {
241 delete P_local;
242 }
243 }
244
245
246 arma_inline elem_type operator[] (const uword i) const { return P_is_vec ? P[i] : P.at(i,i); }
247 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? ( P_is_vec ? P[row] : P.at(row,row) ) : elem_type(0); }
248
249
250 const Mat<eT>* P_local;
251 const Mat<eT>& P;
252 const bool P_is_vec;
253 const uword n_elem;
254 };
255
256
257
258 template<typename eT>
259 class diagmat_proxy_check< Row<eT> >
260 {
261 public:
262
263 typedef eT elem_type;
264 typedef typename get_pod_type<elem_type>::result pod_type;
265
266 inline diagmat_proxy_check(const Row<eT>& X, const Mat<eT>& out)
267 : P_local ( (&X == reinterpret_cast<const Row<eT>*>(&out)) ? new Row<eT>(X) : 0 )
268 , P ( (&X == reinterpret_cast<const Row<eT>*>(&out)) ? (*P_local) : X )
269 , P_is_vec(true)
270 , n_elem (P.n_elem)
271 {
272 arma_extra_debug_sigprint();
273 }
274
275
276 inline ~diagmat_proxy_check()
277 {
278 if(P_local)
279 {
280 delete P_local;
281 }
282 }
283
284
285 arma_inline elem_type operator[] (const uword i) const { return P[i]; }
286 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); }
287
288
289 const Row<eT>* P_local;
290 const Row<eT>& P;
291 const bool P_is_vec;
292 const uword n_elem;
293 };
294
295
296
297
298
299
300 template<typename eT>
301 class diagmat_proxy_check< Col<eT> >
302 {
303 public:
304
305 typedef eT elem_type;
306 typedef typename get_pod_type<elem_type>::result pod_type;
307
308 inline diagmat_proxy_check(const Col<eT>& X, const Mat<eT>& out)
309 : P_local ( (&X == reinterpret_cast<const Col<eT>*>(&out)) ? new Col<eT>(X) : 0 )
310 , P ( (&X == reinterpret_cast<const Col<eT>*>(&out)) ? (*P_local) : X )
311 , P_is_vec(true)
312 , n_elem (P.n_elem)
313 {
314 arma_extra_debug_sigprint();
315 }
316
317
318 inline ~diagmat_proxy_check()
319 {
320 if(P_local)
321 {
322 delete P_local;
323 }
324 }
325
326
327 arma_inline elem_type operator[] (const uword i) const { return P[i]; }
328 arma_inline elem_type at (const uword row, const uword col) const { return (row == col) ? P[row] : elem_type(0); }
329
330
331 const Col<eT>* P_local;
332 const Col<eT>& P;
333 const bool P_is_vec;
334 const uword n_elem;
335 };
336
337
338
339 //! @}