From 63393dd662512031d654641010acbb691e9b9b96 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 24 Apr 2023 17:57:22 +0200 Subject: [PATCH] MLPPVector math api rework pt3. --- mlpp/lin_alg/mlpp_vector.cpp | 204 ++++++++++++++++++++++++----------- mlpp/lin_alg/mlpp_vector.h | 29 +++-- 2 files changed, 162 insertions(+), 71 deletions(-) diff --git a/mlpp/lin_alg/mlpp_vector.cpp b/mlpp/lin_alg/mlpp_vector.cpp index 55c6dc6..3ae9c8e 100644 --- a/mlpp/lin_alg/mlpp_vector.cpp +++ b/mlpp/lin_alg/mlpp_vector.cpp @@ -558,16 +558,16 @@ void MLPPVector::cbrtb(const Ref &a) { return exponentiateb(a, static_cast(1) / static_cast(3)); } -real_t MLPPVector::dotnv(const Ref &a, const Ref &b) { - int a_size = a->size(); +real_t MLPPVector::dot(const Ref &b) { + ERR_FAIL_COND_V(!b.is_valid(), 0); - ERR_FAIL_COND_V(a_size != b->size(), 0); + ERR_FAIL_COND_V(_size != b->size(), 0); - const real_t *a_ptr = a->ptr(); + const real_t *a_ptr = ptr(); const real_t *b_ptr = b->ptr(); real_t c = 0; - for (int i = 0; i < a_size; ++i) { + for (int i = 0; i < _size; ++i) { c += a_ptr[i] * b_ptr[i]; } return c; @@ -586,26 +586,42 @@ std::vector MLPPVector::cross(std::vector a, std::vector } */ -Ref MLPPVector::absv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); +void MLPPVector::abs() { + real_t *out_ptr = ptrw(); + for (int i = 0; i < _size; ++i) { + out_ptr[i] = ABS(out_ptr[i]); + } +} +Ref MLPPVector::absn() { Ref out; out.instance(); + out->resize(_size); - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); + const real_t *a_ptr = ptr(); real_t *out_ptr = out->ptrw(); - for (int i = 0; i < size; ++i) { + for (int i = 0; i < _size; ++i) { out_ptr[i] = ABS(a_ptr[i]); } return out; } +void MLPPVector::absb(const Ref &a) { + ERR_FAIL_COND(!a.is_valid()); -Ref MLPPVector::zerovecnv(int n) { + int s = a->size(); + resize(s); + + const real_t *a_ptr = a->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < s; ++i) { + out_ptr[i] = ABS(a_ptr[i]); + } +} + +Ref MLPPVector::zero_vec(int n) { Ref vec; vec.instance(); @@ -614,7 +630,7 @@ Ref MLPPVector::zerovecnv(int n) { return vec; } -Ref MLPPVector::onevecnv(int n) { +Ref MLPPVector::one_vec(int n) { Ref vec; vec.instance(); @@ -623,7 +639,7 @@ Ref MLPPVector::onevecnv(int n) { return vec; } -Ref MLPPVector::fullnv(int n, int k) { +Ref MLPPVector::full_vec(int n, int k) { Ref vec; vec.instance(); @@ -633,83 +649,153 @@ Ref MLPPVector::fullnv(int n, int k) { return vec; } -Ref MLPPVector::sinnv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); +void MLPPVector::sin() { + real_t *out_ptr = ptrw(); + for (int i = 0; i < _size; ++i) { + out_ptr[i] = Math::sin(out_ptr[i]); + } +} +Ref MLPPVector::sinn() { Ref out; out.instance(); + out->resize(_size); - int size = a->size(); - out->resize(size); - - const real_t *a_ptr = a->ptr(); + const real_t *a_ptr = ptr(); real_t *out_ptr = out->ptrw(); - for (int i = 0; i < size; ++i) { + for (int i = 0; i < _size; ++i) { out_ptr[i] = Math::sin(a_ptr[i]); } return out; } -Ref MLPPVector::cosnv(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); +void MLPPVector::sinb(const Ref &a) { + ERR_FAIL_COND(!a.is_valid()); - Ref out; - out.instance(); - - int size = a->size(); - out->resize(size); + int s = a->size(); + resize(s); const real_t *a_ptr = a->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < s; ++i) { + out_ptr[i] = Math::sin(a_ptr[i]); + } +} + +void MLPPVector::cos() { + real_t *out_ptr = ptrw(); + + for (int i = 0; i < _size; ++i) { + out_ptr[i] = Math::sqrt(out_ptr[i]); + } +} +Ref MLPPVector::cosn() { + Ref out; + out.instance(); + out->resize(_size); + + const real_t *a_ptr = ptr(); real_t *out_ptr = out->ptrw(); - for (int i = 0; i < size; ++i) { + for (int i = 0; i < _size; ++i) { out_ptr[i] = Math::cos(a_ptr[i]); } return out; } +void MLPPVector::cosb(const Ref &a) { + ERR_FAIL_COND(!a.is_valid()); -Ref MLPPVector::maxnvv(const Ref &a, const Ref &b) { - Ref ret; - ret.instance(); + int s = a->size(); + resize(s); - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), ret); + const real_t *a_ptr = a->ptr(); + real_t *out_ptr = ptrw(); - int a_size = a->size(); + for (int i = 0; i < s; ++i) { + out_ptr[i] = Math::cos(a_ptr[i]); + } +} - ERR_FAIL_COND_V(a_size != b->size(), ret); +void MLPPVector::maxv(const Ref &b) { + ERR_FAIL_COND(!b.is_valid()); + ERR_FAIL_COND(_size != b->size()); - ret->resize(a_size); + const real_t *a_ptr = ptr(); + const real_t *b_ptr = b->ptr(); + real_t *out_ptr = ptrw(); - const real_t *aa = a->ptr(); - const real_t *ba = b->ptr(); - real_t *ret_ptr = ret->ptrw(); - - for (int i = 0; i < a_size; i++) { - real_t aa_i = aa[i]; - real_t bb_i = ba[i]; + for (int i = 0; i < _size; ++i) { + real_t aa_i = a_ptr[i]; + real_t bb_i = b_ptr[i]; if (aa_i > bb_i) { - ret_ptr[i] = aa_i; + out_ptr[i] = aa_i; } else { - ret_ptr[i] = bb_i; + out_ptr[i] = bb_i; + } + } +} +Ref MLPPVector::maxvn(const Ref &b) { + ERR_FAIL_COND_V(!b.is_valid(), Ref()); + ERR_FAIL_COND_V(_size != b->size(), Ref()); + + Ref out; + out.instance(); + out->resize(_size); + + const real_t *a_ptr = ptr(); + const real_t *b_ptr = b->ptr(); + real_t *out_ptr = out->ptrw(); + + for (int i = 0; i < _size; ++i) { + real_t aa_i = a_ptr[i]; + real_t bb_i = b_ptr[i]; + + if (aa_i > bb_i) { + out_ptr[i] = aa_i; + } else { + out_ptr[i] = bb_i; } } - return ret; + return out; +} +void MLPPVector::maxvb(const Ref &a, const Ref &b) { + ERR_FAIL_COND(!a.is_valid() || !b.is_valid()); + + int s = a->size(); + + ERR_FAIL_COND(s != b->size()); + + if (unlikely(size() != s)) { + resize(s); + } + + const real_t *a_ptr = a->ptr(); + const real_t *b_ptr = b->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < s; ++i) { + real_t aa_i = a_ptr[i]; + real_t bb_i = b_ptr[i]; + + if (aa_i > bb_i) { + out_ptr[i] = aa_i; + } else { + out_ptr[i] = bb_i; + } + } } -real_t MLPPVector::maxvr(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), -Math_INF); - - int a_size = a->size(); - - const real_t *aa = a->ptr(); +real_t MLPPVector::max_element() { + const real_t *aa = ptr(); real_t max_element = -Math_INF; - for (int i = 0; i < a_size; i++) { + for (int i = 0; i < _size; i++) { real_t current_element = aa[i]; if (current_element > max_element) { @@ -719,16 +805,12 @@ real_t MLPPVector::maxvr(const Ref &a) { return max_element; } -real_t MLPPVector::minvr(const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Math_INF); - - int a_size = a->size(); - - const real_t *aa = a->ptr(); +real_t MLPPVector::min_element() { + const real_t *aa = ptr(); real_t min_element = Math_INF; - for (int i = 0; i < a_size; i++) { + for (int i = 0; i < _size; i++) { real_t current_element = aa[i]; if (current_element > min_element) { diff --git a/mlpp/lin_alg/mlpp_vector.h b/mlpp/lin_alg/mlpp_vector.h index 8e9643c..3309d8f 100644 --- a/mlpp/lin_alg/mlpp_vector.h +++ b/mlpp/lin_alg/mlpp_vector.h @@ -394,23 +394,32 @@ public: Ref cbrtn(); void cbrtb(const Ref &a); - real_t dotnv(const Ref &a, const Ref &b); + real_t dot(const Ref &b); //std::vector cross(std::vector a, std::vector b); - Ref absv(const Ref &a); + void abs(); + Ref absn(); + void absb(const Ref &a); - Ref zerovecnv(int n); - Ref onevecnv(int n); - Ref fullnv(int n, int k); + Ref zero_vec(int n); + Ref one_vec(int n); + Ref full_vec(int n, int k); - Ref sinnv(const Ref &a); - Ref cosnv(const Ref &a); + void sin(); + Ref sinn(); + void sinb(const Ref &a); - Ref maxnvv(const Ref &a, const Ref &b); + void cos(); + Ref cosn(); + void cosb(const Ref &a); - real_t maxvr(const Ref &a); - real_t minvr(const Ref &a); + void maxv(const Ref &b); + Ref maxvn(const Ref &b); + void maxvb(const Ref &a, const Ref &b); + + real_t max_element(); + real_t min_element(); //std::vector round(std::vector a);