pmlpp/mlpp/lin_alg/lin_alg.h

269 lines
9.7 KiB
C
Raw Normal View History

2023-01-24 18:57:18 +01:00
#ifndef MLPP_LIN_ALG_H
#define MLPP_LIN_ALG_H
//
// LinAlg.hpp
//
// Created by Marc Melikyan on 1/8/21.
//
2023-01-27 13:01:16 +01:00
#include "core/math/math_defs.h"
#include "../lin_alg/mlpp_matrix.h"
#include "../lin_alg/mlpp_vector.h"
#include <tuple>
2023-01-24 19:00:54 +01:00
#include <vector>
2023-01-25 00:29:02 +01:00
class MLPPLinAlg {
2023-01-24 19:00:54 +01:00
public:
// MATRIX FUNCTIONS
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> gramMatrix(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
bool linearIndependenceChecker(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> gaussianNoise(int n, int m);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> addition(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> subtraction(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> matmult(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> hadamard_product(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> kronecker_product(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> elementWiseDivision(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> transpose(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> scalarMultiply(real_t scalar, std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> scalarAdd(real_t scalar, std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> log(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> log10(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> exp(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> erf(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> exponentiate(std::vector<std::vector<real_t>> A, real_t p);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> sqrt(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> cbrt(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> matrixPower(std::vector<std::vector<real_t>> A, int n);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> abs(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
real_t det(std::vector<std::vector<real_t>> A, int d);
2023-01-27 13:01:16 +01:00
real_t trace(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> cofactor(std::vector<std::vector<real_t>> A, int n, int i, int j);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> adjoint(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> inverse(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> pinverse(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> zeromat(int n, int m);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> onemat(int n, int m);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> full(int n, int m, int k);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> sin(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> cos(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> rotate(std::vector<std::vector<real_t>> A, real_t theta, int axis = -1);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> max(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B);
2023-01-27 13:01:16 +01:00
real_t max(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
real_t min(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> round(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
real_t norm_2(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> identity(real_t d);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> cov(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> eig(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
struct EigenResult {
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> eigen_vectors;
std::vector<std::vector<real_t>> eigen_values;
2023-01-26 14:52:49 +01:00
};
2023-01-27 13:01:16 +01:00
EigenResult eigen(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
2023-01-27 13:01:16 +01:00
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> SVD(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
struct SDVResult {
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> U;
std::vector<std::vector<real_t>> S;
std::vector<std::vector<real_t>> Vt;
2023-01-26 14:52:49 +01:00
};
2023-01-27 13:01:16 +01:00
SDVResult svd(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
2023-01-27 13:01:16 +01:00
std::vector<real_t> vectorProjection(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> gramSchmidtProcess(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> QRD(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
struct QRDResult {
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> Q;
std::vector<std::vector<real_t>> R;
2023-01-26 14:52:49 +01:00
};
2023-01-27 13:01:16 +01:00
QRDResult qrd(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
2023-01-27 13:01:16 +01:00
std::tuple<std::vector<std::vector<real_t>>, std::vector<std::vector<real_t>>> chol(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
struct CholeskyResult {
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> L;
std::vector<std::vector<real_t>> Lt;
2023-01-26 14:52:49 +01:00
};
2023-01-27 13:01:16 +01:00
CholeskyResult cholesky(std::vector<std::vector<real_t>> A);
2023-01-26 14:52:49 +01:00
2023-01-27 13:01:16 +01:00
real_t sum_elements(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<real_t> flatten(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
std::vector<real_t> solve(std::vector<std::vector<real_t>> A, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
bool positiveDefiniteChecker(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
bool negativeDefiniteChecker(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
bool zeroEigenvalue(std::vector<std::vector<real_t>> A);
2023-01-27 13:01:16 +01:00
void printMatrix(std::vector<std::vector<real_t>> A);
2023-01-24 19:00:54 +01:00
// VECTOR FUNCTIONS
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> outerProduct(std::vector<real_t> a, std::vector<real_t> b); // This multiplies a, bT
2023-01-27 13:01:16 +01:00
std::vector<real_t> hadamard_product(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> elementWiseDivision(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> scalarMultiply(real_t scalar, std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> scalarAdd(real_t scalar, std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> addition(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> subtraction(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> subtractMatrixRows(std::vector<real_t> a, std::vector<std::vector<real_t>> B);
2023-01-27 13:01:16 +01:00
std::vector<real_t> log(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> log10(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> exp(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> erf(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> exponentiate(std::vector<real_t> a, real_t p);
2023-01-27 13:01:16 +01:00
std::vector<real_t> sqrt(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> cbrt(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
real_t dot(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> cross(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> abs(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> zerovec(int n);
2023-01-27 13:01:16 +01:00
std::vector<real_t> onevec(int n);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> diag(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> full(int n, int k);
2023-01-27 13:01:16 +01:00
std::vector<real_t> sin(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> cos(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> max(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
real_t max(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
real_t min(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
std::vector<real_t> round(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
real_t euclideanDistance(std::vector<real_t> a, std::vector<real_t> b);
real_t euclidean_distance(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
real_t euclidean_distance_squared(const Ref<MLPPVector> &a, const Ref<MLPPVector> &b);
2023-01-27 13:01:16 +01:00
real_t norm_2(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
real_t norm_sq(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
real_t sum_elements(std::vector<real_t> a);
2023-01-27 13:01:16 +01:00
real_t cosineSimilarity(std::vector<real_t> a, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
void printVector(std::vector<real_t> a);
2023-01-24 19:00:54 +01:00
// MATRIX-VECTOR FUNCTIONS
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> mat_vec_add(std::vector<std::vector<real_t>> A, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> mat_vec_mult(std::vector<std::vector<real_t>> A, std::vector<real_t> b);
2023-01-24 19:00:54 +01:00
// TENSOR FUNCTIONS
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> addition(std::vector<std::vector<std::vector<real_t>>> A, std::vector<std::vector<std::vector<real_t>>> B);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> elementWiseDivision(std::vector<std::vector<std::vector<real_t>>> A, std::vector<std::vector<std::vector<real_t>>> B);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> sqrt(std::vector<std::vector<std::vector<real_t>>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> exponentiate(std::vector<std::vector<std::vector<real_t>>> A, real_t p);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> tensor_vec_mult(std::vector<std::vector<std::vector<real_t>>> A, std::vector<real_t> b);
2023-01-27 13:01:16 +01:00
std::vector<real_t> flatten(std::vector<std::vector<std::vector<real_t>>> A);
2023-01-27 13:01:16 +01:00
void printTensor(std::vector<std::vector<std::vector<real_t>>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> scalarMultiply(real_t scalar, std::vector<std::vector<std::vector<real_t>>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> scalarAdd(real_t scalar, std::vector<std::vector<std::vector<real_t>>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> resize(std::vector<std::vector<std::vector<real_t>>> A, std::vector<std::vector<std::vector<real_t>>> B);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> hadamard_product(std::vector<std::vector<std::vector<real_t>>> A, std::vector<std::vector<std::vector<real_t>>> B);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> max(std::vector<std::vector<std::vector<real_t>>> A, std::vector<std::vector<std::vector<real_t>>> B);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> abs(std::vector<std::vector<std::vector<real_t>>> A);
2023-01-27 13:01:16 +01:00
real_t norm_2(std::vector<std::vector<std::vector<real_t>>> A);
2023-01-27 13:01:16 +01:00
std::vector<std::vector<std::vector<real_t>>> vector_wise_tensor_product(std::vector<std::vector<std::vector<real_t>>> A, std::vector<std::vector<real_t>> B);
2023-01-24 19:00:54 +01:00
private:
};
#endif /* LinAlg_hpp */