max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-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 diagview max@0: //! @{ max@0: max@0: max@0: template max@0: inline max@0: diagview::~diagview() max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: template max@0: arma_inline max@0: diagview::diagview(const Mat& in_m, const uword in_row_offset, const uword in_col_offset, const uword in_len) max@0: : m(in_m) max@0: , m_ptr(0) max@0: , row_offset(in_row_offset) max@0: , col_offset(in_col_offset) max@0: , n_rows(in_len) max@0: , n_elem(in_len) 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: diagview::diagview(Mat& in_m, const uword in_row_offset, const uword in_col_offset, const uword in_len) max@0: : m(in_m) max@0: , m_ptr(&in_m) max@0: , row_offset(in_row_offset) max@0: , col_offset(in_col_offset) max@0: , n_rows(in_len) max@0: , n_elem(in_len) max@0: { max@0: arma_extra_debug_sigprint(); max@0: } max@0: max@0: max@0: max@0: //! set a diagonal of our matrix using a diagonal from a foreign matrix max@0: template max@0: inline max@0: void max@0: diagview::operator= (const diagview& x) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: diagview& t = *this; max@0: max@0: arma_debug_check( (t.n_elem != x.n_elem), "diagview: diagonals have incompatible lengths"); max@0: max@0: Mat& t_m = *(t.m_ptr); max@0: const Mat& x_m = x.m; max@0: max@0: if(&t_m != &x_m) max@0: { max@0: const uword t_n_elem = t.n_elem; max@0: const uword t_row_offset = t.row_offset; max@0: const uword t_col_offset = t.col_offset; max@0: max@0: const uword x_row_offset = x.row_offset; max@0: const uword x_col_offset = x.col_offset; max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < t_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = x_m.at(i + x_row_offset, i + x_col_offset); max@0: const eT tmp_j = x_m.at(j + x_row_offset, j + x_col_offset); max@0: max@0: t_m.at(i + t_row_offset, i + t_col_offset) = tmp_i; max@0: t_m.at(j + t_row_offset, j + t_col_offset) = tmp_j; max@0: } max@0: max@0: if(i < t_n_elem) max@0: { max@0: t_m.at(i + t_row_offset, i + t_col_offset) = x_m.at(i + x_row_offset, i + x_col_offset); max@0: } max@0: } max@0: else max@0: { max@0: const Mat tmp = x; max@0: max@0: (*this).operator=(tmp); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: diagview::operator+=(const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: Mat& t_m = (*m_ptr); max@0: max@0: const uword t_n_elem = n_elem; max@0: const uword t_row_offset = row_offset; max@0: const uword t_col_offset = col_offset; max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: diagview::operator-=(const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: Mat& t_m = (*m_ptr); max@0: max@0: const uword t_n_elem = n_elem; max@0: const uword t_row_offset = row_offset; max@0: const uword t_col_offset = col_offset; max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: diagview::operator*=(const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: Mat& t_m = (*m_ptr); max@0: max@0: const uword t_n_elem = n_elem; max@0: const uword t_row_offset = row_offset; max@0: const uword t_col_offset = col_offset; max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: diagview::operator/=(const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: Mat& t_m = (*m_ptr); max@0: max@0: const uword t_n_elem = n_elem; max@0: const uword t_row_offset = row_offset; max@0: const uword t_col_offset = col_offset; max@0: max@0: for(uword i=0; i max@0: template max@0: inline max@0: void max@0: diagview::operator= (const Base& o) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const unwrap tmp(o.get_ref()); max@0: const Mat& x = tmp.M; max@0: max@0: diagview& t = *this; max@0: max@0: arma_debug_check max@0: ( max@0: ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ), max@0: "diagview: given object has incompatible size" max@0: ); max@0: max@0: Mat& t_m = *(t.m_ptr); max@0: max@0: const uword t_n_elem = t.n_elem; max@0: const uword t_row_offset = t.row_offset; max@0: const uword t_col_offset = t.col_offset; max@0: max@0: const eT* x_mem = x.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < t_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = x_mem[i]; max@0: const eT tmp_j = x_mem[j]; max@0: max@0: t_m.at( i + t_row_offset, i + t_col_offset) = tmp_i; max@0: t_m.at( j + t_row_offset, j + t_col_offset) = tmp_j; max@0: } max@0: max@0: if(i < t_n_elem) max@0: { max@0: t_m.at( i + t_row_offset, i + t_col_offset) = x_mem[i]; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: diagview::operator+=(const Base& o) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const unwrap tmp(o.get_ref()); max@0: const Mat& x = tmp.M; max@0: max@0: diagview& t = *this; max@0: max@0: arma_debug_check max@0: ( max@0: ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ), max@0: "diagview: given object has incompatible size" max@0: ); max@0: max@0: Mat& t_m = *(t.m_ptr); max@0: max@0: const uword t_n_elem = t.n_elem; max@0: const uword t_row_offset = t.row_offset; max@0: const uword t_col_offset = t.col_offset; max@0: max@0: const eT* x_mem = x.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < t_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = x_mem[i]; max@0: const eT tmp_j = x_mem[j]; max@0: max@0: t_m.at( i + t_row_offset, i + t_col_offset) += tmp_i; max@0: t_m.at( j + t_row_offset, j + t_col_offset) += tmp_j; max@0: } max@0: max@0: if(i < t_n_elem) max@0: { max@0: t_m.at( i + t_row_offset, i + t_col_offset) += x_mem[i]; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: diagview::operator-=(const Base& o) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const unwrap tmp(o.get_ref()); max@0: const Mat& x = tmp.M; max@0: max@0: diagview& t = *this; max@0: max@0: arma_debug_check max@0: ( max@0: ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ), max@0: "diagview: given object has incompatible size" max@0: ); max@0: max@0: Mat& t_m = *(t.m_ptr); max@0: max@0: const uword t_n_elem = t.n_elem; max@0: const uword t_row_offset = t.row_offset; max@0: const uword t_col_offset = t.col_offset; max@0: max@0: const eT* x_mem = x.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < t_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = x_mem[i]; max@0: const eT tmp_j = x_mem[j]; max@0: max@0: t_m.at( i + t_row_offset, i + t_col_offset) -= tmp_i; max@0: t_m.at( j + t_row_offset, j + t_col_offset) -= tmp_j; max@0: } max@0: max@0: if(i < t_n_elem) max@0: { max@0: t_m.at( i + t_row_offset, i + t_col_offset) -= x_mem[i]; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: diagview::operator%=(const Base& o) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const unwrap tmp(o.get_ref()); max@0: const Mat& x = tmp.M; max@0: max@0: diagview& t = *this; max@0: max@0: arma_debug_check max@0: ( max@0: ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ), max@0: "diagview: given object has incompatible size" max@0: ); max@0: max@0: Mat& t_m = *(t.m_ptr); max@0: max@0: const uword t_n_elem = t.n_elem; max@0: const uword t_row_offset = t.row_offset; max@0: const uword t_col_offset = t.col_offset; max@0: max@0: const eT* x_mem = x.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < t_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = x_mem[i]; max@0: const eT tmp_j = x_mem[j]; max@0: max@0: t_m.at( i + t_row_offset, i + t_col_offset) *= tmp_i; max@0: t_m.at( j + t_row_offset, j + t_col_offset) *= tmp_j; max@0: } max@0: max@0: if(i < t_n_elem) max@0: { max@0: t_m.at( i + t_row_offset, i + t_col_offset) *= x_mem[i]; max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: template max@0: inline max@0: void max@0: diagview::operator/=(const Base& o) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const unwrap tmp(o.get_ref()); max@0: const Mat& x = tmp.M; max@0: max@0: diagview& t = *this; max@0: max@0: arma_debug_check max@0: ( max@0: ( (t.n_elem != x.n_elem) || (x.is_vec() == false) ), max@0: "diagview: given object has incompatible size" max@0: ); max@0: max@0: Mat& t_m = *(t.m_ptr); max@0: max@0: const uword t_n_elem = t.n_elem; max@0: const uword t_row_offset = t.row_offset; max@0: const uword t_col_offset = t.col_offset; max@0: max@0: const eT* x_mem = x.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < t_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = x_mem[i]; max@0: const eT tmp_j = x_mem[j]; max@0: max@0: t_m.at( i + t_row_offset, i + t_col_offset) /= tmp_i; max@0: t_m.at( j + t_row_offset, j + t_col_offset) /= tmp_j; max@0: } max@0: max@0: if(i < t_n_elem) max@0: { max@0: t_m.at( i + t_row_offset, i + t_col_offset) /= x_mem[i]; max@0: } max@0: } max@0: max@0: max@0: max@0: //! extract a diagonal and store it as a column vector max@0: template max@0: inline max@0: void max@0: diagview::extract(Mat& out, const diagview& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing; max@0: // size setting and alias checking is done by either the Mat contructor or operator=() max@0: max@0: const Mat& in_m = in.m; max@0: max@0: const uword in_n_elem = in.n_elem; max@0: const uword in_row_offset = in.row_offset; max@0: const uword in_col_offset = in.col_offset; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < in_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset ); max@0: const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset ); max@0: max@0: out_mem[i] = tmp_i; max@0: out_mem[j] = tmp_j; max@0: } max@0: max@0: if(i < in_n_elem) max@0: { max@0: out_mem[i] = in_m.at( i + in_row_offset, i + in_col_offset ); max@0: } max@0: } max@0: max@0: max@0: max@0: //! X += Y.diag() max@0: template max@0: inline max@0: void max@0: diagview::plus_inplace(Mat& out, const diagview& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "addition"); max@0: max@0: const Mat& in_m = in.m; max@0: max@0: const uword in_n_elem = in.n_elem; max@0: const uword in_row_offset = in.row_offset; max@0: const uword in_col_offset = in.col_offset; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < in_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset ); max@0: const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset ); max@0: max@0: out_mem[i] += tmp_i; max@0: out_mem[j] += tmp_j; max@0: } max@0: max@0: if(i < in_n_elem) max@0: { max@0: out_mem[i] += in_m.at( i + in_row_offset, i + in_col_offset ); max@0: } max@0: } max@0: max@0: max@0: max@0: //! X -= Y.diag() max@0: template max@0: inline max@0: void max@0: diagview::minus_inplace(Mat& out, const diagview& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "subtraction"); max@0: max@0: const Mat& in_m = in.m; max@0: max@0: const uword in_n_elem = in.n_elem; max@0: const uword in_row_offset = in.row_offset; max@0: const uword in_col_offset = in.col_offset; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < in_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset ); max@0: const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset ); max@0: max@0: out_mem[i] -= tmp_i; max@0: out_mem[j] -= tmp_j; max@0: } max@0: max@0: if(i < in_n_elem) max@0: { max@0: out_mem[i] -= in_m.at( i + in_row_offset, i + in_col_offset ); max@0: } max@0: } max@0: max@0: max@0: max@0: //! X %= Y.diag() max@0: template max@0: inline max@0: void max@0: diagview::schur_inplace(Mat& out, const diagview& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "element-wise multiplication"); max@0: max@0: const Mat& in_m = in.m; max@0: max@0: const uword in_n_elem = in.n_elem; max@0: const uword in_row_offset = in.row_offset; max@0: const uword in_col_offset = in.col_offset; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < in_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset ); max@0: const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset ); max@0: max@0: out_mem[i] *= tmp_i; max@0: out_mem[j] *= tmp_j; max@0: } max@0: max@0: if(i < in_n_elem) max@0: { max@0: out_mem[i] *= in_m.at( i + in_row_offset, i + in_col_offset ); max@0: } max@0: } max@0: max@0: max@0: max@0: //! X /= Y.diag() max@0: template max@0: inline max@0: void max@0: diagview::div_inplace(Mat& out, const diagview& in) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: arma_debug_assert_same_size(out.n_rows, out.n_cols, in.n_rows, in.n_cols, "element-wise division"); max@0: max@0: const Mat& in_m = in.m; max@0: max@0: const uword in_n_elem = in.n_elem; max@0: const uword in_row_offset = in.row_offset; max@0: const uword in_col_offset = in.col_offset; max@0: max@0: eT* out_mem = out.memptr(); max@0: max@0: uword i,j; max@0: for(i=0, j=1; j < in_n_elem; i+=2, j+=2) max@0: { max@0: const eT tmp_i = in_m.at( i + in_row_offset, i + in_col_offset ); max@0: const eT tmp_j = in_m.at( j + in_row_offset, j + in_col_offset ); max@0: max@0: out_mem[i] /= tmp_i; max@0: out_mem[j] /= tmp_j; max@0: } max@0: max@0: if(i < in_n_elem) max@0: { max@0: out_mem[i] /= in_m.at( i + in_row_offset, i + in_col_offset ); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT& max@0: diagview::operator[](const uword i) max@0: { max@0: return (*m_ptr).at(i+row_offset, i+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT max@0: diagview::operator[](const uword i) const max@0: { max@0: return m.at(i+row_offset, i+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT& max@0: diagview::at(const uword i) max@0: { max@0: return (*m_ptr).at(i+row_offset, i+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT max@0: diagview::at(const uword i) const max@0: { max@0: return m.at(i+row_offset, i+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT& max@0: diagview::operator()(const uword i) max@0: { max@0: arma_debug_check( (i >= n_elem), "diagview::operator(): out of bounds" ); max@0: max@0: return (*m_ptr).at(i+row_offset, i+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT max@0: diagview::operator()(const uword i) const max@0: { max@0: arma_debug_check( (i >= n_elem), "diagview::operator(): out of bounds" ); max@0: max@0: return m.at(i+row_offset, i+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT& max@0: diagview::at(const uword row, const uword col) max@0: { max@0: return (*m_ptr).at(row+row_offset, row+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT max@0: diagview::at(const uword row, const uword col) const max@0: { max@0: return m.at(row+row_offset, row+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT& max@0: diagview::operator()(const uword row, const uword col) max@0: { max@0: arma_debug_check( ((row >= n_elem) || (col > 0)), "diagview::operator(): out of bounds" ); max@0: max@0: return (*m_ptr).at(row+row_offset, row+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: arma_inline max@0: eT max@0: diagview::operator()(const uword row, const uword col) const max@0: { max@0: arma_debug_check( ((row >= n_elem) || (col > 0)), "diagview::operator(): out of bounds" ); max@0: max@0: return m.at(row+row_offset, row+col_offset); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: diagview::fill(const eT val) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: Mat& x = (*m_ptr); max@0: max@0: for(uword i=0; i max@0: inline max@0: void max@0: diagview::zeros() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: (*this).fill(eT(0)); max@0: } max@0: max@0: max@0: max@0: template max@0: inline max@0: void max@0: diagview::ones() max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: (*this).fill(eT(1)); max@0: } max@0: max@0: max@0: max@0: //! @}