2
$\begingroup$

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 ?

$\endgroup$
4
  • 2
    $\begingroup$ 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 $\endgroup$
    – kglr
    Commented Oct 6, 2019 at 16:40
  • $\begingroup$ 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] $\endgroup$ Commented Oct 7, 2019 at 5:13
  • $\begingroup$ I tried what you suggested it is not converting x to x_ $\endgroup$ Commented Oct 7, 2019 at 5:13
  • 1
    $\begingroup$ Guammala, try symbols = Array[Symbol["x" <> ToString@#] &, 5]; ClearAll[f]; f[## & @@ (Pattern[#, Blank[]] & /@ symbols)] := Evaluate[Sin[Times @@ symbols]]; f[a, b, c, d, e] $\endgroup$
    – kglr
    Commented Oct 7, 2019 at 5:39

2 Answers 2

1
$\begingroup$
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]  *)
$\endgroup$
2
$\begingroup$
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]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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