// 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 Base //! @{ template arma_inline const derived& Base::get_ref() const { return static_cast(*this); } template inline void Base::print(const std::string extra_text) const { arma_extra_debug_sigprint(); const quasi_unwrap 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 Base::print(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const quasi_unwrap 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 Base::raw_print(const std::string extra_text) const { arma_extra_debug_sigprint(); const quasi_unwrap 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 Base::raw_print(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const quasi_unwrap 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 Base::brief_print(const std::string extra_text) const { arma_extra_debug_sigprint(); const quasi_unwrap 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 Base::brief_print(std::ostream& user_stream, const std::string extra_text) const { arma_extra_debug_sigprint(); const quasi_unwrap 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); } template inline elem_type Base::min() const { return op_min::min( (*this).get_ref() ); } template inline elem_type Base::max() const { return op_max::max( (*this).get_ref() ); } template inline elem_type Base::min(uword& index_of_min_val) const { const Proxy P( (*this).get_ref() ); return op_min::min_with_index(P, index_of_min_val); } template inline elem_type Base::max(uword& index_of_max_val) const { const Proxy P( (*this).get_ref() ); return op_max::max_with_index(P, index_of_max_val); } template inline elem_type Base::min(uword& row_of_min_val, uword& col_of_min_val) const { const Proxy P( (*this).get_ref() ); uword index = 0; const elem_type val = op_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 Base::max(uword& row_of_max_val, uword& col_of_max_val) const { const Proxy P( (*this).get_ref() ); uword index = 0; const elem_type val = op_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 Base::index_min() const { const Proxy P( (*this).get_ref() ); uword index = 0; if(P.get_n_elem() == 0) { arma_debug_check(true, "index_min(): object has no elements"); } else { op_min::min_with_index(P, index); } return index; } template inline uword Base::index_max() const { const Proxy P( (*this).get_ref() ); uword index = 0; if(P.get_n_elem() == 0) { arma_debug_check(true, "index_max(): object has no elements"); } else { op_max::max_with_index(P, index); } return index; } template inline bool Base::is_symmetric() const { arma_extra_debug_sigprint(); const quasi_unwrap U( (*this).get_ref() ); const Mat& A = U.M; if(A.n_rows != A.n_cols) { return false; } if(A.n_elem <= 1 ) { return true; } const uword N = A.n_rows; const uword Nm1 = N-1; const elem_type* A_col = A.memptr(); for(uword j=0; j < Nm1; ++j) { const uword jp1 = j+1; const elem_type* A_row = &(A.at(j,jp1)); for(uword i=jp1; i < N; ++i) { if(A_col[i] != (*A_row)) { return false; } A_row += N; } A_col += N; } return true; } template inline bool Base::is_symmetric(const typename get_pod_type::result tol) const { arma_extra_debug_sigprint(); typedef typename get_pod_type::result T; if(tol == T(0)) { return (*this).is_symmetric(); } arma_debug_check( (tol < T(0)), "is_symmetric(): parameter 'tol' must be >= 0" ); const quasi_unwrap U( (*this).get_ref() ); const Mat& A = U.M; if(A.n_rows != A.n_cols) { return false; } if(A.n_elem <= 1 ) { return true; } const T norm_A = as_scalar( arma::max(sum(abs(A), 1), 0) ); if(norm_A == T(0)) { return true; } const T norm_A_Ast = as_scalar( arma::max(sum(abs(A - A.st()), 1), 0) ); return ( (norm_A_Ast / norm_A) <= tol ); } template inline bool Base::is_hermitian() const { arma_extra_debug_sigprint(); typedef typename get_pod_type::result T; const quasi_unwrap U( (*this).get_ref() ); const Mat& A = U.M; if(A.n_rows != A.n_cols) { return false; } if(A.n_elem == 0 ) { return true; } const uword N = A.n_rows; const elem_type* A_col = A.memptr(); for(uword j=0; j < N; ++j) { if( access::tmp_imag(A_col[j]) != T(0) ) { return false; } A_col += N; } A_col = A.memptr(); const uword Nm1 = N-1; for(uword j=0; j < Nm1; ++j) { const uword jp1 = j+1; const elem_type* A_row = &(A.at(j,jp1)); for(uword i=jp1; i < N; ++i) { if(A_col[i] != access::alt_conj(*A_row)) { return false; } A_row += N; } A_col += N; } return true; } template inline bool Base::is_hermitian(const typename get_pod_type::result tol) const { arma_extra_debug_sigprint(); typedef typename get_pod_type::result T; if(tol == T(0)) { return (*this).is_hermitian(); } arma_debug_check( (tol < T(0)), "is_hermitian(): parameter 'tol' must be >= 0" ); const quasi_unwrap U( (*this).get_ref() ); const Mat& A = U.M; if(A.n_rows != A.n_cols) { return false; } if(A.n_elem == 0 ) { return true; } const T norm_A = as_scalar( arma::max(sum(abs(A), 1), 0) ); if(norm_A == T(0)) { return true; } const T norm_A_At = as_scalar( arma::max(sum(abs(A - A.t()), 1), 0) ); return ( (norm_A_At / norm_A) <= tol ); } template inline bool Base::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" ); if(Proxy::use_at || is_Mat::stored_type>::value) { const quasi_unwrap U( (*this).get_ref() ); return arrayops::is_zero( U.M.memptr(), U.M.n_elem, tol ); } const Proxy P( (*this).get_ref() ); const uword n_elem = P.get_n_elem(); if(n_elem == 0) { return false; } const typename Proxy::ea_type Pea = P.get_ea(); if(is_cx::yes) { for(uword i=0; i tol) { return false; } if(eop_aux::arma_abs(val_imag) > tol) { return false; } } } else // not complex { for(uword i=0; i tol) { return false; } } } return true; } template inline bool Base::is_trimatu() const { arma_extra_debug_sigprint(); const quasi_unwrap U( (*this).get_ref() ); if(U.M.n_rows != U.M.n_cols) { return false; } if(U.M.n_elem <= 1) { return true; } return trimat_helper::is_triu(U.M); } template inline bool Base::is_trimatl() const { arma_extra_debug_sigprint(); const quasi_unwrap U( (*this).get_ref() ); if(U.M.n_rows != U.M.n_cols) { return false; } if(U.M.n_elem <= 1) { return true; } return trimat_helper::is_tril(U.M); } template inline bool Base::is_diagmat() const { arma_extra_debug_sigprint(); const quasi_unwrap U( (*this).get_ref() ); const Mat& A = U.M; if(A.n_elem <= 1) { return true; } // NOTE: we're NOT assuming the matrix has a square size const uword A_n_rows = A.n_rows; const uword A_n_cols = A.n_cols; const elem_type* A_mem = A.memptr(); if(A_mem[1] != elem_type(0)) { return false; } // if we got to this point, do a thorough check for(uword A_col=0; A_col < A_n_cols; ++A_col) { for(uword A_row=0; A_row < A_n_rows; ++A_row) { if( (A_mem[A_row] != elem_type(0)) && (A_row != A_col) ) { return false; } } A_mem += A_n_rows; } return true; } template inline bool Base::is_empty() const { arma_extra_debug_sigprint(); const Proxy P( (*this).get_ref() ); return (P.get_n_elem() == uword(0)); } template inline bool Base::is_square() const { arma_extra_debug_sigprint(); const Proxy P( (*this).get_ref() ); return (P.get_n_rows() == P.get_n_cols()); } template inline bool Base::is_vec() const { arma_extra_debug_sigprint(); if( (Proxy::is_row) || (Proxy::is_col) || (Proxy::is_xvec) ) { return true; } const Proxy P( (*this).get_ref() ); return ( (P.get_n_rows() == uword(1)) || (P.get_n_cols() == uword(1)) ); } template inline bool Base::is_colvec() const { arma_extra_debug_sigprint(); if(Proxy::is_col) { return true; } const Proxy P( (*this).get_ref() ); return (P.get_n_cols() == uword(1)); } template inline bool Base::is_rowvec() const { arma_extra_debug_sigprint(); if(Proxy::is_row) { return true; } const Proxy P( (*this).get_ref() ); return (P.get_n_rows() == uword(1)); } template inline bool Base::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_Mat::stored_type>::value) { const quasi_unwrap U( (*this).get_ref() ); return arrayops::is_finite( U.M.memptr(), U.M.n_elem ); } else { const Proxy P( (*this).get_ref() ); if(Proxy::use_at == false) { const typename Proxy::ea_type Pea = P.get_ea(); const uword n_elem = P.get_n_elem(); for(uword i=0; i inline bool Base::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_Mat::stored_type>::value) { const quasi_unwrap U( (*this).get_ref() ); return arrayops::has_inf( U.M.memptr(), U.M.n_elem ); } else { const Proxy P( (*this).get_ref() ); if(Proxy::use_at == false) { const typename Proxy::ea_type Pea = P.get_ea(); const uword n_elem = P.get_n_elem(); for(uword i=0; i inline bool Base::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_Mat::stored_type>::value) { const quasi_unwrap U( (*this).get_ref() ); return arrayops::has_nan( U.M.memptr(), U.M.n_elem ); } else { const Proxy P( (*this).get_ref() ); if(Proxy::use_at == false) { const typename Proxy::ea_type Pea = P.get_ea(); const uword n_elem = P.get_n_elem(); for(uword i=0; i inline bool Base::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_Mat::stored_type>::value) { const quasi_unwrap U( (*this).get_ref() ); return (arrayops::is_finite( U.M.memptr(), U.M.n_elem ) == false); } else { const Proxy P( (*this).get_ref() ); if(Proxy::use_at == false) { const typename Proxy::ea_type Pea = P.get_ea(); const uword n_elem = P.get_n_elem(); for(uword i=0; i inline const Op Base::as_col() const { return Op( (*this).get_ref() ); } template inline const Op Base::as_row() const { return Op( (*this).get_ref() ); } // // extra functions defined in Base_extra_yes template inline const Op Base_extra_yes::i() const { return Op(static_cast(*this)); } template inline bool Base_extra_yes::is_sympd() const { arma_extra_debug_sigprint(); typedef typename get_pod_type::result T; Mat X = static_cast(*this); // default value for tol const T tol = T(100) * std::numeric_limits::epsilon() * norm(X, "fro"); if(X.is_hermitian(tol) == false) { return false; } if(X.is_empty()) { return false; } X.diag() -= elem_type(tol); return auxlib::chol_simple(X); } template inline bool Base_extra_yes::is_sympd(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_sympd(): parameter 'tol' must be >= 0" ); Mat X = static_cast(*this); if(X.is_hermitian(tol) == false) { return false; } if(X.is_empty()) { return false; } X.diag() -= elem_type(tol); return auxlib::chol_simple(X); } // // extra functions defined in Base_eval_Mat template arma_inline const derived& Base_eval_Mat::eval() const { arma_extra_debug_sigprint(); return static_cast(*this); } // // extra functions defined in Base_eval_expr template inline Mat Base_eval_expr::eval() const { arma_extra_debug_sigprint(); return Mat( static_cast(*this) ); } // // extra functions defined in Base_trans_cx template arma_inline const Op Base_trans_cx::t() const { return Op( static_cast(*this) ); } template arma_inline const Op Base_trans_cx::ht() const { return Op( static_cast(*this) ); } template arma_inline const Op Base_trans_cx::st() const { return Op( static_cast(*this) ); } // // extra functions defined in Base_trans_default template arma_inline const Op Base_trans_default::t() const { return Op( static_cast(*this) ); } template arma_inline const Op Base_trans_default::ht() const { return Op( static_cast(*this) ); } template arma_inline const Op Base_trans_default::st() const { return Op( static_cast(*this) ); } //! @}