Is there an easier/better way (default arguments)?
- To: mathgroup at smc.vnet.net
- Subject: [mg27793] Is there an easier/better way (default arguments)?
- From: johntodd at fake.com (John Todd)
- Date: Sat, 17 Mar 2001 05:41:17 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hello, I recently was wrangling with the problem of taking the derivative with respect to a particular variable of each element of a list. As an example (I am using Mathematica 4.0): (* First, create the list *) myt = Table[Cos[\[Theta] + (n - 1) \[Alpha]], {n, 10}] (* The method outlined next didn't work for me, and I feel it has to do with the fact that the D function requires two arguments. If someone could point out another way other than my forthcoming solution to using a multi-parameter function with Map, I'd be obliged to see it. *) Map[D, myt] (* I then decided to define my own function and supply and default argument of alpha.... *) mymapfunc[arg1_, arg2_:\[Alpha]] := D[arg1, arg2] (* Now, when I call *) Map[mymapfunc, myt] (* It works as I had hoped it would, in that it takes the derivative with respect to alpha of each element in myt. *) (*I am quite new to Mathematica, and I hope that this question is not too mundane and simple, but, since I got it to work, I figured I'd run it by the group and see what the consensus is. One other quick question, whenever I would 're-execute' my function definition after changing the default value of arg2, it would retain the old default value unless I called Clear[mymapfunc] and then 're-executed' the function definition.*) (* For example, if this is my original mymapfunc .. *) mymapfunc[arg1_, arg2_:\[Alpha]] := D[arg1, arg2] (* And then I change it to *) mymapfunc[arg1_, arg2_:\[Theta]] := D[arg1, arg2] (* And press the 'Return' key, it retains the default of alpha. But if I call Clear[mymapfunc] and then 're-execute' it has the new value of theta as a default. Whenever I change any other part of my function, the change sticks without first having to clear. Anyone else come across this problem? *) Thanks very much, JT