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/op_min_meat.hpp | 1325 ++++++++++++++++++++ 1 file changed, 1325 insertions(+) create mode 100644 src/armadillo/include/armadillo_bits/op_min_meat.hpp (limited to 'src/armadillo/include/armadillo_bits/op_min_meat.hpp') diff --git a/src/armadillo/include/armadillo_bits/op_min_meat.hpp b/src/armadillo/include/armadillo_bits/op_min_meat.hpp new file mode 100644 index 0000000..9879185 --- /dev/null +++ b/src/armadillo/include/armadillo_bits/op_min_meat.hpp @@ -0,0 +1,1325 @@ +// 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 op_min +//! @{ + + + +template +inline +void +op_min::apply(Mat& out, const Op& in) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const uword dim = in.aux_uword_a; + arma_debug_check( (dim > 1), "min(): parameter 'dim' must be 0 or 1" ); + + const quasi_unwrap U(in.m); + const Mat& X = U.M; + + if(U.is_alias(out) == false) + { + op_min::apply_noalias(out, X, dim); + } + else + { + Mat tmp; + + op_min::apply_noalias(tmp, X, dim); + + out.steal_mem(tmp); + } + } + + + +template +inline +void +op_min::apply_noalias(Mat& out, const Mat& X, const uword dim, const typename arma_not_cx::result* junk) + { + arma_extra_debug_sigprint(); + arma_ignore(junk); + + const uword X_n_rows = X.n_rows; + const uword X_n_cols = X.n_cols; + + if(dim == 0) + { + arma_extra_debug_print("op_min::apply(): dim = 0"); + + out.set_size((X_n_rows > 0) ? 1 : 0, X_n_cols); + + if(X_n_rows == 0) { return; } + + eT* out_mem = out.memptr(); + + for(uword col=0; col 0) ? 1 : 0); + + if(X_n_cols == 0) { return; } + + eT* out_mem = out.memptr(); + + arrayops::copy(out_mem, X.colptr(0), X_n_rows); + + for(uword col=1; col +inline +void +op_min::apply_noalias(Mat& out, const Mat& X, const uword dim, const typename arma_cx_only::result* junk) + { + arma_extra_debug_sigprint(); + arma_ignore(junk); + + const uword X_n_rows = X.n_rows; + const uword X_n_cols = X.n_cols; + + if(dim == 0) + { + arma_extra_debug_print("op_min::apply(): dim = 0"); + + out.set_size((X_n_rows > 0) ? 1 : 0, X_n_cols); + + if(X_n_rows == 0) { return; } + + eT* out_mem = out.memptr(); + + for(uword col=0; col 0) ? 1 : 0); + + if(X_n_cols == 0) { return; } + + eT* out_mem = out.memptr(); + + for(uword row=0; row +inline +void +op_min::apply(Cube& out, const OpCube& in) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const uword dim = in.aux_uword_a; + arma_debug_check( (dim > 2), "min(): parameter 'dim' must be 0 or 1 or 2" ); + + const unwrap_cube U(in.m); + + if(U.is_alias(out) == false) + { + op_min::apply_noalias(out, U.M, dim); + } + else + { + Cube tmp; + + op_min::apply_noalias(tmp, U.M, dim); + + out.steal_mem(tmp); + } + } + + + +template +inline +void +op_min::apply_noalias(Cube& out, const Cube& X, const uword dim, const typename arma_not_cx::result* junk) + { + arma_extra_debug_sigprint(); + arma_ignore(junk); + + const uword X_n_rows = X.n_rows; + const uword X_n_cols = X.n_cols; + const uword X_n_slices = X.n_slices; + + if(dim == 0) + { + arma_extra_debug_print("op_min::apply(): dim = 0"); + + out.set_size((X_n_rows > 0) ? 1 : 0, X_n_cols, X_n_slices); + + if(X_n_rows == 0) { return; } + + for(uword slice=0; slice < X_n_slices; ++slice) + { + eT* out_mem = out.slice_memptr(slice); + + for(uword col=0; col < X_n_cols; ++col) + { + out_mem[col] = op_min::direct_min( X.slice_colptr(slice,col), X_n_rows ); + } + } + } + else + if(dim == 1) + { + arma_extra_debug_print("op_min::apply(): dim = 1"); + + out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0, X_n_slices); + + if(X_n_cols == 0) { return; } + + for(uword slice=0; slice < X_n_slices; ++slice) + { + eT* out_mem = out.slice_memptr(slice); + + arrayops::copy(out_mem, X.slice_colptr(slice,0), X_n_rows); + + for(uword col=1; col < X_n_cols; ++col) + { + const eT* col_mem = X.slice_colptr(slice,col); + + for(uword row=0; row < X_n_rows; ++row) + { + const eT col_val = col_mem[row]; + + if(col_val < out_mem[row]) { out_mem[row] = col_val; } + } + } + } + } + else + if(dim == 2) + { + arma_extra_debug_print("op_min::apply(): dim = 2"); + + out.set_size(X_n_rows, X_n_cols, (X_n_slices > 0) ? 1 : 0); + + if(X_n_slices == 0) { return; } + + const uword N = X.n_elem_slice; + + eT* out_mem = out.slice_memptr(0); + + arrayops::copy(out_mem, X.slice_memptr(0), N); + + for(uword slice=1; slice < X_n_slices; ++slice) + { + const eT* X_mem = X.slice_memptr(slice); + + for(uword i=0; i < N; ++i) + { + const eT val = X_mem[i]; + + if(val < out_mem[i]) { out_mem[i] = val; } + } + } + } + } + + + +template +inline +void +op_min::apply_noalias(Cube& out, const Cube& X, const uword dim, const typename arma_cx_only::result* junk) + { + arma_extra_debug_sigprint(); + arma_ignore(junk); + + const uword X_n_rows = X.n_rows; + const uword X_n_cols = X.n_cols; + const uword X_n_slices = X.n_slices; + + if(dim == 0) + { + arma_extra_debug_print("op_min::apply(): dim = 0"); + + out.set_size((X_n_rows > 0) ? 1 : 0, X_n_cols, X_n_slices); + + if(X_n_rows == 0) { return; } + + for(uword slice=0; slice < X_n_slices; ++slice) + { + eT* out_mem = out.slice_memptr(slice); + + for(uword col=0; col < X_n_cols; ++col) + { + out_mem[col] = op_min::direct_min( X.slice_colptr(slice,col), X_n_rows ); + } + } + } + else + if(dim == 1) + { + arma_extra_debug_print("op_min::apply(): dim = 1"); + + out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0, X_n_slices); + + if(X_n_cols == 0) { return; } + + for(uword slice=0; slice < X_n_slices; ++slice) + { + eT* out_mem = out.slice_memptr(slice); + + const Mat tmp('j', X.slice_memptr(slice), X_n_rows, X_n_cols); + + for(uword row=0; row < X_n_rows; ++row) + { + out_mem[row] = op_min::direct_min(tmp, row); + } + } + } + else + if(dim == 2) + { + arma_extra_debug_print("op_min::apply(): dim = 2"); + + out.set_size(X_n_rows, X_n_cols, (X_n_slices > 0) ? 1 : 0); + + if(X_n_slices == 0) { return; } + + const uword N = X.n_elem_slice; + + eT* out_mem = out.slice_memptr(0); + + arrayops::copy(out_mem, X.slice_memptr(0), N); + + for(uword slice=1; slice < X_n_slices; ++slice) + { + const eT* X_mem = X.slice_memptr(slice); + + for(uword i=0; i < N; ++i) + { + const eT& val = X_mem[i]; + + if(std::abs(val) < std::abs(out_mem[i])) { out_mem[i] = val; } + } + } + } + } + + + +template +inline +eT +op_min::direct_min(const eT* const X, const uword n_elem) + { + arma_extra_debug_sigprint(); + + eT min_val_i = priv::most_pos(); + eT min_val_j = priv::most_pos(); + + uword i,j; + for(i=0, j=1; j +inline +eT +op_min::direct_min(const eT* const X, const uword n_elem, uword& index_of_min_val) + { + arma_extra_debug_sigprint(); + + eT min_val_i = priv::most_pos(); + eT min_val_j = priv::most_pos(); + + uword best_index_i = 0; + uword best_index_j = 0; + + uword i,j; + for(i=0, j=1; j +inline +eT +op_min::direct_min(const Mat& X, const uword row) + { + arma_extra_debug_sigprint(); + + const uword X_n_cols = X.n_cols; + + eT min_val_i = priv::most_pos(); + eT min_val_j = priv::most_pos(); + + uword i,j; + for(i=0, j=1; j < X_n_cols; i+=2, j+=2) + { + const eT tmp_i = X.at(row,i); + const eT tmp_j = X.at(row,j); + + if(tmp_i < min_val_i) { min_val_i = tmp_i; } + if(tmp_j < min_val_j) { min_val_j = tmp_j; } + } + + if(i < X_n_cols) + { + const eT tmp_i = X.at(row,i); + + if(tmp_i < min_val_i) { min_val_i = tmp_i; } + } + + return (min_val_i < min_val_j) ? min_val_i : min_val_j; + } + + + +template +inline +eT +op_min::min(const subview& X) + { + arma_extra_debug_sigprint(); + + if(X.n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + const uword X_n_rows = X.n_rows; + const uword X_n_cols = X.n_cols; + + if(X_n_rows == 1) + { + eT min_val_i = priv::most_pos(); + eT min_val_j = priv::most_pos(); + + const Mat& A = X.m; + + const uword start_row = X.aux_row1; + const uword start_col = X.aux_col1; + + const uword end_col_p1 = start_col + X_n_cols; + + uword i,j; + for(i=start_col, j=start_col+1; j < end_col_p1; i+=2, j+=2) + { + const eT tmp_i = A.at(start_row, i); + const eT tmp_j = A.at(start_row, j); + + if(tmp_i < min_val_i) { min_val_i = tmp_i; } + if(tmp_j < min_val_j) { min_val_j = tmp_j; } + } + + if(i < end_col_p1) + { + const eT tmp_i = A.at(start_row, i); + + if(tmp_i < min_val_i) { min_val_i = tmp_i; } + } + + return (min_val_i < min_val_j) ? min_val_i : min_val_j; + } + + eT min_val = priv::most_pos(); + + for(uword col=0; col < X_n_cols; ++col) + { + min_val = (std::min)(min_val, op_min::direct_min(X.colptr(col), X_n_rows)); + } + + return min_val; + } + + + +template +inline +typename arma_not_cx::result +op_min::min(const Base& X) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const Proxy P(X.get_ref()); + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + eT min_val_i = priv::most_pos(); + eT min_val_j = priv::most_pos(); + + if(Proxy::use_at == false) + { + typedef typename Proxy::ea_type ea_type; + + ea_type A = P.get_ea(); + + uword i,j; + + for(i=0, j=1; j +inline +typename arma_not_cx::result +op_min::min(const BaseCube& X) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const ProxyCube P(X.get_ref()); + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + eT min_val = priv::most_pos(); + + if(ProxyCube::use_at == false) + { + eT min_val_i = priv::most_pos(); + eT min_val_j = priv::most_pos(); + + typedef typename ProxyCube::ea_type ea_type; + + ea_type A = P.get_ea(); + + uword i,j; + + for(i=0, j=1; j +inline +typename arma_not_cx::result +op_min::min_with_index(const Proxy& P, uword& index_of_min_val) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + eT best_val = priv::most_pos(); + uword best_index = 0; + + if(Proxy::use_at == false) + { + typedef typename Proxy::ea_type ea_type; + + ea_type A = P.get_ea(); + + for(uword i=0; i +inline +typename arma_not_cx::result +op_min::min_with_index(const ProxyCube& P, uword& index_of_min_val) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + eT best_val = priv::most_pos(); + uword best_index = 0; + + if(ProxyCube::use_at == false) + { + typedef typename ProxyCube::ea_type ea_type; + + ea_type A = P.get_ea(); + + for(uword i=0; i < n_elem; ++i) + { + const eT tmp = A[i]; + + if(tmp < best_val) { best_val = tmp; best_index = i; } + } + } + else + { + const uword n_rows = P.get_n_rows(); + const uword n_cols = P.get_n_cols(); + const uword n_slices = P.get_n_slices(); + + uword count = 0; + + for(uword slice=0; slice < n_slices; ++slice) + for(uword col=0; col < n_cols; ++col ) + for(uword row=0; row < n_rows; ++row ) + { + const eT tmp = P.at(row,col,slice); + + if(tmp < best_val) { best_val = tmp; best_index = count; } + + ++count; + } + } + + index_of_min_val = best_index; + + return best_val; + } + + + +template +inline +std::complex +op_min::direct_min(const std::complex* const X, const uword n_elem) + { + arma_extra_debug_sigprint(); + + uword index = 0; + T min_val = priv::most_pos(); + + for(uword i=0; i +inline +std::complex +op_min::direct_min(const std::complex* const X, const uword n_elem, uword& index_of_min_val) + { + arma_extra_debug_sigprint(); + + uword index = 0; + T min_val = priv::most_pos(); + + for(uword i=0; i +inline +std::complex +op_min::direct_min(const Mat< std::complex >& X, const uword row) + { + arma_extra_debug_sigprint(); + + const uword X_n_cols = X.n_cols; + + uword index = 0; + T min_val = priv::most_pos(); + + for(uword col=0; col +inline +std::complex +op_min::min(const subview< std::complex >& X) + { + arma_extra_debug_sigprint(); + + typedef typename std::complex eT; + + if(X.n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + const Mat& A = X.m; + + const uword X_n_rows = X.n_rows; + const uword X_n_cols = X.n_cols; + + const uword start_row = X.aux_row1; + const uword start_col = X.aux_col1; + + const uword end_row_p1 = start_row + X_n_rows; + const uword end_col_p1 = start_col + X_n_cols; + + T min_val = priv::most_pos(); + + uword best_row = 0; + uword best_col = 0; + + if(X_n_rows == 1) + { + best_col = 0; + + for(uword col=start_col; col < end_col_p1; ++col) + { + const T tmp_val = std::abs( A.at(start_row, col) ); + + if(tmp_val < min_val) + { + min_val = tmp_val; + best_col = col; + } + } + + best_row = start_row; + } + else + { + for(uword col=start_col; col < end_col_p1; ++col) + for(uword row=start_row; row < end_row_p1; ++row) + { + const T tmp_val = std::abs( A.at(row, col) ); + + if(tmp_val < min_val) + { + min_val = tmp_val; + best_row = row; + best_col = col; + } + } + } + + return A.at(best_row, best_col); + } + + + +template +inline +typename arma_cx_only::result +op_min::min(const Base& X) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + typedef typename get_pod_type::result T; + + const Proxy P(X.get_ref()); + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + T min_val = priv::most_pos(); + + if(Proxy::use_at == false) + { + typedef typename Proxy::ea_type ea_type; + + ea_type A = P.get_ea(); + + uword index = 0; + + for(uword i=0; i +inline +typename arma_cx_only::result +op_min::min(const BaseCube& X) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + typedef typename get_pod_type::result T; + + const ProxyCube P(X.get_ref()); + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + T min_val = priv::most_pos(); + + if(ProxyCube::use_at == false) + { + typedef typename ProxyCube::ea_type ea_type; + + ea_type A = P.get_ea(); + + uword index = 0; + + for(uword i=0; i +inline +typename arma_cx_only::result +op_min::min_with_index(const Proxy& P, uword& index_of_min_val) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + typedef typename get_pod_type::result T; + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + T best_val = priv::most_pos(); + + if(Proxy::use_at == false) + { + typedef typename Proxy::ea_type ea_type; + + ea_type A = P.get_ea(); + + uword best_index = 0; + + for(uword i=0; i +inline +typename arma_cx_only::result +op_min::min_with_index(const ProxyCube& P, uword& index_of_min_val) + { + arma_extra_debug_sigprint(); + + typedef typename T1::elem_type eT; + typedef typename get_pod_type::result T; + + const uword n_elem = P.get_n_elem(); + + if(n_elem == 0) + { + arma_debug_check(true, "min(): object has no elements"); + + return Datum::nan; + } + + T best_val = priv::most_pos(); + + if(ProxyCube::use_at == false) + { + typedef typename ProxyCube::ea_type ea_type; + + ea_type A = P.get_ea(); + + uword best_index = 0; + + for(uword i=0; i < n_elem; ++i) + { + const T tmp = std::abs(A[i]); + + if(tmp < best_val) { best_val = tmp; best_index = i; } + } + + index_of_min_val = best_index; + + return( A[best_index] ); + } + else + { + const uword n_rows = P.get_n_rows(); + const uword n_cols = P.get_n_cols(); + const uword n_slices = P.get_n_slices(); + + eT best_val_orig = eT(0); + uword best_index = 0; + uword count = 0; + + for(uword slice=0; slice < n_slices; ++slice) + for(uword col=0; col < n_cols; ++col ) + for(uword row=0; row < n_rows; ++row ) + { + const eT tmp_orig = P.at(row,col,slice); + const T tmp = std::abs(tmp_orig); + + if(tmp < best_val) + { + best_val = tmp; + best_val_orig = tmp_orig; + best_index = count; + } + + ++count; + } + + index_of_min_val = best_index; + + return best_val_orig; + } + } + + + +//! @} -- cgit v1.2.1