0
$\begingroup$

I'm trying to figure out if I can use the letters contained in Alphabet[] as variables for a function. As a simplified example, I'd like to do something like:

g[a_] := f[Alphabet[][[1]]]

Which however doesn't work since if I call, for example, g[3] it returns again f[a], rather than f[3].

Does anyone know a way out?

Thanks!

EDIT:

To explain better what I need this for. I have a defined a function which, given a Lagrangian, computes the corresponding Feynman rules. The function is such that when it is called, for example, as:

FeynRule[dot[\[DoubledPi]]^3]

It returns:

6 \!\(\*SubscriptBox[\(\[Omega]\), \("a"\)]\) \!\(\*SubscriptBox[\(\[Omega]\), \("b"\)]\) \!\(\*SubscriptBox[\(\[Omega]\), \("c"\)]\)

Which is actually correct.

However, I would like user to be able to replace the a,b,c,... labels with whatever they want; including sums of labels.

The solution of using Symbol@Alphbet[][[1]] does work, but as anticipated by Lucas Lang, it does not work with SetDelayed[].

Is there a better way of doing it?

EDIT 2

As a practical example, I've simplified my function, which now looks like this:

FeynRule[L_] := Do[
  n = Exponent[L, del[x].del[x]];
  y = Product[
    Sprod[Alphabet[][[i]], Alphabet[][[i + 1]]], {i, 1, n}];
  Return[y];
  , 1]

where Sprod[,] is a function I have defined somewhere else, whose only properties I'm interested in are that Sprod[x+y,z]=Sprod[x,z]+Sprod[y,z], and that it's symmetric on its two arguments.

Now, if I run a simple example for the FeynRule function:

FeynRule[del[x].del[x]]

it correctly gives:

Sprod[Alphabet[][[1]], Alphabet[][[2]]]

Now, I would like to be able to define a function by simply doing

f[a_,b_]:=FeynRule[del[x].del[x]]

So that, for example, f[a+b,c] returns Sprod[a,c]+Sprod[b,c]. This is the part that does not work. The only work around I've found so far is to do:

f[c_, d_] := 
 FeynRule[del[x].del[x]] /. 
  Sprod[Alphabet[][[1]], Alphabet[][[2]]] -> Sprod[c, d]

This does work, but it is a bit cumbersome and it would require to explain the user how to do it.

$\endgroup$
  • $\begingroup$ Do you want f[3] or f["c"]? $\endgroup$ – Kuba Dec 9 '19 at 9:40
  • $\begingroup$ I'd like to get back f[3], not f["c"]. I'm essentially doing this because I'm writing a code which will have a large number of variables in sequential order, and the alphabet seems to be the best way. Also because the code is supposed to be user friendly, so that different people in my collaboration case use it. $\endgroup$ – Einj Dec 9 '19 at 9:42
  • $\begingroup$ Perhaps you are looking for Symbol. $\endgroup$ – C. E. Dec 9 '19 at 10:20
  • 2
    $\begingroup$ Note that any potential solution will be quite fragile, and none of them will (easily) work with SetDelayed, since the substitution of argument values happens too early for you to catch. I would suggest you explain your usecase for this, then we can probably help you to find a cleaner solution for your problem. (Because as it stands, you will for example still have to type out all your variable names on the left side of the function definition) $\endgroup$ – Lukas Lang Dec 9 '19 at 11:08
  • 2
    $\begingroup$ Could you prepare a small example that shows what you need, does not require external packages and does not use special characters? The easier it is to use the more likely someone will help. $\endgroup$ – Kuba Dec 9 '19 at 13:59
1
$\begingroup$

Here is an alternative, unless I got your question wrong, but tbh I am not sure I know what you want:

rule[n_] := Module[{body},
  body = Product[Sprod[Slot[i], Slot[i + 1]], {i, 1, n}];
  Function @ Evaluate @ body
]

rule[2]
Sprod[#1, #2] Sprod[#2, #3] &
f = rule[2];

f[a, b, c]
Sprod[a, b] Sprod[b, c]
| improve this answer | |
$\endgroup$
0
$\begingroup$

this is what you need

g[a_] = f[ToExpression@Alphabet[][[1]]]

worth reminding: if you use := then it will be like this.

g[3] -> evaluate f[Alphabet[][[1]]] -> evaluate Alphabet[][[1]] -> get a -> get f[a]

you can run this to see what happens.

t = Alphabet[];
g[a_] := f[t[[1]]];
g[3] // Trace

I set t because trace the Alphabet[] will be a mess.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thanks wuyudi! But as it was suggested in the comments, this doesn't really work with SetDelayed, which rathe restrictive for me. $\endgroup$ – Einj Dec 9 '19 at 12:46

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.