mirror of
https://github.com/Relintai/pmlpp.git
synced 2025-03-12 22:38:51 +01:00
More MLPPVector math api standardization.
This commit is contained in:
parent
f8f3edf6ef
commit
f00d7e21c3
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include "mlpp_matrix.h"
|
#include "mlpp_matrix.h"
|
||||||
|
|
||||||
|
|
||||||
void MLPPVector::flatten_vectors(const Vector<Ref<MLPPVector>> &A) {
|
void MLPPVector::flatten_vectors(const Vector<Ref<MLPPVector>> &A) {
|
||||||
int vsize = 0;
|
int vsize = 0;
|
||||||
for (int i = 0; i < A.size(); ++i) {
|
for (int i = 0; i < A.size(); ++i) {
|
||||||
@ -28,7 +27,6 @@ void MLPPVector::flatten_vectors(const Vector<Ref<MLPPVector>> &A) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Ref<MLPPVector> MLPPVector::flatten_vectorsn(const Vector<Ref<MLPPVector>> &A) {
|
Ref<MLPPVector> MLPPVector::flatten_vectorsn(const Vector<Ref<MLPPVector>> &A) {
|
||||||
Ref<MLPPVector> a;
|
Ref<MLPPVector> a;
|
||||||
a.instance();
|
a.instance();
|
||||||
@ -111,184 +109,231 @@ void MLPPVector::hadamard_productb(const Ref<MLPPVector> &a, const Ref<MLPPVecto
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<MLPPVector> MLPPVector::element_wise_divisionnv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b) {
|
void MLPPVector::element_wise_division(const Ref<MLPPVector> &b) {
|
||||||
ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref<MLPPVector>());
|
ERR_FAIL_COND(!b.is_valid());
|
||||||
|
|
||||||
Ref<MLPPVector> out;
|
Ref<MLPPVector> out;
|
||||||
out.instance();
|
out.instance();
|
||||||
|
|
||||||
int size = a->size();
|
ERR_FAIL_COND(_size != b->size());
|
||||||
|
|
||||||
ERR_FAIL_COND_V(size != b->size(), Ref<MLPPVector>());
|
const real_t *a_ptr = ptr();
|
||||||
|
|
||||||
out->resize(size);
|
|
||||||
|
|
||||||
const real_t *a_ptr = a->ptr();
|
|
||||||
const real_t *b_ptr = b->ptr();
|
const real_t *b_ptr = b->ptr();
|
||||||
real_t *out_ptr = out->ptrw();
|
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> MLPPVector::element_wise_divisionn(const Ref<MLPPVector> &b) {
|
||||||
|
ERR_FAIL_COND_V(!b.is_valid(), Ref<MLPPVector>());
|
||||||
|
|
||||||
|
Ref<MLPPVector> out;
|
||||||
|
out.instance();
|
||||||
|
|
||||||
|
ERR_FAIL_COND_V(_size != b->size(), Ref<MLPPVector>());
|
||||||
|
|
||||||
|
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];
|
out_ptr[i] = a_ptr[i] / b_ptr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<MLPPVector> MLPPVector::scalar_multiplynv(real_t scalar, const Ref<MLPPVector> &a) {
|
void MLPPVector::element_wise_divisionb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b) {
|
||||||
ERR_FAIL_COND_V(!a.is_valid(), Ref<MLPPVector>());
|
ERR_FAIL_COND(!a.is_valid() || !b.is_valid());
|
||||||
|
|
||||||
Ref<MLPPVector> out;
|
int s = a->size();
|
||||||
out.instance();
|
|
||||||
|
|
||||||
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 *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> MLPPVector::scalar_multiplyn(real_t scalar) {
|
||||||
|
Ref<MLPPVector> out;
|
||||||
|
out.instance();
|
||||||
|
out->resize(_size);
|
||||||
|
|
||||||
|
const real_t *a_ptr = ptr();
|
||||||
real_t *out_ptr = out->ptrw();
|
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;
|
out_ptr[i] = a_ptr[i] * scalar;
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
void MLPPVector::scalar_multiplyv(real_t scalar, const Ref<MLPPVector> &a, Ref<MLPPVector> out) {
|
void MLPPVector::scalar_multiplyb(real_t scalar, const Ref<MLPPVector> &a) {
|
||||||
ERR_FAIL_COND(!a.is_valid() || !out.is_valid());
|
int s = a->size();
|
||||||
|
|
||||||
int size = a->size();
|
if (unlikely(size() != s)) {
|
||||||
|
resize(s);
|
||||||
if (unlikely(out->size() != size)) {
|
|
||||||
out->resize(size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const real_t *a_ptr = a->ptr();
|
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;
|
out_ptr[i] = a_ptr[i] * scalar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<MLPPVector> MLPPVector::scalar_addnv(real_t scalar, const Ref<MLPPVector> &a) {
|
void MLPPVector::scalar_add(real_t scalar) {
|
||||||
ERR_FAIL_COND_V(!a.is_valid(), Ref<MLPPVector>());
|
real_t *out_ptr = ptrw();
|
||||||
|
|
||||||
|
for (int i = 0; i < _size; ++i) {
|
||||||
|
out_ptr[i] = out_ptr[i] + scalar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ref<MLPPVector> MLPPVector::scalar_addn(real_t scalar) {
|
||||||
Ref<MLPPVector> out;
|
Ref<MLPPVector> out;
|
||||||
out.instance();
|
out.instance();
|
||||||
|
|
||||||
int size = a->size();
|
out->resize(_size);
|
||||||
|
|
||||||
out->resize(size);
|
const real_t *a_ptr = ptr();
|
||||||
|
|
||||||
const real_t *a_ptr = a->ptr();
|
|
||||||
real_t *out_ptr = out->ptrw();
|
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;
|
out_ptr[i] = a_ptr[i] + scalar;
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
void MLPPVector::scalar_addv(real_t scalar, const Ref<MLPPVector> &a, Ref<MLPPVector> out) {
|
void MLPPVector::scalar_addb(real_t scalar, const Ref<MLPPVector> &a) {
|
||||||
ERR_FAIL_COND(!a.is_valid() || !out.is_valid());
|
ERR_FAIL_COND(!a.is_valid());
|
||||||
|
|
||||||
int size = a->size();
|
int s = a->size();
|
||||||
|
|
||||||
if (unlikely(out->size() != size)) {
|
if (unlikely(size() != s)) {
|
||||||
out->resize(size);
|
resize(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
const real_t *a_ptr = a->ptr();
|
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;
|
out_ptr[i] = a_ptr[i] + scalar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<MLPPVector> MLPPVector::additionnv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b) {
|
void MLPPVector::add(const Ref<MLPPVector> &b) {
|
||||||
ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref<MLPPVector>());
|
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<MLPPVector>());
|
for (int i = 0; i < _size; ++i) {
|
||||||
|
out_ptr[i] += b_ptr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ref<MLPPVector> MLPPVector::addn(const Ref<MLPPVector> &b) {
|
||||||
|
ERR_FAIL_COND_V(!b.is_valid(), Ref<MLPPVector>());
|
||||||
|
ERR_FAIL_COND_V(_size != b->size(), Ref<MLPPVector>());
|
||||||
|
|
||||||
Ref<MLPPVector> out;
|
Ref<MLPPVector> out;
|
||||||
out.instance();
|
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();
|
const real_t *b_ptr = b->ptr();
|
||||||
real_t *out_ptr = out->ptrw();
|
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];
|
out_ptr[i] = a_ptr[i] + b_ptr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
void MLPPVector::additionv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b, Ref<MLPPVector> out) {
|
void MLPPVector::addb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b) {
|
||||||
ERR_FAIL_COND(!a.is_valid() || !b.is_valid() || !out.is_valid());
|
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)) {
|
if (unlikely(size() != s)) {
|
||||||
out->resize(size);
|
resize(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
const real_t *a_ptr = a->ptr();
|
const real_t *a_ptr = a->ptr();
|
||||||
const real_t *b_ptr = b->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];
|
out_ptr[i] = a_ptr[i] + b_ptr[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<MLPPVector> MLPPVector::subtractionnv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b) {
|
void MLPPVector::sub(const Ref<MLPPVector> &b) {
|
||||||
ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref<MLPPVector>());
|
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<MLPPVector>());
|
for (int i = 0; i < _size; ++i) {
|
||||||
|
out_ptr[i] -= b_ptr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ref<MLPPVector> MLPPVector::subn(const Ref<MLPPVector> &b) {
|
||||||
|
ERR_FAIL_COND_V(!b.is_valid(), Ref<MLPPVector>());
|
||||||
|
ERR_FAIL_COND_V(_size != b->size(), Ref<MLPPVector>());
|
||||||
|
|
||||||
Ref<MLPPVector> out;
|
Ref<MLPPVector> out;
|
||||||
out.instance();
|
out.instance();
|
||||||
|
out->resize(_size);
|
||||||
|
|
||||||
if (unlikely(size == 0)) {
|
const real_t *a_ptr = ptr();
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
out->resize(size);
|
|
||||||
|
|
||||||
const real_t *a_ptr = a->ptr();
|
|
||||||
const real_t *b_ptr = b->ptr();
|
const real_t *b_ptr = b->ptr();
|
||||||
real_t *out_ptr = out->ptrw();
|
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];
|
out_ptr[i] = a_ptr[i] - b_ptr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
void MLPPVector::subtractionv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b, Ref<MLPPVector> out) {
|
void MLPPVector::subb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b) {
|
||||||
ERR_FAIL_COND(!a.is_valid() || !b.is_valid() || !out.is_valid());
|
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)) {
|
if (unlikely(size() != s)) {
|
||||||
out->resize(size);
|
resize(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
const real_t *a_ptr = a->ptr();
|
const real_t *a_ptr = a->ptr();
|
||||||
const real_t *b_ptr = b->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];
|
out_ptr[i] = a_ptr[i] - b_ptr[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -339,7 +339,6 @@ public:
|
|||||||
//Ref<MLPPVector> hadamard_productn(const Ref<MLPPVector> &b); <- n -> new
|
//Ref<MLPPVector> hadamard_productn(const Ref<MLPPVector> &b); <- n -> new
|
||||||
//void hadamard_productb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b); <- b -> between, result is stored in *this
|
//void hadamard_productb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b); <- b -> between, result is stored in *this
|
||||||
|
|
||||||
|
|
||||||
void flatten_vectors(const Vector<Ref<MLPPVector>> &A);
|
void flatten_vectors(const Vector<Ref<MLPPVector>> &A);
|
||||||
Ref<MLPPVector> flatten_vectorsn(const Vector<Ref<MLPPVector>> &A);
|
Ref<MLPPVector> flatten_vectorsn(const Vector<Ref<MLPPVector>> &A);
|
||||||
|
|
||||||
@ -347,19 +346,25 @@ public:
|
|||||||
Ref<MLPPVector> hadamard_productn(const Ref<MLPPVector> &b);
|
Ref<MLPPVector> hadamard_productn(const Ref<MLPPVector> &b);
|
||||||
void hadamard_productb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
void hadamard_productb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
||||||
|
|
||||||
Ref<MLPPVector> element_wise_divisionnv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
void element_wise_division(const Ref<MLPPVector> &b);
|
||||||
|
Ref<MLPPVector> element_wise_divisionn(const Ref<MLPPVector> &b);
|
||||||
|
void element_wise_divisionb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
||||||
|
|
||||||
Ref<MLPPVector> scalar_multiplynv(real_t scalar, const Ref<MLPPVector> &a);
|
void scalar_multiply(real_t scalar);
|
||||||
void scalar_multiplyv(real_t scalar, const Ref<MLPPVector> &a, Ref<MLPPVector> out);
|
Ref<MLPPVector> scalar_multiplyn(real_t scalar);
|
||||||
|
void scalar_multiplyb(real_t scalar, const Ref<MLPPVector> &a);
|
||||||
|
|
||||||
Ref<MLPPVector> scalar_addnv(real_t scalar, const Ref<MLPPVector> &a);
|
void scalar_add(real_t scalar);
|
||||||
void scalar_addv(real_t scalar, const Ref<MLPPVector> &a, Ref<MLPPVector> out);
|
Ref<MLPPVector> scalar_addn(real_t scalar);
|
||||||
|
void scalar_addb(real_t scalar, const Ref<MLPPVector> &a);
|
||||||
|
|
||||||
Ref<MLPPVector> additionnv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
void add(const Ref<MLPPVector> &b);
|
||||||
void additionv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b, Ref<MLPPVector> out);
|
Ref<MLPPVector> addn(const Ref<MLPPVector> &b);
|
||||||
|
void addb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
||||||
|
|
||||||
Ref<MLPPVector> subtractionnv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
void sub(const Ref<MLPPVector> &b);
|
||||||
void subtractionv(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b, Ref<MLPPVector> out);
|
Ref<MLPPVector> subn(const Ref<MLPPVector> &b);
|
||||||
|
void subb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
||||||
|
|
||||||
Ref<MLPPVector> lognv(const Ref<MLPPVector> &a);
|
Ref<MLPPVector> lognv(const Ref<MLPPVector> &a);
|
||||||
Ref<MLPPVector> log10nv(const Ref<MLPPVector> &a);
|
Ref<MLPPVector> log10nv(const Ref<MLPPVector> &a);
|
||||||
|
Loading…
Reference in New Issue
Block a user