Re: Detecting unsuccessful computations
- To: mathgroup at smc.vnet.net
- Subject: [mg39337] Re: [mg39302] Detecting unsuccessful computations
- From: Selwyn Hollis <selwynh at earthlink.net>
- Date: Tue, 11 Feb 2003 04:47:28 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Uri, The following illustrates a technique I've used. (I'm sure I nicked it from somewhere, but I can't remember where.) progname::"err "= "error message"; soln = x /. Solve[eqn, x]; If[soln===x, Message[ progname::"err"]; Return[$Failed]]; --- Selwyn Hollis On Monday, February 10, 2003, at 01:07 AM, Uri Zwick wrote: > 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 | > --------------------------------------------------------------------- > >