Discussion:
Using automatic differentiation module
a_lyckegaard
2006-11-24 11:11:14 UTC
Permalink
Hi,

I found this very nice module by Will Farr for automatic
differentiation here:
http://wmfarr.blogspot.com/2006/10/automatic-differentiation-in-ocaml.html

Now I wanted to try out his code, but I can't seem to grasp the Ocaml
module system. I think it is completely obvious, but I can't seem to
get it working.

Can anyone give me an example.

d/dx sin(x)

Thanks
Anders
Richard Jones
2006-11-24 11:59:32 UTC
Permalink
Post by a_lyckegaard
I found this very nice module by Will Farr for automatic
http://wmfarr.blogspot.com/2006/10/automatic-differentiation-in-ocaml.html
Now I wanted to try out his code, but I can't seem to grasp the Ocaml
module system. I think it is completely obvious, but I can't seem to
get it working.
Can anyone give me an example.
d/dx sin(x)
Will's code is a functor, which is probably the hardest part
of the module system to grasp.

If you want to pick up modules in general, try:
http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html or
http://www.ocaml-tutorial.org/the_structure_of_ocaml_programs (scroll
down to "Modules and open").

OK back to Will's code though ... He issued a corrected version here:
http://wmfarr.blogspot.com/2006/10/correction-to-automatic.html

Paste his (corrected) code into a file called "diff.ml"
(http://annexia.org/tmp/diff.ml) and compile it using:

ocamlc -c diff.ml

Note that this code, per se, doesn't do anything except define this
strange functor thing (remember: the hardest part of the module system
to understand). To get it to do something we need to write the main
part of the program, so create another file called test_diff.ml
(http://annexia.org/tmp/test_diff.ml) containing:

open Diff.DFloat;;

let () =
print_diff stdout (sin (C 1.0));
print_endline "";
let d_sin = d sin in
print_diff stdout (d_sin (C 1.0));
print_endline ""

Compile and link together:

ocamlc -c test_diff.ml
ocamlc -o test_diff diff.cmo test_diff.cmo
./test_diff

Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!
a_lyckegaard
2006-11-28 09:13:50 UTC
Permalink
Post by Richard Jones
Post by a_lyckegaard
Now I wanted to try out his code, but I can't seem to grasp the Ocaml
module system. I think it is completely obvious, but I can't seem to
get it working.
Will's code is a functor, which is probably the hardest part
of the module system to grasp.
Thank you, now I feel slightly less foolish.
Post by Richard Jones
http://caml.inria.fr/pub/docs/manual-ocaml/manual004.html or
http://www.ocaml-tutorial.org/the_structure_of_ocaml_programs
Thank you, I did find the reference in the manual, but I'll give it
another try. Now that I have some working code to experiment with I
hope it will be easier to study.
Post by Richard Jones
http://wmfarr.blogspot.com/2006/10/correction-to-automatic.html
Paste his (corrected) code into a file called "diff.ml"
ocamlc -c diff.ml
Note that this code, per se, doesn't do anything except define this
strange functor thing (remember: the hardest part of the module system
to understand). To get it to do something we need to write the main
part of the program, so create another file called test_diff.ml
open Diff.DFloat;;
let () =
print_diff stdout (sin (C 1.0));
print_endline "";
let d_sin = d sin in
print_diff stdout (d_sin (C 1.0));
print_endline ""
ocamlc -c test_diff.ml
ocamlc -o test_diff diff.cmo test_diff.cmo
./test_diff
Thank you for the elaborate example. It works perfectly.

Cheers,
Anders

Loading...