From 9df031e10cbc842963f02bfc31f2f864a8ed1bab Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 25 Apr 2023 13:47:18 +0200 Subject: [PATCH] Simplified element_wise_division in MLPPMatrix. --- mlpp/lin_alg/mlpp_matrix.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/mlpp/lin_alg/mlpp_matrix.cpp b/mlpp/lin_alg/mlpp_matrix.cpp index d0fab0c..61fbc29 100644 --- a/mlpp/lin_alg/mlpp_matrix.cpp +++ b/mlpp/lin_alg/mlpp_matrix.cpp @@ -439,20 +439,21 @@ void MLPPMatrix::element_wise_division(const Ref &B) { ERR_FAIL_COND(!B.is_valid()); ERR_FAIL_COND(_size != B->size()); + int ds = data_size(); + const real_t *b_ptr = B->ptr(); real_t *c_ptr = ptrw(); - for (int i = 0; i < _size.y; i++) { - for (int j = 0; j < _size.x; j++) { - int ind_i_j = calculate_index(i, j); - c_ptr[ind_i_j] /= b_ptr[ind_i_j]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] /= b_ptr[i]; } } Ref MLPPMatrix::element_wise_divisionn(const Ref &B) const { ERR_FAIL_COND_V(!B.is_valid(), Ref()); ERR_FAIL_COND_V(_size != B->size(), Ref()); + int ds = data_size(); + Ref C; C.instance(); C->resize(_size); @@ -461,11 +462,8 @@ Ref MLPPMatrix::element_wise_divisionn(const Ref &B) con const real_t *b_ptr = B->ptr(); real_t *c_ptr = C->ptrw(); - for (int i = 0; i < _size.y; i++) { - for (int j = 0; j < _size.x; j++) { - int ind_i_j = calculate_index(i, j); - c_ptr[ind_i_j] = a_ptr[ind_i_j] / b_ptr[ind_i_j]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] = a_ptr[i] / b_ptr[i]; } return C; @@ -479,15 +477,14 @@ void MLPPMatrix::element_wise_divisionb(const Ref &A, const Refptr(); const real_t *b_ptr = B->ptr(); real_t *c_ptr = ptrw(); - for (int i = 0; i < a_size.y; i++) { - for (int j = 0; j < a_size.x; j++) { - int ind_i_j = A->calculate_index(i, j); - c_ptr[ind_i_j] = a_ptr[ind_i_j] / b_ptr[ind_i_j]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] = a_ptr[i] / b_ptr[i]; } }