// 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 SpBase //! @{ template arma_inline const derived& SpBase::get_ref() const { return static_cast(*this); } template arma_inline bool SpBase::is_alias(const SpMat& X) const { return (*this).get_ref().is_alias(X); } template inline const SpOp SpBase::t() const { return SpOp( (*this).get_ref() ); } template inline const SpOp SpBase::ht() const { return SpOp( (*this).get_ref() ); } template inline const SpOp SpBase::st() const { return SpOp( (*this).get_ref() ); } template inline void SpBase::print(const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = get_cout_stream().width(); get_cout_stream() << extra_text << '\n'; get_cout_stream().width(orig_width); } arma_ostream::print(get_cout_stream(), tmp.M, true); } template inline void SpBase::print(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = user_stream.width(); user_stream << extra_text << '\n'; user_stream.width(orig_width); } arma_ostream::print(user_stream, tmp.M, true); } template inline void SpBase::raw_print(const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = get_cout_stream().width(); get_cout_stream() << extra_text << '\n'; get_cout_stream().width(orig_width); } arma_ostream::print(get_cout_stream(), tmp.M, false); } template inline void SpBase::raw_print(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = user_stream.width(); user_stream << extra_text << '\n'; user_stream.width(orig_width); } arma_ostream::print(user_stream, tmp.M, false); } template inline void SpBase::print_dense(const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = get_cout_stream().width(); get_cout_stream() << extra_text << '\n'; get_cout_stream().width(orig_width); } arma_ostream::print_dense(get_cout_stream(), tmp.M, true); } template inline void SpBase::print_dense(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = user_stream.width(); user_stream << extra_text << '\n'; user_stream.width(orig_width); } arma_ostream::print_dense(user_stream, tmp.M, true); } template inline void SpBase::raw_print_dense(const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = get_cout_stream().width(); get_cout_stream() << extra_text << '\n'; get_cout_stream().width(orig_width); } arma_ostream::print_dense(get_cout_stream(), tmp.M, false); } template inline void SpBase::raw_print_dense(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = user_stream.width(); user_stream << extra_text << '\n'; user_stream.width(orig_width); } arma_ostream::print_dense(user_stream, tmp.M, false); } template inline void SpBase::brief_print(const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = get_cout_stream().width(); get_cout_stream() << extra_text << '\n'; get_cout_stream().width(orig_width); } arma_ostream::brief_print(get_cout_stream(), tmp.M); } template inline void SpBase::brief_print(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); if(extra_text.length() != 0) { const std::streamsize orig_width = user_stream.width(); user_stream << extra_text << '\n'; user_stream.width(orig_width); } arma_ostream::brief_print(user_stream, tmp.M); } // // extra functions defined in SpBase_eval_SpMat template inline const derived& SpBase_eval_SpMat::eval() const { arma_extra_debug_sigprint(); return static_cast(*this); } // // extra functions defined in SpBase_eval_expr template inline SpMat SpBase_eval_expr::eval() const { arma_extra_debug_sigprint(); return SpMat( static_cast(*this) ); } template inline elem_type SpBase::min() const { return spop_min::min( (*this).get_ref() ); } template inline elem_type SpBase::max() const { return spop_max::max( (*this).get_ref() ); } template inline elem_type SpBase::min(uword& index_of_min_val) const { const SpProxy P( (*this).get_ref() ); return spop_min::min_with_index(P, index_of_min_val); } template inline elem_type SpBase::max(uword& index_of_max_val) const { const SpProxy P( (*this).get_ref() ); return spop_max::max_with_index(P, index_of_max_val); } template inline elem_type SpBase::min(uword& row_of_min_val, uword& col_of_min_val) const { const SpProxy P( (*this).get_ref() ); uword index = 0; const elem_type val = spop_min::min_with_index(P, index); const uword local_n_rows = P.get_n_rows(); row_of_min_val = index % local_n_rows; col_of_min_val = index / local_n_rows; return val; } template inline elem_type SpBase::max(uword& row_of_max_val, uword& col_of_max_val) const { const SpProxy P( (*this).get_ref() ); uword index = 0; const elem_type val = spop_max::max_with_index(P, index); const uword local_n_rows = P.get_n_rows(); row_of_max_val = index % local_n_rows; col_of_max_val = index / local_n_rows; return val; } template inline uword SpBase::index_min() const { const SpProxy P( (*this).get_ref() ); uword index = 0; if(P.get_n_elem() == 0) { arma_debug_check(true, "index_min(): object has no elements"); } else { spop_min::min_with_index(P, index); } return index; } template inline uword SpBase::index_max() const { const SpProxy P( (*this).get_ref() ); uword index = 0; if(P.get_n_elem() == 0) { arma_debug_check(true, "index_max(): object has no elements"); } else { spop_max::max_with_index(P, index); } return index; } template inline bool SpBase::is_symmetric() const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); return tmp.M.is_symmetric(); } template inline bool SpBase::is_symmetric(const typename get_pod_type::result tol) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); return tmp.M.is_symmetric(tol); } template inline bool SpBase::is_hermitian() const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); return tmp.M.is_hermitian(); } template inline bool SpBase::is_hermitian(const typename get_pod_type::result tol) const { arma_extra_debug_sigprint(); const unwrap_spmat tmp( (*this).get_ref() ); return tmp.M.is_hermitian(tol); } template inline bool SpBase::is_zero(const typename get_pod_type::result tol) const { arma_extra_debug_sigprint(); typedef typename get_pod_type::result T; arma_debug_check( (tol < T(0)), "is_zero(): parameter 'tol' must be >= 0" ); const SpProxy P( (*this).get_ref() ); if(P.get_n_elem() == 0) { return false; } if(P.get_n_nonzero() == 0) { return true; } if(is_SpMat::stored_type>::value) { const unwrap_spmat::stored_type> U(P.Q); return arrayops::is_zero(U.M.values, U.M.n_nonzero, tol); } typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); if(is_cx::yes) { while(it != it_end) { const elem_type val = (*it); const T val_real = access::tmp_real(val); const T val_imag = access::tmp_imag(val); if(eop_aux::arma_abs(val_real) > tol) { return false; } if(eop_aux::arma_abs(val_imag) > tol) { return false; } ++it; } } else // not complex { while(it != it_end) { if(eop_aux::arma_abs(*it) > tol) { return false; } ++it; } } return true; } template inline bool SpBase::is_trimatu() const { arma_extra_debug_sigprint(); const SpProxy P( (*this).get_ref() ); if(P.get_n_rows() != P.get_n_cols()) { return false; } typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); while(it != it_end) { if(it.row() > it.col()) { return false; } ++it; } return true; } template inline bool SpBase::is_trimatl() const { arma_extra_debug_sigprint(); const SpProxy P( (*this).get_ref() ); if(P.get_n_rows() != P.get_n_cols()) { return false; } typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); while(it != it_end) { if(it.row() < it.col()) { return false; } ++it; } return true; } template inline bool SpBase::is_diagmat() const { arma_extra_debug_sigprint(); const SpProxy P( (*this).get_ref() ); typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); while(it != it_end) { if(it.row() != it.col()) { return false; } ++it; } return true; } template inline bool SpBase::is_empty() const { arma_extra_debug_sigprint(); const SpProxy P( (*this).get_ref() ); return (P.get_n_elem() == uword(0)); } template inline bool SpBase::is_square() const { arma_extra_debug_sigprint(); const SpProxy P( (*this).get_ref() ); return (P.get_n_rows() == P.get_n_cols()); } template inline bool SpBase::is_vec() const { arma_extra_debug_sigprint(); if( (SpProxy::is_row) || (SpProxy::is_col) || (SpProxy::is_xvec) ) { return true; } const SpProxy P( (*this).get_ref() ); return ( (P.get_n_rows() == uword(1)) || (P.get_n_cols() == uword(1)) ); } template inline bool SpBase::is_colvec() const { arma_extra_debug_sigprint(); if(SpProxy::is_col) { return true; } const SpProxy P( (*this).get_ref() ); return (P.get_n_cols() == uword(1)); } template inline bool SpBase::is_rowvec() const { arma_extra_debug_sigprint(); if(SpProxy::is_row) { return true; } const SpProxy P( (*this).get_ref() ); return (P.get_n_rows() == uword(1)); } template inline bool SpBase::is_finite() const { arma_extra_debug_sigprint(); if(arma_config::fast_math_warn) { arma_debug_warn_level(1, "is_finite(): detection of non-finite values is not reliable in fast math mode"); } if(is_SpMat::stored_type>::value) { const unwrap_spmat U( (*this).get_ref() ); return U.M.internal_is_finite(); } else { const SpProxy P( (*this).get_ref() ); typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); while(it != it_end) { if(arma_isfinite(*it) == false) { return false; } ++it; } } return true; } template inline bool SpBase::has_inf() const { arma_extra_debug_sigprint(); if(arma_config::fast_math_warn) { arma_debug_warn_level(1, "has_inf(): detection of non-finite values is not reliable in fast math mode"); } if(is_SpMat::stored_type>::value) { const unwrap_spmat U( (*this).get_ref() ); return U.M.internal_has_inf(); } else { const SpProxy P( (*this).get_ref() ); typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); while(it != it_end) { if(arma_isinf(*it)) { return true; } ++it; } } return false; } template inline bool SpBase::has_nan() const { arma_extra_debug_sigprint(); if(arma_config::fast_math_warn) { arma_debug_warn_level(1, "has_nan(): detection of non-finite values is not reliable in fast math mode"); } if(is_SpMat::stored_type>::value) { const unwrap_spmat U( (*this).get_ref() ); return U.M.internal_has_nan(); } else { const SpProxy P( (*this).get_ref() ); typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); while(it != it_end) { if(arma_isnan(*it)) { return true; } ++it; } } return false; } template inline bool SpBase::has_nonfinite() const { arma_extra_debug_sigprint(); if(arma_config::fast_math_warn) { arma_debug_warn_level(1, "has_nonfinite(): detection of non-finite values is not reliable in fast math mode"); } if(is_SpMat::stored_type>::value) { const unwrap_spmat U( (*this).get_ref() ); return U.M.internal_has_nonfinite(); } else { const SpProxy P( (*this).get_ref() ); typename SpProxy::const_iterator_type it = P.begin(); typename SpProxy::const_iterator_type it_end = P.end(); while(it != it_end) { if(arma_isfinite(*it) == false) { return true; } ++it; } } return false; } template inline const SpOp SpBase::as_col() const { return SpOp( (*this).get_ref() ); } template inline const SpOp SpBase::as_row() const { return SpOp( (*this).get_ref() ); } //! @}