Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7240 > unrolled thread
| Started by | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| First post | 2011-12-15 10:06 +0000 |
| Last post | 2011-12-16 10:59 +0000 |
| Articles | 2 — 2 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.
Re: NMinimize problem: fct minimized uses FindRoot Oliver Ruebenkoenig <ruebenko@wolfram.com> - 2011-12-15 10:06 +0000
Re: NMinimize problem: fct minimized uses FindRoot "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-16 10:59 +0000
| From | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| Date | 2011-12-15 10:06 +0000 |
| Subject | Re: NMinimize problem: fct minimized uses FindRoot |
| Message-ID | <jccgq9$ml4$1@smc.vnet.net> |
On Wed, 14 Dec 2011, Oleksandr Rasputinov wrote:
> On Tue, 13 Dec 2011 10:41:11 -0000, Andrzej Kozlowski <akoz@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.
>
> The documentation for Return also states that "Return[expr] exits control
> structures within the definition of a function, and gives the value expr
> for the whole function", by which it apparently means that delayed
> definitions do count as enclosing control flow, although in apparent
> contradiction to the documented behaviour Function does not. (This seems
> to be another example of a virtually meaningless statement about functions
> in the documentation made on the assumption that the reader is stuck in
> the mindset of another language and prefers a loose analogy to the actual
> behaviour over a factually accurate account.)
>
> Oliver Ruebenkoenig's reply demonstrates the almost undocumented
> two-argument form of Return, the existence of which (I think) suggests
> that the list of constructs accepted by Return were not considered
> completely indisputable by the designers of Mathematica. However, the
> two-argument form still cannot be used inside Compile, where it seems it
> would be most useful.
>
>
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
[toc] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-12-16 10:59 +0000 |
| Message-ID | <jcf8b3$6tl$1@smc.vnet.net> |
| In reply to | #7240 |
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):
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.
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
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
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web