MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Detecting unsuccessful computations

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

At 12:07 AM 2/10/2003, Uri Zwick wrote:
>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?
>|  Prof. Uri Zwick             |    |
>|  Dept. of Computer Science   |  zwick at              |
>|  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.


 From In[1]:=
LinearSolve::nosol: Linear equation encountered which has no solution.

{0.02 Second,LinearSolve[{{1,3},{2,6}},{1,1}]}


{0. Second,True}

Even if it was reevaluated, the value is cached so very little time is lost.


 From In[3]:=
LinearSolve::nosol: Linear equation encountered which has no solution.

{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"

  • Prev by Date: Fwd: Linear and logarithmic fit
  • Next by Date: Bug in ColorFunctionScaling ???
  • Previous by thread: Re: Detecting unsuccessful computations
  • Next by thread: Re: Detecting unsuccessful computations