// 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 subview_elem1 //! @{ template inline subview_elem1::~subview_elem1() { arma_extra_debug_sigprint(); } template arma_inline subview_elem1::subview_elem1(const Mat& in_m, const Base& in_a) : m(in_m) , a(in_a) { arma_extra_debug_sigprint(); } template arma_inline subview_elem1::subview_elem1(const Cube& in_q, const Base& in_a) : fake_m( const_cast< eT* >(in_q.memptr()), in_q.n_elem, 1, false ) , m( fake_m ) , a( in_a ) { arma_extra_debug_sigprint(); } template template inline void subview_elem1::inplace_op(const eT val) { arma_extra_debug_sigprint(); Mat& m_local = const_cast< Mat& >(m); eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; const unwrap_check_mixed 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 must be a vector" ); const uword* aa_mem = aa.memptr(); const uword aa_n_elem = aa.n_elem; uword iq,jq; for(iq=0, jq=1; jq < aa_n_elem; iq+=2, jq+=2) { const uword ii = aa_mem[iq]; const uword jj = aa_mem[jq]; arma_debug_check_bounds( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { m_mem[ii] = val; m_mem[jj] = val; } if(is_same_type::yes) { m_mem[ii] += val; m_mem[jj] += val; } if(is_same_type::yes) { m_mem[ii] -= val; m_mem[jj] -= val; } if(is_same_type::yes) { m_mem[ii] *= val; m_mem[jj] *= val; } if(is_same_type::yes) { m_mem[ii] /= val; m_mem[jj] /= val; } } if(iq < aa_n_elem) { const uword ii = aa_mem[iq]; arma_debug_check_bounds( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { m_mem[ii] = val; } if(is_same_type::yes) { m_mem[ii] += val; } if(is_same_type::yes) { m_mem[ii] -= val; } if(is_same_type::yes) { m_mem[ii] *= val; } if(is_same_type::yes) { m_mem[ii] /= val; } } } template template inline void subview_elem1::inplace_op(const subview_elem1& x) { arma_extra_debug_sigprint(); subview_elem1& s = *this; if(&(s.m) == &(x.m)) { arma_extra_debug_print("subview_elem1::inplace_op(): aliasing detected"); const Mat tmp(x); if(is_same_type::yes) { s.operator= (tmp); } if(is_same_type::yes) { s.operator+=(tmp); } if(is_same_type::yes) { s.operator-=(tmp); } if(is_same_type::yes) { s.operator%=(tmp); } if(is_same_type::yes) { s.operator/=(tmp); } } else { Mat& s_m_local = const_cast< Mat& >(s.m); const Mat& x_m_local = x.m; const unwrap_check_mixed s_tmp(s.a.get_ref(), s_m_local); const unwrap_check_mixed x_tmp(x.a.get_ref(), s_m_local); const umat& s_aa = s_tmp.M; const umat& x_aa = x_tmp.M; arma_debug_check ( ( ((s_aa.is_vec() == false) && (s_aa.is_empty() == false)) || ((x_aa.is_vec() == false) && (x_aa.is_empty() == false)) ), "Mat::elem(): given object must be a vector" ); const uword* s_aa_mem = s_aa.memptr(); const uword* x_aa_mem = x_aa.memptr(); const uword s_aa_n_elem = s_aa.n_elem; arma_debug_check( (s_aa_n_elem != x_aa.n_elem), "Mat::elem(): size mismatch" ); eT* s_m_mem = s_m_local.memptr(); const uword s_m_n_elem = s_m_local.n_elem; const eT* x_m_mem = x_m_local.memptr(); const uword x_m_n_elem = x_m_local.n_elem; uword iq,jq; for(iq=0, jq=1; jq < s_aa_n_elem; iq+=2, jq+=2) { const uword s_ii = s_aa_mem[iq]; const uword s_jj = s_aa_mem[jq]; const uword x_ii = x_aa_mem[iq]; const uword x_jj = x_aa_mem[jq]; arma_debug_check_bounds ( (s_ii >= s_m_n_elem) || (s_jj >= s_m_n_elem) || (x_ii >= x_m_n_elem) || (x_jj >= x_m_n_elem), "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { s_m_mem[s_ii] = x_m_mem[x_ii]; s_m_mem[s_jj] = x_m_mem[x_jj]; } if(is_same_type::yes) { s_m_mem[s_ii] += x_m_mem[x_ii]; s_m_mem[s_jj] += x_m_mem[x_jj]; } if(is_same_type::yes) { s_m_mem[s_ii] -= x_m_mem[x_ii]; s_m_mem[s_jj] -= x_m_mem[x_jj]; } if(is_same_type::yes) { s_m_mem[s_ii] *= x_m_mem[x_ii]; s_m_mem[s_jj] *= x_m_mem[x_jj]; } if(is_same_type::yes) { s_m_mem[s_ii] /= x_m_mem[x_ii]; s_m_mem[s_jj] /= x_m_mem[x_jj]; } } if(iq < s_aa_n_elem) { const uword s_ii = s_aa_mem[iq]; const uword x_ii = x_aa_mem[iq]; arma_debug_check_bounds ( ( (s_ii >= s_m_n_elem) || (x_ii >= x_m_n_elem) ), "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { s_m_mem[s_ii] = x_m_mem[x_ii]; } if(is_same_type::yes) { s_m_mem[s_ii] += x_m_mem[x_ii]; } if(is_same_type::yes) { s_m_mem[s_ii] -= x_m_mem[x_ii]; } if(is_same_type::yes) { s_m_mem[s_ii] *= x_m_mem[x_ii]; } if(is_same_type::yes) { s_m_mem[s_ii] /= x_m_mem[x_ii]; } } } } template template inline void subview_elem1::inplace_op(const Base& x) { arma_extra_debug_sigprint(); Mat& m_local = const_cast< Mat& >(m); eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; const unwrap_check_mixed aa_tmp(a.get_ref(), m_local); const umat& aa = aa_tmp.M; arma_debug_check ( ( (aa.is_vec() == false) && (aa.is_empty() == false) ), "Mat::elem(): given object must be a vector" ); const uword* aa_mem = aa.memptr(); const uword aa_n_elem = aa.n_elem; const Proxy P(x.get_ref()); arma_debug_check( (aa_n_elem != P.get_n_elem()), "Mat::elem(): size mismatch" ); const bool is_alias = P.is_alias(m); if( (is_alias == false) && (Proxy::use_at == false) ) { typename Proxy::ea_type X = P.get_ea(); uword iq,jq; for(iq=0, jq=1; jq < aa_n_elem; iq+=2, jq+=2) { const uword ii = aa_mem[iq]; const uword jj = aa_mem[jq]; arma_debug_check_bounds( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { m_mem[ii] = X[iq]; m_mem[jj] = X[jq]; } if(is_same_type::yes) { m_mem[ii] += X[iq]; m_mem[jj] += X[jq]; } if(is_same_type::yes) { m_mem[ii] -= X[iq]; m_mem[jj] -= X[jq]; } if(is_same_type::yes) { m_mem[ii] *= X[iq]; m_mem[jj] *= X[jq]; } if(is_same_type::yes) { m_mem[ii] /= X[iq]; m_mem[jj] /= X[jq]; } } if(iq < aa_n_elem) { const uword ii = aa_mem[iq]; arma_debug_check_bounds( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { m_mem[ii] = X[iq]; } if(is_same_type::yes) { m_mem[ii] += X[iq]; } if(is_same_type::yes) { m_mem[ii] -= X[iq]; } if(is_same_type::yes) { m_mem[ii] *= X[iq]; } if(is_same_type::yes) { m_mem[ii] /= X[iq]; } } } else { arma_extra_debug_print("subview_elem1::inplace_op(): aliasing or use_at detected"); const unwrap_check::stored_type> tmp(P.Q, is_alias); const Mat& M = tmp.M; const eT* X = M.memptr(); uword iq,jq; for(iq=0, jq=1; jq < aa_n_elem; iq+=2, jq+=2) { const uword ii = aa_mem[iq]; const uword jj = aa_mem[jq]; arma_debug_check_bounds( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { m_mem[ii] = X[iq]; m_mem[jj] = X[jq]; } if(is_same_type::yes) { m_mem[ii] += X[iq]; m_mem[jj] += X[jq]; } if(is_same_type::yes) { m_mem[ii] -= X[iq]; m_mem[jj] -= X[jq]; } if(is_same_type::yes) { m_mem[ii] *= X[iq]; m_mem[jj] *= X[jq]; } if(is_same_type::yes) { m_mem[ii] /= X[iq]; m_mem[jj] /= X[jq]; } } if(iq < aa_n_elem) { const uword ii = aa_mem[iq]; arma_debug_check_bounds( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { m_mem[ii] = X[iq]; } if(is_same_type::yes) { m_mem[ii] += X[iq]; } if(is_same_type::yes) { m_mem[ii] -= X[iq]; } if(is_same_type::yes) { m_mem[ii] *= X[iq]; } if(is_same_type::yes) { m_mem[ii] /= X[iq]; } } } } // // template arma_inline const Op,op_htrans> subview_elem1::t() const { return Op,op_htrans>(*this); } template arma_inline const Op,op_htrans> subview_elem1::ht() const { return Op,op_htrans>(*this); } template arma_inline const Op,op_strans> subview_elem1::st() const { return Op,op_strans>(*this); } template inline void subview_elem1::replace(const eT old_val, const eT new_val) { arma_extra_debug_sigprint(); Mat& m_local = const_cast< Mat& >(m); eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; const unwrap_check_mixed 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 must be 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_bounds( (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_bounds( (ii >= m_n_elem), "Mat::elem(): index out of bounds" ); eT& val = m_mem[ii]; val = (val == old_val) ? new_val : val; } } } template inline void subview_elem1::clean(const pod_type threshold) { arma_extra_debug_sigprint(); Mat tmp(*this); tmp.clean(threshold); (*this).operator=(tmp); } template inline void subview_elem1::clamp(const eT min_val, const eT max_val) { arma_extra_debug_sigprint(); Mat tmp(*this); tmp.clamp(min_val, max_val); (*this).operator=(tmp); } template inline void subview_elem1::fill(const eT val) { arma_extra_debug_sigprint(); inplace_op(val); } template inline void subview_elem1::zeros() { arma_extra_debug_sigprint(); inplace_op(eT(0)); } template inline void subview_elem1::ones() { arma_extra_debug_sigprint(); inplace_op(eT(1)); } template inline void subview_elem1::randu() { arma_extra_debug_sigprint(); Mat& m_local = const_cast< Mat& >(m); eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; const unwrap_check_mixed 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 must be a vector" ); const uword* aa_mem = aa.memptr(); const uword aa_n_elem = aa.n_elem; uword iq,jq; for(iq=0, jq=1; jq < aa_n_elem; iq+=2, jq+=2) { const uword ii = aa_mem[iq]; const uword jj = aa_mem[jq]; arma_debug_check_bounds( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); const eT val1 = eT(arma_rng::randu()); const eT val2 = eT(arma_rng::randu()); m_mem[ii] = val1; m_mem[jj] = val2; } if(iq < aa_n_elem) { const uword ii = aa_mem[iq]; arma_debug_check_bounds( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); m_mem[ii] = eT(arma_rng::randu()); } } template inline void subview_elem1::randn() { arma_extra_debug_sigprint(); Mat& m_local = const_cast< Mat& >(m); eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; const unwrap_check_mixed 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 must be a vector" ); const uword* aa_mem = aa.memptr(); const uword aa_n_elem = aa.n_elem; uword iq,jq; for(iq=0, jq=1; jq < aa_n_elem; iq+=2, jq+=2) { const uword ii = aa_mem[iq]; const uword jj = aa_mem[jq]; arma_debug_check_bounds( ( (ii >= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); arma_rng::randn::dual_val( m_mem[ii], m_mem[jj] ); } if(iq < aa_n_elem) { const uword ii = aa_mem[iq]; arma_debug_check_bounds( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); m_mem[ii] = eT(arma_rng::randn()); } } template inline void subview_elem1::operator+= (const eT val) { arma_extra_debug_sigprint(); inplace_op(val); } template inline void subview_elem1::operator-= (const eT val) { arma_extra_debug_sigprint(); inplace_op(val); } template inline void subview_elem1::operator*= (const eT val) { arma_extra_debug_sigprint(); inplace_op(val); } template inline void subview_elem1::operator/= (const eT val) { arma_extra_debug_sigprint(); inplace_op(val); } // // template template inline void subview_elem1::operator_equ(const subview_elem1& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator= (const subview_elem1& x) { arma_extra_debug_sigprint(); (*this).operator_equ(x); } //! work around compiler bugs template inline void subview_elem1::operator= (const subview_elem1& x) { arma_extra_debug_sigprint(); (*this).operator_equ(x); } template template inline void subview_elem1::operator+= (const subview_elem1& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator-= (const subview_elem1& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator%= (const subview_elem1& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator/= (const subview_elem1& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator= (const Base& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator+= (const Base& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator-= (const Base& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator%= (const Base& x) { arma_extra_debug_sigprint(); inplace_op(x); } template template inline void subview_elem1::operator/= (const Base& x) { arma_extra_debug_sigprint(); inplace_op(x); } // // template inline void subview_elem1::extract(Mat& actual_out, const subview_elem1& in) { arma_extra_debug_sigprint(); const unwrap_check_mixed tmp1(in.a.get_ref(), actual_out); const umat& aa = tmp1.M; arma_debug_check ( ( (aa.is_vec() == false) && (aa.is_empty() == false) ), "Mat::elem(): given object must be a vector" ); const uword* aa_mem = aa.memptr(); const uword aa_n_elem = aa.n_elem; const Mat& m_local = in.m; const eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; const bool alias = (&actual_out == &m_local); if(alias) { arma_extra_debug_print("subview_elem1::extract(): aliasing detected"); } Mat* tmp_out = alias ? new Mat() : nullptr; Mat& out = alias ? *tmp_out : actual_out; out.set_size(aa_n_elem, 1); eT* out_mem = out.memptr(); uword i,j; for(i=0, j=1; j= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); out_mem[i] = m_mem[ii]; out_mem[j] = m_mem[jj]; } if(i < aa_n_elem) { const uword ii = aa_mem[i]; arma_debug_check_bounds( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); out_mem[i] = m_mem[ii]; } if(alias) { actual_out.steal_mem(out); delete tmp_out; } } template template inline void subview_elem1::mat_inplace_op(Mat& out, const subview_elem1& in) { arma_extra_debug_sigprint(); const unwrap tmp1(in.a.get_ref()); const umat& aa = tmp1.M; arma_debug_check ( ( (aa.is_vec() == false) && (aa.is_empty() == false) ), "Mat::elem(): given object must be a vector" ); const uword* aa_mem = aa.memptr(); const uword aa_n_elem = aa.n_elem; const unwrap_check< Mat > tmp2(in.m, out); const Mat& m_local = tmp2.M; const eT* m_mem = m_local.memptr(); const uword m_n_elem = m_local.n_elem; arma_debug_check( (out.n_elem != aa_n_elem), "Mat::elem(): size mismatch" ); eT* out_mem = out.memptr(); uword i,j; for(i=0, j=1; j= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { out_mem[i] += m_mem[ii]; out_mem[j] += m_mem[jj]; } if(is_same_type::yes) { out_mem[i] -= m_mem[ii]; out_mem[j] -= m_mem[jj]; } if(is_same_type::yes) { out_mem[i] *= m_mem[ii]; out_mem[j] *= m_mem[jj]; } if(is_same_type::yes) { out_mem[i] /= m_mem[ii]; out_mem[j] /= m_mem[jj]; } } if(i < aa_n_elem) { const uword ii = aa_mem[i]; arma_debug_check_bounds( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); if(is_same_type::yes) { out_mem[i] += m_mem[ii]; } if(is_same_type::yes) { out_mem[i] -= m_mem[ii]; } if(is_same_type::yes) { out_mem[i] *= m_mem[ii]; } if(is_same_type::yes) { out_mem[i] /= m_mem[ii]; } } } template inline void subview_elem1::plus_inplace(Mat& out, const subview_elem1& in) { arma_extra_debug_sigprint(); mat_inplace_op(out, in); } template inline void subview_elem1::minus_inplace(Mat& out, const subview_elem1& in) { arma_extra_debug_sigprint(); mat_inplace_op(out, in); } template inline void subview_elem1::schur_inplace(Mat& out, const subview_elem1& in) { arma_extra_debug_sigprint(); mat_inplace_op(out, in); } template inline void subview_elem1::div_inplace(Mat& out, const subview_elem1& in) { arma_extra_debug_sigprint(); mat_inplace_op(out, in); } //! @}