I would like to write a function in OCaml that will calculate the definite integral for the given function. The problem is that I aim for the following syntax:
let sphere r phi theta = r *. sin phi *. cos theta in
let dphi = 10 in (* number of parts *)
let dtheta = 10 in (* number of parts *)
let zero = 0.0 in
let two_pi = 2.0 *. 3.14159
in
integral zero two_pi (integral zero two_pi (sphere 3.0) dphi) dtheta
The problem is that using rule like trapezoidal rule I need to write something like:
0.5 *. (f a +. f b) *. d
Which expects that the f a
and f b
are not partially applicated functions.
I don't expect that the result from the last integral
function call will return me a float number, I'm totally fine with some functional.
I've realized that the question is very unspecific. Let me restate it in a more general way:
I have a function float->float->float
which after the application of integral
function should give me float->float
. It should be general, so the integral
of float->float
should result in float
.
The problem is that I need subtract two functions of the same order: f(a) -. f(b)
, where both of them could be float->float->float
, float->float
or even float->float->float
.
To decrease the order of a function I need a signature like: (float->'a->float) -> ('a->float)
.
Is this even possible? Specifically in OCaml?
The more I think about this problem of having one function calculating the integral that can be chained, the more it seems like an impossible task/stupid way to do it.
In fact I've implemented this but using my own data type (called function_type
which can be Scalar3rdOrderFunction
, Scalar2ndOrderFunction
, Scalar1stOrderFunction
, Scalar0thOrderFunction
). But for the prize of polymorphism the compiler cannot warn me when I try apply the integral three times for function float->float->float
.