restriction of a two-parameters function to a one-parameter function

Discussion in 'C++' started by Boris Sargos, Apr 23, 2004.

1. Boris SargosGuest

Hi,

suppose we have these two functions (not very interesting, but this is for
clarity) :

-) a function Primitive that computes at t the primitive of a function
f :

double Primitive ( double(*f)(double), t);

-) a function Indicatrice that defines if the parameter t is in the
interval [i, i+1] :

double Indicatrice ( int, double );

I'd like to write a function Indicatrice_i, restriction of Indicatrice to
i, like this :

double (*Indicatrice_i)(double) = Indicatrice(i);

to use it such this manner :

for(int i ...; {
// Computation of the primitive at t
double It = Primitive ( Indicatrice_i , t );
}

Someone could help ?

Thanks.
Boris.

Boris Sargos, Apr 23, 2004

2. BusterGuest

Boris Sargos wrote:
> Hi,
>
> suppose we have these two functions (not very interesting, but this is for
> clarity) :
>
> -) a function Primitive that computes at t the primitive of a function
> f :
>
> double Primitive ( double(*f)(double), t);

I haven't done much maths in French but I think I "primitive" means
antiderivative. In that case your description of this function is
nonsense.

> -) a function Indicatrice that defines if the parameter t is in the
> interval [i, i+1] :
>
> double Indicatrice ( int, double );

The characteristic function of the interval.

> I'd like to write a function Indicatrice_i, restriction of Indicatrice to
> i, like this :
>
> double (*Indicatrice_i)(double) = Indicatrice(i);
> to use it such this manner :
>
> for(int i ...; {
> // Computation of the primitive at t
> double It = Primitive ( Indicatrice_i , t );
> }
>
> Someone could help ?

Identifiers do not work like that.

You could use "std::bind1st (std:tr_fun (Indicatrice), i)". The type
of that expression is complicated so (a) it's not straightforward to
give the function object a name, and (b) you have to modify Primitive to
accept function objects of any appropriate type.

#include <functional>
#include <limits>
#include <iostream>

template <typename F> double Primitive (F f, double t)
{
// Note: here the function object has a name.
return std::numeric_limits <double>::quiet_NaN ();
}

double Indicatrice (int i, double t)
{
return i <= t && t <= i + 1 ? 1.0 : 0.0;
}

int main ()
{
double t = 0.0;
for (int i = 0; i != 10; ++ i)
{
using std::bind1st;
using std:tr_fun;
std::cout << Primitive (bind1st (ptr_fun (Indicatrice), i), t)
<< '\n';
}
}

--
Regards,
Buster.

Buster, Apr 23, 2004

3. Boris SargosGuest

Hi Buster,

thank you very much for your answer. It's exactly what I was looking for.
Great !

> I haven't done much maths in French but I think I "primitive" means

antiderivative.
Ok, thank you too for the lesson of english

> In that case your description of this function is nonsense.

Mathematically, you're right, of course. But we can give it a sense if we
choose an origin value. For example, suppose F the antiderivative of f. Then
we can choose F(0) = 0 (if f is well-defined around 0) like this :

double Primitive ( double(*f)(double), t ) {
return Simson ( f, t, 0) ;
}

Thanks again.
Boris

Boris Sargos, Apr 23, 2004
4. Boris SargosGuest

Hi Buster,

thank you very much for your answer. It's exactly what I was looking for.
Great !

> I haven't done much maths in French but I think I "primitive" means

antiderivative.
Ok, thank you too for the lesson of english

> In that case your description of this function is nonsense.

Mathematically, you're right, of course. But we can give it a sense if we
choose an origin value. For example, suppose F the antiderivative of f. Then
we can choose F(0) = 0 (if f is well-defined around 0) like this :

double Primitive ( double(*f)(double), t ) {
return Simson ( f, t, 0) ;
}

Thanks again.
Boris

Boris Sargos, Apr 23, 2004