Re: FindMinimum v. NMinimize and an external program
- To: mathgroup at smc.vnet.net
- Subject: [mg125176] Re: FindMinimum v. NMinimize and an external program
- From: Szabolcs <szhorvat at gmail.com>
- Date: Sun, 26 Feb 2012 04:20:16 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- 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.