#ifndef MLPP_LIN_ALG_H #define MLPP_LIN_ALG_H // // LinAlg.hpp // // Created by Marc Melikyan on 1/8/21. // //TODO Methods here should probably use error macros in a way where they get disabled in non-tools(?) (maybe release?) builds #include "core/math/math_defs.h" #include "core/object/reference.h" #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" #include #include class MLPPLinAlg : public Reference { GDCLASS(MLPPLinAlg, Reference); public: // MATRIX FUNCTIONS std::vector> gramMatrix(std::vector> A); bool linearIndependenceChecker(std::vector> A); std::vector> gaussianNoise(int n, int m); Ref gaussian_noise(int n, int m); std::vector> addition(std::vector> A, std::vector> B); std::vector> subtraction(std::vector> A, std::vector> B); std::vector> matmult(std::vector> A, std::vector> B); Ref additionnm(const Ref &A, const Ref &B); Ref subtractionnm(const Ref &A, const Ref &B); Ref matmultnm(const Ref &A, const Ref &B); std::vector> hadamard_product(std::vector> A, std::vector> B); std::vector> kronecker_product(std::vector> A, std::vector> B); std::vector> elementWiseDivision(std::vector> A, std::vector> B); Ref hadamard_productnm(const Ref &A, const Ref &B); Ref kronecker_productnm(const Ref &A, const Ref &B); Ref element_wise_divisionnvnm(const Ref &A, const Ref &B); std::vector> transpose(std::vector> A); std::vector> scalarMultiply(real_t scalar, std::vector> A); std::vector> scalarAdd(real_t scalar, std::vector> A); Ref transposenm(const Ref &A); Ref scalar_multiplynm(real_t scalar, const Ref &A); Ref scalar_addnm(real_t scalar, const Ref &A); std::vector> log(std::vector> A); std::vector> log10(std::vector> A); std::vector> exp(std::vector> A); std::vector> erf(std::vector> A); std::vector> exponentiate(std::vector> A, real_t p); std::vector> sqrt(std::vector> A); std::vector> cbrt(std::vector> A); Ref lognm(const Ref &A); Ref log10nm(const Ref &A); Ref expnm(const Ref &A); Ref erfnm(const Ref &A); Ref exponentiatenm(const Ref &A, real_t p); Ref sqrtnm(const Ref &A); Ref cbrtnm(const Ref &A); std::vector> matrixPower(std::vector> A, int n); std::vector> abs(std::vector> A); Ref absnm(const Ref &A); real_t det(std::vector> A, int d); real_t detm(const Ref &A, int d); real_t trace(std::vector> A); std::vector> cofactor(std::vector> A, int n, int i, int j); std::vector> adjoint(std::vector> A); std::vector> inverse(std::vector> A); std::vector> pinverse(std::vector> A); Ref cofactornm(const Ref &A, int n, int i, int j); Ref adjointnm(const Ref &A); Ref inversenm(const Ref &A); Ref pinversenm(const Ref &A); std::vector> zeromat(int n, int m); std::vector> onemat(int n, int m); std::vector> full(int n, int m, int k); Ref zeromatnm(int n, int m); Ref onematnm(int n, int m); Ref fullnm(int n, int m, int k); std::vector> sin(std::vector> A); std::vector> cos(std::vector> A); Ref sinnm(const Ref &A); Ref cosnm(const Ref &A); std::vector> rotate(std::vector> A, real_t theta, int axis = -1); std::vector> max(std::vector> A, std::vector> B); Ref maxnm(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); std::vector> identity(real_t d); Ref identitym(int d); std::vector> cov(std::vector> A); Ref covnm(const Ref &A); std::tuple>, std::vector>> eig(std::vector> A); struct EigenResultOld { std::vector> eigen_vectors; std::vector> eigen_values; }; EigenResultOld eigen_old(std::vector> A); struct EigenResult { Ref eigen_vectors; Ref eigen_values; }; EigenResult eigen(Ref A); struct SVDResultOld { std::vector> U; std::vector> S; std::vector> Vt; }; SVDResultOld SVD(std::vector> A); struct SVDResult { Ref U; Ref S; Ref Vt; }; SVDResult svd(const Ref &A); std::vector vectorProjection(std::vector a, std::vector b); std::vector> gramSchmidtProcess(std::vector> A); std::tuple>, std::vector>> QRD(std::vector> A); struct QRDResult { std::vector> Q; std::vector> R; }; QRDResult qrd(std::vector> A); std::tuple>, std::vector>> chol(std::vector> A); struct CholeskyResult { std::vector> L; std::vector> Lt; }; CholeskyResult cholesky(std::vector> A); real_t sum_elements(std::vector> A); std::vector flatten(std::vector> A); Ref flattenmnv(const Vector> &A); Ref flattenvvnv(const Ref &A); std::vector solve(std::vector> A, std::vector b); bool positiveDefiniteChecker(std::vector> A); bool negativeDefiniteChecker(std::vector> A); bool zeroEigenvalue(std::vector> A); void printMatrix(std::vector> A); // VECTOR FUNCTIONS std::vector> outerProduct(std::vector a, std::vector b); // This multiplies a, bT Ref outer_product(const Ref &a, const Ref &b); // This multiplies a, bT std::vector hadamard_product(std::vector a, std::vector b); Ref hadamard_productnv(const Ref &a, const Ref &b); void hadamard_productv(const Ref &a, const Ref &b, Ref out); std::vector elementWiseDivision(std::vector a, std::vector b); Ref element_wise_divisionnv(const Ref &a, const Ref &b); std::vector scalarMultiply(real_t scalar, std::vector a); Ref scalar_multiplynv(real_t scalar, const Ref &a); void scalar_multiplyv(real_t scalar, const Ref &a, Ref out); std::vector scalarAdd(real_t scalar, std::vector a); Ref scalar_addnv(real_t scalar, const Ref &a); void scalar_addv(real_t scalar, const Ref &a, Ref out); std::vector addition(std::vector a, std::vector b); Ref additionnv(const Ref &a, const Ref &b); void additionv(const Ref &a, const Ref &b, Ref out); std::vector subtraction(std::vector a, std::vector b); Ref subtractionnv(const Ref &a, const Ref &b); void subtractionv(const Ref &a, const Ref &b, Ref out); std::vector subtractMatrixRows(std::vector a, std::vector> B); Ref subtract_matrix_rowsnv(const Ref &a, const Ref &B); Ref lognv(const Ref &a); Ref log10nv(const Ref &a); Ref expnv(const Ref &a); Ref erfnv(const Ref &a); Ref exponentiatenv(const Ref &a, real_t p); Ref sqrtnv(const Ref &a); Ref cbrtnv(const Ref &a); real_t dot(std::vector a, std::vector b); real_t dotnv(const Ref &a, const Ref &b); std::vector cross(std::vector a, std::vector b); std::vector abs(std::vector a); std::vector zerovec(int n); std::vector onevec(int n); std::vector full(int n, int k); Ref absv(const Ref &a); Ref zerovecnv(int n); Ref onevecnv(int n); Ref fullnv(int n, int k); std::vector> diag(std::vector a); Ref diagnm(const Ref &a); std::vector sin(std::vector a); std::vector cos(std::vector a); Ref sinnv(const Ref &a); Ref cosnv(const Ref &a); std::vector max(std::vector a, std::vector b); Ref maxnvv(const Ref &a, const Ref &b); real_t max(std::vector a); real_t min(std::vector a); real_t maxvr(const Ref &a); real_t minvr(const Ref &a); std::vector round(std::vector a); real_t euclideanDistance(std::vector a, std::vector b); real_t euclidean_distance(const Ref &a, const Ref &b); real_t euclidean_distance_squared(const Ref &a, const Ref &b); real_t norm_2(std::vector a); real_t norm_sq(std::vector a); real_t norm_sqv(const Ref &a); real_t sum_elements(std::vector a); real_t sum_elementsv(const Ref &a); real_t cosineSimilarity(std::vector a, std::vector b); void printVector(std::vector a); // MATRIX-VECTOR FUNCTIONS std::vector> mat_vec_add(std::vector> A, std::vector b); std::vector mat_vec_mult(std::vector> A, std::vector b); Ref mat_vec_addnm(const Ref &A, const Ref &b); Ref mat_vec_multnv(const Ref &A, const Ref &b); // TENSOR FUNCTIONS std::vector>> addition(std::vector>> A, std::vector>> B); Vector> addition_vt(const Vector> &A, const Vector> &B); std::vector>> elementWiseDivision(std::vector>> A, std::vector>> B); Vector> element_wise_divisionnv_vt(const Vector> &A, const Vector> &B); std::vector>> sqrt(std::vector>> A); Vector> sqrt_vt(const Vector> &A); std::vector>> exponentiate(std::vector>> A, real_t p); Vector> exponentiate_vt(const Vector> &A, real_t p); std::vector> tensor_vec_mult(std::vector>> A, std::vector b); std::vector flatten(std::vector>> A); void printTensor(std::vector>> A); std::vector>> scalarMultiply(real_t scalar, std::vector>> A); std::vector>> scalarAdd(real_t scalar, std::vector>> A); Vector> scalar_multiply_vm(real_t scalar, Vector> A); Vector> scalar_add_vm(real_t scalar, Vector> A); std::vector>> resize(std::vector>> A, std::vector>> B); Vector> resize_vt(const Vector> &A, const Vector> &B); std::vector>> hadamard_product(std::vector>> A, std::vector>> B); std::vector>> max(std::vector>> A, std::vector>> B); Vector> max_vt(const Vector> &A, const Vector> &B); std::vector>> abs(std::vector>> A); Vector> abs_vt(const Vector> &A); real_t norm_2(std::vector>> A); std::vector>> vector_wise_tensor_product(std::vector>> A, std::vector> B); protected: static void _bind_methods(); }; #endif /* LinAlg_hpp */