Re: Detecting unsuccessful computations

*To*: mathgroup at smc.vnet.net*Subject*: [mg39383] Re: [mg39302] Detecting unsuccessful computations*From*: Omega Consulting <info at omegaconsultinggroup.com>*Date*: Thu, 13 Feb 2003 04:53:46 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

At 12:07 AM 2/10/2003, 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 | >--------------------------------------------------------------------- It is not true that the LinearSolve is reevaluated when you test the Head. You can see this in the Timing and that a second message isn't created. In[1]:= Timing[sol=LinearSolve[{{1,3},{2,6}},{1,1}]] From In[1]:= LinearSolve::nosol: Linear equation encountered which has no solution. Out[1]= {0.02 Second,LinearSolve[{{1,3},{2,6}},{1,1}]} In[2]:= Timing[Head[sol]===LinearSolve] Out[2]= {0. Second,True} Even if it was reevaluated, the value is cached so very little time is lost. In[3]:= Timing[LinearSolve[{{1,3},{2,6}},{1,1}]] From In[3]:= LinearSolve::nosol: Linear equation encountered which has no solution. Out[3]= {0. Second,LinearSolve[{{1,3},{2,6}},{1,1}]} So testing the head of the result is a fine method for error checking. -------------------------------------------------------------- Omega Consulting "The final answer to your Mathematica needs" http://omegaconsultinggroup.com