Boost logo

Boost Users :

Subject: [Boost-users] proto: analytical-only math functions
From: Hicham Mouline (hicham_at_[hidden])
Date: 2009-02-02 15:35:07


Hello,

There are a few posts these days about generic libraries for math derivation
of math functions, analytically or numerically.

My objective is to start simple with analytical functions, and to expand the
result gradually.

I have used proto to define terminals :

1. 1D constants

2. 1D variables

3. 1D basic functions <double (double)> ( c++03 cmath + boost::math )

4. user-defined functions ( 0 or more dimensions )

 

HEADER

struct constant_tag {};

typedef proto::terminal< constant_tag >::type constant;

struct variable_tag {};

typedef proto::terminal< variable_tag >::type variable;

template < double (*basic_function_ptr)(double) >

struct basic_function_tag {

  typedef double result_type;

  result_type operator()(double d) const { return basic_function_ptr(d); }

};
proto::terminal< basic_function_tag<std::log> >::type const log = {{}}; //
1 example, I have one of these for each function

template <size_t dim =1> struct function_tag {};

template <size_t dim>

struct function : proto::terminal< function_tag<1> >::type

{};

USER CODE

------------------
constant c;
variable x,y,z,t,u,v;

function<0> h;
function<1> f;
function<5> g;

My grammar will be very simple for a start. All I want to allow are
statements like:
1.variable = constant(proto terminal) or literal (actual number, does it
have to be proto terminal), for e.g.
                    x = 5;

2. function definition with the = operator. The LHS of the operator can one
of:
                    h =
                    h() =
                    f(x) =
                    g(x,y,z,t,u) =
// All arguments of f should be "variable"s only and they can't be repeated,
and they have to match the dimension

my LHS grammar looks like:

template <size_t dim>

struct function_def_lhs_grammar

  : proto::function< function_tag<dim>, proto::vararg< proto::terminal<
variable_tag > > >

{};

 

 

template <>

struct function_def_lhs_grammar<0>

  : proto::or_<

  proto::terminal< function_tag<0> >,

  proto::function< function_tag<0>, proto::vararg< proto::terminal<
variable_tag > > >

{};

But vararg is not what I want at all:
I want to have 0 variables (nothing) for the 0-dim function in the
proto::function case,

And I want to have exactly dim variables in the dim>0 proto::function case.

 

 

Is it possible to "math-define" the function at the same time of the
function<> object definition (in c++ terms) and have the dimension deducted,
like:

function f(x,y,z) = x+y+z ;

 

The next step will be to worry about the RHS which is the much more
complicated bit,

 

regards,



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net