// Copyright (C) 2008-2015 National ICT Australia (NICTA) // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. // ------------------------------------------------------------------- // // Written by Conrad Sanderson - http://conradsanderson.id.au //! \addtogroup fn_conv_to //! @{ //! conversion from Armadillo Base and BaseCube objects to scalars //! (kept only for compatibility with old code; use as_scalar() instead for Base objects like Mat) template class conv_to { public: template inline static out_eT from(const Base& in, const typename arma_not_cx::result* junk = 0); template inline static out_eT from(const Base& in, const typename arma_cx_only::result* junk = 0); template inline static out_eT from(const BaseCube& in, const typename arma_not_cx::result* junk = 0); template inline static out_eT from(const BaseCube& in, const typename arma_cx_only::result* junk = 0); }; template template 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 doesn't have exactly one element" ); return out_eT(Proxy::prefer_at_accessor ? P.at(0,0) : P[0]); } template template 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 doesn't have exactly one element" ); out_eT out; arrayops::convert_cx_scalar(out, (Proxy::prefer_at_accessor ? P.at(0,0) : P[0])); return out; } template template 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 doesn't have exactly one element" ); return out_eT(ProxyCube::prefer_at_accessor ? P.at(0,0,0) : P[0]); } template template 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 doesn't have exactly one element" ); out_eT out; arrayops::convert_cx_scalar(out, (ProxyCube::prefer_at_accessor ? 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 = 0); template inline static Mat from(const Base& in, const typename arma_cx_only::result* junk = 0); template inline static Mat from(const SpBase& in); template inline static Mat from(const std::vector& in, const typename arma_not_cx::result* junk = 0); template inline static Mat from(const std::vector& in, const typename arma_cx_only::result* junk = 0); }; template template 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); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template 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); arrayops::convert_cx( out.memptr(), X.memptr(), X.n_elem ); return out; } template template inline Mat conv_to< Mat >::from(const SpBase& in) { arma_extra_debug_sigprint(); return Mat(in.get_ref()); } template template 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); if(N > 0) { arrayops::convert( out.memptr(), &(in[0]), N ); } return out; } template template 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); 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 = 0); template inline static Row from(const Base& in, const typename arma_cx_only::result* junk = 0); template inline static Row from(const std::vector& in, const typename arma_not_cx::result* junk = 0); template inline static Row from(const std::vector& in, const typename arma_cx_only::result* junk = 0); }; template template 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 can't be interpreted as a vector" ); Row out(X.n_elem); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template 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 can't be interpreted as a vector" ); Row out(X.n_rows, X.n_cols); arrayops::convert_cx( out.memptr(), X.memptr(), X.n_elem ); return out; } template template 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); if(N > 0) { arrayops::convert( out.memptr(), &(in[0]), N ); } return out; } template template 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); 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 = 0); template inline static Col from(const Base& in, const typename arma_cx_only::result* junk = 0); template inline static Col from(const std::vector& in, const typename arma_not_cx::result* junk = 0); template inline static Col from(const std::vector& in, const typename arma_cx_only::result* junk = 0); }; template template 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 can't be interpreted as a vector" ); Col out(X.n_elem); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template 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 can't be interpreted as a vector" ); Col out(X.n_rows, X.n_cols); arrayops::convert_cx( out.memptr(), X.memptr(), X.n_elem ); return out; } template template 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); if(N > 0) { arrayops::convert( out.memptr(), &(in[0]), N ); } return out; } template template 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); if(N > 0) { arrayops::convert_cx( out.memptr(), &(in[0]), N ); } return out; } template class conv_to< SpMat > { public: template inline static SpMat from(const Base& in); }; template template 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 = 0); template inline static Cube from(const BaseCube& in, const typename arma_cx_only::result* junk = 0); }; template template 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); arrayops::convert( out.memptr(), X.memptr(), X.n_elem ); return out; } template template 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); 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 = 0); template inline static std::vector from(const Base& in, const typename arma_cx_only::result* junk = 0); }; template template 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 can't 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 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 can't 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; } //! @}