1
$\begingroup$

Suppose I have two lists of parameters: $\delta=\{\delta1, \delta2, …\}$ and $\gamma = \{\gamma1, \gamma2, …\}$.

The question is: how can one use $\delta$ and $\gamma$ (the parameter vectors) within a function definition to avoid typing out all of the parameters one-by-one? That is, how can I write $F[\delta \_,\gamma\_] :=...$ instead of having to type out all the individual parameters as in: $F[\delta1\_,\delta2\_,..., \gamma1\_,\gamma2\_,...] :=...$?

NOTE: Below is some code that generates the parameter vectors that I would like to use within the function definition:

Nobs = 5; 
\[delta] = 
  Symbol[#] & /@ Table[l[i] = “\[delta]" <> ToString[i], {i, 1, Nobs}];
\[gamma] = 
  Symbol[#] & /@ 
   Table[l[i] = “\[gamma]" <> ToString[i], {i, 1, Nobs}];

PS: sorry this is somewhat of an odd question... But my problem has a lot of parameters, and this is a possible fix.

$\endgroup$
  • 2
    $\begingroup$ while one can do what you describe I hardly can believe it really is what you want. Doesn't Davids answer do exactly what you want? You probably should describe more clearly what your ultimate goal is... $\endgroup$ – Albert Retey Feb 25 '15 at 22:46
  • $\begingroup$ If delta and gamma are treated each as one unit in g then you simply use f[x_, y_] := g[Sequence @@ x, Sequence @@ y] $\endgroup$ – Algohi Feb 25 '15 at 23:25
  • $\begingroup$ This question is confusing. I agree with Albert that it is unlikely that you actually want to do what (he interprets that) you write. Some questions that may be related to your goal: (6588), (15749), (26686), (52057), (55833) $\endgroup$ – Mr.Wizard Feb 26 '15 at 0:03
  • $\begingroup$ Thanks for the suggestions, and sorry for the confusion. I will try to edit the question. $\endgroup$ – Seb Feb 26 '15 at 0:23
3
$\begingroup$

As mentioned in my comment I would not recommend to do what follows except for certain special cases and I'm almost sure that there is a better solution for your actual problem than this. Nevertheless, what you ask for can be done like this:

Nobs = 10;
d = Symbol[#] & /@ Table["d" <> ToString[i], {i, 1, Nobs}];
g = Symbol[#] & /@ Table[ "g" <> ToString[i], {i, 1, Nobs}];

With[{
    dargs = Sequence @@ (Pattern[#, Blank[]] & /@ d),
    gargs = Sequence @@ (Pattern[#, Blank[]] & /@ g)
  },
  f[dargs, gargs] := Evaluate[h[d, g]]
];

by checking downvalues you can verify that it did what you want:

DownValues[f]

The trick here is as usual to look at the FullForm of the pattern you want to generate:

FullForm[x_]

this, plus applying Sequence to the list of patterns are the two building blocks which make the above code do what it does...

$\endgroup$
2
$\begingroup$

Define your function with list variables and be sure that your function definition on the right-hand side applies to lists (as in this case of Total).

f[x_List, y_List] := Total[x] Total[y];

f[{3}, {4}]

(* 12 *)

f[{3, 6}, {4}]

(* 36 *)

f[{3, 6}, {4, 2, 1}]

(* 63 *)

$\endgroup$
2
$\begingroup$

If you have need of individually addressing the parameters by name you could use:

δ = {δ1, δ2, δ3, δ4, δ5};
γ = {γ1, γ2, γ3, γ4, γ5};

Quiet[toPattern[s_Symbol] := s_]

foo[toPattern /@ δ, toPattern /@ γ] := {δ3/γ1, δ4/γ3, δ5/γ5}

Check:

?foo
Global`foo

foo[{δ1_,δ2_,δ3_,δ4_,δ5_},{γ1_,γ2_,γ3_,γ4_,γ5_}] := {δ3/γ1, δ4/γ3, δ5/γ5}
foo[{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}]
{5/2, 7/6, 9/10}

In most cases such things are not necessary. If you give a broader context for your question a different method may be recommended.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.