I have the following exercise to make a class that calculates several of the properties of a mathematical function.
Consider a function
y=f(x)
defined on an interval[a,b]
. You want to sample this function. To this end, the values of will be stored in one vector, and the values of corresponding to each value of will be stored inx
y
another . This problem seeks to create a class that allows storing the sampled function and carrying out certain calculations: first derivative, second derivative, relative maximums and minimums.y
x
To this end, it is requested:
Design a class (not implement) that stores a sampled function, its first and second derivatives, and allows certain operations to be performed. For this it must include:
Attributes that allow to store x , y , y' and y''
A constructor that is passed a function
y=f(x)
, an interval[a,b]
, and the number of samples as parametersn
, and creates and stores x , y , y' and y''
I tried to start the class like this but got stuck:
Trying to do part a)
class function{
private:
vector_item_t* x_; // x abajo os dejo la clase vector_t en la que me base
vector_item_t* y_; // y
vector_item_t* y1; // y'
vector_item_t* y2; // y''
public:
/*b) constructor */ function(¿?)
}
I got stuck in the second section, how do I make the parameter that calculates the image of x? As they are vectors, I don't know how the first and second derivatives should be programmed, because they are not polynomials that are ordered according to their degree.
I appreciate any ideas.
classvector_t
typedef double vector_item_t;
typedef unsigned short int vector_inx_t;
class vector_t {
private:
vector_item_t* v_;
vector_inx_t sz_;
public:
vector_t (vector_inx_t n);
~vector_t (void);
void mostrarVector(void);
vector_item_t get_vector_item(vector_inx_t i);
void set_vector_item(vector_inx_t i, vector_item_t it);
vector_inx_t get_sz(void);
};
If I understand the statement correctly, the exercise consists of a class that, given a function, samples it in
n
steps in a closed interval betweena
andb
, for this it is necessary:Thus, you will need to pass a function that returns a value and receives a value, assuming that both return and parameter are of type,
double
we are talking about functions that have the same signature as this:Being
parametro
the value ofx
and the return offuncion
the value ofy
.Pointers to function.
C++ is a strongly statically typed language , this implies (among other things) that everything (including functions) has an underlying type, C++ function types follow the following format:
retorno([parámetros...])
, so the type of a function that receives adouble
and returns adouble
would bedouble(double)
.Once we know the type we want to work with, we can use it as a parameter, this is an example of an object that stores a function:
It can be used like this 1 :
function object .
As an alternative to the function pointer, a function object can be used:
The advantage of the function object over the function pointer is that it can store lambdas and functors , which function pointer 2 cannot do , it can be used like this:
Your case.
To finish fulfilling the statement, you will need three additional parameters (two for interval and one for sampling):
You can see the code working in [Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ.
1 Function is passed but pointer is stored, functions have implicit conversion to pointer-to-function.
2 If (and only if) the lambda does not capture values, it can be saved in pointer-to-function.