Chris@49: // Copyright (C) 2010-2013 NICTA (www.nicta.com.au) Chris@49: // Copyright (C) 2010-2013 Conrad Sanderson Chris@49: // Copyright (C) 2010 Dimitrios Bouzas Chris@49: // Chris@49: // This Source Code Form is subject to the terms of the Mozilla Public Chris@49: // License, v. 2.0. If a copy of the MPL was not distributed with this Chris@49: // file, You can obtain one at http://mozilla.org/MPL/2.0/. Chris@49: Chris@49: Chris@49: Chris@49: //! \addtogroup op_find Chris@49: //! @{ Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: uword Chris@49: op_find::helper Chris@49: ( Chris@49: Mat& indices, Chris@49: const Base& X Chris@49: ) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename T1::elem_type eT; Chris@49: Chris@49: const Proxy A(X.get_ref()); Chris@49: Chris@49: const uword n_elem = A.get_n_elem(); Chris@49: Chris@49: indices.set_size(n_elem, 1); Chris@49: Chris@49: uword* indices_mem = indices.memptr(); Chris@49: uword n_nz = 0; Chris@49: Chris@49: if(Proxy::prefer_at_accessor == false) Chris@49: { Chris@49: typename Proxy::ea_type PA = A.get_ea(); Chris@49: Chris@49: for(uword i=0; i Chris@49: inline Chris@49: uword Chris@49: op_find::helper Chris@49: ( Chris@49: Mat& indices, Chris@49: const mtOp& X, Chris@49: const typename arma_op_rel_only::result junk1, Chris@49: const typename arma_not_cx::result junk2 Chris@49: ) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: arma_ignore(junk1); Chris@49: arma_ignore(junk2); Chris@49: Chris@49: typedef typename T1::elem_type eT; Chris@49: Chris@49: const eT val = X.aux; Chris@49: Chris@49: const Proxy A(X.m); Chris@49: Chris@49: const uword n_elem = A.get_n_elem(); Chris@49: Chris@49: indices.set_size(n_elem, 1); Chris@49: Chris@49: uword* indices_mem = indices.memptr(); Chris@49: uword n_nz = 0; Chris@49: Chris@49: if(Proxy::prefer_at_accessor == false) Chris@49: { Chris@49: typename Proxy::ea_type PA = A.get_ea(); Chris@49: Chris@49: uword i,j; Chris@49: for(i=0, j=1; j < n_elem; i+=2, j+=2) Chris@49: { Chris@49: const eT tpi = PA[i]; Chris@49: const eT tpj = PA[j]; Chris@49: Chris@49: bool not_zero_i; Chris@49: bool not_zero_j; Chris@49: Chris@49: if(is_same_type::value == true) { not_zero_i = (val < tpi); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (tpi < val); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (val > tpi); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (tpi > val); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (val <= tpi); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (tpi <= val); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (val >= tpi); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (tpi >= val); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (tpi == val); } Chris@49: else if(is_same_type::value == true) { not_zero_i = (tpi != val); } Chris@49: else not_zero_i = false; Chris@49: Chris@49: if(is_same_type::value == true) { not_zero_j = (val < tpj); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (tpj < val); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (val > tpj); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (tpj > val); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (val <= tpj); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (tpj <= val); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (val >= tpj); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (tpj >= val); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (tpj == val); } Chris@49: else if(is_same_type::value == true) { not_zero_j = (tpj != val); } Chris@49: else not_zero_j = false; Chris@49: Chris@49: if(not_zero_i == true) { indices_mem[n_nz] = i; ++n_nz; } Chris@49: if(not_zero_j == true) { indices_mem[n_nz] = j; ++n_nz; } Chris@49: } Chris@49: Chris@49: if(i < n_elem) Chris@49: { Chris@49: bool not_zero; Chris@49: Chris@49: const eT tmp = PA[i]; Chris@49: Chris@49: if(is_same_type::value == true) { not_zero = (val < tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp < val); } Chris@49: else if(is_same_type::value == true) { not_zero = (val > tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp > val); } Chris@49: else if(is_same_type::value == true) { not_zero = (val <= tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp <= val); } Chris@49: else if(is_same_type::value == true) { not_zero = (val >= tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp >= val); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp == val); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp != val); } Chris@49: else not_zero = false; Chris@49: Chris@49: if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; } Chris@49: } Chris@49: } Chris@49: else Chris@49: { Chris@49: const uword n_rows = A.get_n_rows(); Chris@49: const uword n_cols = A.get_n_cols(); Chris@49: Chris@49: uword i = 0; Chris@49: Chris@49: for(uword col=0; col < n_cols; ++col) Chris@49: for(uword row=0; row < n_rows; ++row) Chris@49: { Chris@49: const eT tmp = A.at(row,col); Chris@49: Chris@49: bool not_zero; Chris@49: Chris@49: if(is_same_type::value == true) { not_zero = (val < tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp < val); } Chris@49: else if(is_same_type::value == true) { not_zero = (val > tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp > val); } Chris@49: else if(is_same_type::value == true) { not_zero = (val <= tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp <= val); } Chris@49: else if(is_same_type::value == true) { not_zero = (val >= tmp); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp >= val); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp == val); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp != val); } Chris@49: else not_zero = false; Chris@49: Chris@49: if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; } Chris@49: Chris@49: ++i; Chris@49: } Chris@49: } Chris@49: Chris@49: return n_nz; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: uword Chris@49: op_find::helper Chris@49: ( Chris@49: Mat& indices, Chris@49: const mtOp& X, Chris@49: const typename arma_op_rel_only::result junk1, Chris@49: const typename arma_cx_only::result junk2 Chris@49: ) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: arma_ignore(junk1); Chris@49: arma_ignore(junk2); Chris@49: Chris@49: typedef typename T1::elem_type eT; Chris@49: typedef typename Proxy::ea_type ea_type; Chris@49: Chris@49: const eT val = X.aux; Chris@49: Chris@49: const Proxy A(X.m); Chris@49: Chris@49: ea_type PA = A.get_ea(); Chris@49: const uword n_elem = A.get_n_elem(); Chris@49: Chris@49: indices.set_size(n_elem, 1); Chris@49: Chris@49: uword* indices_mem = indices.memptr(); Chris@49: uword n_nz = 0; Chris@49: Chris@49: for(uword i=0; i::value == true) { not_zero = (tmp == val); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp != val); } Chris@49: else not_zero = false; Chris@49: Chris@49: if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; } Chris@49: } Chris@49: Chris@49: return n_nz; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: uword Chris@49: op_find::helper Chris@49: ( Chris@49: Mat& indices, Chris@49: const mtGlue& X, Chris@49: const typename arma_glue_rel_only::result junk1, Chris@49: const typename arma_not_cx::result junk2, Chris@49: const typename arma_not_cx::result junk3 Chris@49: ) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: arma_ignore(junk1); Chris@49: arma_ignore(junk2); Chris@49: arma_ignore(junk3); Chris@49: Chris@49: typedef typename T1::elem_type eT1; Chris@49: typedef typename T2::elem_type eT2; Chris@49: Chris@49: typedef typename Proxy::ea_type ea_type1; Chris@49: typedef typename Proxy::ea_type ea_type2; Chris@49: Chris@49: const Proxy A(X.A); Chris@49: const Proxy B(X.B); Chris@49: Chris@49: arma_debug_assert_same_size(A, B, "relational operator"); Chris@49: Chris@49: ea_type1 PA = A.get_ea(); Chris@49: ea_type2 PB = B.get_ea(); Chris@49: Chris@49: const uword n_elem = B.get_n_elem(); Chris@49: Chris@49: indices.set_size(n_elem, 1); Chris@49: Chris@49: uword* indices_mem = indices.memptr(); Chris@49: uword n_nz = 0; Chris@49: Chris@49: for(uword i=0; i::value == true) { not_zero = (tmp1 < tmp2); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp1 > tmp2); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp1 <= tmp2); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp1 >= tmp2); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp1 == tmp2); } Chris@49: else if(is_same_type::value == true) { not_zero = (tmp1 != tmp2); } Chris@49: else not_zero = false; Chris@49: Chris@49: if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; } Chris@49: } Chris@49: Chris@49: return n_nz; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: uword Chris@49: op_find::helper Chris@49: ( Chris@49: Mat& indices, Chris@49: const mtGlue& X, Chris@49: const typename arma_glue_rel_only::result junk1, Chris@49: const typename arma_cx_only::result junk2, Chris@49: const typename arma_cx_only::result junk3 Chris@49: ) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: arma_ignore(junk1); Chris@49: arma_ignore(junk2); Chris@49: arma_ignore(junk3); Chris@49: Chris@49: typedef typename Proxy::ea_type ea_type1; Chris@49: typedef typename Proxy::ea_type ea_type2; Chris@49: Chris@49: const Proxy A(X.A); Chris@49: const Proxy B(X.B); Chris@49: Chris@49: arma_debug_assert_same_size(A, B, "relational operator"); Chris@49: Chris@49: ea_type1 PA = A.get_ea(); Chris@49: ea_type2 PB = B.get_ea(); Chris@49: Chris@49: const uword n_elem = B.get_n_elem(); Chris@49: Chris@49: indices.set_size(n_elem, 1); Chris@49: Chris@49: uword* indices_mem = indices.memptr(); Chris@49: uword n_nz = 0; Chris@49: Chris@49: for(uword i=0; i::value == true) { not_zero = (PA[i] == PB[i]); } Chris@49: else if(is_same_type::value == true) { not_zero = (PA[i] != PB[i]); } Chris@49: else not_zero = false; Chris@49: Chris@49: if(not_zero == true) { indices_mem[n_nz] = i; ++n_nz; } Chris@49: } Chris@49: Chris@49: return n_nz; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: op_find::apply(Mat& out, const mtOp& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const uword k = X.aux_uword_a; Chris@49: const uword type = X.aux_uword_b; Chris@49: Chris@49: Mat indices; Chris@49: const uword n_nz = op_find::helper(indices, X.m); Chris@49: Chris@49: if(n_nz > 0) Chris@49: { Chris@49: if(type == 0) // "first" Chris@49: { Chris@49: out = (k > 0 && k <= n_nz) ? indices.rows(0, k-1 ) : indices.rows(0, n_nz-1); Chris@49: } Chris@49: else // "last" Chris@49: { Chris@49: out = (k > 0 && k <= n_nz) ? indices.rows(n_nz-k, n_nz-1) : indices.rows(0, n_nz-1); Chris@49: } Chris@49: } Chris@49: else Chris@49: { Chris@49: out.set_size(0,1); // empty column vector Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! @}