From a676522ba5e525a48a74d52cab175bfa8ee7b47c Mon Sep 17 00:00:00 2001 From: conrad Date: Fri, 22 Nov 2024 12:26:10 +1000 Subject: [PATCH] ensure the first index of extremum value is found --- include/armadillo_bits/op_max_meat.hpp | 35 +++++++++++--------------- include/armadillo_bits/op_min_meat.hpp | 35 +++++++++++--------------- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/include/armadillo_bits/op_max_meat.hpp b/include/armadillo_bits/op_max_meat.hpp index 5e2b39a4..185ce7f7 100644 --- a/include/armadillo_bits/op_max_meat.hpp +++ b/include/armadillo_bits/op_max_meat.hpp @@ -393,32 +393,19 @@ op_max::direct_max(const eT* const X, const uword n_elem, uword& index_of_max_va { arma_debug_sigprint(); - eT max_val_i = priv::most_neg(); - eT max_val_j = priv::most_neg(); + eT best_val = priv::most_neg(); + uword best_index = 0; - uword best_index_i = 0; - uword best_index_j = 0; - - uword i,j; - for(i=0, j=1; j max_val_i) { max_val_i = X_i; best_index_i = i; } - if(X_j > max_val_j) { max_val_j = X_j; best_index_j = j; } + if(val > best_val) { best_val = val; best_index = i; } } - if(i < n_elem) - { - const eT X_i = X[i]; - - if(X_i > max_val_i) { max_val_i = X_i; best_index_i = i; } - } + index_of_max_val = best_index; - index_of_max_val = (max_val_i > max_val_j) ? best_index_i : best_index_j; - - return (max_val_i > max_val_j) ? max_val_i : max_val_j; + return best_val; } @@ -705,6 +692,8 @@ op_max::max_with_index(const Proxy& P, uword& index_of_max_val) { arma_conform_check(true, "max(): object has no elements"); + index_of_max_val = 0; + return Datum::nan; } @@ -786,6 +775,8 @@ op_max::max_with_index(const ProxyCube& P, uword& index_of_max_val) { arma_conform_check(true, "max(): object has no elements"); + index_of_max_val = 0; + return Datum::nan; } @@ -1160,6 +1151,8 @@ op_max::max_with_index(const Proxy& P, uword& index_of_max_val) { arma_conform_check(true, "max(): object has no elements"); + index_of_max_val = 0; + return Datum::nan; } @@ -1263,6 +1256,8 @@ op_max::max_with_index(const ProxyCube& P, uword& index_of_max_val) { arma_conform_check(true, "max(): object has no elements"); + index_of_max_val = 0; + return Datum::nan; } diff --git a/include/armadillo_bits/op_min_meat.hpp b/include/armadillo_bits/op_min_meat.hpp index 8fa6e74f..07a4337f 100644 --- a/include/armadillo_bits/op_min_meat.hpp +++ b/include/armadillo_bits/op_min_meat.hpp @@ -393,32 +393,19 @@ op_min::direct_min(const eT* const X, const uword n_elem, uword& index_of_min_va { arma_debug_sigprint(); - eT min_val_i = priv::most_pos(); - eT min_val_j = priv::most_pos(); + eT best_val = priv::most_pos(); + uword best_index = 0; - uword best_index_i = 0; - uword best_index_j = 0; - - uword i,j; - for(i=0, j=1; j& P, uword& index_of_min_val) { arma_conform_check(true, "min(): object has no elements"); + index_of_min_val = 0; + return Datum::nan; } @@ -786,6 +775,8 @@ op_min::min_with_index(const ProxyCube& P, uword& index_of_min_val) { arma_conform_check(true, "min(): object has no elements"); + index_of_min_val = 0; + return Datum::nan; } @@ -1160,6 +1151,8 @@ op_min::min_with_index(const Proxy& P, uword& index_of_min_val) { arma_conform_check(true, "min(): object has no elements"); + index_of_min_val = 0; + return Datum::nan; } @@ -1263,6 +1256,8 @@ op_min::min_with_index(const ProxyCube& P, uword& index_of_min_val) { arma_conform_check(true, "min(): object has no elements"); + index_of_min_val = 0; + return Datum::nan; }