I'm trying to define a function that replaces certain expressions that appear when calculating scattering amplitudes (not important). Basically, I have a function spab[a_,c_,b_]
, where a,b
are single elements and c
is a list with at most two levels. For example, I can write
spab[1,{2,3,4},5]; spab[4,{3},9]; spab[5,{{3,4},{7,6},2},1];
etc. I have another function spbb[a_,c_,b_]
with the same type of arguments and a last function spa[a_,b_]
with only single element arguments. What I want to do is to substitute spab[a_,c_,b_]
for the product
(*Sum over all i*) spa[a,c[[1,i]]]*spbb[c[[1,i]],c[[2;;]],b].
In other words, I want to take the first element of the list c
and set it as the single element argument for the spa
and spbb
functions. If c[[1]]
is itself a list, then I want to take each element of this sublist, apply the procedure I just described and then sum over these elements. I'll write up some examples of what I want the result to look like:
spab[1,{2,3,4},5]->spa[1,2]*spbb[2,{3,4},5];
spab[5,{{3,4},{7,6},2},1]->spa[5,3]*spbb[3,{7,6},2},1] + spa[5,4]*spbb[4,{7,6},2},1]
I though I could achieve this with something like:
spab[5,{{3,4},{7,6},2},1]/.spab[a_,c_,b_]->Plus[spa[a,#]*spbb[#,c[[2;;]],b]&/@c[[1]]]
In my mind, this first creates a list of products spa[a,c[[1,i]]]*spbb[c[[1,i]],c[[2;;]],b]
by using the Map command with the list c[[1]]
and then sums over al the elements. However, the result is:
During evaluation of In[19]:= Part::partd: Part specification c[[1]] is longer than depth of object.
During evaluation of In[19]:= Part::take: Cannot take positions 2 through -1 in c.
During evaluation of In[19]:= Part::pkspec1: The expression spa[a,1] spbb[1,c[[2;;All]],b] cannot be used as a part specification.
During evaluation of In[19]:= Part::pkspec1: The expression spa[5,1] spbb[1,{{7,6},2},1] cannot be used as a part specification.
Out[19]= (spa[5, {{3, 4}, {7, 6}, 2}] spbb[{{3, 4}, {7, 6}, 2}, {{7, 6}, 2}, 1])[[spa[5, 1]spbb[1, {{7, 6}, 2}, 1]]]
What do all these error messages mean? Why do I get this weird result, and what is the factor between [[ ]]
that appears at the very end? If someone culd help me make things clear and fix this, I would very much appreciate it.
Plus[spa[a, #]*spbb[#, c[[2 ;;]], b] & /@ c[[1]]]
gets evaluated before being used in the replacement rule. At that moment, c has no value (and so is not a list that Part can work on). That leads to a cascade of follow on errors as the ReplaceAll is attempted with a broken rule. $\endgroup$spab[5, {{3, 4}, {7, 6}, 2}, 1] /. spab[a_, c_, b_] :> Plus[spa[a, #]*spbb[#, c[[2 ;;]], b] & /@ c[[1]]]
$\endgroup$