inline void subview_elem1<eT,T1>::replace(const eT old_val, const eT new_val) { arma_extra_debug_sigprint(); Mat<eT>& m_local = const_cast< Mat<eT>& >(m); eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; const unwrap_check_mixed<T1> tmp(a.get_ref(), m_local); const umat& aa = tmp.M; arma_debug_check ( ( (aa.is_vec() == false) && (aa.is_empty() == false) ), "Mat::elem(): given object is not a vector" ); const uword* aa_mem = aa.memptr(); const uword aa_n_elem = aa.n_elem; if(arma_isnan(old_val)) { for(uword iq=0; iq < aa_n_elem; ++iq) { const uword ii = aa_mem[iq]; arma_debug_check( (ii >= m_n_elem), "Mat::elem(): index out of bounds" ); eT& val = m_mem[ii]; val = (arma_isnan(val)) ? new_val : val; } } else { for(uword iq=0; iq < aa_n_elem; ++iq) { const uword ii = aa_mem[iq]; arma_debug_check( (ii >= m_n_elem), "Mat::elem(): index out of bounds" ); eT& val = m_mem[ii]; val = (val == old_val) ? new_val : val; } } }
arma_hot inline bool arrayops::has_nan(const eT* src, const uword n_elem) { uword j; for(j=1; j<n_elem; j+=2) { const eT val_i = (*src); src++; const eT val_j = (*src); src++; if( arma_isnan(val_i) || arma_isnan(val_j) ) { return true; } } if((j-1) < n_elem) { if(arma_isnan(*src)) { return true; } } return false; }
arma_hot inline void arrayops::replace(eT* mem, const uword n_elem, const eT old_val, const eT new_val) { if(arma_isnan(old_val)) { for(uword i=0; i<n_elem; ++i) { eT& val = mem[i]; val = (arma_isnan(val)) ? new_val : val; } } else { for(uword i=0; i<n_elem; ++i) { eT& val = mem[i]; val = (val == old_val) ? new_val : val; } } }
inline bool op_find_unique::apply_helper(Mat<uword>& out, const Proxy<T1>& P, const bool ascending_indices) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_elem = P.get_n_elem(); if(n_elem == 0) { out.set_size(0,1); return true; } if(n_elem == 1) { out.set_size(1,1); out[0] = 0; return true; } uvec indices(n_elem); std::vector< arma_find_unique_packet<eT> > packet_vec(n_elem); if(Proxy<T1>::prefer_at_accessor == false) { typename Proxy<T1>::ea_type Pea = P.get_ea(); for(uword i=0; i<n_elem; ++i) { const eT val = Pea[i]; if(arma_isnan(val)) { return false; } packet_vec[i].val = val; packet_vec[i].index = i; } } else { const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); uword i = 0; for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { const eT val = P.at(row,col); if(arma_isnan(val)) { return false; } packet_vec[i].val = val; packet_vec[i].index = i; ++i; } } arma_find_unique_comparator<eT> comparator; std::sort( packet_vec.begin(), packet_vec.end(), comparator ); uword* indices_mem = indices.memptr(); indices_mem[0] = packet_vec[0].index; uword count = 1; for(uword i=1; i < n_elem; ++i) { const eT diff = packet_vec[i-1].val - packet_vec[i].val; if(diff != eT(0)) { indices_mem[count] = packet_vec[i].index; ++count; } } out.steal_mem_col(indices,count); if(ascending_indices) { std::sort(out.begin(), out.end()); } return true; }
arma_inline bool arma_isnan(const std::complex<T>& x) { return ( arma_isnan(x.real()) || arma_isnan(x.imag()) ); }
inline bool arma_sort_index_helper(Mat<uword>& out, const Proxy<T1>& P, const uword sort_type, typename arma_not_cx<typename T1::elem_type>::result* junk = 0) { arma_extra_debug_sigprint(); arma_ignore(junk); typedef typename T1::elem_type eT; const uword n_elem = P.get_n_elem(); out.set_size(n_elem, 1); std::vector< arma_sort_index_packet<eT, uword> > packet_vec(n_elem); if(Proxy<T1>::prefer_at_accessor == false) { for(uword i=0; i<n_elem; ++i) { const eT val = P[i]; if(arma_isnan(val)) { out.reset(); return false; } packet_vec[i].val = val; packet_vec[i].index = i; } } else { const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); uword i = 0; for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { const eT val = P.at(row,col); if(arma_isnan(val)) { out.reset(); return false; } packet_vec[i].val = val; packet_vec[i].index = i; ++i; } } if(sort_type == 0) { // ascend arma_sort_index_helper_ascend comparator; if(sort_stable == false) { std::sort( packet_vec.begin(), packet_vec.end(), comparator ); } else { std::stable_sort( packet_vec.begin(), packet_vec.end(), comparator ); } } else { // descend arma_sort_index_helper_descend comparator; if(sort_stable == false) { std::sort( packet_vec.begin(), packet_vec.end(), comparator ); } else { std::stable_sort( packet_vec.begin(), packet_vec.end(), comparator ); } } uword* out_mem = out.memptr(); for(uword i=0; i<n_elem; ++i) { out_mem[i] = packet_vec[i].index; } return true; }
inline bool op_unique::apply_helper(Mat<typename T1::elem_type>& out, const Proxy<T1>& P) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); const uword n_elem = P.get_n_elem(); if(n_elem == 0) { out.set_size(n_rows, n_cols); return true; } if(n_elem == 1) { const eT tmp = (Proxy<T1>::use_at) ? P.at(0,0) : P[0]; out.set_size(n_rows, n_cols); out[0] = tmp; return true; } Mat<eT> X(n_elem,1); eT* X_mem = X.memptr(); if(Proxy<T1>::use_at == false) { typename Proxy<T1>::ea_type Pea = P.get_ea(); for(uword i=0; i<n_elem; ++i) { const eT val = Pea[i]; if(arma_isnan(val)) { out.reset(); return false; } X_mem[i] = val; } } else { for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { const eT val = P.at(row,col); if(arma_isnan(val)) { out.reset(); return false; } (*X_mem) = val; X_mem++; } X_mem = X.memptr(); } arma_unique_comparator<eT> comparator; std::sort( X.begin(), X.end(), comparator ); uword N_unique = 1; for(uword i=1; i < n_elem; ++i) { const eT a = X_mem[i-1]; const eT b = X_mem[i ]; const eT diff = a - b; if(diff != eT(0)) { ++N_unique; } } uword out_n_rows; uword out_n_cols; if( (n_rows == 1) || (n_cols == 1) ) { if(n_rows == 1) { out_n_rows = 1; out_n_cols = N_unique; } else { out_n_rows = N_unique; out_n_cols = 1; } } else { out_n_rows = N_unique; out_n_cols = 1; } out.set_size(out_n_rows, out_n_cols); eT* out_mem = out.memptr(); if(n_elem > 0) { (*out_mem) = X_mem[0]; out_mem++; } for(uword i=1; i < n_elem; ++i) { const eT a = X_mem[i-1]; const eT b = X_mem[i ]; const eT diff = a - b; if(diff != eT(0)) { (*out_mem) = b; out_mem++; } } return true; }