max@0: // Copyright (C) 2010-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2010-2011 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup subview_elem1 max@0: //! @{ max@0: max@0: max@0: template max@0: inline max@0: subview_elem1::~subview_elem1() max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: template max@0: arma_inline max@0: subview_elem1::subview_elem1(const Mat& in_m, const Base& in_a) max@0: : m(in_m) max@0: , m_ptr(0) max@0: , a(in_a) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: subview_elem1::subview_elem1(Mat& in_m, const Base& in_a) max@0: : m(in_m) max@0: , m_ptr(&in_m) max@0: , a(in_a) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::inplace_op(const eT val) max@0: { max@0: Mat& m_local = *m_ptr; max@0: max@0: eT* m_mem = m_local.memptr(); max@0: const uword m_n_elem = m_local.n_elem; max@0: max@0: const unwrap_check_mixed tmp(a.get_ref(), m_local); max@0: const umat& aa = tmp.M; max@0: max@0: arma_debug_check max@0: ( max@0: ( aa.is_vec() == false ), max@0: "Mat::elem(): given object is not a vector" max@0: ); max@0: max@0: const uword* aa_mem = aa.memptr(); max@0: const uword aa_n_elem = aa.n_elem; max@0: max@0: uword i,j; max@0: for(i=0, j=1; j= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { m_mem[ii] = val; m_mem[jj] = val; } max@0: else if(is_same_type::value == true) { m_mem[ii] += val; m_mem[jj] += val; } max@0: else if(is_same_type::value == true) { m_mem[ii] -= val; m_mem[jj] -= val; } max@0: else if(is_same_type::value == true) { m_mem[ii] *= val; m_mem[jj] *= val; } max@0: else if(is_same_type::value == true) { m_mem[ii] /= val; m_mem[jj] /= val; } max@0: } max@0: max@0: if(i < aa_n_elem) max@0: { max@0: const uword ii = aa_mem[i]; max@0: max@0: arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { m_mem[ii] = val; } max@0: else if(is_same_type::value == true) { m_mem[ii] += val; } max@0: else if(is_same_type::value == true) { m_mem[ii] -= val; } max@0: else if(is_same_type::value == true) { m_mem[ii] *= val; } max@0: else if(is_same_type::value == true) { m_mem[ii] /= val; } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::inplace_op(const subview_elem1& x) max@0: { max@0: subview_elem1& t = *this; max@0: max@0: if(&(t.m) == &(x.m)) max@0: { max@0: arma_extra_debug_print("subview_elem1::inplace_op(): aliasing detected"); max@0: max@0: const Mat tmp(x); max@0: max@0: if(is_same_type::value == true) { t.operator= (tmp); } max@0: else if(is_same_type::value == true) { t.operator+=(tmp); } max@0: else if(is_same_type::value == true) { t.operator-=(tmp); } max@0: else if(is_same_type::value == true) { t.operator%=(tmp); } max@0: else if(is_same_type::value == true) { t.operator/=(tmp); } max@0: } max@0: else max@0: { max@0: Mat& t_m_local = *(t.m_ptr); max@0: const Mat& x_m_local = x.m; max@0: max@0: const unwrap_check_mixed t_tmp(t.a.get_ref(), t_m_local); max@0: const unwrap_check_mixed x_tmp(x.a.get_ref(), t_m_local); max@0: max@0: const umat& t_aa = t_tmp.M; max@0: const umat& x_aa = x_tmp.M; max@0: max@0: arma_debug_check max@0: ( max@0: ( (t_aa.is_vec() == false) || (x_aa.is_vec() == false) ), max@0: "Mat::elem(): given object is not a vector" max@0: ); max@0: max@0: const uword* t_aa_mem = t_aa.memptr(); max@0: const uword* x_aa_mem = x_aa.memptr(); max@0: max@0: const uword t_aa_n_elem = t_aa.n_elem; max@0: max@0: arma_debug_check( (t_aa_n_elem != x_aa.n_elem), "Mat::elem(): size mismatch" ); max@0: max@0: max@0: eT* t_m_mem = t_m_local.memptr(); max@0: const uword t_m_n_elem = t_m_local.n_elem; max@0: max@0: const eT* x_m_mem = x_m_local.memptr(); max@0: const uword x_m_n_elem = x_m_local.n_elem; max@0: max@0: uword i,j; max@0: for(i=0, j=1; j= t_m_n_elem) || (t_jj >= t_m_n_elem) || (x_ii >= x_m_n_elem) || (x_jj >= x_m_n_elem), max@0: "Mat::elem(): index out of bounds" max@0: ); max@0: max@0: if(is_same_type::value == true) { t_m_mem[t_ii] = x_m_mem[x_ii]; t_m_mem[t_jj] = x_m_mem[x_jj]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] += x_m_mem[x_ii]; t_m_mem[t_jj] += x_m_mem[x_jj]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] -= x_m_mem[x_ii]; t_m_mem[t_jj] -= x_m_mem[x_jj]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] *= x_m_mem[x_ii]; t_m_mem[t_jj] *= x_m_mem[x_jj]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] /= x_m_mem[x_ii]; t_m_mem[t_jj] /= x_m_mem[x_jj]; } max@0: } max@0: max@0: if(i < t_aa_n_elem) max@0: { max@0: const uword t_ii = t_aa_mem[i]; max@0: const uword x_ii = x_aa_mem[i]; max@0: max@0: arma_debug_check max@0: ( max@0: ( (t_ii >= t_m_n_elem) || (x_ii >= x_m_n_elem) ), max@0: "Mat::elem(): index out of bounds" max@0: ); max@0: max@0: if(is_same_type::value == true) { t_m_mem[t_ii] = x_m_mem[x_ii]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] += x_m_mem[x_ii]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] -= x_m_mem[x_ii]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] *= x_m_mem[x_ii]; } max@0: else if(is_same_type::value == true) { t_m_mem[t_ii] /= x_m_mem[x_ii]; } max@0: } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::inplace_op(const Base& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: Mat& m_local = *m_ptr; max@0: max@0: eT* m_mem = m_local.memptr(); max@0: const uword m_n_elem = m_local.n_elem; max@0: max@0: const unwrap_check_mixed tmp(a.get_ref(), m_local); max@0: const umat& aa = tmp.M; max@0: max@0: arma_debug_check max@0: ( max@0: ( aa.is_vec() == false ), max@0: "Mat::elem(): given object is not a vector" max@0: ); max@0: max@0: const uword* aa_mem = aa.memptr(); max@0: const uword aa_n_elem = aa.n_elem; max@0: max@0: const Proxy P(x.get_ref()); max@0: max@0: arma_debug_check( (aa_n_elem != P.get_n_elem()), "Mat::elem(): size mismatch" ); max@0: max@0: if( (P.is_alias(m) == false) && (Proxy::prefer_at_accessor == false) ) max@0: { max@0: typename Proxy::ea_type X = P.get_ea(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { m_mem[ii] = X[i]; m_mem[jj] = X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] += X[i]; m_mem[jj] += X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] -= X[i]; m_mem[jj] -= X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] *= X[i]; m_mem[jj] *= X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] /= X[i]; m_mem[jj] /= X[j]; } max@0: } max@0: max@0: if(i < aa_n_elem) max@0: { max@0: const uword ii = aa_mem[i]; max@0: max@0: arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { m_mem[ii] = X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] += X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] -= X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] *= X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] /= X[i]; } max@0: } max@0: } max@0: else max@0: { max@0: arma_extra_debug_print("subview_elem1::inplace_op(): aliasing or prefer_at_accessor detected"); max@0: max@0: const unwrap_check::stored_type> tmp(P.Q, m_local); max@0: const Mat& M = tmp.M; max@0: max@0: const eT* X = M.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { m_mem[ii] = X[i]; m_mem[jj] = X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] += X[i]; m_mem[jj] += X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] -= X[i]; m_mem[jj] -= X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] *= X[i]; m_mem[jj] *= X[j]; } max@0: else if(is_same_type::value == true) { m_mem[ii] /= X[i]; m_mem[jj] /= X[j]; } max@0: } max@0: max@0: if(i < aa_n_elem) max@0: { max@0: const uword ii = aa_mem[i]; max@0: max@0: arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { m_mem[ii] = X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] += X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] -= X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] *= X[i]; } max@0: else if(is_same_type::value == true) { m_mem[ii] /= X[i]; } max@0: } max@0: } max@0: } max@0: max@0: max@0: max@0: // max@0: // max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::fill(const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(val); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::zeros() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(eT(0)); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::ones() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(eT(1)); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::operator+= (const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(val); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::operator-= (const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(val); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::operator*= (const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(val); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::operator/= (const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(val); max@0: } max@0: max@0: max@0: max@0: // max@0: // max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator_equ(const subview_elem1& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator= (const subview_elem1& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: (*this).operator_equ(x); max@0: } max@0: max@0: max@0: max@0: //! work around compiler bugs max@0: template max@0: inline max@0: void max@0: subview_elem1::operator= (const subview_elem1& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: (*this).operator_equ(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator+= (const subview_elem1& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator-= (const subview_elem1& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator%= (const subview_elem1& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator/= (const subview_elem1& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator= (const Base& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator+= (const Base& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator-= (const Base& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator%= (const Base& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::operator/= (const Base& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: inplace_op(x); max@0: } max@0: max@0: max@0: max@0: // max@0: // max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::extract(Mat& actual_out, const subview_elem1& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const unwrap_check_mixed tmp1(in.a.get_ref(), actual_out); max@0: const umat& aa = tmp1.M; max@0: max@0: arma_debug_check max@0: ( max@0: ( aa.is_vec() == false ), max@0: "Mat::elem(): given object is not a vector" max@0: ); max@0: max@0: const uword* aa_mem = aa.memptr(); max@0: const uword aa_n_elem = aa.n_elem; max@0: max@0: const Mat& m_local = in.m; max@0: max@0: const eT* m_mem = m_local.memptr(); max@0: const uword m_n_elem = m_local.n_elem; max@0: max@0: const bool alias = (&actual_out == &m_local); max@0: max@0: arma_extra_debug_warn(alias, "subview_elem1::extract(): aliasing detected"); max@0: max@0: Mat* tmp_out = alias ? new Mat() : 0; max@0: Mat& out = alias ? *tmp_out : actual_out; max@0: max@0: out.set_size(aa_n_elem, 1); max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); max@0: max@0: out_mem[i] = m_mem[ii]; max@0: out_mem[j] = m_mem[jj]; max@0: } max@0: max@0: if(i < aa_n_elem) max@0: { max@0: const uword ii = aa_mem[i]; max@0: max@0: arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); max@0: max@0: out_mem[i] = m_mem[ii]; max@0: } max@0: max@0: if(alias == true) max@0: { max@0: actual_out = out; max@0: delete tmp_out; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: subview_elem1::mat_inplace_op(Mat& out, const subview_elem1& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const unwrap tmp1(in.a.get_ref()); max@0: const umat& aa = tmp1.M; max@0: max@0: arma_debug_check max@0: ( max@0: ( aa.is_vec() == false ), max@0: "Mat::elem(): given object is not a vector" max@0: ); max@0: max@0: const uword* aa_mem = aa.memptr(); max@0: const uword aa_n_elem = aa.n_elem; max@0: max@0: const unwrap_check< Mat > tmp2(in.m, out); max@0: const Mat& m_local = tmp2.M; max@0: max@0: const eT* m_mem = m_local.memptr(); max@0: const uword m_n_elem = m_local.n_elem; max@0: max@0: arma_debug_check( (out.n_elem != aa_n_elem), "Mat::elem(): size mismatch" ); max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j= m_n_elem) || (jj >= m_n_elem) ), "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { out_mem[i] += m_mem[ii]; out_mem[j] += m_mem[jj]; } max@0: else if(is_same_type::value == true) { out_mem[i] -= m_mem[ii]; out_mem[j] -= m_mem[jj]; } max@0: else if(is_same_type::value == true) { out_mem[i] *= m_mem[ii]; out_mem[j] *= m_mem[jj]; } max@0: else if(is_same_type::value == true) { out_mem[i] /= m_mem[ii]; out_mem[j] /= m_mem[jj]; } max@0: } max@0: max@0: if(i < aa_n_elem) max@0: { max@0: const uword ii = aa_mem[i]; max@0: max@0: arma_debug_check( (ii >= m_n_elem) , "Mat::elem(): index out of bounds" ); max@0: max@0: if(is_same_type::value == true) { out_mem[i] += m_mem[ii]; } max@0: else if(is_same_type::value == true) { out_mem[i] -= m_mem[ii]; } max@0: else if(is_same_type::value == true) { out_mem[i] *= m_mem[ii]; } max@0: else if(is_same_type::value == true) { out_mem[i] /= m_mem[ii]; } max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::plus_inplace(Mat& out, const subview_elem1& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: mat_inplace_op(out, in); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::minus_inplace(Mat& out, const subview_elem1& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: mat_inplace_op(out, in); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::schur_inplace(Mat& out, const subview_elem1& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: mat_inplace_op(out, in); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: subview_elem1::div_inplace(Mat& out, const subview_elem1& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: mat_inplace_op(out, in); max@0: } max@0: max@0: max@0: max@0: //! @}