Store functions in classes

1

I have the following exercise to perform a class that calculates several of the properties of a mathematical function.

  

Consider a y=f(x) function defined in a [a,b] interval. You want to sample this function. To this end, the values of x y will be stored in a vector in another the values of y corresponding to each value of x . This problem seeks to create a class that allows storing the sampled function and carry out certain calculations: first derivative, second derivative, relative maximums and minimums.

     

To this end we ask:

     

Design a class (not to implement) that stores a sampled function, its first and second derivatives, and allows certain operations to be performed. For this it must consist of:

     

Attributes that allow to store x , and , and ' and and' '

     

A constructor passed as a parameter a function y=f(x) , a interval [a,b] , and the number of samples n , and create and store x , and , and ' and and' '

I tried to start the class like that but I got stuck:

I'm trying to do section 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 stuck in the second section how I do the parameter that calculates the image of x? when dealing with vectors I do not know how the first and second derivative should be programmed, because they are not polynomials that are ordered according to their degree.

I appreciate any ideas.

class vector_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);
};
    
asked by AER 09.08.2017 в 14:40
source

1 answer

1

If I understand the statement well, the exercise consists of a class that, given a function, shows it in n steps in a closed interval between a and b , for this it is necessary:

  

A constructor that is passed as a parameter a function y=f(x) , a interval [a,b] , and the number of samples n .

So, you will need to pass a function that returns a value and receives a value, assuming that both the return and the parameter are of double , we are talking about functions that have the same signature as this:

double funcion(double parametro) {
    double resultado;

    // hacer cosas...

    return resultado;
}

With parametro the value of x and the return of funcion the value of y .

Function pointers.

C ++ is a strong typing language and static , this implies (among other things) that everything (including functions) has an underlying type, the types of C ++ functions follow the following format: retorno([parámetros...]) , so the type of a function that receives a double and returns a double would be double(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 has a function:

struct funcion
{
    // Tipo de la funcion que recibe y devuelve double
    using tipo_funcion = double(double);

    // El constructor recibe una funcion que recibe y devuelve double
    funcion(tipo_funcion *parametro) :
        miembro{parametro}
    {}

    // El miembro almacena una funcion que recibe y devuelve double
    tipo_funcion *miembro;
};

You can use it like this 1 :

int main()
{
    // Instancia que recibe la funcion coseno
    funcion f(std::cos);

    // Muestra 1 ya que es el valor de coseno de 0.
    std::cout << f.miembro(.0);

    return 0;
}

Function object .

As an alternative to the function pointer, a function object can be used:

struct funcion
{
    // El constructor recibe un OBJETO FUNCION que recibe y devuelve double
    funcion(std::function<double(double)> parametro) :
        miembro{parametro}
    {}

    // El miembro es un OBJETO FUNCION que recibe y devuelve double
    std::function<double(double)> miembro;
};

The advantage of the function object with respect to the function pointer is that it can store lambdas and < a href="https://es.wikipedia.org/wiki/Funtor"> functors , which the pointer to function 2 can not do, can be used like this:

int main()
{
    struct funtor
    {
        double operator()(double d) { return d+d; }
    };

    funcion f([](double d){return d*d;});
    funcion g(funtor{});

    // Muestra .25 (.5 al cuadrado de la lambda) y 1 (el doble de .5 del funtor)
    std::cout << f.miembro(.5) << ' ' << g.miembro(.5);

    return 0;
}

Your case.

To finish complying with the statement, you will need three additional parameters (two for the interval and one for the sampling):

struct funcion
{
    funcion(std::function<double(double)> parametro, double inicio, double fin, double muestras) :
        miembro{parametro}
    {
        const double rango = fin - inicio;
        const double paso = rango / muestras;

        for (double a = inicio; a <= fin; a+= paso)
            muestreo.push_back(miembro(a));
    }

    std::function<double(double)> miembro;
    std::vector<double> muestreo{};
};

You can see the code working in [Wandbox] 三 へ (へ ਊ) へ ハ ッ ハ ッ .

1 Function passed but pointer stored, functions have implicit conversion to pointer-to-function.

2 If (and only if) the lambda does not capture values, it can be stored in pointer-to-function.

    
answered by 10.08.2017 / 09:30
source