Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7299 > unrolled thread

Re: NMinimize problem: fct minimized uses FindRoot

Started byOliver Ruebenkoenig <ruebenko@wolfram.com>
First post2011-12-17 07:46 +0000
Last post2011-12-21 11:54 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: NMinimize problem: fct minimized uses FindRoot Oliver Ruebenkoenig <ruebenko@wolfram.com> - 2011-12-17 07:46 +0000
    Re: NMinimize problem: fct minimized uses FindRoot "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-18 09:37 +0000
    Re: NMinimize problem: fct minimized uses FindRoot DrMajorBob <btreat1@austin.rr.com> - 2011-12-19 12:17 +0000
    Re: NMinimize problem: fct minimized uses FindRoot "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-20 08:07 +0000
    Re: NMinimize problem: fct minimized uses FindRoot DrMajorBob <btreat1@austin.rr.com> - 2011-12-21 11:54 +0000

#7299 — Re: NMinimize problem: fct minimized uses FindRoot

FromOliver Ruebenkoenig <ruebenko@wolfram.com>
Date2011-12-17 07:46 +0000
SubjectRe: NMinimize problem: fct minimized uses FindRoot
Message-ID<jchhbr$ieu$1@smc.vnet.net>

On Fri, 16 Dec 2011, Oleksandr Rasputinov wrote:

> On Thu, 15 Dec 2011 10:06:01 -0000, Oliver Ruebenkoenig
> <ruebenko@wolfram.com> wrote:
>
>>
>> Just for comepletness sake:
>>
>> this does work
>>
>> cf = Compile[{{n, _Integer}},
>>    Module[{res = 0}, Do[res += i; If[res > 10, Throw[0, cfe]], {i, n}];
>>      res]]
>>
>> Catch[cf[111], cfe]
>>
>>
>> Related:
>> http://stackoverflow.com/questions/8184548/do-return-behave-differently-in-compile-why/8194565#8194565
>>
>> Oliver
>>
>
> Well, sort of. 
>
> cf // InputForm
>
> reveals the MainEvaluate call (opcode 46):

That does not matter - this is evaluated only once; and you want to go 
back to the evaluator anyways.

Calls to MainEvaluate in compiled code are evil if they appear in a loop.

Needs["CompiledFunctionTools`"]
CompilePrint[cf]


>
> CompiledFunction[{8, 8., 5468}, {_Integer}, {{2, 0, 0}, {2, 0, 2}}, {{0,
> {2, 0, 1}}, {10, {2, 0, 5}}}, {1, 7, 0, 0, 0},
> {{6, 1, 2}, {6, 0, 3}, {6, 1, 4}, {3, 7}, {12, 2, 4, 6}, {6, 6, 2}, {25,
> 5, 2, 0}, {2, 0, 3},
> {46, Function[{n}, Throw[0, cfe]], {i, 2, 0, 4, Block}, 2, 0, 0, 6, 0,
> 17}, {3, 1}, {4, 4, 3, -6}, {1}},
> Function[{n}, Module[{res = 0}, Do[res += i; If[res > 10, Throw[0, cfe]],
> {i, n}]; res]], Evaluate]
>
> so the advantage of the two-argument Throw in this case appears limited
> versus, say,
>
> cf = Compile[{{n, _Integer}},
>  Catch@Module[{res = 0},
>   Do[res += i; If[res > 10, Throw[0]], {i, n}];
>   res
>  ]
> ]
>
> which does the same thing without requiring any MainEvaluate calls.

But is less flexible concerning that possibilities where to Catch.

>
> Also:
>
> cf2 = Compile[{{n, _Integer}},
>   Catch[
>    Module[{res = 0},
>     Do[res += i; If[res > 10, Throw[0, cfe]], {i, n}];
>     res
>    ], cfe
>   ]
>  ]
>
> Catch::argx: Catch called with 2 arguments; 1 argument is expected.
>
> cf2 // InputForm

Yes, this seems to me much more of an issue.

I'd cautiously say, that this might be improvable. But I am not sure. I 
think the issue is that the second arg is a expr, which the compiler does 
not know how to deal with. But perhaps if a "string" type were added then 
something like this might work

  cf2 = Compile[{{n, _Integer}},
    Catch[
     Module[{res = 0},
      Do[res += i; If[res > 10, Throw[0, "cfe"]], {i, n}];
      res
     ], "cfe"
    ]
   ]

But this is speculation.


>
> CompiledFunction[{8, 8., 5468}, {_Integer}, {{2, 0, 0}, {3, 0, 0}}, {},
> {0, 1, 1, 0, 0},
> {{46, Function[{n}, Catch[Module[{res = 0}, Do[res += i; If[res > 10,
> Throw[0, cfe]], {i, n}]; res], cfe]], 2, 0, 0, 3, 0, 0}, {1}},
> Function[{n}, Catch[Module[{res = 0}, Do[res += i; If[res > 10, Throw[0,
> cfe]], {i, n}]; res], cfe]], Evaluate]
>
> which is not compiled at all. So neither Throw/Catch nor Return can accept
> two arguments when used in compiled code proper (i.e. not wrapped in
> MainEvaluate). The advantage of the latter, of course, is that the

But this is only a problem if you want to Catch inside compile. So I think 
the way to go is to re-write slow parts of code with compile and then the 
control structure in top level. Perhaps some else has another idea?

> location it returns to is indicated by context without requiring a second
> argument to describe it, which is just as well since the locations Return
> will or will not return to appear to be somewhat arbitrary choices as you
> point out in your StackOverflow posting. I suppose one can use Label/Goto
> to replicate the behaviour of two-argument forms in compiled code if
> absolutely necessary, although this seems fairly anachronistic.

Yes, that might be possible - but hm... ;-)

[toc] | [next] | [standalone]


#7327

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-12-18 09:37 +0000
Message-ID<jckc9n$1lk$1@smc.vnet.net>
In reply to#7299
On Sat, 17 Dec 2011 07:46:03 -0000, Oliver Ruebenkoenig  
<ruebenko@wolfram.com> wrote:

>
> On Fri, 16 Dec 2011, Oleksandr Rasputinov wrote:
>
>> On Thu, 15 Dec 2011 10:06:01 -0000, Oliver Ruebenkoenig
>> <ruebenko@wolfram.com> wrote:
>>
>>>
>>> Just for comepletness sake:
>>>
>>> this does work
>>>
>>> cf = Compile[{{n, _Integer}},
>>>    Module[{res = 0}, Do[res += i; If[res > 10, Throw[0, cfe]], {i, n}];
>>>      res]]
>>>
>>> Catch[cf[111], cfe]
>>>
>>>
>>> Related:
>>> http://stackoverflow.com/questions/8184548/do-return-behave-differently-in-compile-why/8194565#8194565
>>>
>>> Oliver
>>>
>>
>> Well, sort of.
>>
>> cf // InputForm
>>
>> reveals the MainEvaluate call (opcode 46):
>
> That does not matter - this is evaluated only once; and you want to go
> back to the evaluator anyways.

No, I really don't. :) The reason I suggested that a compiled form of the  
two-argument Return, Continue, Break etc. might be useful is that these  
could be used for more complex (Fortran 90-like, say, versus Fortran  
77-like) structured flow control in procedural code. (While Throw is  
usually a better choice than Return, I still tend to prefer Break and  
Continue in a procedural context over co-opting Throw for these purposes  
as well.) But once you Throw using a MainEvaluate call, there is no way to  
get back into compiled code except by starting again from the beginning,  
which is rather contrary to the purpose of flow control in general. As a  
result, Label/Goto is left as the only possible solution, anachronistic as  
it may be.

>>
>> Also:
>>
>> cf2 = Compile[{{n, _Integer}},
>>   Catch[
>>    Module[{res = 0},
>>     Do[res += i; If[res > 10, Throw[0, cfe]], {i, n}];
>>     res
>>    ], cfe
>>   ]
>>  ]
>>
>> Catch::argx: Catch called with 2 arguments; 1 argument is expected.
>>
>> cf2 // InputForm
>
> Yes, this seems to me much more of an issue.
>
> I'd cautiously say, that this might be improvable. But I am not sure. I
> think the issue is that the second arg is a expr, which the compiler does
> not know how to deal with. But perhaps if a "string" type were added then
> something like this might work
>
>   cf2 = Compile[{{n, _Integer}},
>     Catch[
>      Module[{res = 0},
>       Do[res += i; If[res > 10, Throw[0, "cfe"]], {i, n}];
>       res
>      ], "cfe"
>     ]
>    ]
>
> But this is speculation.

At the risk of speculating on top of speculation, I don't think this is  
correct. The compiler can handle exprs, at least in certain contexts, as  
this example demonstrates (where loop can be an arbitrary expr):

cf = Compile[{{x, _Integer, 0}},
   Block[{i = x, n = 0},
    Label[loop];
    n += x;
    If[--i > 0, Goto[loop]];
    n
   ]
  ]

I think the main reason why the compiler cannot deal with the second  
argument to Throw/Catch is that it is not handled as an expr but as a  
pattern. If this requirement can be relaxed, it's not inconceivable that  
the compiler could in future handle a two-argument Throw/Catch block by  
expanding it as a macro. But first, it has to get smarter about handling  
this situation:

Block[{x},
  Block[{},
   x = 1;
   Goto[exit];
  ];
  x = 2;
  Label[exit];
  x
]

This works as expected under normal evaluation, but the presence of the  
inner Block confuses the compiler into generating an incorrect  
MainEvaluate call that makes it fail when compiled.

>
> But this is only a problem if you want to Catch inside compile. So I  
> think
> the way to go is to re-write slow parts of code with compile and then the
> control structure in top level. Perhaps some else has another idea?

I agree, but this may not always be possible while retaining reasonable  
performance. Thus, as mentioned above, one might have to use Label/Goto  
(compiler bugs notwithstanding) or perhaps link in code written in a  
procedural language with more versatile flow control, such as Fortran 90.

[toc] | [prev] | [next] | [standalone]


#7338

FromDrMajorBob <btreat1@austin.rr.com>
Date2011-12-19 12:17 +0000
Message-ID<jcna0i$e3i$1@smc.vnet.net>
In reply to#7299
I see no reason not to write it THIS way:

cf3 = Compile[{{n, _Integer}},
   Module[{sum = 0, i = 0}, While[i < n && sum <= 10, i++; sum += i];
    If[sum > 10, 0, sum]]]

It's not that I don't know how to write spaghetti code! I probably wrote  
more Fortran 5 in 1977-1982 than I've written in all other languages  
combined. (Demon, IBM Assembler, PL/1, Simscript, QGERT, SAS, MathCAD,  
Mathematica, and others I probably forget.)

But, if there's no reason for a troublesome construct, I plan to avoid it.

Bobby

On Sun, 18 Dec 2011 03:36:23 -0600, Oleksandr Rasputinov  
<oleksandr_rasputinov@hmamail.com> wrote:

> On Sat, 17 Dec 2011 07:46:03 -0000, Oliver Ruebenkoenig
> <ruebenko@wolfram.com> wrote:
>
>>
>> On Fri, 16 Dec 2011, Oleksandr Rasputinov wrote:
>>
>>> On Thu, 15 Dec 2011 10:06:01 -0000, Oliver Ruebenkoenig
>>> <ruebenko@wolfram.com> wrote:
>>>
>>>>
>>>> Just for comepletness sake:
>>>>
>>>> this does work
>>>>
>>>> cf = Compile[{{n, _Integer}},
>>>>    Module[{res = 0}, Do[res += i; If[res > 10, Throw[0, cfe]], {i,  
>>>> n}];
>>>>      res]]
>>>>
>>>> Catch[cf[111], cfe]
>>>>
>>>>
>>>> Related:
>>>> http://stackoverflow.com/questions/8184548/do-return-behave-differently-in-compile-why/8194565#8194565
>>>>
>>>> Oliver
>>>>
>>>
>>> Well, sort of.
>>>
>>> cf // InputForm
>>>
>>> reveals the MainEvaluate call (opcode 46):
>>
>> That does not matter - this is evaluated only once; and you want to go
>> back to the evaluator anyways.
>
> No, I really don't. :) The reason I suggested that a compiled form of the
> two-argument Return, Continue, Break etc. might be useful is that these
> could be used for more complex (Fortran 90-like, say, versus Fortran
> 77-like) structured flow control in procedural code. (While Throw is
> usually a better choice than Return, I still tend to prefer Break and
> Continue in a procedural context over co-opting Throw for these purposes
> as well.) But once you Throw using a MainEvaluate call, there is no way  
> to
> get back into compiled code except by starting again from the beginning,
> which is rather contrary to the purpose of flow control in general. As a
> result, Label/Goto is left as the only possible solution, anachronistic  
> as
> it may be.
>
>>>
>>> Also:
>>>
>>> cf2 = Compile[{{n, _Integer}},
>>>   Catch[
>>>    Module[{res = 0},
>>>     Do[res += i; If[res > 10, Throw[0, cfe]], {i, n}];
>>>     res
>>>    ], cfe
>>>   ]
>>>  ]
>>>
>>> Catch::argx: Catch called with 2 arguments; 1 argument is expected.
>>>
>>> cf2 // InputForm
>>
>> Yes, this seems to me much more of an issue.
>>
>> I'd cautiously say, that this might be improvable. But I am not sure. I
>> think the issue is that the second arg is a expr, which the compiler  
>> does
>> not know how to deal with. But perhaps if a "string" type were added  
>> then
>> something like this might work
>>
>>   cf2 = Compile[{{n, _Integer}},
>>     Catch[
>>      Module[{res = 0},
>>       Do[res += i; If[res > 10, Throw[0, "cfe"]], {i, n}];
>>       res
>>      ], "cfe"
>>     ]
>>    ]
>>
>> But this is speculation.
>
> At the risk of speculating on top of speculation, I don't think this is
> correct. The compiler can handle exprs, at least in certain contexts, as
> this example demonstrates (where loop can be an arbitrary expr):
>
> cf = Compile[{{x, _Integer, 0}},
>    Block[{i = x, n = 0},
>     Label[loop];
>     n += x;
>     If[--i > 0, Goto[loop]];
>     n
>    ]
>   ]
>
> I think the main reason why the compiler cannot deal with the second
> argument to Throw/Catch is that it is not handled as an expr but as a
> pattern. If this requirement can be relaxed, it's not inconceivable that
> the compiler could in future handle a two-argument Throw/Catch block by
> expanding it as a macro. But first, it has to get smarter about handling
> this situation:
>
> Block[{x},
>   Block[{},
>    x = 1;
>    Goto[exit];
>   ];
>   x = 2;
>   Label[exit];
>   x
> ]
>
> This works as expected under normal evaluation, but the presence of the
> inner Block confuses the compiler into generating an incorrect
> MainEvaluate call that makes it fail when compiled.
>
>>
>> But this is only a problem if you want to Catch inside compile. So I
>> think
>> the way to go is to re-write slow parts of code with compile and then  
>> the
>> control structure in top level. Perhaps some else has another idea?
>
> I agree, but this may not always be possible while retaining reasonable
> performance. Thus, as mentioned above, one might have to use Label/Goto
> (compiler bugs notwithstanding) or perhaps link in code written in a
> procedural language with more versatile flow control, such as Fortran 90.
>


-- 
DrMajorBob@yahoo.com

[toc] | [prev] | [next] | [standalone]


#7367

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-12-20 08:07 +0000
Message-ID<jcpfo8$qhe$1@smc.vnet.net>
In reply to#7299
On Mon, 19 Dec 2011 12:17:22 -0000, DrMajorBob <btreat1@austin.rr.com>  
wrote:

> I see no reason not to write it THIS way:
>
> cf3 = Compile[{{n, _Integer}},
>    Module[{sum = 0, i = 0}, While[i < n && sum <= 10, i++; sum += i];
>     If[sum > 10, 0, sum]]]

The point of the example is to demonstrate that the compiler can handle  
arbitrary exprs when they appear as labels and thus that there is no  
reason in principle the two-argument form of Throw/Catch could not be  
compiled as long as the tag was not to be treated as a pattern. I thought  
it would be helpful to demonstrate this with a program that actually does  
something, albeit something trivial (i.e. squaring a number) accomplished  
in a way that I sincerely hope nobody would use in practice. Perhaps this  
makes the original intent clearer:

With[{loop = Plot[Sin[x], {x, -Pi, Pi}]},
  cf = Compile[{{x, _Integer, 0}},
    Block[{i = x, n = 0}, Label[loop];
     n += x;
     If[--i > 0, Goto[loop]];
     n
    ]
   ]
]

> It's not that I don't know how to write spaghetti code! I probably wrote
> more Fortran 5 in 1977-1982 than I've written in all other languages
> combined. (Demon, IBM Assembler, PL/1, Simscript, QGERT, SAS, MathCAD,
> Mathematica, and others I probably forget.)
>
> But, if there's no reason for a troublesome construct, I plan to avoid  
> it.

So have you now changed your mind about Throw/Catch to consider it (or at  
least its two-argument form) troublesome and/or leading to spaghetti code?  
When the only viable alternative (as things stand) is Label/Goto, isn't it  
the lesser of two evils?

[toc] | [prev] | [next] | [standalone]


#7376

FromDrMajorBob <btreat1@austin.rr.com>
Date2011-12-21 11:54 +0000
Message-ID<jcshd5$26f$1@smc.vnet.net>
In reply to#7299
> So have you now changed your mind about Throw/Catch to consider it (or at
> least its two-argument form) troublesome and/or leading to spaghetti  
> code?

I didn't change my mind at all. I rarely do, as some may have noticed.

Throw/Catch didn't Compile, did it? So the combination of Throw/Catch and  
Compile is troublesome. GoTo and Label encourage spaghetti code. My  
alternative avoids both.

(Compile's success or failure seems unpredictable in general, so I don't  
use THAT very much, either. I rarely need anything to run in nanoseconds,  
when microseconds will do.)

Bobby

On Tue, 20 Dec 2011 02:05:03 -0600, Oleksandr Rasputinov  
<oleksandr_rasputinov@hmamail.com> wrote:

> On Mon, 19 Dec 2011 12:17:22 -0000, DrMajorBob <btreat1@austin.rr.com>
> wrote:
>
>> I see no reason not to write it THIS way:
>>
>> cf3 = Compile[{{n, _Integer}},
>>    Module[{sum = 0, i = 0}, While[i < n && sum <= 10, i++; sum += i];
>>     If[sum > 10, 0, sum]]]
>
> The point of the example is to demonstrate that the compiler can handle
> arbitrary exprs when they appear as labels and thus that there is no
> reason in principle the two-argument form of Throw/Catch could not be
> compiled as long as the tag was not to be treated as a pattern. I thought
> it would be helpful to demonstrate this with a program that actually does
> something, albeit something trivial (i.e. squaring a number) accomplished
> in a way that I sincerely hope nobody would use in practice. Perhaps this
> makes the original intent clearer:
>
> With[{loop = Plot[Sin[x], {x, -Pi, Pi}]},
>   cf = Compile[{{x, _Integer, 0}},
>     Block[{i = x, n = 0}, Label[loop];
>      n += x;
>      If[--i > 0, Goto[loop]];
>      n
>     ]
>    ]
> ]
>
>> It's not that I don't know how to write spaghetti code! I probably wrote
>> more Fortran 5 in 1977-1982 than I've written in all other languages
>> combined. (Demon, IBM Assembler, PL/1, Simscript, QGERT, SAS, MathCAD,
>> Mathematica, and others I probably forget.)
>>
>> But, if there's no reason for a troublesome construct, I plan to avoid
>> it.
>
> So have you now changed your mind about Throw/Catch to consider it (or at
> least its two-argument form) troublesome and/or leading to spaghetti  
> code?
> When the only viable alternative (as things stand) is Label/Goto, isn't  
> it
> the lesser of two evils?
>


-- 
DrMajorBob@yahoo.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web