Skip to content
Snippets Groups Projects
Commit cf748c4a authored by Jakhes's avatar Jakhes
Browse files

Extracting the helper funktions into their own helper file

parent bd4fe62f
Branches
No related tags found
No related merge requests found
#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;
}
#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
...@@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr ...@@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr
METHOD_NAME=bayesian_linear_regression METHOD_NAME=bayesian_linear_regression
$(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp $(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: clean:
rm $(METHOD_NAME).so rm $(METHOD_NAME).so
\ No newline at end of file
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
#include <mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp> #include <mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp>
#include <mlpack/core.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 arma;
using namespace mlpack; using namespace mlpack;
using namespace std; using namespace std;
...@@ -14,49 +17,6 @@ using namespace mlpack::regression; ...@@ -14,49 +17,6 @@ using namespace mlpack::regression;
// Global Variable of the BayesianLinearRegression object so it can be accessed from all functions // Global Variable of the BayesianLinearRegression object so it can be accessed from all functions
BayesianLinearRegression regressor; 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 // input: const bool , const bool , const size_t , const double
// output: double // output: double
void initModel(SP_integer centerData, SP_integer scaleData, SP_integer nIterMax, double tol) void initModel(SP_integer centerData, SP_integer scaleData, SP_integer nIterMax, double tol)
......
:- 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), :- load_files(library(str_decl),
[when(compile_time), if(changed)]). [when(compile_time), if(changed)]).
......
...@@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr ...@@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr
METHOD_NAME=linear_regression METHOD_NAME=linear_regression
$(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp $(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: clean:
rm $(METHOD_NAME).so rm $(METHOD_NAME).so
...@@ -3,9 +3,12 @@ ...@@ -3,9 +3,12 @@
Always include the glue header in your foreign resource code. Always include the glue header in your foreign resource code.
*/ */
#include "linear_regression_glue.h" #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> #include <mlpack/core.hpp>
// including helper functions for converting between arma structures and arrays
#include "../../helper_files/helper.hpp"
using namespace arma; using namespace arma;
using namespace mlpack; using namespace mlpack;
using namespace std; using namespace std;
...@@ -14,49 +17,6 @@ using namespace mlpack::regression; ...@@ -14,49 +17,6 @@ using namespace mlpack::regression;
// Global Variable of the LinearRegression object so it can be accessed from all functions // Global Variable of the LinearRegression object so it can be accessed from all functions
LinearRegression regressor; 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 // input: const bool , const bool , const size_t , const double
// output: double // output: double
void initModel(SP_integer centerData, SP_integer scaleData, SP_integer nIterMax, double tol) void initModel(SP_integer centerData, SP_integer scaleData, SP_integer nIterMax, double tol)
......
...@@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr ...@@ -3,6 +3,6 @@ splfr=/usr/local/sicstus4.7.1/bin/splfr
METHOD_NAME=new_method METHOD_NAME=new_method
$(METHOD_NAME).so: $(METHOD_NAME).pl $(METHOD_NAME).cpp $(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: clean:
rm $(METHOD_NAME).so rm $(METHOD_NAME).so
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
#include <mlpack/methods/new_method.hpp> #include <mlpack/methods/new_method.hpp>
#include <mlpack/core.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 arma;
using namespace mlpack; using namespace mlpack;
using namespace std; using namespace std;
...@@ -14,48 +17,6 @@ using namespace mlpack::regression; ...@@ -14,48 +17,6 @@ using namespace mlpack::regression;
// Global Variable of the BayesianLinearRegression object so it can be accessed from all functions // Global Variable of the BayesianLinearRegression object so it can be accessed from all functions
BayesianLinearRegression regressor; 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 // input: const bool , const bool , const size_t , const double
// output: double // output: double
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment