From cf748c4a1333cefe36bac42ca60f040d9df7e0e3 Mon Sep 17 00:00:00 2001 From: Jakhes <dean.schmitz@schmitzbauer.de> Date: Thu, 1 Sep 2022 20:33:49 +0200 Subject: [PATCH] Extracting the helper funktions into their own helper file --- src/helper_files/helper.cpp | 45 +++++++++++++++++ src/helper_files/helper.hpp | 18 +++++++ .../bayesian_linear_regression/Makefile | 2 +- .../bayesian_linear_regression.cpp | 46 ++---------------- .../bayesian_linear_regression.pl | 2 +- src/methods/linear_regression/Makefile | 2 +- .../linear_regression/linear_regression.cpp | 48 ++----------------- src/methods/new_method/Makefile | 2 +- src/methods/new_method/new_method.cpp | 45 ++--------------- 9 files changed, 77 insertions(+), 133 deletions(-) create mode 100644 src/helper_files/helper.cpp create mode 100644 src/helper_files/helper.hpp diff --git a/src/helper_files/helper.cpp b/src/helper_files/helper.cpp new file mode 100644 index 0000000..d769073 --- /dev/null +++ b/src/helper_files/helper.cpp @@ -0,0 +1,45 @@ + +#include "helper.hpp" + +// Extra functions to reduce some code for the conversion between arma and float *array + +float *convertArmaToArray(colvec vec) +{ + vector<float> vectorData = conv_to<vector<float>>::from(vec); + int vectorSize = vectorData.size(); + + // using malloc so the memory is still valid outside of the function + float *arr = (float *)malloc (sizeof(float) * vectorSize); + + // save the data in a normal array so it can be send to prolog + for(int i = 0; i < vectorSize; i++) + { + arr[i] = vectorData[i]; + } + return arr; +} +float *convertArmaToArray(rowvec vec) +{ + colvec newVec = conv_to<colvec>::from(vec); + return convertArmaToArray(newVec); +} + +rowvec convertArrayToRowvec(float *arr, int vecSize) +{ + rowvec rVector(vecSize); + for(int i = 0; i < vecSize; i++) + { + rVector[i] = arr[i]; + } + return rVector; +} + +mat convertArrayToMat(float *arr, int vecSize, int rowCount) +{ + mat matrix(rowCount,(vecSize/rowCount)); + for(int i = 0; i < vecSize; i++) + { + matrix[i] = arr[i]; + } + return matrix; +} diff --git a/src/helper_files/helper.hpp b/src/helper_files/helper.hpp new file mode 100644 index 0000000..408868d --- /dev/null +++ b/src/helper_files/helper.hpp @@ -0,0 +1,18 @@ +#ifndef HELPER_HEADER +#define HELPER_HEADER + +#include <mlpack/core.hpp> + +using namespace arma; +using namespace mlpack; +using namespace std; + +float *convertArmaToArray(colvec vec); + +float *convertArmaToArray(rowvec vec); + +rowvec convertArrayToRowvec(float *arr, int vecSize); + +mat convertArrayToMat(float *arr, int vecSize, int rowCount); + +#endif diff --git a/src/methods/bayesian_linear_regression/Makefile b/src/methods/bayesian_linear_regression/Makefile index f61f822..ac7294e 100644 --- a/src/methods/bayesian_linear_regression/Makefile +++ b/src/methods/bayesian_linear_regression/Makefile @@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr METHOD_NAME=bayesian_linear_regression $(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp - $(splfr) -larmadillo -fopenmp -lmlpack -lstdc++ -cxx --struct $(METHOD_NAME).pl $(METHOD_NAME).cpp + $(splfr) -larmadillo -fopenmp -lmlpack -lstdc++ -cxx --struct $(METHOD_NAME).pl $(METHOD_NAME).cpp ../../helper_files/helper.cpp clean: rm $(METHOD_NAME).so \ No newline at end of file diff --git a/src/methods/bayesian_linear_regression/bayesian_linear_regression.cpp b/src/methods/bayesian_linear_regression/bayesian_linear_regression.cpp index 59ab769..d79b7bb 100644 --- a/src/methods/bayesian_linear_regression/bayesian_linear_regression.cpp +++ b/src/methods/bayesian_linear_regression/bayesian_linear_regression.cpp @@ -6,6 +6,9 @@ #include <mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp> #include <mlpack/core.hpp> +// including helper functions for converting between arma structures and arrays +#include "../../helper_files/helper.hpp" + using namespace arma; using namespace mlpack; using namespace std; @@ -14,49 +17,6 @@ using namespace mlpack::regression; // Global Variable of the BayesianLinearRegression object so it can be accessed from all functions BayesianLinearRegression regressor; -// Extra functions to reduce some code for the conversion between arma and float *array -float *convertArmaToArray(colvec vec) -{ - vector<float> vectorData = conv_to<vector<float>>::from(vec); - int vectorSize = vectorData.size(); - - // using malloc so the memory is still valid outside of the function - float *arr = (float *)malloc (sizeof(float) * vectorSize); - - // save the data in a normal array so it can be send to prolog - for(int i = 0; i < vectorSize; i++) - { - arr[i] = vectorData[i]; - } - return arr; -} -float *convertArmaToArray(rowvec vec) -{ - colvec newVec = conv_to<colvec>::from(vec); - return convertArmaToArray(newVec); -} - -rowvec convertArrayToRowvec(float *arr, SP_integer vecSize) -{ - rowvec rVector(vecSize); - for(int i = 0; i < vecSize; i++) - { - rVector[i] = arr[i]; - } - return rVector; -} - -mat convertArrayToMat(float *arr, SP_integer vecSize, SP_integer rowCount) -{ - mat matrix(rowCount,(vecSize/rowCount)); - for(int i = 0; i < vecSize; i++) - { - matrix[i] = arr[i]; - } - return matrix; -} - - // input: const bool , const bool , const size_t , const double // output: double void initModel(SP_integer centerData, SP_integer scaleData, SP_integer nIterMax, double tol) diff --git a/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl b/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl index 663b29e..58b99ef 100644 --- a/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl +++ b/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl @@ -1,4 +1,4 @@ -:- module(bayesian_linear_regression, [initModel/4, alpha/1, beta/1, dataOffset/2, dataScale/2, omega/2, predict/5, predictWithStd/7, rmse/6, train/5, variance/1]). +:- module(bayesian_linear_regression, [initModel/4, alpha/1, beta/1, dataOffset/2, dataScale/2, omega/2, predict/5, predictWithStd/7, rmse/6, train/5, variance/1, predict/2, train/0]). :- load_files(library(str_decl), [when(compile_time), if(changed)]). diff --git a/src/methods/linear_regression/Makefile b/src/methods/linear_regression/Makefile index f5bf954..71995e2 100644 --- a/src/methods/linear_regression/Makefile +++ b/src/methods/linear_regression/Makefile @@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr METHOD_NAME=linear_regression $(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp - $(splfr) -larmadillo -fopenmp -lmlpack -lstdc++ -cxx --struct $(METHOD_NAME).pl $(METHOD_NAME).cpp + $(splfr) -larmadillo -fopenmp -lmlpack -lstdc++ -cxx --struct $(METHOD_NAME).pl $(METHOD_NAME).cpp ../../helper_files/helper.cpp clean: rm $(METHOD_NAME).so diff --git a/src/methods/linear_regression/linear_regression.cpp b/src/methods/linear_regression/linear_regression.cpp index d8a1426..57a231b 100644 --- a/src/methods/linear_regression/linear_regression.cpp +++ b/src/methods/linear_regression/linear_regression.cpp @@ -3,9 +3,12 @@ Always include the glue header in your foreign resource code. */ #include "linear_regression_glue.h" -#include <mlpack/methods/regression/linear_regression/linear_regression.hpp> +#include <mlpack/methods/linear_regression/linear_regression.hpp> #include <mlpack/core.hpp> +// including helper functions for converting between arma structures and arrays +#include "../../helper_files/helper.hpp" + using namespace arma; using namespace mlpack; using namespace std; @@ -14,49 +17,6 @@ using namespace mlpack::regression; // Global Variable of the LinearRegression object so it can be accessed from all functions LinearRegression regressor; -// Extra functions to reduce some code for the conversion between arma and float *array -float *convertArmaToArray(colvec vec) -{ - vector<float> vectorData = conv_to<vector<float>>::from(vec); - int vectorSize = vectorData.size(); - - // using malloc so the memory is still valid outside of the function - float *arr = (float *)malloc (sizeof(float) * vectorSize); - - // save the data in a normal array so it can be send to prolog - for(int i = 0; i < vectorSize; i++) - { - arr[i] = vectorData[i]; - } - return arr; -} -float *convertArmaToArray(rowvec vec) -{ - colvec newVec = conv_to<colvec>::from(vec); - return convertArmaToArray(newVec); -} - -rowvec convertArrayToRowvec(float *arr, SP_integer vecSize) -{ - rowvec rVector(vecSize); - for(int i = 0; i < vecSize; i++) - { - rVector[i] = arr[i]; - } - return rVector; -} - -mat convertArrayToMat(float *arr, SP_integer vecSize, SP_integer rowCount) -{ - mat matrix(rowCount,(vecSize/rowCount)); - for(int i = 0; i < vecSize; i++) - { - matrix[i] = arr[i]; - } - return matrix; -} - - // input: const bool , const bool , const size_t , const double // output: double void initModel(SP_integer centerData, SP_integer scaleData, SP_integer nIterMax, double tol) diff --git a/src/methods/new_method/Makefile b/src/methods/new_method/Makefile index 6530d5c..c95df91 100644 --- a/src/methods/new_method/Makefile +++ b/src/methods/new_method/Makefile @@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr METHOD_NAME=new_method $(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp - $(splfr) -larmadillo -fopenmp -lmlpack -lstdc++ -cxx --struct $(METHOD_NAME).pl $(METHOD_NAME).cpp + $(splfr) -larmadillo -fopenmp -lmlpack -lstdc++ -cxx --struct $(METHOD_NAME).pl $(METHOD_NAME).cpp ../../helper_files/helper.cpp clean: rm $(METHOD_NAME).so diff --git a/src/methods/new_method/new_method.cpp b/src/methods/new_method/new_method.cpp index 9cb96ad..dba044b 100644 --- a/src/methods/new_method/new_method.cpp +++ b/src/methods/new_method/new_method.cpp @@ -6,6 +6,9 @@ #include <mlpack/methods/new_method.hpp> #include <mlpack/core.hpp> +// including helper functions for converting between arma structures and arrays +#include "../../helper_files/helper.hpp" + using namespace arma; using namespace mlpack; using namespace std; @@ -14,48 +17,6 @@ using namespace mlpack::regression; // Global Variable of the BayesianLinearRegression object so it can be accessed from all functions BayesianLinearRegression regressor; -// Extra functions to reduce some code for the conversion between arma and float *array -float *convertArmaToArray(colvec vec) -{ - vector<float> vectorData = conv_to<vector<float>>::from(vec); - int vectorSize = vectorData.size(); - - // using malloc so the memory is still valid outside of the function - float *arr = (float *)malloc (sizeof(float) * vectorSize); - - // save the data in a normal array so it can be send to prolog - for(int i = 0; i < vectorSize; i++) - { - arr[i] = vectorData[i]; - } - return arr; -} -float *convertArmaToArray(rowvec vec) -{ - colvec newVec = conv_to<colvec>::from(vec); - return convertArmaToArray(newVec); -} - -rowvec convertArrayToRowvec(float *arr, SP_integer vecSize) -{ - rowvec rVector(vecSize); - for(int i = 0; i < vecSize; i++) - { - rVector[i] = arr[i]; - } - return rVector; -} - -mat convertArrayToMat(float *arr, SP_integer vecSize, SP_integer rowCount) -{ - mat matrix(rowCount,(vecSize/rowCount)); - for(int i = 0; i < vecSize; i++) - { - matrix[i] = arr[i]; - } - return matrix; -} - // input: const bool , const bool , const size_t , const double // output: double -- GitLab