source: mmcs/armadillo_bits/glue_join_meat.hpp @ 8daa049

matrices
Last change on this file since 8daa049 was 9dd61b1, checked in by rboet <rboet@…>, 9 years ago

Avance del proyecto 60%

  • Property mode set to 100644
File size: 3.6 KB
Line 
1// Copyright (C) 2010-2013 Conrad Sanderson
2// Copyright (C) 2010-2013 NICTA (www.nicta.com.au)
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9//! \addtogroup glue_join
10//! @{
11
12
13
14template<typename T1, typename T2>
15inline
16void
17glue_join::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_join>& X)
18  {
19  arma_extra_debug_sigprint();
20 
21  typedef typename T1::elem_type eT;
22 
23  const uword join_type = X.aux_uword;
24 
25  const unwrap<T1> A_tmp(X.A);
26  const unwrap<T2> B_tmp(X.B);
27 
28  const Mat<eT>& A = A_tmp.M;
29  const Mat<eT>& B = B_tmp.M;
30 
31  if( (&out != &A) && (&out != &B) )
32    {
33    glue_join::apply_noalias(out, A, B, join_type);
34    }
35  else
36    {
37    Mat<eT> tmp;
38   
39    glue_join::apply_noalias(tmp, A, B, join_type);
40   
41    out.steal_mem(tmp);
42    }
43  }
44
45
46
47template<typename eT>
48inline
49void
50glue_join::apply_noalias(Mat<eT>& out, const Mat<eT>& A, const Mat<eT>& B, const uword join_type)
51  {
52  arma_extra_debug_sigprint();
53 
54  const uword A_n_rows = A.n_rows;
55  const uword A_n_cols = A.n_cols;
56 
57  const uword B_n_rows = B.n_rows;
58  const uword B_n_cols = B.n_cols;
59 
60  if(join_type == 0)
61    {
62    arma_debug_check
63      (
64      ( (A_n_cols != B_n_cols) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
65      "join_cols() / join_vert(): number of columns must be the same"
66      );
67    }
68  else
69    {
70    arma_debug_check
71      (
72      ( (A_n_rows != B.n_rows) && ( (A_n_rows > 0) || (A_n_cols > 0) ) && ( (B_n_rows > 0) || (B_n_cols > 0) ) ),
73      "join_rows() / join_horiz(): number of rows must be the same"
74      );
75    }
76 
77 
78  if(join_type == 0)   // join columns (i.e. result matrix has more rows)
79    {
80    out.set_size( A_n_rows + B_n_rows, (std::max)(A_n_cols, B_n_cols) );
81   
82    if( out.n_elem > 0 )
83      {
84      if(A.is_empty() == false)
85        { 
86        out.submat(0,        0,   A_n_rows-1, out.n_cols-1) = A;
87        }
88     
89      if(B.is_empty() == false)
90        {
91        out.submat(A_n_rows, 0, out.n_rows-1, out.n_cols-1) = B;
92        }
93      }
94    }
95  else   // join rows  (i.e. result matrix has more columns)
96    {
97    out.set_size( (std::max)(A_n_rows, B_n_rows), A_n_cols + B_n_cols );
98   
99    if( out.n_elem > 0 )
100      {
101      if(A.is_empty() == false)
102        {
103        out.submat(0, 0,        out.n_rows-1,   A.n_cols-1) = A;
104        }
105     
106      if(B.is_empty() == false)
107        {
108        out.submat(0, A_n_cols, out.n_rows-1, out.n_cols-1) = B;
109        }
110      }
111    }
112  }
113 
114 
115 
116 
117template<typename T1, typename T2>
118inline
119void
120glue_join::apply(Cube<typename T1::elem_type>& out, const GlueCube<T1,T2,glue_join>& X)
121  {
122  arma_extra_debug_sigprint();
123 
124  typedef typename T1::elem_type eT;
125
126  const unwrap_cube<T1> A_tmp(X.A);
127  const unwrap_cube<T2> B_tmp(X.B);
128 
129  const Cube<eT>& A = A_tmp.M;
130  const Cube<eT>& B = B_tmp.M;
131 
132  if(A.n_elem == 0)
133    {
134    out = B;
135    return;
136    }
137 
138  if(B.n_elem == 0)
139    {
140    out = A;
141    return;
142    }
143 
144 
145  arma_debug_check( ( (A.n_rows != B.n_rows) || (A.n_cols != B.n_cols) ), "join_slices(): size of slices must be the same" );
146 
147 
148  if( (&out != &A) && (&out != &B) )
149    {
150    out.set_size(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
151   
152    out.slices(0,          A.n_slices-1  ) = A;
153    out.slices(A.n_slices, out.n_slices-1) = B;
154    }
155  else  // we have aliasing
156    {
157    Cube<eT> C(A.n_rows, A.n_cols, A.n_slices + B.n_slices);
158   
159    C.slices(0,          A.n_slices-1) = A;
160    C.slices(A.n_slices, C.n_slices-1) = B;
161   
162    out.steal_mem(C);
163    }
164 
165  }
166
167
168
169//! @}
Note: See TracBrowser for help on using the repository browser.