2023-01-26 16:02:45 +01:00
|
|
|
#ifndef MLPP_VECTOR_H
|
|
|
|
#define MLPP_VECTOR_H
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
#include "core/math/math_defs.h"
|
2023-01-29 15:46:55 +01:00
|
|
|
#include "core/math/math_funcs.h"
|
2023-01-27 13:01:16 +01:00
|
|
|
|
2023-01-26 16:43:54 +01:00
|
|
|
#include "core/containers/pool_vector.h"
|
|
|
|
#include "core/containers/sort_array.h"
|
|
|
|
#include "core/containers/vector.h"
|
|
|
|
#include "core/error/error_macros.h"
|
|
|
|
#include "core/os/memory.h"
|
|
|
|
|
2023-01-26 16:02:45 +01:00
|
|
|
#include "core/object/reference.h"
|
|
|
|
|
2023-01-26 16:43:54 +01:00
|
|
|
//REMOVE
|
|
|
|
#include <vector>
|
|
|
|
|
2023-04-24 09:22:53 +02:00
|
|
|
class MLPPMatrix;
|
|
|
|
|
2023-01-26 16:02:45 +01:00
|
|
|
class MLPPVector : public Reference {
|
|
|
|
GDCLASS(MLPPVector, Reference);
|
|
|
|
|
|
|
|
public:
|
2023-04-25 18:04:34 +02:00
|
|
|
_FORCE_INLINE_ real_t *ptrw() {
|
2023-01-26 17:05:57 +01:00
|
|
|
return _data;
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 18:04:34 +02:00
|
|
|
_FORCE_INLINE_ const real_t *ptr() const {
|
2023-01-26 17:05:57 +01:00
|
|
|
return _data;
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void push_back(real_t p_elem);
|
|
|
|
void add_mlpp_vector(const Ref<MLPPVector> &p_other);
|
2023-01-26 16:43:54 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void remove(real_t p_index);
|
2023-01-26 16:43:54 +01:00
|
|
|
|
|
|
|
// Removes the item copying the last value into the position of the one to
|
|
|
|
// remove. It's generally faster than `remove`.
|
2023-04-25 18:19:58 +02:00
|
|
|
void remove_unordered(int p_index);
|
2023-01-26 16:43:54 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void erase(const real_t &p_val);
|
|
|
|
int erase_multiple_unordered(const real_t &p_val);
|
2023-01-26 16:43:54 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void invert();
|
2023-01-26 16:43:54 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void clear() { resize(0); }
|
|
|
|
_FORCE_INLINE_ void reset() {
|
2023-01-26 17:05:57 +01:00
|
|
|
if (_data) {
|
|
|
|
memfree(_data);
|
|
|
|
_data = NULL;
|
|
|
|
_size = 0;
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 17:05:57 +01:00
|
|
|
_FORCE_INLINE_ bool empty() const { return _size == 0; }
|
|
|
|
_FORCE_INLINE_ int size() const { return _size; }
|
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void resize(int p_size);
|
2023-01-26 17:05:57 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
_FORCE_INLINE_ const real_t &operator[](int p_index) const {
|
2023-01-27 02:06:44 +01:00
|
|
|
CRASH_BAD_INDEX(p_index, _size);
|
2023-01-26 17:05:57 +01:00
|
|
|
return _data[p_index];
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
2023-01-27 13:01:16 +01:00
|
|
|
_FORCE_INLINE_ real_t &operator[](int p_index) {
|
2023-01-27 02:06:44 +01:00
|
|
|
CRASH_BAD_INDEX(p_index, _size);
|
2023-01-26 17:05:57 +01:00
|
|
|
return _data[p_index];
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
_FORCE_INLINE_ real_t get_element(int p_index) const {
|
2023-01-27 02:06:44 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_index, _size, 0);
|
2023-01-26 17:05:57 +01:00
|
|
|
return _data[p_index];
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
_FORCE_INLINE_ void set_element(int p_index, real_t p_val) {
|
2023-01-27 02:06:44 +01:00
|
|
|
ERR_FAIL_INDEX(p_index, _size);
|
2023-01-26 17:05:57 +01:00
|
|
|
_data[p_index] = p_val;
|
2023-01-26 16:51:34 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void fill(real_t p_val);
|
|
|
|
void insert(int p_pos, real_t p_val);
|
2023-01-26 16:43:54 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
int find(const real_t &p_val, int p_from = 0) const;
|
2023-01-26 16:43:54 +01:00
|
|
|
|
|
|
|
template <class C>
|
|
|
|
void sort_custom() {
|
2023-01-26 17:05:57 +01:00
|
|
|
int len = _size;
|
2023-01-26 16:43:54 +01:00
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
SortArray<real_t, C> sorter;
|
2023-01-26 17:05:57 +01:00
|
|
|
sorter.sort(_data, len);
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sort() {
|
2023-01-27 13:01:16 +01:00
|
|
|
sort_custom<_DefaultComparator<real_t>>();
|
2023-01-26 16:43:54 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void ordered_insert(real_t p_val);
|
2023-01-29 15:46:55 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
Vector<real_t> to_vector() const;
|
|
|
|
PoolRealArray to_pool_vector() const;
|
|
|
|
Vector<uint8_t> to_byte_array() const;
|
2023-01-29 15:46:55 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
Ref<MLPPVector> duplicate() const;
|
2023-01-29 15:46:55 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
void set_from_mlpp_vectorr(const MLPPVector &p_from);
|
|
|
|
void set_from_mlpp_vector(const Ref<MLPPVector> &p_from);
|
|
|
|
void set_from_vector(const Vector<real_t> &p_from);
|
|
|
|
void set_from_pool_vector(const PoolRealArray &p_from);
|
2023-01-29 15:46:55 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
bool is_equal_approx(const Ref<MLPPVector> &p_with, real_t tolerance = static_cast<real_t>(CMP_EPSILON)) const;
|
2023-01-29 15:46:55 +01:00
|
|
|
|
2023-04-24 12:24:09 +02:00
|
|
|
void flatten_vectors(const Vector<Ref<MLPPVector>> &A);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> flatten_vectorsn(const Vector<Ref<MLPPVector>> &A) const;
|
2023-04-24 12:24:09 +02:00
|
|
|
|
|
|
|
void hadamard_product(const Ref<MLPPVector> &b);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> hadamard_productn(const Ref<MLPPVector> &b) const;
|
2023-04-24 12:24:09 +02:00
|
|
|
void hadamard_productb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 13:09:12 +02:00
|
|
|
void element_wise_division(const Ref<MLPPVector> &b);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> element_wise_divisionn(const Ref<MLPPVector> &b) const;
|
2023-04-24 13:09:12 +02:00
|
|
|
void element_wise_divisionb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 13:09:12 +02:00
|
|
|
void scalar_multiply(real_t scalar);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> scalar_multiplyn(real_t scalar) const;
|
2023-04-24 13:09:12 +02:00
|
|
|
void scalar_multiplyb(real_t scalar, const Ref<MLPPVector> &a);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 13:09:12 +02:00
|
|
|
void scalar_add(real_t scalar);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> scalar_addn(real_t scalar) const;
|
2023-04-24 13:09:12 +02:00
|
|
|
void scalar_addb(real_t scalar, const Ref<MLPPVector> &a);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 13:09:12 +02:00
|
|
|
void add(const Ref<MLPPVector> &b);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> addn(const Ref<MLPPVector> &b) const;
|
2023-04-24 13:09:12 +02:00
|
|
|
void addb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 13:09:12 +02:00
|
|
|
void sub(const Ref<MLPPVector> &b);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> subn(const Ref<MLPPVector> &b) const;
|
2023-04-24 13:09:12 +02:00
|
|
|
void subb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 17:31:35 +02:00
|
|
|
void log();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> logn() const;
|
2023-04-24 17:31:35 +02:00
|
|
|
void logb(const Ref<MLPPVector> &a);
|
|
|
|
|
|
|
|
void log10();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> log10n() const;
|
2023-04-24 17:31:35 +02:00
|
|
|
void log10b(const Ref<MLPPVector> &a);
|
|
|
|
|
|
|
|
void exp();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> expn() const;
|
2023-04-24 17:31:35 +02:00
|
|
|
void expb(const Ref<MLPPVector> &a);
|
|
|
|
|
|
|
|
void erf();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> erfn() const;
|
2023-04-24 17:31:35 +02:00
|
|
|
void erfb(const Ref<MLPPVector> &a);
|
|
|
|
|
|
|
|
void exponentiate(real_t p);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> exponentiaten(real_t p) const;
|
2023-04-24 17:31:35 +02:00
|
|
|
void exponentiateb(const Ref<MLPPVector> &a, real_t p);
|
|
|
|
|
|
|
|
void sqrt();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> sqrtn() const;
|
2023-04-24 17:31:35 +02:00
|
|
|
void sqrtb(const Ref<MLPPVector> &a);
|
|
|
|
|
|
|
|
void cbrt();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> cbrtn() const;
|
2023-04-24 17:31:35 +02:00
|
|
|
void cbrtb(const Ref<MLPPVector> &a);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-25 17:46:42 +02:00
|
|
|
real_t dot(const Ref<MLPPVector> &b) const;
|
2023-04-24 08:36:38 +02:00
|
|
|
|
|
|
|
//std::vector<real_t> cross(std::vector<real_t> a, std::vector<real_t> b);
|
|
|
|
|
2023-04-24 17:57:22 +02:00
|
|
|
void abs();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> absn() const;
|
2023-04-24 17:57:22 +02:00
|
|
|
void absb(const Ref<MLPPVector> &a);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> zero_vec(int n) const;
|
|
|
|
Ref<MLPPVector> one_vec(int n) const;
|
|
|
|
Ref<MLPPVector> full_vec(int n, int k) const;
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 17:57:22 +02:00
|
|
|
void sin();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> sinn() const;
|
2023-04-24 17:57:22 +02:00
|
|
|
void sinb(const Ref<MLPPVector> &a);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 17:57:22 +02:00
|
|
|
void cos();
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> cosn() const;
|
2023-04-24 17:57:22 +02:00
|
|
|
void cosb(const Ref<MLPPVector> &a);
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-24 17:57:22 +02:00
|
|
|
void maxv(const Ref<MLPPVector> &b);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> maxvn(const Ref<MLPPVector> &b) const;
|
2023-04-24 17:57:22 +02:00
|
|
|
void maxvb(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
|
|
|
|
|
2023-04-25 17:46:42 +02:00
|
|
|
real_t max_element() const;
|
|
|
|
real_t min_element() const;
|
2023-04-24 08:36:38 +02:00
|
|
|
|
|
|
|
//std::vector<real_t> round(std::vector<real_t> a);
|
|
|
|
|
2023-04-25 17:46:42 +02:00
|
|
|
real_t euclidean_distance(const Ref<MLPPVector> &b) const;
|
|
|
|
real_t euclidean_distance_squared(const Ref<MLPPVector> &b) const;
|
2023-04-24 08:36:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
real_t norm_2(std::vector<real_t> a);
|
|
|
|
*/
|
|
|
|
|
2023-04-25 17:46:42 +02:00
|
|
|
real_t norm_sq() const;
|
2023-04-24 08:36:38 +02:00
|
|
|
|
2023-04-25 17:46:42 +02:00
|
|
|
real_t sum_elements() const;
|
2023-04-24 08:36:38 +02:00
|
|
|
|
|
|
|
//real_t cosineSimilarity(std::vector<real_t> a, std::vector<real_t> b);
|
|
|
|
|
2023-04-24 18:11:22 +02:00
|
|
|
void subtract_matrix_rows(const Ref<MLPPMatrix> &B);
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPVector> subtract_matrix_rowsn(const Ref<MLPPMatrix> &B) const;
|
2023-04-24 18:11:22 +02:00
|
|
|
void subtract_matrix_rowsb(const Ref<MLPPVector> &a, const Ref<MLPPMatrix> &B);
|
|
|
|
|
|
|
|
// This multiplies a, bT
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPMatrix> outer_product(const Ref<MLPPVector> &b) const;
|
2023-04-24 09:22:53 +02:00
|
|
|
|
|
|
|
// as_diagonal_matrix / to_diagonal_matrix
|
2023-04-25 17:46:42 +02:00
|
|
|
Ref<MLPPMatrix> diagnm() const;
|
2023-04-24 09:22:53 +02:00
|
|
|
|
2023-01-27 02:57:33 +01:00
|
|
|
String to_string();
|
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
MLPPVector();
|
|
|
|
MLPPVector(const MLPPVector &p_from);
|
|
|
|
MLPPVector(const Vector<real_t> &p_from);
|
|
|
|
MLPPVector(const PoolRealArray &p_from);
|
2023-01-26 16:43:54 +01:00
|
|
|
|
2023-04-25 18:19:58 +02:00
|
|
|
~MLPPVector();
|
2023-01-26 16:43:54 +01:00
|
|
|
|
|
|
|
// TODO: These are temporary
|
2023-02-09 11:40:16 +01:00
|
|
|
std::vector<real_t> to_std_vector() const;
|
|
|
|
void set_from_std_vector(const std::vector<real_t> &p_from);
|
|
|
|
MLPPVector(const std::vector<real_t> &p_from);
|
2023-01-26 16:02:45 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
2023-01-26 17:05:57 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int _size;
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t *_data;
|
2023-01-26 16:02:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|