MathGroup Archive 1995

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

Search the Archive

Re: Programming Options for Power Expand

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg815] Re: Programming Options for Power Expand
  • From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
  • Date: Mon, 24 Apr 1995 01:10:07 -0400
  • Organization: University of Colorado, Boulder

>The 
>real question is more general:  Should (and can!) one 
>add options to a built in command?  I do not know the 
>answer to whether one should.  I am interested in knowing 
>what is good Mma programming practice in this regard.
>Is what I present next poor programming practice?  Why?
>(I am most interested in the style, less in how to improve 
>my code.)
>
>Step(1):  
>Define rules.  I list a few:
>
>In[1]:  ArcRules = {
>
>	ArcTan[ Tan[x_] ] :> x,
>....
>  
>	ArcCsc[ Sec[x_] ] :> Pi/2 - z}:
		    |               |
****************    I think you have a bug here, Jack!

>In[3]:	 Unprotect[PowerExpand];
>
>In[4]:	PowerExpand[expr_,InverseTrig->True] := 
>
>	  Module[ {fnt},
>		fnt = Simplify[ PowerExpand[#//.ArcRules] ]&;
>		FixedPoint[  fnt,expr ]
>	  ]
>
>I think that I have cheated here.  InverseTrig->True acts like 
>an option but it strikes me that it is in fact a second argument
>to PowerExpand.  But perhaps that's all options are anyway.

Yes, that's essentially all that an option is, but there are some conventions
for their use that you should observe.  So, here are some style comments.

If you are going to define options -- for your own commands or for built-in
ones -- you should set Options[cmd] to an appropriate default and the
user should be able to change default options using SetOptions.  In your case,

In[5]:=
    Options[PowerExpand] =
	Append[Options[PowerExpand], InverseTrig->False]
Out[5]=
    {InverseTrig -> False}

Now your code should be modified to look like this:

    PowerExpand[expr_, opts___Rule]  :=
    Module[ ... (* your code here *)
    ] /; (InverseTrig /. opts /. Options[PowerExpand]) == True

Note the use of opts___Rule to collect all of the options passed to the
function.  The idiom "optionname /. opts /. Options[cmdname]" should
appear somewhere in every function you write that takes options.  It
allows explicitly-passed options to override the defaults.  Most of the
time it will appear in the body of the rule, and its result will be
assigned to a local variable; but in this case, since you don't want
your rule to fire unless InverseTrig is True, it should appear in the
conditional clause.

Also note that this code is only correct because PowerExpand doesn't
take any other options.  If it did, you would have to pass those
options to the "real" PowerExpand.  First, though, you would have to
strip out the InverseTrig options from opts, because the "real"
PowerExpand will gag on it.  This is a simple exercise in functional
programming that I won't go into here, except to say that DeleteCases
seems like a good tool for the job.  Look at Utilities`FilterOptions`
(discussed in Maeder's first book) for extra inspiration.

		Dave Wagner
		Principia Consulting
		(303) 786-8371
		princon at csn.net
		http://www.csn.net/princon


  • Prev by Date: Re: Student version in high schools
  • Next by Date: help w/ SphericalPlot3D
  • Previous by thread: Programming Options for Power Expand
  • Next by thread: Re: Programming Options for Power Expand