Re: Detecting unsuccessful computations

• To: mathgroup at smc.vnet.net
• Subject: [mg39423] Re: Detecting unsuccessful computations
• Date: Fri, 14 Feb 2003 03:24:09 -0500 (EST)
• Organization: Wolfram Research, Inc.
• References: <b2fqoj\$k9d\$1@smc.vnet.net> <3E4B7BB4.1090802@post.tau.ac.il>
• Sender: owner-wri-mathgroup at wolfram.com

```When the Mathematica evaluator sees an expression which evaluated
to itself, it checks whether any values or rules associated with
any symbol contained in the expression have been changed after
the expression was last seen, and if yes it needs to reevaluate
the expression.

FindMinimum code works by assigning local values to its variable,
so if FindMinimum evaluates to itself, it will get reevaluated
each time the evaluator sees it. This is probably true about all
numeric functions with HoldAll attribute.

One can prevent reevaluation of failed computations by using
Check or any other message based method suggested in this thread.

Here is a short code that detects failure and prevents reevaluation
based on the Head of returned expression. It preserves messages,
and preserves any results that may be returned despite messages
being generated. (There may be a better way of doing it. I do not
use numeric functions too often...)

In[1]:= SetAttributes[FailCheck, HoldAll];

With[{ans=Hold[Evaluate[f]]},

In[3]:= FailCheck[FindMinimum[x^2,{x,1,1,2}], FindMinimum]

FindMinimum::fmlim: The minimum could not be bracketed in 30 iterations.

Out[3]= \$Failed

In[4]:= FailCheck[FindMinimum[x^2,{x, 0, 1}], FindMinimum]

Out[4]= {0., {x -> 0.}}

In[5]:= FailCheck[NIntegrate[Random[], {x, 0, 1}], NIntegrate]

NIntegrate::slwcon:
Numerical integration converging too slowly; suspect one of the
following:
singularity, value of the integration being 0, oscillatory
integrand, or
insufficient WorkingPrecision. If your integrand is oscillatory try
using
the option Method->Oscillatory in NIntegrate.

NIntegrate::ncvb:
NIntegrate failed to converge to prescribed accuracy after 7
recursive bisections in x near x = 0.417969.

Out[5]= 0.519509

Best Regards,

Wolfram Research

Uri Zwick wrote:
> Hi,
>
> In a previous post I claimed:
>
>> If we write
>> sol = LinearSolve[...] ;
>> If[ SameQ[Head[sol],LinearSolve] , ... , ... ]
>>
>> then in case of failure, the failed calculation will be
>> attempted again!)
>
>
> This is indeed not true, as pointed out by "Omega Consulting".
> I stand corrected.
>
> BUT, my claim is true if FindMinimum is used instead of LinearSolve.
> In fact, the situation is even worse than I thought. The failed
> computation will be performed THREE times!
>
> Try the following:
>
> In[1]:= FindMinimum[x^2, {x, 1, 1/2, 3/2}]
>
> You will get one error message:
> FindMinimum::regex: Reached the point {...} which is outside ...
>
> Now try
>
> In[2]:= sol = FindMinimum[x^2, {x, 1, 1/2, 3/2}]
>
> You will get TWO error messages:
> FindMinimum::regex: Reached the point {...} which is outside ...
> FindMinimum::regex: Reached the point {...} which is outside ...
>
> The failed computation was performed twice!
>
> Finally,
>
>
> Another error message is printed:
> FindMinimum::regex: Reached the point {...} which is outside ...
>
>
> FindMinimum
>
> The failed computation was performed again!
>
> I suspect the same would happen with other functions,
> not just with FindMinimum.
>
> Would you consider that reasonable?
>
> It is possible to detect failed calculations using
> techniques posted as replies to my original posting.
> (Thank you all for the solutions.) But, I still feel
> that Mathematica should provide a cleaner and more
> convenient solution to the problem.
>
> Uri
>
> ---------------------------------------------------------------------
> |  Prof. Uri Zwick             |  http://www.cs.tau.ac.il/~zwick    |
> |  Dept. of Computer Science   |  zwick at post.tau.ac.il              |
> |  Tel Aviv University         |                                    |
> |  Tel Aviv 69978              |  PHONE: +972 3 6409610             |
> |  ISRAEL                      |  FAX:   +972 3 6409357             |
> ---------------------------------------------------------------------
>

```

• Prev by Date: Re: Problems evaluating Grad
• Next by Date: RE: Bug in ColorFunctionScaling ???
• Previous by thread: Re: Detecting unsuccessful computations
• Next by thread: Input output disappear