I'm trying to make this function called walk
in C. The idea is that the function takes as parameters a point in space (represented by an array), a function (pointer), and a step size / learning rate and moves from the provided point in a direction that minimizes the provided function. The problem that I faced is regarding the input function. In my particular case, the function that should be provided to walk
needs to handle variables (not function variables) that are provided at run time. In my case, the variables are the training data set and the labels. And the variables of the function that should be provided to walk
are different from these. (they are the model's parameters)
As an example, the function that could be provided to walk
can be defined as such:
int foo(int* X, int X_size, int alpha, int beta) {
ret = 0;
for(int k = 0; k < X_size; k++) {
ret = ret + X[k]*X[k]*alpha + beta;
}
return ret;
}
void walk(int* X, int X_size, float step_size, int (*function)(// function variables)) {
// some instructions
}
foo
is meant to represent a multidimensional function of X
, while alpha
and beta
are mere parameters that are defined at runtime. The problem with this sort of definition is that walk
should be hardcoded to know in respect to which variable should the provided function be differentiated, which violates the principle of separation of concerns. It also needs to receive the parameters alpha
and beta
as variables in order to pass them to function
. This doesn't scale well when there is a different number of parameters.
I thought of defining beta
and alpha
as global variables so that I could define foo
as int foot(int* X, int X_size)
and then walk
as:
void walk(int* X, int X_size, float step_size, int (*function)(int* X, int X_size));
But I'm reluctant to use global variables as they tend to come with their own set of problems. If this helps, I intend to make this as a toy library for machine learning.
Is there a better a way that I could go about implementing this? Let me know if I need to add more context or clarifications.