MathGroup Archive 2006

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

Search the Archive

Re: Programming with options.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68064] Re: [mg68037] Programming with options.
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Fri, 21 Jul 2006 17:36:00 -0400 (EDT)
  • References: <200607210937.FAA06857@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Jul 21, 2006, at 5:37 AM, MaxArt wrote:

> Hi,
> I want to create some new functions in Mathematica (4.0), and I  
> want it to
> work with options like Plot and other graphic functions. The idea  
> behind
> this is to modify the result and the behaviour of my functions when  
> extra
> arguments are given, and I thought that the use of options would be  
> perfect.
> For example, if I want to create a function Pow that gives the  
> square of a
> number, except when an option Exponent is given, so that Pow[a,  
> Exponent ->
> 3] yields a^3.
>
> How can I do that?

It is best not to capitalize your own expression names  to avoid name  
collisions, for example Exponent is already a built in Mathematica  
symbol.  That said, here's how I usually implement such a system:

Options[pow]={exponent->None};

pow[a_,opts___?OptionQ]:=Block[{exp},exp=exponent/.{opts}/.Options 
[pow]/.None->2;a^exp]

In[11]:=
pow[3]

pow[3,exponent->3]
Out[11]=
9
Out[12]=
27

Regards,

Ssezi


  • Prev by Date: Re: Applying a list of 2D paramters to a mathematica function
  • Next by Date: Re: line thickness 2D plot legend
  • Previous by thread: Programming with options.
  • Next by thread: RE: Programming with options.