For the past one week, I had been breaking my head over a piece of Mathematica code. I wanted to use NMinimize in Mathematica to minimize a function which was written as a module. In this module, I was solving a linear system of equations (sparse and overdetermined, but that’s not the point). The problem was that even with moderate dimensions, say 20*17, Mathematica ran out of memory and crashed on me.
I realized that the first invocation to my objective function from NMinimize was being done symbolically rather than numerically. Why the hell was it doing that? I read and re-read the documentation (whatever little of it there is) until I knew it by heart. I was just about to punch my computer, when the good guys over at Stack Overflow told me what the problem was.
It turns out that NMinimize does not hold its arguments. This means that as the list of arguments is read from left to right, each argument is evaluated and replaced by the result of the evaluation. So for example, something like the following:
would cause the evaluation of dummy with “x” as an input, which is exactly what was happening in this case.
I changed the call to
and lo and behold! everything was well in my world again.
So, what are the lessons for me in this totally forgettable experience?
#1. If a piece of software (which is not free) is driving you insane, the fault is most probably yours.
#2. You may have worked through all the advanced notebooks your prof gave you while learning Mathematica, but it won’t hit you until it has hit you.
#3. The www makes the world a better place to live in. Strive to contribute sense to this sea of sense and nonsense.