2

I'm writing a function where I need to give many inputs. I created a list of variables from Table function.

My aim is to get

f[Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]]

from table which i create

Table[Subscript[x, i], {i, 1, 3}] 

What i need is

f @@ Blank /@ {Subscript[x, 1], 
    Subscript[x, 2], Subscript[x, 3], 
    Subscript[x, 4], Subscript[x, 5], 
    Subscript[x, 6], Subscript[x, 7], 
    Subscript[x, 8], Subscript[x, 9], 
    Subscript[x, 10]}

But that is not converting x to x_ . How to get that ?

Gummala Navneeth
  • 1,491
  • 7
  • 19
  • 2
    try f @@ (Pattern[#, Blank[]] & /@ {Subscript[x, 1], Subscript[x, 2], Subscript[x, 3], Subscript[x, 4], Subscript[x, 5], Subscript[x, 6], Subscript[x, 7], Subscript[x, 8], Subscript[x, 9], Subscript[x, 10]}) and see this q/a – kglr Oct 06 '19 at 16:40
  • hi, my aim is to convert all elements in the list {x1,x2,x3,x4} to {x1_,x2_,x3_,x4_} so that i can send this list to function f[x1_,x2_,x3_,x4_] = Sin[x1 x2 x3 x4] – Gummala Navneeth Oct 07 '19 at 05:13
  • I tried what you suggested it is not converting x to x_ – Gummala Navneeth Oct 07 '19 at 05:13
  • 1
    Guammala, try symbols = Array[Symbol["x" <> ToString@#] &, 5]; ClearAll[f]; f[## & @@ (Pattern[#, Blank[]] & /@ symbols)] := Evaluate[Sin[Times @@ symbols]]; f[a, b, c, d, e] – kglr Oct 07 '19 at 05:39

2 Answers2

2
symbols = Array[Symbol["x" <> ToString@#] &, 5];

ClearAll[f];
f[## & @@ (Pattern[#, Blank[]] & /@ symbols)] := Evaluate[Sin[Times @@ symbols]];

f[a, b, c, d, e]

Sin[a b c d e]

f @@ {a, b, c, d, e}

Sin[a b c d e]

kglr
  • 394,356
  • 18
  • 477
  • 896
1
Clear[f, lhs, rhs]

{lhs, rhs} = Transpose@ToExpression[
    {StringJoin["x", #, "_"], StringJoin["x", #]} & /@ 
     Array[ToString, 4]];

Evaluate[f @@ lhs] := Evaluate[Sin[Times @@ rhs]]

f[a, b, c, d]

(*  Sin[a b c d]  *)
LouisB
  • 12,528
  • 1
  • 21
  • 31