From b7f453afede6cc84036f1395275e637e19ad87d7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 25 Apr 2023 10:00:24 +0200 Subject: [PATCH] MLPPMatrix math api rework pt5. --- mlpp/lin_alg/mlpp_matrix.cpp | 212 ++++++++++++++++++++++++++++------- mlpp/lin_alg/mlpp_matrix.h | 31 +++-- 2 files changed, 198 insertions(+), 45 deletions(-) diff --git a/mlpp/lin_alg/mlpp_matrix.cpp b/mlpp/lin_alg/mlpp_matrix.cpp index 3e464be..daa5151 100644 --- a/mlpp/lin_alg/mlpp_matrix.cpp +++ b/mlpp/lin_alg/mlpp_matrix.cpp @@ -618,7 +618,7 @@ void MLPPMatrix::log() { out_ptr[i] = Math::log(out_ptr[i]); } } -Ref MLPPMatrix::logn() { +Ref MLPPMatrix::logn() const { Ref out; out.instance(); out->resize(size()); @@ -653,98 +653,234 @@ void MLPPMatrix::logb(const Ref &A) { } } -Ref MLPPMatrix::log10nm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); +void MLPPMatrix::log10() { + int ds = data_size(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::log10(out_ptr[i]); + } +} +Ref MLPPMatrix::log10n() const { Ref out; out.instance(); + out->resize(size()); - int data_size = A->data_size(); - out->resize(A->size()); + int ds = data_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 < data_size; ++i) { + for (int i = 0; i < ds; ++i) { out_ptr[i] = Math::log10(a_ptr[i]); } return out; } -Ref MLPPMatrix::expnm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); +void MLPPMatrix::log10b(const Ref &A) { + ERR_FAIL_COND(!A.is_valid()); - Ref out; - out.instance(); + Size2i a_size = A->size(); - int data_size = A->data_size(); - out->resize(A->size()); + if (a_size != size()) { + resize(a_size); + } + + int ds = data_size(); const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::log10(a_ptr[i]); + } +} + +void MLPPMatrix::exp() { + int ds = data_size(); + + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::exp(out_ptr[i]); + } +} +Ref MLPPMatrix::expn() const { + Ref out; + out.instance(); + out->resize(size()); + + int ds = data_size(); + + const real_t *a_ptr = ptr(); real_t *out_ptr = out->ptrw(); - for (int i = 0; i < data_size; ++i) { + for (int i = 0; i < ds; ++i) { out_ptr[i] = Math::exp(a_ptr[i]); } return out; } -Ref MLPPMatrix::erfnm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); +void MLPPMatrix::expb(const Ref &A) { + ERR_FAIL_COND(!A.is_valid()); - Ref out; - out.instance(); + Size2i a_size = A->size(); - int data_size = A->data_size(); - out->resize(A->size()); + if (a_size != size()) { + resize(a_size); + } + + int ds = data_size(); const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::exp(a_ptr[i]); + } +} + +void MLPPMatrix::erf() { + int ds = data_size(); + + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::erf(out_ptr[i]); + } +} +Ref MLPPMatrix::erfn() const { + Ref out; + out.instance(); + out->resize(size()); + + int ds = data_size(); + + const real_t *a_ptr = ptr(); real_t *out_ptr = out->ptrw(); - for (int i = 0; i < data_size; ++i) { + for (int i = 0; i < ds; ++i) { out_ptr[i] = Math::erf(a_ptr[i]); } return out; } -Ref MLPPMatrix::exponentiatenm(const Ref &A, real_t p) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); +void MLPPMatrix::erfb(const Ref &A) { + ERR_FAIL_COND(!A.is_valid()); - Ref out; - out.instance(); + Size2i a_size = A->size(); - int data_size = A->data_size(); - out->resize(A->size()); + if (a_size != size()) { + resize(a_size); + } + + int ds = data_size(); const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::erf(a_ptr[i]); + } +} + +void MLPPMatrix::exponentiate(real_t p) { + int ds = data_size(); + + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::pow(out_ptr[i], p); + } +} +Ref MLPPMatrix::exponentiaten(real_t p) const { + Ref out; + out.instance(); + out->resize(size()); + + int ds = data_size(); + + const real_t *a_ptr = ptr(); real_t *out_ptr = out->ptrw(); - for (int i = 0; i < data_size; ++i) { + for (int i = 0; i < ds; ++i) { out_ptr[i] = Math::pow(a_ptr[i], p); } return out; } -Ref MLPPMatrix::sqrtnm(const Ref &A) { - ERR_FAIL_COND_V(!A.is_valid(), Ref()); +void MLPPMatrix::exponentiateb(const Ref &A, real_t p) { + ERR_FAIL_COND(!A.is_valid()); - Ref out; - out.instance(); + Size2i a_size = A->size(); - int data_size = A->data_size(); - out->resize(A->size()); + if (a_size != size()) { + resize(a_size); + } + + int ds = data_size(); const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::pow(a_ptr[i], p); + } +} + +void MLPPMatrix::sqrt() { + int ds = data_size(); + + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::sqrt(out_ptr[i]); + } +} +Ref MLPPMatrix::sqrtn() const { + Ref out; + out.instance(); + out->resize(size()); + + int ds = data_size(); + + const real_t *a_ptr = ptr(); real_t *out_ptr = out->ptrw(); - for (int i = 0; i < data_size; ++i) { + for (int i = 0; i < ds; ++i) { out_ptr[i] = Math::sqrt(a_ptr[i]); } return out; } -Ref MLPPMatrix::cbrtnm(const Ref &A) { - return exponentiatenm(A, real_t(1) / real_t(3)); +void MLPPMatrix::sqrtb(const Ref &A) { + ERR_FAIL_COND(!A.is_valid()); + + Size2i a_size = A->size(); + + if (a_size != size()) { + resize(a_size); + } + + int ds = data_size(); + + const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); + + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::sqrt(a_ptr[i]); + } +} + +void MLPPMatrix::cbrt() { + exponentiate(real_t(1) / real_t(3)); +} +Ref MLPPMatrix::cbrtn() const { + return exponentiaten(real_t(1) / real_t(3)); +} +void MLPPMatrix::cbrtb(const Ref &A) { + exponentiateb(A, real_t(1) / real_t(3)); } /* @@ -1227,7 +1363,7 @@ MLPPMatrix::SVDResult MLPPMatrix::svd(const Ref &A) { EigenResult left_eigen = eigen(A->multn(A->transposen())); EigenResult right_eigen = eigen(A->transposen()->multn(A)); - Ref singularvals = sqrtnm(left_eigen.eigen_values); + Ref singularvals = left_eigen.eigen_values->sqrtn(); Ref sigma = zeromatnm(a_size.y, a_size.x); Size2i singularvals_size = singularvals->size(); diff --git a/mlpp/lin_alg/mlpp_matrix.h b/mlpp/lin_alg/mlpp_matrix.h index ca7901f..372a231 100644 --- a/mlpp/lin_alg/mlpp_matrix.h +++ b/mlpp/lin_alg/mlpp_matrix.h @@ -626,15 +626,32 @@ public: void scalar_addb(const real_t scalar, const Ref &A); void log(); - Ref logn(); + Ref logn() const; void logb(const Ref &A); - Ref log10nm(const Ref &A); - Ref expnm(const Ref &A); - Ref erfnm(const Ref &A); - Ref exponentiatenm(const Ref &A, real_t p); - Ref sqrtnm(const Ref &A); - Ref cbrtnm(const Ref &A); + void log10(); + Ref log10n() const; + void log10b(const Ref &A); + + void exp(); + Ref expn() const; + void expb(const Ref &A); + + void erf(); + Ref erfn() const; + void erfb(const Ref &A); + + void exponentiate(real_t p); + Ref exponentiaten(real_t p) const; + void exponentiateb(const Ref &A, real_t p); + + void sqrt(); + Ref sqrtn() const; + void sqrtb(const Ref &A); + + void cbrt(); + Ref cbrtn() const; + void cbrtb(const Ref &A); //std::vector> matrixPower(std::vector> A, int n);