From eda5bc26f44ee9a6f83dcf8c91f17296d7fc509d Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 12 Feb 2024 14:52:43 +0100 Subject: Move into version control --- .../include/armadillo_bits/glue_histc_meat.hpp | 167 +++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 src/armadillo/include/armadillo_bits/glue_histc_meat.hpp (limited to 'src/armadillo/include/armadillo_bits/glue_histc_meat.hpp') diff --git a/src/armadillo/include/armadillo_bits/glue_histc_meat.hpp b/src/armadillo/include/armadillo_bits/glue_histc_meat.hpp new file mode 100644 index 0000000..6e79175 --- /dev/null +++ b/src/armadillo/include/armadillo_bits/glue_histc_meat.hpp @@ -0,0 +1,167 @@ +// 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 glue_histc +//! @{ + + +template +inline +void +glue_histc::apply_noalias(Mat& C, const Mat& A, const Mat& B, const uword dim) + { + arma_extra_debug_sigprint(); + + arma_debug_check( ((B.is_vec() == false) && (B.is_empty() == false)), "histc(): parameter 'edges' must be a vector" ); + + const uword A_n_rows = A.n_rows; + const uword A_n_cols = A.n_cols; + + const uword B_n_elem = B.n_elem; + + if( B_n_elem == uword(0) ) { C.reset(); return; } + + arma_debug_check + ( + ((Col(const_cast(B.memptr()), B_n_elem, false, false)).is_sorted("strictascend") == false), + "hist(): given 'edges' vector does not contain monotonically increasing values" + ); + + const eT* B_mem = B.memptr(); + const uword B_n_elem_m1 = B_n_elem - 1; + + if(dim == uword(0)) + { + C.zeros(B_n_elem, A_n_cols); + + for(uword col=0; col < A_n_cols; ++col) + { + const eT* A_coldata = A.colptr(col); + uword* C_coldata = C.colptr(col); + + for(uword row=0; row < A_n_rows; ++row) + { + const eT x = A_coldata[row]; + + for(uword i=0; i < B_n_elem_m1; ++i) + { + if( (B_mem[i] <= x) && (x < B_mem[i+1]) ) { C_coldata[i]++; break; } + else if( B_mem[B_n_elem_m1] == x ) { C_coldata[B_n_elem_m1]++; break; } // for compatibility with Matlab + } + } + } + } + else + if(dim == uword(1)) + { + C.zeros(A_n_rows, B_n_elem); + + if(A.n_rows == 1) + { + const uword A_n_elem = A.n_elem; + const eT* A_mem = A.memptr(); + uword* C_mem = C.memptr(); + + for(uword j=0; j < A_n_elem; ++j) + { + const eT x = A_mem[j]; + + for(uword i=0; i < B_n_elem_m1; ++i) + { + if( (B_mem[i] <= x) && (x < B_mem[i+1]) ) { C_mem[i]++; break; } + else if( B_mem[B_n_elem_m1] == x ) { C_mem[B_n_elem_m1]++; break; } // for compatibility with Matlab + } + } + } + else + { + for(uword row=0; row < A_n_rows; ++row) + for(uword col=0; col < A_n_cols; ++col) + { + const eT x = A.at(row,col); + + for(uword i=0; i < B_n_elem_m1; ++i) + { + if( (B_mem[i] <= x) && (x < B_mem[i+1]) ) { C.at(row,i)++; break; } + else if( B_mem[B_n_elem_m1] == x ) { C.at(row,B_n_elem_m1)++; break; } // for compatibility with Matlab + } + } + } + } + } + + + +template +inline +void +glue_histc::apply(Mat& C, const mtGlue& expr) + { + arma_extra_debug_sigprint(); + + const uword dim = expr.aux_uword; + + arma_debug_check( (dim > 1), "histc(): parameter 'dim' must be 0 or 1" ); + + const quasi_unwrap UA(expr.A); + const quasi_unwrap UB(expr.B); + + if(UA.is_alias(C) || UB.is_alias(C)) + { + Mat tmp; + + glue_histc::apply_noalias(tmp, UA.M, UB.M, dim); + + C.steal_mem(tmp); + } + else + { + glue_histc::apply_noalias(C, UA.M, UB.M, dim); + } + } + + + +template +inline +void +glue_histc_default::apply(Mat& C, const mtGlue& expr) + { + arma_extra_debug_sigprint(); + + const quasi_unwrap UA(expr.A); + const quasi_unwrap UB(expr.B); + + const uword dim = (T1::is_xvec) ? uword(UA.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0); + + if(UA.is_alias(C) || UB.is_alias(C)) + { + Mat tmp; + + glue_histc::apply_noalias(tmp, UA.M, UB.M, dim); + + C.steal_mem(tmp); + } + else + { + glue_histc::apply_noalias(C, UA.M, UB.M, dim); + } + } + + +//! @} -- cgit v1.2.1