Summary- Testing for Unevaluated Functions

• To: mathgroup at yoda.physics.unc.edu (Mathematica Group)
• Subject: Summary- Testing for Unevaluated Functions
• From: jake at skatter.usask.ca (Jason Breckenridge)
• Date: Fri, 2 Oct 92 0:37:21 CST

```My original question was something like:

> I would like to build a function that uses FindMinimum to search for a
> minimum (obviously).  The problem is that as one varies one of the parameters
> of the function, a definite min. will move outside of the range of the search.
>
> I would like to test for this condition, and if Find Minimum cannot find a
> minimum=, then use one of the endpoints.
>
> The question is, is there no general mechanism for testing the return status
> of a function, or at least a built-in function?
>

>From the replies that I received, it seems that there are basically two ways
to achieve this.

The first is to use the Check[] function which will trap error messages
generated while a function evaluates.

The second is to test the head of the expression.

Examples of the first approach:

From: David Withoff <withoff at wri.com>:

might be useful anyway:

In[17]:= f[s_] := Check[FindMinimum[(x-s)^2, {x, 0, -1, 1}],
{(Abs[s] - 1)^2, {x -> Sign[s]}},
FindMinimum::regex]

FindMinimum::regex:
Reached the point 1. {-1.} + {0.} which is outside the region {{-1., 1.}}.

From: a_rubin at dsg4.dse.beckman.com (arthur rubin):

Check[expr, failexpr] evaluates expr, and returns the result, unless
messages were generated, in which case it evaluates and returns failexpr.
Check[expr, failexpr, s1::t1, s2::t2, ...] checks only for the specified
messages.

So all you have to do is list all of FindMinimum's error messages, and put
them in.

And of the second:

>From keiper at wri.com Tue Sep 29 07:29:16 1992:

I presume that you are using FindMinimum[ ] with a bounded search range
and that you want to test whether the result is valid or not.  The simplest
way is simply to check whether its head is FindMinimum[ ].  Thus

In[13]:= f[x_] := Module[{t, fm},
fm = FindMinimum[Cos[t], {t, x, 2, 5}];
If[Head[fm] === FindMinimum, foo, t /. fm[[2]]]]

Jerry B. Keiper
keiper at wri.com

>From jacobson at cello.hpl.hp.com:

FindMinimum generates a message when it fails you can trap on the
message.  Look up "Check" in the book.  Theoretically, ValueQ, tells
you if something evaluates.  But it may wind up taking the full time
of running FindMinimum to see if it evaluates.  The simplest way I can
think of some something like this:

foo = FindMinimum[xxx]

-- David Jacobson

Thanks to all for their help.

```

• Prev by Date: Portable notebooks
• Next by Date: Obtaining assigned symbols and values
• Previous by thread: Portable notebooks
• Next by thread: Obtaining assigned symbols and values