Added LinAlg.max & LinAlg.min (for matricies), LinAlg.matrixProduct, and LinAlg.sqrt

This commit is contained in:
novak_99 2021-05-30 11:01:11 -07:00
parent ad38db03cd
commit 3a1a85da92
5 changed files with 66 additions and 17 deletions

View File

@ -627,14 +627,14 @@ namespace MLPP{
std::vector<double> Activation::arsinh(std::vector<double> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.onevec(z.size()), alg.exponentiate(alg.addition(alg.hadamard_product(z, z), alg.onevec(z.size())), 0.5)); }
return alg.log(alg.addition(z, alg.exponentiate(alg.addition(alg.hadamard_product(z, z), alg.onevec(z.size())), 0.5)));
if(deriv){ return alg.elementWiseDivision(alg.onevec(z.size()), alg.sqrt(alg.addition(alg.hadamard_product(z, z), alg.onevec(z.size())))); }
return alg.log(alg.addition(z, alg.sqrt(alg.addition(alg.hadamard_product(z, z), alg.onevec(z.size())))));
}
std::vector<std::vector<double>> Activation::arsinh(std::vector<std::vector<double>> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.exponentiate(alg.addition(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())), 0.5)); }
return alg.log(alg.addition(z, alg.exponentiate(alg.addition(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())), 0.5)));
if(deriv){ return alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.sqrt(alg.addition(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())))); }
return alg.log(alg.addition(z, alg.sqrt(alg.addition(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())))));
}
double Activation::arcosh(double z, bool deriv){
@ -646,14 +646,14 @@ namespace MLPP{
std::vector<double> Activation::arcosh(std::vector<double> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.onevec(z.size()), alg.exponentiate(alg.subtraction(alg.hadamard_product(z, z), alg.onevec(z.size())), 0.5)); }
return alg.log(alg.addition(z, alg.exponentiate(alg.subtraction(alg.hadamard_product(z, z), alg.onevec(z.size())), 0.5)));
if(deriv){ return alg.elementWiseDivision(alg.onevec(z.size()), alg.sqrt(alg.subtraction(alg.hadamard_product(z, z), alg.onevec(z.size())))); }
return alg.log(alg.addition(z, alg.sqrt(alg.subtraction(alg.hadamard_product(z, z), alg.onevec(z.size())))));
}
std::vector<std::vector<double>> Activation::arcosh(std::vector<std::vector<double>> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.exponentiate(alg.subtraction(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())), 0.5)); }
return alg.log(alg.addition(z, alg.exponentiate(alg.subtraction(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())), 0.5)));
if(deriv){ return alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.sqrt(alg.subtraction(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())))); }
return alg.log(alg.addition(z, alg.sqrt(alg.subtraction(alg.hadamard_product(z, z), alg.onemat(z.size(), z[0].size())))));
}
double Activation::artanh(double z, bool deriv){
@ -684,14 +684,14 @@ namespace MLPP{
std::vector<double> Activation::arcsch(std::vector<double> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), -1), alg.hadamard_product(alg.hadamard_product(z, z), alg.exponentiate(alg.addition(alg.onevec(z.size()), alg.elementWiseDivision(alg.onevec(z.size()), alg.hadamard_product(z, z))), 0.5))); }
return alg.log(alg.addition(alg.exponentiate(alg.addition(alg.onevec(z.size()), alg.elementWiseDivision(alg.onevec(z.size()), alg.hadamard_product(z, z))), 0.5), alg.elementWiseDivision(alg.onevec(z.size()), z)));
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), -1), alg.hadamard_product(alg.hadamard_product(z, z), alg.sqrt(alg.addition(alg.onevec(z.size()), alg.elementWiseDivision(alg.onevec(z.size()), alg.hadamard_product(z, z)))))); }
return alg.log(alg.addition(alg.sqrt(alg.addition(alg.onevec(z.size()), alg.elementWiseDivision(alg.onevec(z.size()), alg.hadamard_product(z, z)))), alg.elementWiseDivision(alg.onevec(z.size()), z)));
}
std::vector<std::vector<double>> Activation::arcsch(std::vector<std::vector<double>> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), z[0].size(), -1), alg.hadamard_product(alg.hadamard_product(z, z), alg.exponentiate(alg.addition(alg.onemat(z.size(), z[0].size()), alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.hadamard_product(z, z))), 0.5))); }
return alg.log(alg.addition(alg.exponentiate(alg.addition(alg.onemat(z.size(), z[0].size()), alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.hadamard_product(z, z))), 0.5), alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), z)));
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), z[0].size(), -1), alg.hadamard_product(alg.hadamard_product(z, z), alg.sqrt(alg.addition(alg.onemat(z.size(), z[0].size()), alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.hadamard_product(z, z)))))); }
return alg.log(alg.addition(alg.sqrt(alg.addition(alg.onemat(z.size(), z[0].size()), alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.hadamard_product(z, z)))), alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), z)));
}
@ -704,13 +704,13 @@ namespace MLPP{
std::vector<double> Activation::arsech(std::vector<double> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), -1), alg.hadamard_product(z, alg.exponentiate(alg.subtraction(alg.onevec(z.size()), alg.hadamard_product(z, z)), 0.5))); }
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), -1), alg.hadamard_product(z, alg.sqrt(alg.subtraction(alg.onevec(z.size()), alg.hadamard_product(z, z))))); }
return alg.log(alg.addition(alg.elementWiseDivision(alg.onevec(z.size()), z), alg.hadamard_product(alg.addition(alg.elementWiseDivision(alg.onevec(z.size()), z), alg.onevec(z.size())), alg.subtraction(alg.elementWiseDivision(alg.onevec(z.size()), z), alg.onevec(z.size())))));
}
std::vector<std::vector<double>> Activation::arsech(std::vector<std::vector<double>> z, bool deriv){
LinAlg alg;
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), z[0].size(), -1), alg.hadamard_product(z, alg.exponentiate(alg.subtraction(alg.onemat(z.size(), z[0].size()), alg.hadamard_product(z, z)), 0.5))); }
if(deriv){ return alg.elementWiseDivision(alg.full(z.size(), z[0].size(), -1), alg.hadamard_product(z, alg.sqrt(alg.subtraction(alg.onemat(z.size(), z[0].size()), alg.hadamard_product(z, z))))); }
return alg.log(alg.addition(alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), z), alg.hadamard_product(alg.addition(alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), z), alg.onemat(z.size(), z[0].size())), alg.subtraction(alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), z), alg.onemat(z.size(), z[0].size())))));
}

View File

@ -76,7 +76,7 @@ namespace MLPP{
std::vector<std::vector<double>> LinAlg::kronecker_product(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B){
std::vector<std::vector<double>> C;
// [1,1,1,1] [1,2,3,4,5]
// [1,1,1,1] [1,2,3,4,5]
// [1,2,3,4,5]
@ -215,6 +215,24 @@ namespace MLPP{
return A;
}
std::vector<std::vector<double>> LinAlg::sqrt(std::vector<std::vector<double>> A){
return exponentiate(A, 0.5);
}
std::vector<std::vector<double>> LinAlg::matrixPower(std::vector<std::vector<double>> A, int n){
std::vector<std::vector<double>> B = identity(A.size());
if(n == 0){
return identity(A.size());
}
else if(n < 0){
A = inverse(A);
}
for(int i = 0; i < abs(n); i++){
B = matmult(B, A);
}
return B;
}
double LinAlg::det(std::vector<std::vector<double>> A, int d){
double deter = 0;
@ -354,6 +372,22 @@ namespace MLPP{
return full;
}
double LinAlg::max(std::vector<std::vector<double>> A){
std::vector<double> max_elements;
for(int i = 0; i < A.size(); i++){
max_elements.push_back(max(A[i]));
}
return max(max_elements);
}
double LinAlg::min(std::vector<std::vector<double>> A){
std::vector<double> max_elements;
for(int i = 0; i < A.size(); i++){
max_elements.push_back(min(A[i]));
}
return min(max_elements);
}
std::vector<std::vector<double>> LinAlg::round(std::vector<std::vector<double>> A){
std::vector<std::vector<double>> B;
B.resize(A.size());
@ -529,7 +563,7 @@ namespace MLPP{
auto [left_eigenvecs, eigenvals] = eig(matmult(A, transpose(A)));
auto [right_eigenvecs, right_eigenvals] = eig(matmult(transpose(A), A));
std::vector<std::vector<double>> singularvals = exponentiate(eigenvals, 0.5);
std::vector<std::vector<double>> singularvals = sqrt(eigenvals);
std::vector<std::vector<double>> sigma = zeromat(A.size(), A[0].size());
for(int i = 0; i < singularvals.size(); i++){
for(int j = 0; j < singularvals[i].size(); j++){
@ -682,6 +716,10 @@ namespace MLPP{
return b;
}
std::vector<double> LinAlg::sqrt(std::vector<double> a){
return exponentiate(a, 0.5);
}
double LinAlg::dot(std::vector<double> a, std::vector<double> b){
double c = 0;
for(int i = 0; i < a.size(); i++){
@ -763,7 +801,7 @@ namespace MLPP{
}
double LinAlg::cosineSimilarity(std::vector<double> a, std::vector<double> b){
return dot(a, b) / (sqrt(norm_sq(a)) * sqrt(norm_sq(b)));
return dot(a, b) / (std::sqrt(norm_sq(a)) * std::sqrt(norm_sq(b)));
}
void LinAlg::printVector(std::vector<double> a){

View File

@ -43,6 +43,10 @@ namespace MLPP{
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>> matrixPower(std::vector<std::vector<double>> A, int n);
double det(std::vector<std::vector<double>> A, int d);
@ -62,6 +66,10 @@ namespace MLPP{
std::vector<std::vector<double>> full(int n, int m, int k);
double max(std::vector<std::vector<double>> A);
double min(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> round(std::vector<std::vector<double>> A);
std::vector<std::vector<double>> identity(double d);
@ -105,6 +113,8 @@ namespace MLPP{
std::vector<double> erf(std::vector<double> a);
std::vector<double> exponentiate(std::vector<double> a, double p);
std::vector<double> sqrt(std::vector<double> a);
double dot(std::vector<double> a, std::vector<double> b);

Binary file not shown.

View File

@ -365,6 +365,7 @@ int main() {
// alg.printMatrix(alg.pinverse({{1,2}, {3,4}}));
// alg.printMatrix(alg.diag({1,2,3,4,5}));
// alg.printMatrix(alg.kronecker_product({{1,2,3,4,5}}, {{6,7,8,9,10}}));
// alg.printMatrix(alg.matrixPower({{5,5},{5,5}}, 2));
return 0;
}