Re: Detecting unsuccessful computations
- To: mathgroup at smc.vnet.net
- Subject: [mg39330] Re: Detecting unsuccessful computations
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Tue, 11 Feb 2003 04:43:22 -0500 (EST)
- References: <b27fn8$n9t$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Uri,
I hope that the following experiment will provide some ideas for
development.
SilentCheck below is a modification of a posting by Carl Woll.
SilentCheckPlus::usage = "SilentCheckPlus[expr, failexpr] evaluates expr
and then gives its value, unless messages have been generated, in which
case it gives
the value of failexpr.No messages are displayed.";
SetAttributes[SilentCheck, HoldAll]
SilentCheck[expr_, failexpr_]:=
Block[{Message},
Message[f_/;Not[MatchQ[f,_$Off]],___]:= Throw[failexpr,SilentCheck];
Catch[expr,SilentCheck]
]
Define a variant of LinearSolve
LS[m_,c_]/;c=!={}:=
SilentCheck[LinearSolve[m,c],LS[Rest[m],Rest[c]]]
LS[{},{}]:= $Failed
TEST
LS[{{1, 2}, {3, 4}, {5, 6}}, {-1, 2, -3}]
{-12, 19/2}
LS[{{2,1},{1, 2}, {0,0}, {5, 6}}, {-1, 2, 5,6}]
{6/5, 0}
LS[{{2,1},{1, 2}, {0,0}, {5, 6}}, {-1, 2, 5,1}]
{1/5, 0}
LS[{{2,1},{1, 2}, {0,1}, {0, 0}}, {-1, 2, 5,1}]
$Failed
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Uri Zwick" <zwick at tau.ac.il> wrote in message
news:b27fn8$n9t$1 at smc.vnet.net...
> Hi,
>
> Mathematica functions such as LinearSolve, LinearProgramming,
> FindMinimum and many others, sometimes cannot perform the
> requested task. (The linear system of equations has no solution, the
> linear program has no feasible points, etc.)
>
> When that happens, Mathematica usually prints some error messages
> and then returns the expression it tried to evaluate. This happens,
> for example, with LinearSolve[{{1, 2}, {3, 4}, {5, 6}}, {-1, 2, -3}].
>
> This may be reasonable when only one such calculation is performed.
> But, suppose we write a program that has to solve many linear
> programs, some of them without a feasible solution. When there is
> no solution, we want to take an appropriate action.
>
> What is the recommended way of calling a function like
> LinearProgramming and getting an indication that something
> went wrong, WITHOUT getting any error message printed?
>
> It is possible to catch errors using 'Check'. But, as far as I
> understand it only works if error messages are printed.
> It will not work if the relevant error messages are turned off
> using 'Off'.
>
> It is possible to turn off the error messages and check the head
> of the expression returned to see if it the name of the function called.
> For example:
>
> Off[LinearSolve::"nosol"] ;
>
> If[ SameQ[ Head[sol = LinearSolve[...]] , LinearSolve] ,
> (* The calculation failed. *) ,
> (* sol is a solution. *) ]
>
> (Note that we have to put sol = LinearSolve[...] inside
> If[ SameQ[ Head[...] ... . If we wrote
>
> sol = LinearSolve[...] ;
> If[ SameQ[Head[sol],LinearSolve] , ... , ... ]
>
> then in case of failure, the failed calculation will be
> attempted again!)
>
> Is there a cleaner solution?
>
> 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 |
> ---------------------------------------------------------------------
>
>