123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- // 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<typename elem_type, typename derived>
- arma_inline
- const derived&
- Base<elem_type,derived>::get_ref() const
- {
- return static_cast<const derived&>(*this);
- }
- template<typename elem_type, typename derived>
- arma_cold
- inline
- void
- Base<elem_type,derived>::print(const std::string extra_text) const
- {
- const quasi_unwrap<derived> tmp( (*this).get_ref() );
-
- tmp.M.impl_print(extra_text);
- }
- template<typename elem_type, typename derived>
- arma_cold
- inline
- void
- Base<elem_type,derived>::print(std::ostream& user_stream, const std::string extra_text) const
- {
- const quasi_unwrap<derived> tmp( (*this).get_ref() );
-
- tmp.M.impl_print(user_stream, extra_text);
- }
-
- template<typename elem_type, typename derived>
- arma_cold
- inline
- void
- Base<elem_type,derived>::raw_print(const std::string extra_text) const
- {
- const quasi_unwrap<derived> tmp( (*this).get_ref() );
-
- tmp.M.impl_raw_print(extra_text);
- }
- template<typename elem_type, typename derived>
- arma_cold
- inline
- void
- Base<elem_type,derived>::raw_print(std::ostream& user_stream, const std::string extra_text) const
- {
- const quasi_unwrap<derived> tmp( (*this).get_ref() );
-
- tmp.M.impl_raw_print(user_stream, extra_text);
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- elem_type
- Base<elem_type,derived>::min() const
- {
- return op_min::min( (*this).get_ref() );
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- elem_type
- Base<elem_type,derived>::max() const
- {
- return op_max::max( (*this).get_ref() );
- }
- template<typename elem_type, typename derived>
- inline
- elem_type
- Base<elem_type,derived>::min(uword& index_of_min_val) const
- {
- const Proxy<derived> P( (*this).get_ref() );
-
- return op_min::min_with_index(P, index_of_min_val);
- }
- template<typename elem_type, typename derived>
- inline
- elem_type
- Base<elem_type,derived>::max(uword& index_of_max_val) const
- {
- const Proxy<derived> P( (*this).get_ref() );
-
- return op_max::max_with_index(P, index_of_max_val);
- }
- template<typename elem_type, typename derived>
- inline
- elem_type
- Base<elem_type,derived>::min(uword& row_of_min_val, uword& col_of_min_val) const
- {
- const Proxy<derived> 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<typename elem_type, typename derived>
- inline
- elem_type
- Base<elem_type,derived>::max(uword& row_of_max_val, uword& col_of_max_val) const
- {
- const Proxy<derived> 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- uword
- Base<elem_type,derived>::index_min() const
- {
- const Proxy<derived> 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- uword
- Base<elem_type,derived>::index_max() const
- {
- const Proxy<derived> 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_symmetric() const
- {
- arma_extra_debug_sigprint();
-
- const quasi_unwrap<derived> U( (*this).get_ref() );
-
- const Mat<elem_type>& 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_symmetric(const typename get_pod_type<elem_type>::result tol) const
- {
- arma_extra_debug_sigprint();
-
- typedef typename get_pod_type<elem_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<derived> U( (*this).get_ref() );
-
- const Mat<elem_type>& 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_hermitian() const
- {
- arma_extra_debug_sigprint();
-
- typedef typename get_pod_type<elem_type>::result T;
-
- const quasi_unwrap<derived> U( (*this).get_ref() );
-
- const Mat<elem_type>& 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_hermitian(const typename get_pod_type<elem_type>::result tol) const
- {
- arma_extra_debug_sigprint();
-
- typedef typename get_pod_type<elem_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<derived> U( (*this).get_ref() );
-
- const Mat<elem_type>& 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_zero(const typename get_pod_type<elem_type>::result tol) const
- {
- arma_extra_debug_sigprint();
-
- typedef typename get_pod_type<elem_type>::result T;
-
- arma_debug_check( (tol < T(0)), "is_zero(): parameter 'tol' must be >= 0" );
-
- if(Proxy<derived>::use_at || is_Mat<typename Proxy<derived>::stored_type>::value)
- {
- const quasi_unwrap<derived> U( (*this).get_ref() );
-
- return arrayops::is_zero( U.M.memptr(), U.M.n_elem, tol );
- }
-
- const Proxy<derived> P( (*this).get_ref() );
-
- const uword n_elem = P.get_n_elem();
-
- if(n_elem == 0) { return false; }
-
- const typename Proxy<derived>::ea_type Pea = P.get_ea();
-
- if(is_cx<elem_type>::yes)
- {
- for(uword i=0; i<n_elem; ++i)
- {
- const elem_type val = Pea[i];
-
- 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; }
- }
- }
- else // not complex
- {
- for(uword i=0; i<n_elem; ++i)
- {
- if(eop_aux::arma_abs(Pea[i]) > tol) { return false; }
- }
- }
-
- return true;
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_trimatu() const
- {
- arma_extra_debug_sigprint();
-
- const quasi_unwrap<derived> 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_trimatl() const
- {
- arma_extra_debug_sigprint();
-
- const quasi_unwrap<derived> 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_diagmat() const
- {
- arma_extra_debug_sigprint();
-
- const quasi_unwrap<derived> U( (*this).get_ref() );
-
- const Mat<elem_type>& 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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_empty() const
- {
- arma_extra_debug_sigprint();
-
- const Proxy<derived> P( (*this).get_ref() );
-
- return (P.get_n_elem() == uword(0));
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_square() const
- {
- arma_extra_debug_sigprint();
-
- const Proxy<derived> P( (*this).get_ref() );
-
- return (P.get_n_rows() == P.get_n_cols());
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_vec() const
- {
- arma_extra_debug_sigprint();
-
- if( (Proxy<derived>::is_row) || (Proxy<derived>::is_col) || (Proxy<derived>::is_xvec) ) { return true; }
-
- const Proxy<derived> P( (*this).get_ref() );
-
- return ( (P.get_n_rows() == uword(1)) || (P.get_n_cols() == uword(1)) );
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_colvec() const
- {
- arma_extra_debug_sigprint();
-
- if(Proxy<derived>::is_col) { return true; }
-
- const Proxy<derived> P( (*this).get_ref() );
-
- return (P.get_n_cols() == uword(1));
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_rowvec() const
- {
- arma_extra_debug_sigprint();
-
- if(Proxy<derived>::is_row) { return true; }
-
- const Proxy<derived> P( (*this).get_ref() );
-
- return (P.get_n_rows() == uword(1));
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::is_finite() const
- {
- arma_extra_debug_sigprint();
-
- const Proxy<derived> P( (*this).get_ref() );
-
- if(is_Mat<typename Proxy<derived>::stored_type>::value)
- {
- const quasi_unwrap<typename Proxy<derived>::stored_type> U(P.Q);
-
- return arrayops::is_finite( U.M.memptr(), U.M.n_elem );
- }
-
- if(Proxy<derived>::use_at == false)
- {
- const typename Proxy<derived>::ea_type Pea = P.get_ea();
-
- const uword n_elem = P.get_n_elem();
-
- for(uword i=0; i<n_elem; ++i)
- {
- if(arma_isfinite(Pea[i]) == false) { return false; }
- }
- }
- else
- {
- const uword n_rows = P.get_n_rows();
- const uword n_cols = P.get_n_cols();
-
- for(uword col=0; col<n_cols; ++col)
- for(uword row=0; row<n_rows; ++row)
- {
- if(arma_isfinite(P.at(row,col)) == false) { return false; }
- }
- }
-
- return true;
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::has_inf() const
- {
- arma_extra_debug_sigprint();
-
- const Proxy<derived> P( (*this).get_ref() );
-
- if(is_Mat<typename Proxy<derived>::stored_type>::value)
- {
- const quasi_unwrap<typename Proxy<derived>::stored_type> U(P.Q);
-
- return arrayops::has_inf( U.M.memptr(), U.M.n_elem );
- }
-
- if(Proxy<derived>::use_at == false)
- {
- const typename Proxy<derived>::ea_type Pea = P.get_ea();
-
- const uword n_elem = P.get_n_elem();
-
- for(uword i=0; i<n_elem; ++i)
- {
- if(arma_isinf(Pea[i])) { return true; }
- }
- }
- else
- {
- const uword n_rows = P.get_n_rows();
- const uword n_cols = P.get_n_cols();
-
- for(uword col=0; col<n_cols; ++col)
- for(uword row=0; row<n_rows; ++row)
- {
- if(arma_isinf(P.at(row,col))) { return true; }
- }
- }
-
- return false;
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base<elem_type,derived>::has_nan() const
- {
- arma_extra_debug_sigprint();
-
- const Proxy<derived> P( (*this).get_ref() );
-
- if(is_Mat<typename Proxy<derived>::stored_type>::value)
- {
- const quasi_unwrap<typename Proxy<derived>::stored_type> U(P.Q);
-
- return arrayops::has_nan( U.M.memptr(), U.M.n_elem );
- }
-
- if(Proxy<derived>::use_at == false)
- {
- const typename Proxy<derived>::ea_type Pea = P.get_ea();
-
- const uword n_elem = P.get_n_elem();
-
- for(uword i=0; i<n_elem; ++i)
- {
- if(arma_isnan(Pea[i])) { return true; }
- }
- }
- else
- {
- const uword n_rows = P.get_n_rows();
- const uword n_cols = P.get_n_cols();
-
- for(uword col=0; col<n_cols; ++col)
- for(uword row=0; row<n_rows; ++row)
- {
- if(arma_isnan(P.at(row,col))) { return true; }
- }
- }
-
- return false;
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- const Op<derived,op_vectorise_col>
- Base<elem_type, derived>::as_col() const
- {
- return Op<derived,op_vectorise_col>( (*this).get_ref() );
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- const Op<derived,op_vectorise_row>
- Base<elem_type, derived>::as_row() const
- {
- return Op<derived,op_vectorise_row>( (*this).get_ref() );
- }
- //
- // extra functions defined in Base_extra_yes
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- const Op<derived,op_inv>
- Base_extra_yes<elem_type, derived>::i() const
- {
- return Op<derived,op_inv>(static_cast<const derived&>(*this));
- }
- template<typename elem_type, typename derived>
- arma_deprecated
- inline
- const Op<derived,op_inv>
- Base_extra_yes<elem_type, derived>::i(const bool) const // argument kept only for compatibility with old user code
- {
- // arma_debug_warn(".i(bool) is deprecated and will be removed; change to .i()");
-
- return Op<derived,op_inv>(static_cast<const derived&>(*this));
- }
- template<typename elem_type, typename derived>
- arma_deprecated
- inline
- const Op<derived,op_inv>
- Base_extra_yes<elem_type, derived>::i(const char*) const // argument kept only for compatibility with old user code
- {
- // arma_debug_warn(".i(char*) is deprecated and will be removed; change to .i()");
-
- return Op<derived,op_inv>(static_cast<const derived&>(*this));
- }
- template<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base_extra_yes<elem_type,derived>::is_sympd() const
- {
- arma_extra_debug_sigprint();
-
- typedef typename get_pod_type<elem_type>::result T;
-
- Mat<elem_type> X = static_cast<const derived&>(*this);
-
- // default value for tol
- const T tol = T(100) * std::numeric_limits<T>::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<typename elem_type, typename derived>
- inline
- arma_warn_unused
- bool
- Base_extra_yes<elem_type,derived>::is_sympd(typename get_pod_type<elem_type>::result tol) const
- {
- arma_extra_debug_sigprint();
-
- typedef typename get_pod_type<elem_type>::result T;
-
- arma_debug_check( (tol < T(0)), "is_sympd(): parameter 'tol' must be >= 0" );
-
- Mat<elem_type> X = static_cast<const derived&>(*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<typename elem_type, typename derived>
- arma_inline
- const derived&
- Base_eval_Mat<elem_type, derived>::eval() const
- {
- arma_extra_debug_sigprint();
-
- return static_cast<const derived&>(*this);
- }
- //
- // extra functions defined in Base_eval_expr
- template<typename elem_type, typename derived>
- arma_inline
- Mat<elem_type>
- Base_eval_expr<elem_type, derived>::eval() const
- {
- arma_extra_debug_sigprint();
-
- return Mat<elem_type>( static_cast<const derived&>(*this) );
- }
- //
- // extra functions defined in Base_trans_cx
- template<typename derived>
- arma_inline
- const Op<derived,op_htrans>
- Base_trans_cx<derived>::t() const
- {
- return Op<derived,op_htrans>( static_cast<const derived&>(*this) );
- }
- template<typename derived>
- arma_inline
- const Op<derived,op_htrans>
- Base_trans_cx<derived>::ht() const
- {
- return Op<derived,op_htrans>( static_cast<const derived&>(*this) );
- }
- template<typename derived>
- arma_inline
- const Op<derived,op_strans>
- Base_trans_cx<derived>::st() const
- {
- return Op<derived,op_strans>( static_cast<const derived&>(*this) );
- }
- //
- // extra functions defined in Base_trans_default
- template<typename derived>
- arma_inline
- const Op<derived,op_htrans>
- Base_trans_default<derived>::t() const
- {
- return Op<derived,op_htrans>( static_cast<const derived&>(*this) );
- }
- template<typename derived>
- arma_inline
- const Op<derived,op_htrans>
- Base_trans_default<derived>::ht() const
- {
- return Op<derived,op_htrans>( static_cast<const derived&>(*this) );
- }
- template<typename derived>
- arma_inline
- const Op<derived,op_htrans>
- Base_trans_default<derived>::st() const
- {
- return Op<derived,op_htrans>( static_cast<const derived&>(*this) );
- }
- //! @}
|