0

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.

Bartek Chaber
  • 236
  • 1
  • 3
  • 7
  • I see no problem with the information you provided, it is very much possible. – lukstafi Nov 03 '13 at 10:47
  • 1
    lukstafi, I see one. Look, with function of two arguments you can't evaluate `(f lower -. f upper) /. 2.0` because type of `f lower` is not `float`. – Kakadu Nov 03 '13 at 11:24
  • 5
    I see, `sphere 3.0` is not a function of one argument. What about: `integral zero two_pi (fun th -> integral zero two_pi (sphere 3.0 th) dphi) dtheta`. – lukstafi Nov 03 '13 at 15:26

0 Answers0