#ifndef MLPP_MATRIX_H #define MLPP_MATRIX_H #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/reference.h" #include "mlpp_vector.h" class MLPPMatrix : public Reference { GDCLASS(MLPPMatrix, Reference); public: _FORCE_INLINE_ real_t *ptrw() { return _data; } _FORCE_INLINE_ const real_t *ptr() const { return _data; } void add_row(const Vector &p_row); void add_row_pool_vector(const PoolRealArray &p_row); void add_row_mlpp_vector(const Ref &p_row); void add_rows_mlpp_matrix(const Ref &p_other); void remove_row(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 remove_row_unordered(int p_index); void swap_row(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 get_element_index(int p_index) const { ERR_FAIL_INDEX_V(p_index, data_size(), 0); return _data[p_index]; } _FORCE_INLINE_ void set_element_index(int p_index, real_t p_val) { ERR_FAIL_INDEX(p_index, data_size()); _data[p_index] = p_val; } _FORCE_INLINE_ real_t get_element(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 set_element(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 get_row_vector(int p_index_y) const; PoolRealArray get_row_pool_vector(int p_index_y) const; Ref get_row_mlpp_vector(int p_index_y) const; void get_row_into_mlpp_vector(int p_index_y, Ref target) const; void set_row_vector(int p_index_y, const Vector &p_row); void set_row_pool_vector(int p_index_y, const PoolRealArray &p_row); void set_row_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() 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); //std::vector> gramMatrix(std::vector> A); //bool linearIndependenceChecker(std::vector> A); Ref gaussian_noise(int n, int m) const; void gaussian_noise_fill(); 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 element_wise_division(const Ref &B); Ref element_wise_divisionn(const Ref &B) const; void element_wise_divisionb(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); //std::vector> matrixPower(std::vector> A, int n); 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(std::vector> A); 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 zero_mat(int n, int m) const; Ref one_mat(int n, int m) const; Ref full_mat(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); //std::vector> rotate(std::vector> 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); //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; 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; /* std::vector solve(std::vector> A, std::vector b); 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 set_diagonal(const Ref &a); Ref set_diagonaln(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; String to_string(); MLPPMatrix(); MLPPMatrix(const MLPPMatrix &p_from); MLPPMatrix(const Vector> &p_from); MLPPMatrix(const Array &p_from); ~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