Detecting unsuccessful computations
- To: mathgroup at smc.vnet.net
- Subject: [mg39302] Detecting unsuccessful computations
- From: Uri Zwick <zwick at tau.ac.il>
- Date: Mon, 10 Feb 2003 01:07:43 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
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 | ---------------------------------------------------------------------