I want to write a simple gradient descent for a function with 5 parameters on C++. Now I stumble upon a implementation model problem: Should I sacrifice speed for folding my arguments in vector/arrays. Here is what I mean. I can implement funcition value and gradient calculation like this:
double function(double arg1, double arg2, ..., double arg5);
double functionGradient1(double arg1, double arg2, ..., double arg5);
double functionGradient2(double arg1, double arg2, ..., double arg5);
...
double functionGradient5(double arg1, double arg2, ..., double arg5);
or:
double function(double * args);
double functionGradientAt(double * args, int i);
The last one is easier to implement, however I am afraid I'll lose lots of speed if i am going to constantly allocate/free memory.
My questions are:
- Which one would YOU pick?
- If you ever implemented gradient descent, how did you handle the case?