// SPDX-License-Identifier: Apache-2.0 // // 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 op_vectorise //! @{ template inline void op_vectorise_col::apply(Mat& out, const Op& in) { arma_debug_sigprint(); op_vectorise_col::apply_direct(out, in.m); } template inline void op_vectorise_col::apply_direct(Mat& out, const T1& expr) { arma_debug_sigprint(); typedef typename T1::elem_type eT; // allow detection of in-place operation if(is_Mat::value || (arma_config::openmp && Proxy::use_mp)) { const unwrap U(expr); if(&out == &(U.M)) { // output matrix is the same as the input matrix out.set_size(out.n_elem, 1); // set_size() doesn't destroy data as long as the number of elements in the matrix remains the same } else { out.set_size(U.M.n_elem, 1); arrayops::copy(out.memptr(), U.M.memptr(), U.M.n_elem); } } else if(is_subview::value) { const subview& sv = reinterpret_cast< const subview& >(expr); if(&out == &(sv.m)) { Mat tmp; op_vectorise_col::apply_subview(tmp, sv); out.steal_mem(tmp); } else { op_vectorise_col::apply_subview(out, sv); } } else { const Proxy P(expr); const bool is_alias = P.is_alias(out); if(is_Mat::stored_type>::value) { const quasi_unwrap::stored_type> U(P.Q); if(is_alias) { Mat tmp(U.M.memptr(), U.M.n_elem, 1); out.steal_mem(tmp); } else { out.set_size(U.M.n_elem, 1); arrayops::copy(out.memptr(), U.M.memptr(), U.M.n_elem); } } else { if(is_alias) { Mat tmp; op_vectorise_col::apply_proxy(tmp, P); out.steal_mem(tmp); } else { op_vectorise_col::apply_proxy(out, P); } } } } template inline void op_vectorise_col::apply_subview(Mat& out, const subview& sv) { arma_debug_sigprint(); const uword sv_n_rows = sv.n_rows; const uword sv_n_cols = sv.n_cols; out.set_size(sv.n_elem, 1); eT* out_mem = out.memptr(); for(uword col=0; col < sv_n_cols; ++col) { arrayops::copy(out_mem, sv.colptr(col), sv_n_rows); out_mem += sv_n_rows; } } template inline void op_vectorise_col::apply_proxy(Mat& out, const Proxy& P) { arma_debug_sigprint(); typedef typename T1::elem_type eT; const uword N = P.get_n_elem(); out.set_size(N, 1); eT* outmem = out.memptr(); if(Proxy::use_at == false) { // TODO: add handling of aligned access ? typename Proxy::ea_type A = P.get_ea(); uword i,j; for(i=0, j=1; j < N; i+=2, j+=2) { const eT tmp_i = A[i]; const eT tmp_j = A[j]; outmem[i] = tmp_i; outmem[j] = tmp_j; } if(i < N) { outmem[i] = A[i]; } } else { const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); if(n_rows == 1) { for(uword i=0; i < n_cols; ++i) { outmem[i] = P.at(0,i); } } else { for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { *outmem = P.at(row,col); outmem++; } } } } template inline void op_vectorise_row::apply(Mat& out, const Op& in) { arma_debug_sigprint(); op_vectorise_row::apply_direct(out, in.m); } template inline void op_vectorise_row::apply_direct(Mat& out, const T1& expr) { arma_debug_sigprint(); typedef typename T1::elem_type eT; const Proxy P(expr); if(P.is_alias(out)) { Mat tmp; op_vectorise_row::apply_proxy(tmp, P); out.steal_mem(tmp); } else { op_vectorise_row::apply_proxy(out, P); } } template inline void op_vectorise_row::apply_proxy(Mat& out, const Proxy& P) { arma_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); const uword n_elem = P.get_n_elem(); out.set_size(1, n_elem); eT* outmem = out.memptr(); if(n_cols == 1) { if(is_Mat::stored_type>::value) { const unwrap::stored_type> tmp(P.Q); arrayops::copy(out.memptr(), tmp.M.memptr(), n_elem); } else { for(uword i=0; i < n_elem; ++i) { outmem[i] = P.at(i,0); } } } else { for(uword row=0; row < n_rows; ++row) { uword i,j; for(i=0, j=1; j < n_cols; i+=2, j+=2) { const eT tmp_i = P.at(row,i); const eT tmp_j = P.at(row,j); *outmem = tmp_i; outmem++; *outmem = tmp_j; outmem++; } if(i < n_cols) { *outmem = P.at(row,i); outmem++; } } } } template inline void op_vectorise_all::apply(Mat& out, const Op& in) { arma_debug_sigprint(); const uword dim = in.aux_uword_a; if(dim == 0) { op_vectorise_col::apply_direct(out, in.m); } else { op_vectorise_row::apply_direct(out, in.m); } } // template inline void op_vectorise_cube_col::apply(Mat& out, const CubeToMatOp& in) { arma_debug_sigprint(); typedef typename T1::elem_type eT; if(is_same_type< T1, subview_cube >::yes) { op_vectorise_cube_col::apply_subview(out, reinterpret_cast< const subview_cube& >(in.m)); } else { if(is_Cube::value || (arma_config::openmp && ProxyCube::use_mp)) { op_vectorise_cube_col::apply_unwrap(out, in.m); } else { op_vectorise_cube_col::apply_proxy(out, in.m); } } } template inline void op_vectorise_cube_col::apply_subview(Mat& out, const subview_cube& sv) { arma_debug_sigprint(); const uword sv_nr = sv.n_rows; const uword sv_nc = sv.n_cols; const uword sv_ns = sv.n_slices; out.set_size(sv.n_elem, 1); eT* out_mem = out.memptr(); for(uword s=0; s < sv_ns; ++s) for(uword c=0; c < sv_nc; ++c) { arrayops::copy(out_mem, sv.slice_colptr(s,c), sv_nr); out_mem += sv_nr; } } template inline void op_vectorise_cube_col::apply_unwrap(Mat& out, const T1& expr) { arma_debug_sigprint(); const unwrap_cube U(expr); out.set_size(U.M.n_elem, 1); arrayops::copy(out.memptr(), U.M.memptr(), U.M.n_elem); } template inline void op_vectorise_cube_col::apply_proxy(Mat& out, const T1& expr) { arma_debug_sigprint(); typedef typename T1::elem_type eT; const ProxyCube P(expr); if(is_Cube::stored_type>::value) { op_vectorise_cube_col::apply_unwrap(out, P.Q); return; } const uword N = P.get_n_elem(); out.set_size(N, 1); eT* outmem = out.memptr(); if(ProxyCube::use_at == false) { typename ProxyCube::ea_type A = P.get_ea(); uword i,j; for(i=0, j=1; j < N; i+=2, j+=2) { const eT tmp_i = A[i]; const eT tmp_j = A[j]; outmem[i] = tmp_i; outmem[j] = tmp_j; } if(i < N) { outmem[i] = A[i]; } } else { const uword nr = P.get_n_rows(); const uword nc = P.get_n_cols(); const uword ns = P.get_n_slices(); for(uword s=0; s < ns; ++s) for(uword c=0; c < nc; ++c) for(uword r=0; r < nr; ++r) { *outmem = P.at(r,c,s); outmem++; } } } //! @}