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