Also added vector/matrix methods to MLPPVector and MLPPMatrix from LinAlg.

This commit is contained in:
Relintai 2023-04-24 09:22:53 +02:00
parent 2e0870a4c3
commit 818f9ec1af
4 changed files with 150 additions and 0 deletions

View File

@ -1066,6 +1066,81 @@ bool MLPPMatrix::zeroEigenvalue(std::vector<std::vector<real_t>> A) {
}
*/
Ref<MLPPVector> MLPPMatrix::mat_vec_multnv(const Ref<MLPPMatrix> &A, const Ref<MLPPVector> &b) {
ERR_FAIL_COND_V(!A.is_valid() || !b.is_valid(), Ref<MLPPMatrix>());
Size2i a_size = A->size();
int b_size = b->size();
ERR_FAIL_COND_V(a_size.x < b->size(), Ref<MLPPMatrix>());
Ref<MLPPVector> c;
c.instance();
c->resize(a_size.y);
c->fill(0);
const real_t *a_ptr = A->ptr();
const real_t *b_ptr = b->ptr();
real_t *c_ptr = c->ptrw();
for (int i = 0; i < a_size.y; ++i) {
for (int k = 0; k < b_size; ++k) {
int mat_index = A->calculate_index(i, k);
c_ptr[i] += a_ptr[mat_index] * b_ptr[k];
}
}
return c;
}
Ref<MLPPMatrix> MLPPMatrix::mat_vec_addnm(const Ref<MLPPMatrix> &A, const Ref<MLPPVector> &b) {
ERR_FAIL_COND_V(!A.is_valid() || !b.is_valid(), Ref<MLPPMatrix>());
Size2i a_size = A->size();
ERR_FAIL_COND_V(a_size.x != b->size(), Ref<MLPPMatrix>());
Ref<MLPPMatrix> ret;
ret.instance();
ret->resize(a_size);
const real_t *a_ptr = A->ptr();
const real_t *b_ptr = b->ptr();
real_t *ret_ptr = ret->ptrw();
for (int i = 0; i < a_size.y; ++i) {
for (int j = 0; j < a_size.x; ++j) {
int mat_index = A->calculate_index(i, j);
ret_ptr[mat_index] = a_ptr[mat_index] + b_ptr[j];
}
}
return ret;
}
Ref<MLPPMatrix> MLPPMatrix::diagnm(const Ref<MLPPVector> &a) {
int a_size = a->size();
Ref<MLPPMatrix> B;
B.instance();
B->resize(Size2i(a_size, a_size));
B->fill(0);
const real_t *a_ptr = a->ptr();
real_t *b_ptr = B->ptrw();
for (int i = 0; i < a_size; ++i) {
b_ptr[B->calculate_index(i, i)] = a_ptr[i];
}
return B;
}
String MLPPMatrix::to_string() {
String str;

View File

@ -698,6 +698,13 @@ public:
bool zeroEigenvalue(std::vector<std::vector<real_t>> A);
*/
Ref<MLPPVector> mat_vec_multnv(const Ref<MLPPMatrix> &A, const Ref<MLPPVector> &b);
Ref<MLPPMatrix> mat_vec_addnm(const Ref<MLPPMatrix> &A, const Ref<MLPPVector> &b);
// set_diagonal (just sets diagonal), set_as_diagonal (zeros, then sets diagonal to vec)
// Also a variant that copies
Ref<MLPPMatrix> diagnm(const Ref<MLPPVector> &a);
_FORCE_INLINE_ bool is_equal_approx(const Ref<MLPPMatrix> &p_with, real_t tolerance = static_cast<real_t>(CMP_EPSILON)) const {
ERR_FAIL_COND_V(!p_with.is_valid(), false);

View File

@ -1,6 +1,8 @@
#include "mlpp_vector.h"
#include "mlpp_matrix.h"
Ref<MLPPVector> MLPPVector::flattenmnv(const Vector<Ref<MLPPVector>> &A) {
Ref<MLPPVector> a;
a.instance();
@ -644,6 +646,64 @@ real_t MLPPVector::cosineSimilarity(std::vector<real_t> a, std::vector<real_t> b
}
*/
Ref<MLPPVector> MLPPVector::subtract_matrix_rowsnv(const Ref<MLPPVector> &a, const Ref<MLPPMatrix> &B) {
Ref<MLPPVector> c = a->duplicate();
Size2i b_size = B->size();
ERR_FAIL_COND_V(b_size.x != c->size(), c);
const real_t *b_ptr = B->ptr();
real_t *c_ptr = c->ptrw();
for (int i = 0; i < b_size.y; ++i) {
for (int j = 0; j < b_size.x; ++j) {
c_ptr[j] -= b_ptr[B->calculate_index(i, j)];
}
}
return c;
}
Ref<MLPPMatrix> MLPPVector::outer_product(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b) {
Ref<MLPPMatrix> C;
C.instance();
Size2i size = Size2i(b->size(), a->size());
C->resize(size);
const real_t *a_ptr = a->ptr();
const real_t *b_ptr = b->ptr();
for (int i = 0; i < size.y; ++i) {
real_t curr_a = a_ptr[i];
for (int j = 0; j < size.x; ++j) {
C->set_element(i, j, curr_a * b_ptr[j]);
}
}
return C;
}
Ref<MLPPMatrix> MLPPVector::diagnm(const Ref<MLPPVector> &a) {
int a_size = a->size();
Ref<MLPPMatrix> B;
B.instance();
B->resize(Size2i(a_size, a_size));
B->fill(0);
const real_t *a_ptr = a->ptr();
real_t *b_ptr = B->ptrw();
for (int i = 0; i < a_size; ++i) {
b_ptr[B->calculate_index(i, i)] = a_ptr[i];
}
return B;
}
String MLPPVector::to_string() {
String str;

View File

@ -15,6 +15,8 @@
//REMOVE
#include <vector>
class MLPPMatrix;
class MLPPVector : public Reference {
GDCLASS(MLPPVector, Reference);
@ -392,6 +394,12 @@ public:
//real_t cosineSimilarity(std::vector<real_t> a, std::vector<real_t> b);
Ref<MLPPVector> subtract_matrix_rowsnv(const Ref<MLPPVector> &a, const Ref<MLPPMatrix> &B);
Ref<MLPPMatrix> outer_product(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b); // This multiplies a, bT
// as_diagonal_matrix / to_diagonal_matrix
Ref<MLPPMatrix> diagnm(const Ref<MLPPVector> &a);
String to_string();
_FORCE_INLINE_ MLPPVector() {