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