1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup glue_join
//! @{
template<typename T1, typename T2>
inline
void
glue_join_cols::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& A, const Proxy<T2>& B)
{
arma_extra_debug_sigprint();
const uword A_n_rows = A.get_n_rows();
const uword A_n_cols = A.get_n_cols();
const uword B_n_rows = B.get_n_rows();
const uword B_n_cols = B.get_n_cols();
arma_debug_check
(
( (A_n_cols != B_n_cols) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
"join_cols() / join_vert(): number of columns must be the same"
);
out.set_size( A_n_rows + B_n_rows, (std::max)(A_n_cols, B_n_cols) );
if( out.n_elem > 0 )
{
if(A.get_n_elem() > 0)
{
out.submat(0, 0, A_n_rows-1, out.n_cols-1) = A.Q;
}
if(B.get_n_elem() > 0)
{
out.submat(A_n_rows, 0, out.n_rows-1, out.n_cols-1) = B.Q;
}
}
}
template<typename T1, typename T2>
inline
void
glue_join_cols::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_join_cols>& X)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const Proxy<T1> A(X.A);
const Proxy<T2> B(X.B);
if( (A.is_alias(out) == false) && (B.is_alias(out) == false) )
{
glue_join_cols::apply_noalias(out, A, B);
}
else
{
Mat<eT> tmp;
glue_join_cols::apply_noalias(tmp, A, B);
out.steal_mem(tmp);
}
}
template<typename eT, typename T1, typename T2, typename T3>
inline
void
glue_join_cols::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr)
{
arma_extra_debug_sigprint();
const quasi_unwrap<T1> UA(A_expr.get_ref());
const quasi_unwrap<T2> UB(B_expr.get_ref());
const quasi_unwrap<T3> UC(C_expr.get_ref());
const Mat<eT>& A = UA.M;
const Mat<eT>& B = UB.M;
const Mat<eT>& C = UC.M;
const uword out_n_rows = A.n_rows + B.n_rows + C.n_rows;
const uword out_n_cols = (std::max)((std::max)(A.n_cols, B.n_cols), C.n_cols);
arma_debug_check( ((A.n_cols != out_n_cols) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
arma_debug_check( ((B.n_cols != out_n_cols) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
arma_debug_check( ((C.n_cols != out_n_cols) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
out.set_size(out_n_rows, out_n_cols);
if(out.n_elem == 0) { return; }
uword row_start = 0;
uword row_end_p1 = 0;
if(A.n_elem > 0) { row_end_p1 += A.n_rows; out.rows(row_start, row_end_p1 - 1) = A; }
row_start = row_end_p1;
if(B.n_elem > 0) { row_end_p1 += B.n_rows; out.rows(row_start, row_end_p1 - 1) = B; }
row_start = row_end_p1;
if(C.n_elem > 0) { row_end_p1 += C.n_rows; out.rows(row_start, row_end_p1 - 1) = C; }
}
template<typename eT, typename T1, typename T2, typename T3, typename T4>
inline
void
glue_join_cols::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr, const Base<eT,T4>& D_expr)
{
arma_extra_debug_sigprint();
const quasi_unwrap<T1> UA(A_expr.get_ref());
const quasi_unwrap<T2> UB(B_expr.get_ref());
const quasi_unwrap<T3> UC(C_expr.get_ref());
const quasi_unwrap<T4> UD(D_expr.get_ref());
const Mat<eT>& A = UA.M;
const Mat<eT>& B = UB.M;
const Mat<eT>& C = UC.M;
const Mat<eT>& D = UD.M;
const uword out_n_rows = A.n_rows + B.n_rows + C.n_rows + D.n_rows;
const uword out_n_cols = (std::max)(((std::max)((std::max)(A.n_cols, B.n_cols), C.n_cols)), D.n_cols);
arma_debug_check( ((A.n_cols != out_n_cols) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
arma_debug_check( ((B.n_cols != out_n_cols) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
arma_debug_check( ((C.n_cols != out_n_cols) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
arma_debug_check( ((D.n_cols != out_n_cols) && ((D.n_rows > 0) || (D.n_cols > 0))), "join_cols() / join_vert(): number of columns must be the same" );
out.set_size(out_n_rows, out_n_cols);
if(out.n_elem == 0) { return; }
uword row_start = 0;
uword row_end_p1 = 0;
if(A.n_elem > 0) { row_end_p1 += A.n_rows; out.rows(row_start, row_end_p1 - 1) = A; }
row_start = row_end_p1;
if(B.n_elem > 0) { row_end_p1 += B.n_rows; out.rows(row_start, row_end_p1 - 1) = B; }
row_start = row_end_p1;
if(C.n_elem > 0) { row_end_p1 += C.n_rows; out.rows(row_start, row_end_p1 - 1) = C; }
row_start = row_end_p1;
if(D.n_elem > 0) { row_end_p1 += D.n_rows; out.rows(row_start, row_end_p1 - 1) = D; }
}
template<typename T1, typename T2>
inline
void
glue_join_rows::apply_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& A, const Proxy<T2>& B)
{
arma_extra_debug_sigprint();
const uword A_n_rows = A.get_n_rows();
const uword A_n_cols = A.get_n_cols();
const uword B_n_rows = B.get_n_rows();
const uword B_n_cols = B.get_n_cols();
arma_debug_check
(
( (A_n_rows != B_n_rows) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
"join_rows() / join_horiz(): number of rows must be the same"
);
out.set_size( (std::max)(A_n_rows, B_n_rows), A_n_cols + B_n_cols );
if( out.n_elem > 0 )
{
if(A.get_n_elem() > 0)
{
out.submat(0, 0, out.n_rows-1, A_n_cols-1) = A.Q;
}
if(B.get_n_elem() > 0)
{
out.submat(0, A_n_cols, out.n_rows-1, out.n_cols-1) = B.Q;
}
}
}
template<typename T1, typename T2>
inline
void
glue_join_rows::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_join_rows>& X)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const Proxy<T1> A(X.A);
const Proxy<T2> B(X.B);
if( (A.is_alias(out) == false) && (B.is_alias(out) == false) )
{
glue_join_rows::apply_noalias(out, A, B);
}
else
{
Mat<eT> tmp;
glue_join_rows::apply_noalias(tmp, A, B);
out.steal_mem(tmp);
}
}
template<typename eT, typename T1, typename T2, typename T3>
inline
void
glue_join_rows::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr)
{
arma_extra_debug_sigprint();
const quasi_unwrap<T1> UA(A_expr.get_ref());
const quasi_unwrap<T2> UB(B_expr.get_ref());
const quasi_unwrap<T3> UC(C_expr.get_ref());
const Mat<eT>& A = UA.M;
const Mat<eT>& B = UB.M;
const Mat<eT>& C = UC.M;
const uword out_n_rows = (std::max)((std::max)(A.n_rows, B.n_rows), C.n_rows);
const uword out_n_cols = A.n_cols + B.n_cols + C.n_cols;
arma_debug_check( ((A.n_rows != out_n_rows) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
arma_debug_check( ((B.n_rows != out_n_rows) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
arma_debug_check( ((C.n_rows != out_n_rows) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
out.set_size(out_n_rows, out_n_cols);
if(out.n_elem == 0) { return; }
uword col_start = 0;
uword col_end_p1 = 0;
if(A.n_elem > 0) { col_end_p1 += A.n_cols; out.cols(col_start, col_end_p1 - 1) = A; }
col_start = col_end_p1;
if(B.n_elem > 0) { col_end_p1 += B.n_cols; out.cols(col_start, col_end_p1 - 1) = B; }
col_start = col_end_p1;
if(C.n_elem > 0) { col_end_p1 += C.n_cols; out.cols(col_start, col_end_p1 - 1) = C; }
}
template<typename eT, typename T1, typename T2, typename T3, typename T4>
inline
void
glue_join_rows::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const Base<eT,T3>& C_expr, const Base<eT,T4>& D_expr)
{
arma_extra_debug_sigprint();
const quasi_unwrap<T1> UA(A_expr.get_ref());
const quasi_unwrap<T2> UB(B_expr.get_ref());
const quasi_unwrap<T3> UC(C_expr.get_ref());
const quasi_unwrap<T4> UD(D_expr.get_ref());
const Mat<eT>& A = UA.M;
const Mat<eT>& B = UB.M;
const Mat<eT>& C = UC.M;
const Mat<eT>& D = UD.M;
const uword out_n_rows = (std::max)(((std::max)((std::max)(A.n_rows, B.n_rows), C.n_rows)), D.n_rows);
const uword out_n_cols = A.n_cols + B.n_cols + C.n_cols + D.n_cols;
arma_debug_check( ((A.n_rows != out_n_rows) && ((A.n_rows > 0) || (A.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
arma_debug_check( ((B.n_rows != out_n_rows) && ((B.n_rows > 0) || (B.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
arma_debug_check( ((C.n_rows != out_n_rows) && ((C.n_rows > 0) || (C.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
arma_debug_check( ((D.n_rows != out_n_rows) && ((D.n_rows > 0) || (D.n_cols > 0))), "join_rows() / join_horiz(): number of rows must be the same" );
out.set_size(out_n_rows, out_n_cols);
if(out.n_elem == 0) { return; }
uword col_start = 0;
uword col_end_p1 = 0;
if(A.n_elem > 0) { col_end_p1 += A.n_cols; out.cols(col_start, col_end_p1 - 1) = A; }
col_start = col_end_p1;
if(B.n_elem > 0) { col_end_p1 += B.n_cols; out.cols(col_start, col_end_p1 - 1) = B; }
col_start = col_end_p1;
if(C.n_elem > 0) { col_end_p1 += C.n_cols; out.cols(col_start, col_end_p1 - 1) = C; }
col_start = col_end_p1;
if(D.n_elem > 0) { col_end_p1 += D.n_cols; out.cols(col_start, col_end_p1 - 1) = D; }
}
template<typename T1, typename T2>
inline
void
glue_join_slices::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1,T2,glue_join_slices>& X)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_cube<T1> A_tmp(X.A);
const unwrap_cube<T2> B_tmp(X.B);
const Cube<eT>& A = A_tmp.M;
const Cube<eT>& B = B_tmp.M;
if(A.n_elem == 0) { out = B; return; }
if(B.n_elem == 0) { out = A; return; }
arma_debug_check( ( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) ), "join_slices(): size of slices must be the same" );
if( (&out != &A) && (&out != &B) )
{
out.set_size(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
out.slices(0, A.n_slices-1 ) = A;
out.slices(A.n_slices, out.n_slices-1) = B;
}
else // we have aliasing
{
Cube<eT> C(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
C.slices(0, A.n_slices-1) = A;
C.slices(A.n_slices, C.n_slices-1) = B;
out.steal_mem(C);
}
}
//! @}
|