MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NMinimize problem: fct minimized uses FindRoot


On 14 Dec 2011, at 12:02, Oleksandr Rasputinov wrote:

> On Tue, 13 Dec 2011 10:41:11 -0000, 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
>>
>
> Thanks for the additional examples. Yes, there are limitations to what 
> Return (or Break, or Continue) will accept as a control flow construct; I 
> imagine most people would have suspected that Block does not count but it 
> is a little surprising to find that While doesn't either.

My view of this is a little different. I think there is a good reason why the While example returns Return[1] rather than just 1. To see my point, look at the example on memberQ I posted earlier. With a Do loop this does not work

memberQ[x_List, y_] := (Do[If[i === y, Return[True]], {i, x}]; False)

Now use the While loop:

Clear[memberQ]

memberQ[x_List, y_] := (i = 1;
  While[i <= Length[x], If[x[[i]] === y, Return[True], i++]]; False)

memberQ[{a, b, c}, b]

True

The While loop works fine. Why is that? Well, I think it is exactly for the same reason why my earlier While example  (when While was not in the body of a function) "did not work" - i.e. when we got Return[1] instead of 1. This returning Return[something] rather than just something is not the result of some sort of confusion in the design (although it is, no doubt, confusing) but it actually the means by which Return in a While loop gets through all the control flow structures while in a Do loop it only exits the innermost control structure. As I pointed out, to achieve the same result in a Do loop, you will need a double Return:

Clear[memberQ]

memberQ[x_List,
  y_] := (Do[If[i == y, Return[Return[True]]], {i, x}]; False)

memberQ[{a, b, c}, b]

True


Andrzej Kozlowski




  • Prev by Date: Re: color-bar legend for the LisContourPlot
  • Next by Date: Re: How to run ".nb" file always in dos command line
  • Previous by thread: Re: NMinimize problem: fct minimized uses FindRoot
  • Next by thread: Re: NMinimize problem: fct minimized uses FindRoot