diff --git a/mlpp/lin_alg/mlpp_vector.cpp b/mlpp/lin_alg/mlpp_vector.cpp index 9dc4ad1..a0e778c 100644 --- a/mlpp/lin_alg/mlpp_vector.cpp +++ b/mlpp/lin_alg/mlpp_vector.cpp @@ -3,7 +3,6 @@ #include "mlpp_matrix.h" - void MLPPVector::flatten_vectors(const Vector> &A) { int vsize = 0; for (int i = 0; i < A.size(); ++i) { @@ -28,7 +27,6 @@ void MLPPVector::flatten_vectors(const Vector> &A) { } } - Ref MLPPVector::flatten_vectorsn(const Vector> &A) { Ref a; a.instance(); @@ -111,184 +109,231 @@ void MLPPVector::hadamard_productb(const Ref &a, const Ref MLPPVector::element_wise_divisionnv(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); +void MLPPVector::element_wise_division(const Ref &b) { + ERR_FAIL_COND(!b.is_valid()); Ref out; out.instance(); - int size = a->size(); + ERR_FAIL_COND(_size != b->size()); - ERR_FAIL_COND_V(size != b->size(), Ref()); - - out->resize(size); - - const real_t *a_ptr = a->ptr(); + 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) { + for (int i = 0; i < _size; ++i) { + out_ptr[i] = a_ptr[i] / b_ptr[i]; + } +} + +Ref MLPPVector::element_wise_divisionn(const Ref &b) { + ERR_FAIL_COND_V(!b.is_valid(), Ref()); + + Ref out; + out.instance(); + + ERR_FAIL_COND_V(_size != b->size(), Ref()); + + 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) { out_ptr[i] = a_ptr[i] / b_ptr[i]; } return out; } -Ref MLPPVector::scalar_multiplynv(real_t scalar, const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); +void MLPPVector::element_wise_divisionb(const Ref &a, const Ref &b) { + ERR_FAIL_COND(!a.is_valid() || !b.is_valid()); - Ref out; - out.instance(); + int s = a->size(); - int size = a->size(); + ERR_FAIL_COND(s != b->size()); - out->resize(size); + 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) { + out_ptr[i] = a_ptr[i] / b_ptr[i]; + } +} + +void MLPPVector::scalar_multiply(real_t scalar) { + real_t *out_ptr = ptrw(); + + for (int i = 0; i < _size; ++i) { + out_ptr[i] = out_ptr[i] * scalar; + } +} +Ref MLPPVector::scalar_multiplyn(real_t scalar) { + 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] = a_ptr[i] * scalar; } return out; } -void MLPPVector::scalar_multiplyv(real_t scalar, const Ref &a, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !out.is_valid()); +void MLPPVector::scalar_multiplyb(real_t scalar, const Ref &a) { + int s = a->size(); - int size = a->size(); - - if (unlikely(out->size() != size)) { - out->resize(size); + if (unlikely(size() != s)) { + resize(s); } const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < size; ++i) { + for (int i = 0; i < s; ++i) { out_ptr[i] = a_ptr[i] * scalar; } } -Ref MLPPVector::scalar_addnv(real_t scalar, const Ref &a) { - ERR_FAIL_COND_V(!a.is_valid(), Ref()); +void MLPPVector::scalar_add(real_t scalar) { + real_t *out_ptr = ptrw(); + for (int i = 0; i < _size; ++i) { + out_ptr[i] = out_ptr[i] + scalar; + } +} +Ref MLPPVector::scalar_addn(real_t scalar) { Ref out; out.instance(); - int size = a->size(); + out->resize(_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] = a_ptr[i] + scalar; } return out; } -void MLPPVector::scalar_addv(real_t scalar, const Ref &a, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !out.is_valid()); +void MLPPVector::scalar_addb(real_t scalar, const Ref &a) { + ERR_FAIL_COND(!a.is_valid()); - int size = a->size(); + int s = a->size(); - if (unlikely(out->size() != size)) { - out->resize(size); + if (unlikely(size() != s)) { + resize(s); } const real_t *a_ptr = a->ptr(); - real_t *out_ptr = out->ptrw(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < size; ++i) { + for (int i = 0; i < s; ++i) { out_ptr[i] = a_ptr[i] + scalar; } } -Ref MLPPVector::additionnv(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); +void MLPPVector::add(const Ref &b) { + ERR_FAIL_COND(!b.is_valid()); + ERR_FAIL_COND(_size != b->size()); - int size = a->size(); + const real_t *b_ptr = b->ptr(); + real_t *out_ptr = ptrw(); - ERR_FAIL_COND_V(size != b->size(), Ref()); + for (int i = 0; i < _size; ++i) { + out_ptr[i] += b_ptr[i]; + } +} +Ref MLPPVector::addn(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); + out->resize(_size); - const real_t *a_ptr = a->ptr(); + 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) { + for (int i = 0; i < _size; ++i) { out_ptr[i] = a_ptr[i] + b_ptr[i]; } return out; } -void MLPPVector::additionv(const Ref &a, const Ref &b, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !b.is_valid() || !out.is_valid()); +void MLPPVector::addb(const Ref &a, const Ref &b) { + ERR_FAIL_COND(!a.is_valid() || !b.is_valid()); - int size = a->size(); + int s = a->size(); - ERR_FAIL_COND(size != b->size()); + ERR_FAIL_COND(s != b->size()); - if (unlikely(out->size() != size)) { - out->resize(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 = out->ptrw(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < size; ++i) { + for (int i = 0; i < s; ++i) { out_ptr[i] = a_ptr[i] + b_ptr[i]; } } -Ref MLPPVector::subtractionnv(const Ref &a, const Ref &b) { - ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); +void MLPPVector::sub(const Ref &b) { + ERR_FAIL_COND(!b.is_valid()); + ERR_FAIL_COND(_size != b->size()); - int size = a->size(); + const real_t *b_ptr = b->ptr(); + real_t *out_ptr = ptrw(); - ERR_FAIL_COND_V(size != b->size(), Ref()); + for (int i = 0; i < _size; ++i) { + out_ptr[i] -= b_ptr[i]; + } +} +Ref MLPPVector::subn(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); - if (unlikely(size == 0)) { - return out; - } - - out->resize(size); - - const real_t *a_ptr = a->ptr(); + 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) { + for (int i = 0; i < _size; ++i) { out_ptr[i] = a_ptr[i] - b_ptr[i]; } return out; } -void MLPPVector::subtractionv(const Ref &a, const Ref &b, Ref out) { - ERR_FAIL_COND(!a.is_valid() || !b.is_valid() || !out.is_valid()); +void MLPPVector::subb(const Ref &a, const Ref &b) { + ERR_FAIL_COND(!a.is_valid() || !b.is_valid()); - int size = a->size(); + int s = a->size(); - ERR_FAIL_COND(size != b->size()); + ERR_FAIL_COND(s != b->size()); - if (unlikely(out->size() != size)) { - out->resize(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 = out->ptrw(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < size; ++i) { + for (int i = 0; i < s; ++i) { out_ptr[i] = a_ptr[i] - b_ptr[i]; } } diff --git a/mlpp/lin_alg/mlpp_vector.h b/mlpp/lin_alg/mlpp_vector.h index e5ebb7e..c86f8dc 100644 --- a/mlpp/lin_alg/mlpp_vector.h +++ b/mlpp/lin_alg/mlpp_vector.h @@ -333,13 +333,12 @@ public: //void substract(const Ref &b); <- this should be the simplest / most obvious method //Ref substractn(const Ref &b); //void substractb(const Ref &a, const Ref &b); -> result is in this (subtractionv like) - + // Or: //void hadamard_product(const Ref &b); <- this should be the simplest / most obvious method //Ref hadamard_productn(const Ref &b); <- n -> new //void hadamard_productb(const Ref &a, const Ref &b); <- b -> between, result is stored in *this - void flatten_vectors(const Vector> &A); Ref flatten_vectorsn(const Vector> &A); @@ -347,19 +346,25 @@ public: Ref hadamard_productn(const Ref &b); void hadamard_productb(const Ref &a, const Ref &b); - Ref element_wise_divisionnv(const Ref &a, const Ref &b); + void element_wise_division(const Ref &b); + Ref element_wise_divisionn(const Ref &b); + void element_wise_divisionb(const Ref &a, const Ref &b); - Ref scalar_multiplynv(real_t scalar, const Ref &a); - void scalar_multiplyv(real_t scalar, const Ref &a, Ref out); + void scalar_multiply(real_t scalar); + Ref scalar_multiplyn(real_t scalar); + void scalar_multiplyb(real_t scalar, const Ref &a); - Ref scalar_addnv(real_t scalar, const Ref &a); - void scalar_addv(real_t scalar, const Ref &a, Ref out); + void scalar_add(real_t scalar); + Ref scalar_addn(real_t scalar); + void scalar_addb(real_t scalar, const Ref &a); - Ref additionnv(const Ref &a, const Ref &b); - void additionv(const Ref &a, const Ref &b, Ref out); + void add(const Ref &b); + Ref addn(const Ref &b); + void addb(const Ref &a, const Ref &b); - Ref subtractionnv(const Ref &a, const Ref &b); - void subtractionv(const Ref &a, const Ref &b, Ref out); + void sub(const Ref &b); + Ref subn(const Ref &b); + void subb(const Ref &a, const Ref &b); Ref lognv(const Ref &a); Ref log10nv(const Ref &a);