#ifndef MLPP_MATRIX_H #define MLPP_MATRIX_H #ifndef GDNATIVE #include "core/math/math_defs.h" #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/math/vector2i.h" #include "core/os/memory.h" #include "core/object/resource.h" #else #include "core/containers/vector.h" #include "core/defs.h" #include "core/math_funcs.h" #include "core/os/memory.h" #include "core/pool_arrays.h" #include "gen/resource.h" #endif #include "mlpp_vector.h" class Image; class MLPPMatrix : public Resource { GDCLASS(MLPPMatrix, Resource); public: Array get_data(); void set_data(const Array &p_from); _FORCE_INLINE_ real_t *ptrw() { return _data; } _FORCE_INLINE_ const real_t *ptr() const { return _data; } void row_add(const Vector &p_row); void row_add_pool_vector(const PoolRealArray &p_row); void row_add_mlpp_vector(const Ref &p_row); void rows_add_mlpp_matrix(const Ref &p_other); void row_remove(int p_index); // Removes the item copying the last value into the position of the one to // remove. It's generally faster than `remove`. void row_remove_unordered(int p_index); void row_swap(int p_index_1, int p_index_2); _FORCE_INLINE_ void clear() { resize(Size2i()); } _FORCE_INLINE_ void reset() { if (_data) { memfree(_data); _data = NULL; _size = Vector2i(); } } _FORCE_INLINE_ bool empty() const { return data_size() == 0; } _FORCE_INLINE_ int data_size() const { return _size.x * _size.y; } _FORCE_INLINE_ Size2i size() const { return _size; } void resize(const Size2i &p_size); _FORCE_INLINE_ int calculate_index(int p_index_y, int p_index_x) const { return p_index_y * _size.x + p_index_x; } _FORCE_INLINE_ const real_t &operator[](int p_index) const { CRASH_BAD_INDEX(p_index, data_size()); return _data[p_index]; } _FORCE_INLINE_ real_t &operator[](int p_index) { CRASH_BAD_INDEX(p_index, data_size()); return _data[p_index]; } _FORCE_INLINE_ real_t element_get_index(int p_index) const { ERR_FAIL_INDEX_V(p_index, data_size(), 0); return _data[p_index]; } _FORCE_INLINE_ void element_set_index(int p_index, real_t p_val) { ERR_FAIL_INDEX(p_index, data_size()); _data[p_index] = p_val; } _FORCE_INLINE_ real_t element_get(int p_index_y, int p_index_x) const { ERR_FAIL_INDEX_V(p_index_x, _size.x, 0); ERR_FAIL_INDEX_V(p_index_y, _size.y, 0); return _data[p_index_y * _size.x + p_index_x]; } _FORCE_INLINE_ void element_set(int p_index_y, int p_index_x, real_t p_val) { ERR_FAIL_INDEX(p_index_x, _size.x); ERR_FAIL_INDEX(p_index_y, _size.y); _data[p_index_y * _size.x + p_index_x] = p_val; } Vector row_get_vector(int p_index_y) const; PoolRealArray row_get_pool_vector(int p_index_y) const; Ref row_get_mlpp_vector(int p_index_y) const; void row_get_into_mlpp_vector(int p_index_y, Ref target) const; void row_set_vector(int p_index_y, const Vector &p_row); void row_set_pool_vector(int p_index_y, const PoolRealArray &p_row); void row_set_mlpp_vector(int p_index_y, const Ref &p_row); void fill(real_t p_val); Vector to_flat_vector() const; PoolRealArray to_flat_pool_vector() const; Vector to_flat_byte_array() const; Ref duplicate_fast() const; void set_from_mlpp_matrix(const Ref &p_from); void set_from_mlpp_matrixr(const MLPPMatrix &p_from); void set_from_mlpp_vectors(const Vector> &p_from); void set_from_mlpp_vectors_array(const Array &p_from); void set_from_vectors(const Vector> &p_from); void set_from_arrays(const Array &p_from); void set_from_ptr(const real_t *p_from, const int p_size_y, const int p_size_x); //std::vector> gramMatrix(std::vector> A); //bool linearIndependenceChecker(std::vector> A); Ref gaussian_noise(int n, int m) const; void gaussian_noise_fill(); static Ref create_gaussian_noise(int n, int m); void add(const Ref &B); Ref addn(const Ref &B) const; void addb(const Ref &A, const Ref &B); void sub(const Ref &B); Ref subn(const Ref &B) const; void subb(const Ref &A, const Ref &B); void mult(const Ref &B); Ref multn(const Ref &B) const; void multb(const Ref &A, const Ref &B); void hadamard_product(const Ref &B); Ref hadamard_productn(const Ref &B) const; void hadamard_productb(const Ref &A, const Ref &B); void kronecker_product(const Ref &B); Ref kronecker_productn(const Ref &B) const; void kronecker_productb(const Ref &A, const Ref &B); void division_element_wise(const Ref &B); Ref division_element_wisen(const Ref &B) const; void division_element_wiseb(const Ref &A, const Ref &B); void transpose(); Ref transposen() const; void transposeb(const Ref &A); void scalar_multiply(const real_t scalar); Ref scalar_multiplyn(const real_t scalar) const; void scalar_multiplyb(const real_t scalar, const Ref &A); void scalar_add(const real_t scalar); Ref scalar_addn(const real_t scalar) const; void scalar_addb(const real_t scalar, const Ref &A); void log(); Ref logn() const; void logb(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); Ref matrix_powern(const int n) const; void abs(); Ref absn() const; void absb(const Ref &A); real_t det(int d = -1) const; real_t detb(const Ref &A, int d) const; real_t trace() const; Ref cofactor(int n, int i, int j) const; void cofactoro(int n, int i, int j, Ref out) const; Ref adjoint() const; void adjointo(Ref out) const; Ref inverse() const; void inverseo(Ref out) const; Ref pinverse() const; void pinverseo(Ref out) const; Ref matn_zero(int n, int m) const; Ref matn_one(int n, int m) const; Ref matn_full(int n, int m, int k) const; void sin(); Ref sinn() const; void sinb(const Ref &A); void cos(); Ref cosn() const; void cosb(const Ref &A); Ref create_rotation_matrix(real_t theta, int axis = -1); void rotate(real_t theta, int axis = -1); Ref rotaten(real_t theta, int axis = -1); void rotateb(const Ref &A, real_t theta, int axis = -1); void max(const Ref &B); Ref maxn(const Ref &B) const; void maxb(const Ref &A, const Ref &B); void min(const Ref &B); Ref minn(const Ref &B) const; void minb(const Ref &A, const Ref &B); //real_t max(std::vector> A); //real_t min(std::vector> A); //std::vector> round(std::vector> A); //real_t norm_2(std::vector> A); void identity(); Ref identityn() const; Ref identity_mat(int d) const; static Ref create_identity_mat(int d); Ref cov() const; void covo(Ref out) const; struct EigenResult { Ref eigen_vectors; Ref eigen_values; }; EigenResult eigen() const; EigenResult eigenb(const Ref &A) const; Array eigen_bind(); Array eigenb_bind(const Ref &A); struct SVDResult { Ref U; Ref S; Ref Vt; }; SVDResult svd() const; SVDResult svdb(const Ref &A) const; Array svd_bind(); Array svdb_bind(const Ref &A); //std::vector vectorProjection(std::vector a, std::vector b); //std::vector> gramSchmidtProcess(std::vector> A); /* struct QRDResult { std::vector> Q; std::vector> R; }; */ //QRDResult qrd(std::vector> A); /* struct CholeskyResult { std::vector> L; std::vector> Lt; }; CholeskyResult cholesky(std::vector> A); */ //real_t sum_elements(std::vector> A); Ref flatten() const; void flatteno(Ref out) const; Ref solve(const Ref& b) const; /* bool positiveDefiniteChecker(std::vector> A); bool negativeDefiniteChecker(std::vector> A); bool zeroEigenvalue(std::vector> A); */ Ref mult_vec(const Ref &b) const; void mult_veco(const Ref &b, Ref out); void add_vec(const Ref &b); Ref add_vecn(const Ref &b) const; void add_vecb(const Ref &A, const Ref &b); // This multiplies a, bT void outer_product(const Ref &a, const Ref &b); Ref outer_productn(const Ref &a, const Ref &b) const; // Just sets the diagonal void diagonal_set(const Ref &a); Ref diagonal_setn(const Ref &a) const; // Sets the diagonals, everythign else will get zeroed void diagonal_zeroed(const Ref &a); Ref diagonal_zeroedn(const Ref &a) const; bool is_equal_approx(const Ref &p_with, real_t tolerance = static_cast(CMP_EPSILON)) const; Ref get_as_image() const; void get_into_image(Ref out) const; void set_from_image(const Ref &p_img, const int p_image_channel); String to_string(); MLPPMatrix(); MLPPMatrix(const MLPPMatrix &p_from); MLPPMatrix(const Vector> &p_from); MLPPMatrix(const Array &p_from); MLPPMatrix(const real_t *p_from, const int p_size_y, const int p_size_x); ~MLPPMatrix(); // TODO: These are temporary std::vector to_flat_std_vector() const; void set_from_std_vectors(const std::vector> &p_from); std::vector> to_std_vector(); void set_row_std_vector(int p_index_y, const std::vector &p_row); MLPPMatrix(const std::vector> &p_from); protected: static void _bind_methods(); protected: Size2i _size; real_t *_data; }; #endif