// 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 fn_conv_to //! @{ //! conversion from Armadillo Base and BaseCube objects to scalars //! NOTE: use as_scalar() instead; this functionality is kept only for compatibility with old user code template class conv_to { public: template inline static out_eT from(const Base& in, const typename arma_not_cx::result* junk = nullptr); template inline static out_eT from(const Base& in, const typename arma_cx_only::result* junk = nullptr); template inline static out_eT from(const BaseCube& in, const typename arma_not_cx::result* junk = nullptr); template inline static out_eT from(const BaseCube& in, const typename arma_cx_only::result* junk = nullptr); }; template template arma_warn_unused inline out_eT conv_to::from(const Base& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); arma_type_check(( is_supported_elem_type::value == false )); const Proxy P(in.get_ref()); arma_debug_check( (P.get_n_elem() != 1), "conv_to(): given object does not have exactly one element" ); return out_eT(Proxy::use_at ? P.at(0,0) : P[0]); } template template arma_warn_unused inline out_eT conv_to::from(const Base& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); arma_type_check(( is_supported_elem_type::value == false )); const Proxy P(in.get_ref()); arma_debug_check( (P.get_n_elem() != 1), "conv_to(): given object does not have exactly one element" ); out_eT out; arrayops::convert_cx_scalar(out, (Proxy::use_at ? P.at(0,0) : P[0])); return out; } template template arma_warn_unused inline out_eT conv_to::from(const BaseCube& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); arma_type_check(( is_supported_elem_type::value == false )); const ProxyCube P(in.get_ref()); arma_debug_check( (P.get_n_elem() != 1), "conv_to(): given object does not have exactly one element" ); return out_eT(ProxyCube::use_at ? P.at(0,0,0) : P[0]); } template template arma_warn_unused inline out_eT conv_to::from(const BaseCube& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); arma_type_check(( is_supported_elem_type::value == false )); const ProxyCube P(in.get_ref()); arma_debug_check( (P.get_n_elem() != 1), "conv_to(): given object does not have exactly one element" ); out_eT out; arrayops::convert_cx_scalar(out, (ProxyCube::use_at ? P.at(0,0,0) : P[0])); return out; } //! conversion to Armadillo matrices from Armadillo Base objects, as well as from std::vector template class conv_to< Mat > { public: template inline static Mat from(const Base& in, const typename arma_not_cx::result* junk = nullptr); template inline static Mat from(const Base& in, const typename arma_cx_only::result* junk = nullptr); template inline static Mat from(const SpBase& in); template inline static Mat from(const std::vector& in, const typename arma_not_cx::result* junk = nullptr); template inline static Mat from(const std::vector& in, const typename arma_cx_only::result* junk = nullptr); }; template template arma_warn_unused inline Mat conv_to< Mat >::from(const Base& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; Mat out(X.n_rows, X.n_cols, arma_nozeros_indicator()); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template arma_warn_unused inline Mat conv_to< Mat >::from(const Base& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; Mat out(X.n_rows, X.n_cols, arma_nozeros_indicator()); arrayops::convert_cx( out.memptr(), X.memptr(), X.n_elem ); return out; } template template arma_warn_unused inline Mat conv_to< Mat >::from(const SpBase& in) { arma_extra_debug_sigprint(); return Mat(in.get_ref()); } template template arma_warn_unused inline Mat conv_to< Mat >::from(const std::vector& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const uword N = uword( in.size() ); Mat out(N, 1, arma_nozeros_indicator()); if(N > 0) { arrayops::convert( out.memptr(), &(in[0]), N ); } return out; } template template arma_warn_unused inline Mat conv_to< Mat >::from(const std::vector& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const uword N = uword( in.size() ); Mat out(N, 1, arma_nozeros_indicator()); if(N > 0) { arrayops::convert_cx( out.memptr(), &(in[0]), N ); } return out; } //! conversion to Armadillo row vectors from Armadillo Base objects, as well as from std::vector template class conv_to< Row > { public: template inline static Row from(const Base& in, const typename arma_not_cx::result* junk = nullptr); template inline static Row from(const Base& in, const typename arma_cx_only::result* junk = nullptr); template inline static Row from(const std::vector& in, const typename arma_not_cx::result* junk = nullptr); template inline static Row from(const std::vector& in, const typename arma_cx_only::result* junk = nullptr); }; template template arma_warn_unused inline Row conv_to< Row >::from(const Base& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; arma_debug_check( ( (X.is_vec() == false) && (X.is_empty() == false) ), "conv_to(): given object cannot be interpreted as a vector" ); Row out(X.n_elem, arma_nozeros_indicator()); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template arma_warn_unused inline Row conv_to< Row >::from(const Base& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; arma_debug_check( ( (X.is_vec() == false) && (X.is_empty() == false) ), "conv_to(): given object cannot be interpreted as a vector" ); Row out(X.n_rows, X.n_cols, arma_nozeros_indicator()); arrayops::convert_cx( out.memptr(), X.memptr(), X.n_elem ); return out; } template template arma_warn_unused inline Row conv_to< Row >::from(const std::vector& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const uword N = uword( in.size() ); Row out(N, arma_nozeros_indicator()); if(N > 0) { arrayops::convert( out.memptr(), &(in[0]), N ); } return out; } template template arma_warn_unused inline Row conv_to< Row >::from(const std::vector& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const uword N = uword( in.size() ); Row out(N, arma_nozeros_indicator()); if(N > 0) { arrayops::convert_cx( out.memptr(), &(in[0]), N ); } return out; } //! conversion to Armadillo column vectors from Armadillo Base objects, as well as from std::vector template class conv_to< Col > { public: template inline static Col from(const Base& in, const typename arma_not_cx::result* junk = nullptr); template inline static Col from(const Base& in, const typename arma_cx_only::result* junk = nullptr); template inline static Col from(const std::vector& in, const typename arma_not_cx::result* junk = nullptr); template inline static Col from(const std::vector& in, const typename arma_cx_only::result* junk = nullptr); }; template template arma_warn_unused inline Col conv_to< Col >::from(const Base& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; arma_debug_check( ( (X.is_vec() == false) && (X.is_empty() == false) ), "conv_to(): given object cannot be interpreted as a vector" ); Col out(X.n_elem, arma_nozeros_indicator()); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template arma_warn_unused inline Col conv_to< Col >::from(const Base& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; arma_debug_check( ( (X.is_vec() == false) && (X.is_empty() == false) ), "conv_to(): given object cannot be interpreted as a vector" ); Col out(X.n_rows, X.n_cols, arma_nozeros_indicator()); arrayops::convert_cx( out.memptr(), X.memptr(), X.n_elem ); return out; } template template arma_warn_unused inline Col conv_to< Col >::from(const std::vector& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const uword N = uword( in.size() ); Col out(N, arma_nozeros_indicator()); if(N > 0) { arrayops::convert( out.memptr(), &(in[0]), N ); } return out; } template template arma_warn_unused inline Col conv_to< Col >::from(const std::vector& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const uword N = uword( in.size() ); Col out(N, arma_nozeros_indicator()); if(N > 0) { arrayops::convert_cx( out.memptr(), &(in[0]), N ); } return out; } //! convert between SpMat types template class conv_to< SpMat > { public: template inline static SpMat from(const SpBase& in, const typename arma_not_cx::result* junk = nullptr); template inline static SpMat from(const SpBase& in, const typename arma_cx_only::result* junk = nullptr); template inline static SpMat from(const Base& in); }; template template arma_warn_unused inline SpMat conv_to< SpMat >::from(const SpBase& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const unwrap_spmat tmp(in.get_ref()); const SpMat& X = tmp.M; SpMat out(arma_layout_indicator(), X); arrayops::convert( access::rwp(out.values), X.values, X.n_nonzero ); out.remove_zeros(); return out; } template template arma_warn_unused inline SpMat conv_to< SpMat >::from(const SpBase& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const unwrap_spmat tmp(in.get_ref()); const SpMat& X = tmp.M; SpMat out(arma_layout_indicator(), X); arrayops::convert_cx( access::rwp(out.values), X.values, X.n_nonzero ); out.remove_zeros(); return out; } template template arma_warn_unused inline SpMat conv_to< SpMat >::from(const Base& in) { arma_extra_debug_sigprint(); return SpMat(in.get_ref()); } //! conversion to Armadillo cubes from Armadillo BaseCube objects template class conv_to< Cube > { public: template inline static Cube from(const BaseCube& in, const typename arma_not_cx::result* junk = nullptr); template inline static Cube from(const BaseCube& in, const typename arma_cx_only::result* junk = nullptr); }; template template arma_warn_unused inline Cube conv_to< Cube >::from(const BaseCube& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const unwrap_cube tmp( in.get_ref() ); const Cube& X = tmp.M; Cube out(X.n_rows, X.n_cols, X.n_slices, arma_nozeros_indicator()); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template arma_warn_unused inline Cube conv_to< Cube >::from(const BaseCube& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const unwrap_cube tmp( in.get_ref() ); const Cube& X = tmp.M; Cube out(X.n_rows, X.n_cols, X.n_slices, arma_nozeros_indicator()); arrayops::convert_cx( out.memptr(), X.memptr(), X.n_elem ); return out; } //! conversion to std::vector from Armadillo Base objects template class conv_to< std::vector > { public: template inline static std::vector from(const Base& in, const typename arma_not_cx::result* junk = nullptr); template inline static std::vector from(const Base& in, const typename arma_cx_only::result* junk = nullptr); }; template template arma_warn_unused inline std::vector conv_to< std::vector >::from(const Base& in, const typename arma_not_cx::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; arma_debug_check( ( (X.is_vec() == false) && (X.is_empty() == false) ), "conv_to(): given object cannot be interpreted as a vector" ); const uword N = X.n_elem; std::vector out(N); if(N > 0) { arrayops::convert( &(out[0]), X.memptr(), N ); } return out; } template template arma_warn_unused inline std::vector conv_to< std::vector >::from(const Base& in, const typename arma_cx_only::result* junk) { arma_extra_debug_sigprint(); arma_ignore(junk); const quasi_unwrap tmp(in.get_ref()); const Mat& X = tmp.M; arma_debug_check( ( (X.is_vec() == false) && (X.is_empty() == false) ), "conv_to(): given object cannot be interpreted as a vector" ); const uword N = X.n_elem; std::vector out(N); if(N > 0) { arrayops::convert_cx( &(out[0]), X.memptr(), N ); } return out; } //! @}