Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #8757
| From | Szabolcs <szhorvat@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: FindMinimum v. NMinimize and an external program |
| Date | 2012-02-26 09:21 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jictj6$emj$1@smc.vnet.net> (permalink) |
| References | <jia0qc$1c4$1@smc.vnet.net> |
On Saturday, 25 February 2012 08:58:20 UTC+2, Ian wrote:
> Can anyone say whether the following behavior is expected and why? I'd like to avoid learning how to use MathLink, but I need to use FindMinimum on a large external calculation and my way is broken.
>
> Say there's a command line program called 'prog' that calculates y = (x-1)^2, reading x from a named pipe called 'in' and writing y to a named pipe called 'out'. Mathematica interacts with prog by reading and writing to the pipes. Like ...
>
> > in = OpenWrite["in"];
> > f[x_?NumericQ]:=(Write[in, CForm[x]]; First[ReadList["!cat out", Number]])
>
> Calling NMinimize on f gives the correct answer, but FindMinimum doesn't. Specifically, FindMinimum[f[x], {x, 0}] returns the message FindMinimum::fmgz. That is, Mathematica thinks the gradient is zero.
>
> I can't find a reason the two functions should behave differently. Any thoughts?
>
> A couple details for the DIYers. Here's prog:
> #include <stdio.h>
> #include <math.h>
> int main()
> {
> FILE * io;
> int j;
> double x,y;
>
> while((j = scanf("%lf", &x)) != EOF)
> {
> y = pow((x-1.0), 2);
> io = fopen("out", "w");
> fprintf(io, "%e\n", y);
> fclose(io);
> }
> return;
> }
>
> Run prog with a redirect for stdin, as in
> /prog < in
Not a direct answer, but I very much recommend learning LibraryLink (not MathLink). Please see the answers to a question I asked about which is the easiest way to integrate Mathematica with C/C++ code:
http://stackoverflow.com/questions/8140869/minimal-effort-method-for-integrating-c-functions-into-mathematica
There's a short but complete guide to setting up a LibraryLink program in Arnoud Buzing's answer. Please take a look at it. Your situation will be even easier because both your input and output are scalars.
Advantages of LibraryLink compared to pipes:
* fast -- you have direct access to Mathematica's memory (no need for I/O, converting between strings and numbers, slow process launching)
* more robust (no need to create named pipes---I don't even know how to do that on Windows)
* cross platform
I find it's less work and less worry than hackish approaches such as pipes, also it probably won't take you more than 30 minutes to get it working the first time.
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
FindMinimum v. NMinimize and an external program EcoTheory <carroll.ian@gmail.com> - 2012-02-25 06:58 +0000
Re: FindMinimum v. NMinimize and an external program Szabolcs <szhorvat@gmail.com> - 2012-02-26 09:22 +0000
Re: FindMinimum v. NMinimize and an external program Szabolcs <szhorvat@gmail.com> - 2012-02-26 09:21 +0000
[solved] Re: FindMinimum v. NMinimize and an external program Ian <carroll.ian@gmail.com> - 2012-02-27 11:43 +0000
csiph-web