pmlpp/mlpp/lin_alg/lin_alg.h

262 lines
9.4 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.
//
#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
std::vector<std::vector<double>> gramMatrix(std::vector<std::vector<double>> A);
bool linearIndependenceChecker(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> gaussianNoise(int n, int m);
std::vector<std::vector<double>> addition(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B);
std::vector<std::vector<double>> subtraction(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B);
std::vector<std::vector<double>> matmult(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B);
std::vector<std::vector<double>> hadamard_product(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B);
std::vector<std::vector<double>> kronecker_product(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B);
std::vector<std::vector<double>> elementWiseDivision(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B);
std::vector<std::vector<double>> transpose(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> scalarMultiply(double scalar, std::vector<std::vector<double>> A);
std::vector<std::vector<double>> scalarAdd(double scalar, std::vector<std::vector<double>> A);
std::vector<std::vector<double>> log(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> log10(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> exp(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> erf(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> exponentiate(std::vector<std::vector<double>> A, double p);
std::vector<std::vector<double>> sqrt(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> cbrt(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> matrixPower(std::vector<std::vector<double>> A, int n);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> abs(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
double det(std::vector<std::vector<double>> A, int d);
2023-01-24 19:00:54 +01:00
double trace(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> cofactor(std::vector<std::vector<double>> A, int n, int i, int j);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> adjoint(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> inverse(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> pinverse(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> zeromat(int n, int m);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> onemat(int n, int m);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> full(int n, int m, int k);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> sin(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> cos(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> rotate(std::vector<std::vector<double>> A, double theta, int axis = -1);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> max(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B);
2023-01-24 19:00:54 +01:00
double max(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
double min(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> round(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
double norm_2(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> identity(double d);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> cov(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::tuple<std::vector<std::vector<double>>, std::vector<std::vector<double>>> eig(std::vector<std::vector<double>> A);
2023-01-26 14:52:49 +01:00
struct EigenResult {
std::vector<std::vector<double>> eigen_vectors;
std::vector<std::vector<double>> eigen_values;
};
EigenResult eigen(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::tuple<std::vector<std::vector<double>>, std::vector<std::vector<double>>, std::vector<std::vector<double>>> SVD(std::vector<std::vector<double>> A);
2023-01-26 14:52:49 +01:00
struct SDVResult {
std::vector<std::vector<double>> U;
std::vector<std::vector<double>> S;
std::vector<std::vector<double>> Vt;
};
SDVResult svd(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<double> vectorProjection(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> gramSchmidtProcess(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::tuple<std::vector<std::vector<double>>, std::vector<std::vector<double>>> QRD(std::vector<std::vector<double>> A);
2023-01-26 14:52:49 +01:00
struct QRDResult {
std::vector<std::vector<double>> Q;
std::vector<std::vector<double>> R;
};
QRDResult qrd(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::tuple<std::vector<std::vector<double>>, std::vector<std::vector<double>>> chol(std::vector<std::vector<double>> A);
2023-01-26 14:52:49 +01:00
struct CholeskyResult {
std::vector<std::vector<double>> L;
std::vector<std::vector<double>> Lt;
};
CholeskyResult cholesky(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
double sum_elements(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<double> flatten(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
std::vector<double> solve(std::vector<std::vector<double>> A, std::vector<double> b);
2023-01-24 19:00:54 +01:00
bool positiveDefiniteChecker(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
bool negativeDefiniteChecker(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
bool zeroEigenvalue(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
void printMatrix(std::vector<std::vector<double>> A);
2023-01-24 19:00:54 +01:00
// VECTOR FUNCTIONS
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> outerProduct(std::vector<double> a, std::vector<double> b); // This multiplies a, bT
2023-01-24 19:00:54 +01:00
std::vector<double> hadamard_product(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> elementWiseDivision(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> scalarMultiply(double scalar, std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> scalarAdd(double scalar, std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> addition(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> subtraction(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> subtractMatrixRows(std::vector<double> a, std::vector<std::vector<double>> B);
2023-01-24 19:00:54 +01:00
std::vector<double> log(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> log10(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> exp(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> erf(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> exponentiate(std::vector<double> a, double p);
2023-01-24 19:00:54 +01:00
std::vector<double> sqrt(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> cbrt(std::vector<double> a);
2023-01-24 19:00:54 +01:00
double dot(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> cross(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> abs(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> zerovec(int n);
2023-01-24 19:00:54 +01:00
std::vector<double> onevec(int n);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> diag(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> full(int n, int k);
2023-01-24 19:00:54 +01:00
std::vector<double> sin(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> cos(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> max(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
double max(std::vector<double> a);
2023-01-24 19:00:54 +01:00
double min(std::vector<double> a);
2023-01-24 19:00:54 +01:00
std::vector<double> round(std::vector<double> a);
2023-01-24 19:00:54 +01:00
double euclideanDistance(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
double norm_2(std::vector<double> a);
2023-01-24 19:00:54 +01:00
double norm_sq(std::vector<double> a);
2023-01-24 19:00:54 +01:00
double sum_elements(std::vector<double> a);
2023-01-24 19:00:54 +01:00
double cosineSimilarity(std::vector<double> a, std::vector<double> b);
2023-01-24 19:00:54 +01:00
void printVector(std::vector<double> a);
2023-01-24 19:00:54 +01:00
// MATRIX-VECTOR FUNCTIONS
std::vector<std::vector<double>> mat_vec_add(std::vector<std::vector<double>> A, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> mat_vec_mult(std::vector<std::vector<double>> A, std::vector<double> b);
2023-01-24 19:00:54 +01:00
// TENSOR FUNCTIONS
std::vector<std::vector<std::vector<double>>> addition(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<std::vector<double>>> B);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> elementWiseDivision(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<std::vector<double>>> B);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> sqrt(std::vector<std::vector<std::vector<double>>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> exponentiate(std::vector<std::vector<std::vector<double>>> A, double p);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<double>> tensor_vec_mult(std::vector<std::vector<std::vector<double>>> A, std::vector<double> b);
2023-01-24 19:00:54 +01:00
std::vector<double> flatten(std::vector<std::vector<std::vector<double>>> A);
2023-01-24 19:00:54 +01:00
void printTensor(std::vector<std::vector<std::vector<double>>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> scalarMultiply(double scalar, std::vector<std::vector<std::vector<double>>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> scalarAdd(double scalar, std::vector<std::vector<std::vector<double>>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> resize(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<std::vector<double>>> B);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> hadamard_product(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<std::vector<double>>> B);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> max(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<std::vector<double>>> B);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> abs(std::vector<std::vector<std::vector<double>>> A);
2023-01-24 19:00:54 +01:00
double norm_2(std::vector<std::vector<std::vector<double>>> A);
2023-01-24 19:00:54 +01:00
std::vector<std::vector<std::vector<double>>> vector_wise_tensor_product(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<double>> B);
2023-01-24 19:00:54 +01:00
private:
};
#endif /* LinAlg_hpp */