List Info

Thread: "ocaml_beginners"::[] Using automatic differentiation module




"ocaml_beginners"::[] Using automatic differentiation module
user name
2006-11-24 11:59:32

On Fri, Nov 24, 2006 at 11:11:14AM -0000, a_lyckegaard wrote:
> 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)

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!

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: Using automatic differentiation module
user name
2006-11-28 09:13:50

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, Richard Jones <rich...> wrote:
&gt; > 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.

> 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

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.

> 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:
&gt;
> ocamlc -c diff.ml
&gt;
> Note that this code, per se, doesn't do anything except define this
> strange functor thing (remember: the hardest part of the module system
&gt; 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));
&gt; print_endline "&quot;;
> let d_sin = d sin in
> print_diff stdout (d_sin (C 1.0));
&gt; print_endline "&quot;
>
> Compile and link together:
>
> 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

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )