comparison armadillo-2.4.4/include/armadillo_bits/glue_join_meat.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 glue_join
15 //! @{
16
17
18
19 template<typename T1, typename T2>
20 inline
21 void
22 glue_join::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_join>& X)
23 {
24 arma_extra_debug_sigprint();
25
26 typedef typename T1::elem_type eT;
27
28 const unwrap<T1> A_tmp(X.A);
29 const unwrap<T2> B_tmp(X.B);
30
31 const Mat<eT>& A = A_tmp.M;
32 const Mat<eT>& B = B_tmp.M;
33
34 const uword A_n_rows = A.n_rows;
35 const uword A_n_cols = A.n_cols;
36
37 const uword B_n_rows = B.n_rows;
38 const uword B_n_cols = B.n_cols;
39
40 const uword join_type = X.aux_uword;
41
42 if(join_type == 0)
43 {
44 arma_debug_check
45 (
46 ( (A_n_cols != B_n_cols) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
47 "join_cols(): number of columns must be the same"
48 );
49 }
50 else
51 {
52 arma_debug_check
53 (
54 ( (A_n_rows != B.n_rows) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
55 "join_rows(): number of rows must be the same"
56 );
57 }
58
59
60 if( (&out != &A) && (&out != &B) )
61 {
62 if(join_type == 0) // join columns (i.e. result matrix has more rows)
63 {
64 out.set_size( A_n_rows + B_n_rows, (std::max)(A_n_cols, B_n_cols) );
65
66 if( out.n_elem > 0 )
67 {
68 if(A.is_empty() == false)
69 {
70 out.submat(0, 0, A_n_rows-1, out.n_cols-1) = A;
71 }
72
73 if(B.is_empty() == false)
74 {
75 out.submat(A_n_rows, 0, out.n_rows-1, out.n_cols-1) = B;
76 }
77 }
78 }
79 else // join rows (i.e. result matrix has more columns)
80 {
81 out.set_size( (std::max)(A_n_rows, B_n_rows), A_n_cols + B_n_cols );
82
83 if( out.n_elem > 0 )
84 {
85 if(A.is_empty() == false)
86 {
87 out.submat(0, 0, out.n_rows-1, A.n_cols-1) = A;
88 }
89
90 if(B.is_empty() == false)
91 {
92 out.submat(0, A_n_cols, out.n_rows-1, out.n_cols-1) = B;
93 }
94 }
95 }
96 }
97 else // we have aliasing
98 {
99 Mat<eT> C;
100
101 if(join_type == 0)
102 {
103 C.set_size( A_n_rows + B_n_rows, (std::max)(A_n_cols, B_n_cols) );
104
105 if( C.n_elem > 0 )
106 {
107 if(A.is_empty() == false)
108 {
109 C.submat(0, 0, A_n_rows-1, C.n_cols-1) = A;
110 }
111
112 if(B.is_empty() == false)
113 {
114 C.submat(A_n_rows, 0, C.n_rows-1, C.n_cols-1) = B;
115 }
116 }
117 }
118 else
119 {
120 C.set_size( (std::max)(A_n_rows, B_n_rows), A_n_cols + B_n_cols );
121
122 if( C.n_elem > 0 )
123 {
124 if(A.is_empty() == false)
125 {
126 C.submat(0, 0, C.n_rows-1, A_n_cols-1) = A;
127 }
128
129 if(B.is_empty() == false)
130 {
131 C.submat(0, A_n_cols, C.n_rows-1, C.n_cols-1) = B;
132 }
133 }
134 }
135
136 out.steal_mem(C);
137 }
138
139 }
140
141
142
143 template<typename T1, typename T2>
144 inline
145 void
146 glue_join::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1,T2,glue_join>& X)
147 {
148 arma_extra_debug_sigprint();
149
150 typedef typename T1::elem_type eT;
151
152 const unwrap_cube<T1> A_tmp(X.A);
153 const unwrap_cube<T2> B_tmp(X.B);
154
155 const Cube<eT>& A = A_tmp.M;
156 const Cube<eT>& B = B_tmp.M;
157
158 if(A.n_elem == 0)
159 {
160 out = B;
161 return;
162 }
163
164 if(B.n_elem == 0)
165 {
166 out = A;
167 return;
168 }
169
170
171 arma_debug_check( ( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) ), "join_slices(): size of slices must be the same" );
172
173
174 if( (&out != &A) && (&out != &B) )
175 {
176 out.set_size(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
177
178 out.slices(0, A.n_slices-1 ) = A;
179 out.slices(A.n_slices, out.n_slices-1) = B;
180 }
181 else // we have aliasing
182 {
183 Cube<eT> C(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
184
185 C.slices(0, A.n_slices-1) = A;
186 C.slices(A.n_slices, C.n_slices-1) = B;
187
188 out.steal_mem(C);
189 }
190
191 }
192
193
194
195 //! @}