[Date Index]
[Thread Index]
[Author Index]
Re: Passing function arguments as lists of replacement rules
*To*: mathgroup at smc.vnet.net
*Subject*: [mg104706] Re: [mg104661] Passing function arguments as lists of replacement rules
*From*: "David Park" <djmpark at comcast.net>
*Date*: Sat, 7 Nov 2009 06:49:28 -0500 (EST)
*References*: <3794357.1257503749087.JavaMail.root@n11> <004601ca5ee9$21b680c0$65238240$@net> <29412214.1257538683067.JavaMail.root@n11>
Leo,
This is certainly an interesting question and it is very worthwhile writing
routines that make it easy to use Mathematica for your particular purposes.
I call them convenience routines. There may be many ways to do this and you
may get many suggestions, including dynamic input. Here is one other
possibility with a sample foo that has most of the features that you want.
ClearAll[foo]
Options[foo] = {PrintParameters -> False};
foo[parameters_List: {1, 2, 3}, OptionsPattern[]][x_, y_] :=
Module[{a, b, c},
{a, b, c} = parameters;
If[OptionValue[PrintParameters], Print[{a, b, c}]];
a x + b y + c x y
]
Now we could just use this in a normal manner:
foo[][x, y]
foo[PrintParameters -> True][x, y]
foo[{3, 2, 1}][x, y]
But if you wanted to actually edit the input parameters, and maybe if you
had a longer list of parameters, you could first type the following
statement:
With[
{a = 1,(* Temperature Kelvin *)
b = 2, (* Velocity m/s *)
c = 3}, (* Altitude meters *)
foo[{a, b, c}][x, y]]
Now open up the cell to get the underlying expression. (Shift-Ctrl-E, or
Menu -> Cell -> Show Expression) Copy the Cell expression. Then paste it in
the following expression:
fullfoo :=
CellPrint[Cell[BoxData[RowBox[{"With", "[", "
", RowBox[{RowBox[{"{",
RowBox[{RowBox[{"a", "=", "1"}], ",",
RowBox[{"(*", " ", RowBox[{"Temperature", " ", "Kelvin"}],
" ", "*)"}], "
", RowBox[{"b", "=", "2"}], ",", " ",
RowBox[{"(*", " ",
RowBox[{"Velocity", " ", RowBox[{"m", "/", "s"}]}], " ",
"*)"}], "
", RowBox[{"c", "=", "3"}]}], "}"}], ",", " ",
RowBox[{"(*", " ", RowBox[{"Altitude", " ", "meters"}], " ",
"*)"}], "
",
RowBox[{RowBox[{"foo", "[",
RowBox[{"{", RowBox[{"a", ",", "b", ",", "c"}], "}"}],
"]"}], "[", RowBox[{"x", ",", "y"}], "]"}]}], "]"}]],
"Input"]]
Now, if you evaluate fullfoo, you will obtain the initial expression with
the default values of the parameters and annotation. One can then just edit
whichever values one wishes, or put the option into foo, and evaluate.
So, you can have it both ways, short if you set the parameters once and for
all, and long if you want to play with the parameters. The long way is also
nice if you are writing it for someone else who doesn't know Mathematica
that well and you don't know what changes they may wish to make.
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/
From: leo.alekseyev at gmail.com [mailto:leo.alekseyev at gmail.com] On Behalf Of
Leo Alekseyev
This is certainly one option, but does it have any benefits over the
list of string replacement rules?..
I recently started using both the options pattern _and_ a replacement
list. The actual functions I'm working with look something like
foo[x_,y_,parameters_,OptionsPattern[]],
where x and y are bona fide dependent variables, parameters is a list
of fixed constants pertinent to the system (provided as replacement
rules of the form, e.g. {"a"->1,...}, and OptionsPattern[] conveys
certain options pertaining to the computation itself, e.g.
{AvoidDivideByZero->True}
So far it seems like a sensible approach, but I am still wondering if
other people do similar things in their code.
--Leo
On Fri, Nov 6, 2009 at 8:57 AM, David Park <djmpark at comcast.net> wrote:
> Use the newer Version 6 Options facilities.
>
> ClearAll[foo]
> Options[foo] = {a -> 0, b -> 0, c -> 0};
> SyntaxInformation[foo] = {"ArgumentsPattern" -> {OptionsPattern[]}};
> foo[OptionsPattern[]] :=
> Module[
> {aval = OptionValue[a],
> bval = OptionValue[b],
> cval = OptionValue[c]},
> {aval, bval, cval}]
>
> foo[a -> 1, b -> 2]
> {1, 2, 0}
>
> Then try typing:
>
> foo[a -> 1, q -> 2]
>
>
> David Park
> djmpark at comcast.net
> http://home.comcast.net/~djmpark/
>
>
> From: dnquark [mailto:dnquark at gmail.com]
>
>
> I wish to (a) avoid having to pass a dozen parameters to a function
> and have to deal with remembering which goes where and (b) retain the
> flexibility in terms of adding new parameters. It seems that a good
> solution would be to pass my parameters as a structure with named
> fields (this is how I do it in another system). In Mathematica, I came up
> with something like this:
>
> foo[paramList_] := Module[{a,b,c},
> {a,b,c} = {"a","b","c"}/.paramList;
> {a,b,c}
> ]
> sample usage: e.g. foo[{"a"->1,"b"->2}]
>
> My question to the group: is this a good solution? Are there better
> ways to achieve my goals?..
> Thanks,
> --Leo
>
>
>
Prev by Date:
**Re: How to make floating control elements?**
Next by Date:
**Re: How to make floating control elements?**
Previous by thread:
**Re: Passing function arguments as lists of replacement**
Next by thread:
**Simplification problem**
| |