Re: NMinimize problem: fct minimized uses FindRoot
- To: mathgroup at smc.vnet.net
- Subject: [mg123744] Re: NMinimize problem: fct minimized uses FindRoot
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Sat, 17 Dec 2011 02:44:06 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201112101227.HAA19219@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
If posters put forth examples where Return was, say, only 1% pointless...
we could have a different discussion. Until that happens, I'll stick to my
current opinion.
(And no, I don't plan to search thousands of archived posts to prove
myself wrong.)
Bobby
On Fri, 16 Dec 2011 06:07:47 -0600, Andrzej Kozlowski <akoz at mimuw.edu.pl>
wrote:
> You are absolutely right, but that is mostly due to the fact that the
> use of Return and Throw in the posted examples was 100% pointless.
>
> It's a very different matter in cases when you really do need to
> interrupt a control structure. If you look in the list archives you will
> find many examples of compiled procedural code posted by Daniel
> Lichtblau where Throw is used and where is going to be a lot harder to
> produce equivalent code that matches the performance and avoids the use
> of Throw, Return or Break.
>
> Andrzej
>
>
>
> On 16 Dec 2011, at 11:47, DrMajorBob wrote:
>
>> The third code below is far superior... and not just as a matter of
>> speed:
>>
>> temp = RandomInteger[{0, 100}, #] & /@ RandomInteger[{1, 100}, 100000];
>> returnExample[x_] :=
>> Module[{max, min}, If[Total[x] < 100, Return[0]];
>> {max, min} = {Max[x], Min[x]};
>> max^min]
>> throwExample[x_] := Module[{max, min}, If[Total[x] < 100, Throw[0]];
>> {max, min} = {Max[x], Min[x]};
>> max^min]
>> simple[x_] := If[Total@x < 100, 0, {Max@x, Min@x}]
>>
>> Timing[returnExample /@ temp;]
>> Timing[Catch[throwExample[#]] & /@ temp;]
>> Timing[simple /@ temp;]
>>
>> {1.61005, Null}
>>
>> {1.73297, Null}
>>
>> {0.458204, Null}
>>
>> Others have suggested Throw and Catch... but I almost never use THOSE,
>> either.
>>
>> Bobby
>>
>> On Thu, 15 Dec 2011 12:38:09 -0600, W Craig Carter <ccarter at mit.edu>
>> wrote:
>>
>>> Hello,
>>> I didn't understand the rationale for the "do not use Return" maxim, so
>>> I constructed a little experiment:
>>>
>>> (****** snip start *******)
>>> (*list of lists of random lengths*)
>>> temp = RandomInteger[{0, 100}, #] & /@ RandomInteger[{1, 100}, 100000];
>>>
>>> (*artificial examples of using Throw or Return*)
>>>
>>> returnExample[x_] := Module[
>>> {max, min},
>>> If[Total[x] < 100, Return[0]];
>>> {max, min} = {Max[x], Min[x]};
>>> max^min
>>> ]
>>>
>>> throwExample[x_] := Module[
>>> {max, min},
>>> If[Total[x] < 100, Throw[0]];
>>> {max, min} = {Max[x], Min[x]};
>>> max^min
>>> ]
>>>
>>> (*timings*)
>>> Timing[returnExample /@ temp;]
>>> (*I get: {1.08535, Null}*)
>>>
>>> Timing[Catch[throwExample[#]] & /@ temp;]
>>>
>>> (*I get: {1.20134, Null}*)
>>>
>>> (*******snip end********)
>>>
>>> OK, this is artificial, but how do I understand why Return is eschewed
>>> when it appears to be faster and its construction is simpler?
>>>
>>> Thanks, Craig
>>>
>>>
>>> W Craig Carter
>>> Professor of Materials Science, MIT
>>>
>>>
>>>
>>> On Dec 14, 2011, at Wed, Dec 14, 11 ---6:00 AM, DrMajorBob wrote:
>>>
>>>> There's a large gap between "explained and justified" versus "worth
>>>> the
>>>> trouble" or even "sensible".
>>>>
>>>> Do not use Return. It isn't worth the spit you'll need to polish it.
>>>>
>>>> Bobby
>>>>
>>>> On Tue, 13 Dec 2011 04:39:04 -0600, Andrzej Kozlowski
>>>> <akoz at mimuw.edu.pl>
>>>> wrote:
>>>>
>>>>>
>>>>> On 12 Dec 2011, at 12:43, Oleksandr Rasputinov wrote:
>>>>>
>>>>>> For example, the erroneous usage:
>>>>>>
>>>>>> Block[{a = 1, b = 2}, Return[a]; Return[b]]
>>>>>>
>>>>>> gives:
>>>>>>
>>>>>> Return[1]
>>>>>>
>>>>>> but a correct usage:
>>>>>>
>>>>>> Do[Block[{a = 1, b = 2}, Return[a]; Return[b]], {1}]
>>>>>>
>>>>>> gives (because of the enclosing Do):
>>>>>>
>>>>>> 1
>>>>>
>>>>> This is not quite the whole story. For example, while the above use
>>>>> of
>>>>> Return in Block does not work, this one (in a function call) does:
>>>>>
>>>>> f[] := Block[{a = 1, b = 2}, Return[a]; Return[b]]
>>>>>
>>>>> f[]
>>>>>
>>>>> 1
>>>>>
>>>>> Moreover, if you replace the Do loop in your example with a While
>>>>> loop,
>>>>> you will get:
>>>>>
>>>>> n = 1; While[n < 10, Block[{a = 1, b = 2}, Return[a]; Return[b]]]
>>>>>
>>>>> Return[1]
>>>>>
>>>>> All of this is purposeful design and can be explained and justified
>>>>> but
>>>>> I don't think it's worth the bother since Mathematica has far
>>>>> superior
>>>>> means of flow control than this clumsy and arcane construct.
>>>>>
>>>>> Andrzej
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> DrMajorBob at yahoo.com
>>>>
>>>
>>
>>
>> --
>> DrMajorBob at yahoo.com
>>
>
--
DrMajorBob at yahoo.com
- References:
- NMinimize problem: fct minimized uses FindRoot
- From: "Doug Tinkham" <dtinkham@live.ca>
- NMinimize problem: fct minimized uses FindRoot