MathGroup Archive 1995

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

Search the Archive

Re: Programming

  • To: mathgroup at
  • Subject: [mg2830] Re: [mg2819] Programming
  • From: Preston Nichols <nichols at>
  • Date: Sat, 23 Dec 1995 03:18:13 -0500

Jack Goldberg writes:

"When BuiltIn fails I want to try  MyOperation[expr_], but ONLY
if BuiltIn fails!"

This sounds like a job for If:

If[condition, t, f] gives t if condition evaluates to
   True, and f if it evaluates to False. If[condition, t,
   f, u] gives u if condition evaluates to neither True
   nor False.

Something like the following should do the job:

ExtendedBuiltIn[args___] :=
	If[ Head[ resultOfBuiltIn=BuiltIn[args] ] =!= BuiltIn,
		MyOperation[args] ]

This detects whether BuiltIn fails by having it execute and return  
its result to the test in the first argument of If.  "Failure of  
BuiltIn" is defined to be when BuiltIn[args] returns an expression  
with BuiltIn as its head.  (Is this criterion for "failure"  
(equivalent to) the one you need?)  Placing the assignment to  
resultOfBuiltIn inside the test makes it possible for Mathematica to  
evaluate BuiltIn[args] only once.

If you want your extension of BuiltIn to be more invisible,

BuiltIn[args___] :=
	If[ Head[ resultOfBuiltIn=BuiltIn[args] ] =!= BuiltIn,
		MyOperation[args] ]

Modifying built-in commands is generally rather risky, but I think  
something like this should be pretty safe.

Preston Nichols
Visiting Assistant Professor
Department of Mathematics
Carnegie Mellon University
(Seeking employment for Fall, 1996)

>From: Jack Goldberg <jackgold at>
>To: mathgroup at
>Subject: [mg2819] Programming
>Organization: University of Michigan - College of Literature,  
>Science, and TheArts

Hi Group;

	Here is my problem:  Suppose  BuiltIn[expr_]  is an
operation either native to Mma or an operation appearing in
some complicated, lengthly package.  Suppose that  BuiltIn
does not always work.  (An example might be Integrate which
fails to find the anti-derivative  Integrate[1/(1+Log[x]),x].)
When BuiltIn fails I want to try  MyOperation[expr_], but ONLY
if BuiltIn fails!  Interactively, this is trivial.  Whenever
BuiltIn fails, I recognize the failure because Mma echos the
Input in the Output.  (The output of Integrate[1/(1+Log[x]),x]
is itself.)  I then apply MyOperation[expr_]. But how does one code 
this so that Mma will do MyOperation automatically? That is, I
want a snippet of code that determines the failure of BuiltIn
and then applies MyOperation.  (In my illustrative example,
suppose I have written a general change of variable routine.
I want this routine to go in effect whenever Integrate returns
unsimplified.  For the example given  u = Log[x]  converts the
integral into one recognizable by Mma.  So my code will recognize
the fail and call "change of variable" without my intervention.
Let me make clear that the integration example is only an illustration
of the problem, not the reason for this posting.)

Jack Goldberg
University of Michigan

  • Prev by Date: Re: LISTS and more LISTS
  • Next by Date: Re: Programming
  • Previous by thread: Re: Programming
  • Next by thread: Re: Programming