MathGroup Archive 2009

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

Search the Archive

Re: Is it possible to dynamically construct arguments to With[]?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105782] Re: Is it possible to dynamically construct arguments to With[]?
  • From: dr DanW <dmaxwarren at gmail.com>
  • Date: Fri, 18 Dec 2009 06:24:54 -0500 (EST)
  • References: <hgd7ts$bn3$1@smc.vnet.net>

This is a good one.  Often when I am working with equations I like to
keep everything symbolic until I am ready to get a numerical result,
then I substitute from a list of parameters of the form {a->2 b, b-
>3.0, "This Parameter"-> Pi, d:>6, ...}.  This can get ungainly when I
have a Block[] with several expressions that I need to substitute.  I
also found one case where ReplaceAll does not work, and this is
important enough to highlight:

ReplaceAll does not work with SparseArray's.

I have written the series of functions below to help me work with
ParameterList's.  The function ParameterBlock[plist, expr] does what
you are asking for, and the substitution also works within
SparseArray's.

By the way, I used this package to experiment with Workbench and have
it fully implemented as a paclet, complete with Mathematica 7
compatible Doc Center files with a Tutorial and a Guide.  Let me know
if you would like the complete paclet.

---- snip -----
ParameterListQ[
  pl : {(Rule[(_String | _Symbol), _] |
       RuleDelayed[(_String | _Symbol), _]) ...}] :=
    Max[ Tally[ pl[[All, 1]] ][[All, 2]] ] == 1

ParameterListQ[___] :=
    False;

ParameterList[rules___?OptionQ] :=
    DeleteDuplicates[Flatten[{rules}], SameQ[First[#1], First[#2]] &]

ParameterBlock[pl_?ParameterListQ, expr_] :=
    Block[ Evaluate[FilterRules[pl, _Symbol][[All, 1]]],
        Replace[FilterRules[pl, _Symbol], {Rule -> Set,
          RuleDelayed -> SetDelayed}, {2}, Heads -> True];
        expr //. FilterRules[pl, _String]
    ]

ResolveParameters[rules___?OptionQ] :=
    Module[ {pl},
        pl = ParameterList[rules];
        pl[[All, 2]] = ParameterBlock[pl, pl[[All, 1]] ];
        pl
    ]

---- end ---

Daniel


  • Prev by Date: vector and matrix valued functions
  • Next by Date: Re: Return in function
  • Previous by thread: Re: Is it possible to dynamically construct arguments to With[]?
  • Next by thread: Re: Special Characters Palette