[Date Index]
[Thread Index]
[Author Index]
Re: can Mathematica generate itself a function?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg31782] Re: [mg31769] can Mathematica generate itself a function?
*From*: Andrzej Kozlowski <andrzej at tuins.ac.jp>
*Date*: Sun, 2 Dec 2001 04:24:55 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
You can't use patterns like a[1]_ because Mathematica always interprets
this as
In[1]:=
a[1]_//FullForm
Out[1]//FullForm=
Times[a[1],Blank[]]
You can however replace non-atomic expressions like a[1] by symbols like
a1,a2 etc, and achieve the same thing that you wanted with a rather
clumsy piece of code:
In[3]:=
(Evaluate[
function[w_,
Sequence@@
ToExpression[AddUnderscore2/@(("a"<>ToString[#])&/@Range[5])]]]=
Sum[(a[i]*w^2)/(1+a[i]^2*w^2),{i,1,5}]/.a[i_]:>
ToExpression[ToString[a]<>ToString[i]]);
In[4]:=
function[z,a,b,c,d,e]
Out[4]=
2 2 2 2 2
a z b z c z d z e z
--------- + --------- + --------- + --------- + ---------
2 2 2 2 2 2 2 2 2 2
1 + a z 1 + b z 1 + c z 1 + d z 1 + e z
However, it is much more elegant to dispense with this approach
altogether (unless for some reason you really need to specify the number
of parameters which in the example you gave is not the case) and use
something like
In[5]:=
Clear[function]
In[6]:=
function[w_,x__]:=Tr[(w^2*#^2/(1+#^2*w^2))&/@{x}]
In[7]:=
function[w,a,b,c,d,e]
Out[7]=
2 2 2 2 2 2 2 2 2 2
a w b w c w d w e w
--------- + --------- + --------- + --------- + ---------
2 2 2 2 2 2 2 2 2 2
1 + a w 1 + b w 1 + c w 1 + d w 1 + e w
You can of course now use any number of arguments, e.g.
In[8]:=
function[w,a,b,c,d,e,f]
Out[8]=
2 2 2 2 2 2 2 2 2 2
a w b w c w d w e w
--------- + --------- + --------- + --------- + --------- +
2 2 2 2 2 2 2 2 2 2
1 + a w 1 + b w 1 + c w 1 + d w 1 + e w
2 2
f w
---------
2 2
1 + f w
On Saturday, December 1, 2001, at 04:45 PM, Romoscanu, Alexandre wrote:
> Hello
>
> I am trying to set Mathematica to create itself a function, and then
> consider it as a function definition.
>
> Suppose I want to create a function of the form
>
> function[w,a1,...aN]
>
> where w is a variable and a1...aN, a total of N parameters (such a
> function can for example be used as a fitting model with N parameters in
> NonlinearRegress, etc...)
>
> In some cases, 2 parameters will be requested, i.e. N=2.
>
> In some, one would like the function to have 5 parameters(N=5) to
> define, say a 1-variable, 5-parameter fitting model.
>
> I would like to program Mathematica to create the function itself on a
> given pattern, my only input being the number of parameters (here, N). I
> just do not want to retype a new function each time for the cases I want
> 2 paramaters, or 10...or N.
>
> Keeping in mind that a function is defined as f[x_,y_]=Sin[x+y] (for
> instance), if the total number of parameter pairs is defined by the
> positive integer
>
> NumberOfModules (=5 for example)
>
> then the following expressions obviously create the left part of a
> potential definition statement (like "f[x_,y_]=Sin[x+y]"):
>
> In[1]:= AddUnderscore2[varname_String] := varname <> "_";
>
> In[2]:= function[w_,
> Sequence @@ (ToExpression[
> AddUnderscore2 /@ (ToString /@ Array[a, NumberOfModules])])
>
>
> give
>
> Out[2]:= function[w_, a[1] _, a[2] _, a[3] _, a[4] _, a[5] _]
>
> because NumberOfModules was chosen to be 5.(+notice the problematic
> blank in front of the _.)
>
> The right part could look as
>
> Sum[(a[i]*w^2)/(1 + a[i]^2*w^2), {i, 1, NumberOfModules}]
>
> Hence,
>
> function[x_, y_,
> Sequence @@ (AddUnderscore2 /@ (ToString /@ Array[a,
> NumberOfModules]))] =
>
> Sum[(a[i]*w^2)/(1 + a[i]^2*w^2), {i, 1, NumberOfModules}]
>
> Looks like the definition of a formula.
>
> My problem is the last step...Mathematica does not (without surprize)
> look at the expression right above as it looks to "f[x_,y_]=Sin[x+y]".
>
> There must be a trick with ToExpression, ToString, or some other
> low-level Mathematica command.
>
> Thank you for any hint-
>
> Alexandre Romoscanu
> Grad Student, ETH Zurich
>
> PS Please, mail me a copy of a potential reply, my newsgroup access is
> quite unreliable.
>
>
>
Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/
Prev by Date:
**Re: can Mathematica generate itself a function?**
Next by Date:
**coloring everything outside a circle**
Previous by thread:
**Re: can Mathematica generate itself a function?**
Next by thread:
**Re: can Mathematica generate itself a function?**
| |