Re: evaluation until failure
- To: mathgroup at smc.vnet.net
- Subject: [mg41679] Re: evaluation until failure
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Fri, 30 May 2003 03:56:24 -0400 (EDT)
- Organization: University of Washington
- References: <bb50ch$4bc$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
David, This issue has come up before, and there have been two approaches suggested. 1. The following function is Allan Hayes' version based on an approach I advocated in the past: SetAttributes[SilentCheck, {HoldAll}] SilentCheck::usage = "SilentCheck[expr,failexpr] evaluates expr, and returns the result, \ unless messages were generated, in which case it evaluates and returns \ failexpr. SilentCheck suppresses the output of the messages generated in \ evaluating expr."; SilentCheck[expr_, err_] := Block[{Message}, Message[_?(Not[MatchQ[#,_$Off]]&),__] := Throw[err, SilentCheck]; Catch[expr, SilentCheck] ] One can enhance SilentCheck to check for particular messages, a la the original Check: SilentCheck[expr_, err_, msgs__] := Module[{ans}, Unprotect[Message]; Message[_?(MemberQ[{msgs}, #] &), __] := Throw[err, SilentCheck]; ans = Catch[expr, SilentCheck]; Unprotect[Message]; Clear[Message]; Protect[Message]; ans ] The second Unprotect is unfortunately necessary, as some packages will automatically Protect Message when loaded. 2. Alternatively, one can temporarily override the Message channel as you suggest in your post, and use Check. For example: SetAttributes[SilentCheck, {HoldAll}] SilentCheck[expr_,err_]:=Block[{$Messages}, Check[expr,err] ] The only drawback with this approach (as you state) is that you can't check only for particular messages. Carl Woll Physics Dept U of Washington <davidmma at freemail.hu> wrote in message news:bb50ch$4bc$1 at smc.vnet.net... > Dear MathGroup, > > It is a haunting problem for me, that I would like to iterate a function > (eg. ConstrainedMin) with different arguments until it fails to give a > solution. In this case I used to "catch" the Message thrown by > ConstrainedMin with Check, but I can't deal with the side effect, that > ConstrainedMin prints a message ("The specified constraints cannot be > satisfied") when it fails. I would like to avoid printing this message. > > If I turn off the ConstrainedMin::nsat message, I can't Check it. > > If I redirect the $Messages output to nowhere, I may lose other > messages as well. > > Are there any other (better) solutions? > > Thanks you in advance, > > David Rock > > >