MathGroup Archive 2005

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

Search the Archive

Re: Passing options through user-defined functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg59013] Re: Passing options through user-defined functions
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Tue, 26 Jul 2005 04:03:54 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 7/25/05 at 1:30 AM, joe at eshu.net (Joe Christy) wrote:

>I have a function implemnted as module which uses various builtin
>functions. What is the best way to pass options to the builtin
>functions?

>For example, I have something like

>foo[p,q]:=
>Module[
>   {bar},
>   bar=Table[
>       f[x,y],
>       {x,1,p},{y,1,q}
>   ];
>   ArrayPlot[bar]
>]

>and I would like to be able to write something like

>foo[3,5, size -> 100]

>and see the results of ArrayPlot[bar, ImageSize->{3*100,5*100}]

Your specific example could be done as follows:

foo[p_,q_,opts___]:=
Module[
    {bar},
    bar=Table[
        f[x,y],
        {x,1,p},{y,1,q}
    ];
    ArrayPlot[bar,ImageSize->size {p,q}/.opts]
]

You may also find it useful to define default options for foo using

Options[foo]={size->100}

Also if you use options frequently in your functions you will find the package Utilities`FilterOptions` useful. The FilterOptions package allows you to have options for your function as well as the called functions. Assuming this package is loaded your example could become

foo[p_,q_,opts___]:=
Module[
    {bar},
    bar=Table[
        f[x,y],
        {x,1,p},{y,1,q}
    ];
    ArrayPlot[barImageSize->size {p,q}/.opts,
      FilterOptions[ArrayPlot,opts]
    ]
]

With this version you can include any of the options for ArrayPlot as arguements to foo.
--
To reply via email subtract one hundred and four


  • Prev by Date: Problem with loading SuperWidgetPackage
  • Next by Date: Re: Re: Re: Re: Mathematica 5.2: The 64-bit and multicore release
  • Previous by thread: Re: Passing options through user-defined functions
  • Next by thread: Re: Passing options through user-defined functions