# Pattern matching & simple replacement

I have a question about an expression that I want to modify. Here is the expression:

-(((cA - 2 cF) (-cF (1 + x) log[1 - x] +
cF (-(1/3) π^2 delta[1 - x] + plusd[(2 Log[1 - x])/(1 - x)])))/(2 cF)) +
1/2 cA (delta[
1 - x] (1/2 (-3 + mgl^2/(mgl^2 + sa1)) + (mgl^2 Log[mgl^2/(mgl^2 + sa1)])/
sa1) + (3/2 cF delta[
1 - x] ((2 mgl)/(mgl + Sqrt[mgl^2 + sa1]) +
Log[(2 mgl^2 + sa1 - 2 mgl Sqrt[mgl^2 + sa1])/sa1]) -
cF (1 + x) Log[(sa1 (1 - x))/(mgl^2 + sa1 (1 - x))])/
cF + (2 Log[2 - x])/(-1 + x) +
2 Log[(sa1 (-2 + x))/(-mgl^2 + sa1 (-2 + x))] plusd[1/(1 - x)] +
Log[2 + mgl^2/sa1 - x] plusd[-(2/(-1 + x))] + 2 plusd[Log[1 - x]/(1 - x)] +
plusd[-((sa1^2 (-1 + x))/(2 (mgl^2 + sa1 - sa1 x)^2)) + (2 (1 +
Log[1 + mgl^2/sa1 - x]))/(-1 + x)]) +
1/2 (cA - 2 cF) (-cF (5 - π^2) delta[1 - x] +
cF (1 - x - (1 + x) Log[(1 - x)/x] + plusd[(2 Log[(1 - x)/x])/(1 - x)]))


hm, looks a bit weird but I am new to this forum and don't know how to format that expression in a proper way :D.

So, now I want to collect some terms. I have a function, called plusd[]. The function has different arguments including x and the function is multiplied by a sequence of other expressions.

What I want is to replace x with 1 in each factor that is multiplied by that function, not in the function argument itself and not in the part not multiplied by the function. The other part, which is not multiplied by that function I want to set to zero.

I guess it is easy with some pattern matching stuff but my skills are not good enough so I am asking here for help.

I am glad for any advice and of course also how to post my formulas in a nicer way.

Cheers and best regards,

Marcel

seems to work:

expr /. Times[xx_?(! FreeQ[#, x] &) , p_plusd, rest___] :> Times[xx /. x -> 1, p, rest]


and terms like:

2 Log[(sa1 (-2 + x))/(-mgl^2 + sa1 (-2 + x))] plusd[1/(1 - x)] and

Log[2 + mgl^2/sa1 - x] plusd[-(2/(-1 + x))]

were reduced to

2 Log[-(sa1/(-mgl^2 - sa1))] plusd[1/(1 - x)] and

Log[1 + mgl^2/sa1] plusd[-(2/(-1 + x))]

[...] The other part, which is not multiplied by that function I want to set to zero. [...]

The expression is partially collected so let's expand it before:

Map[
If[
FreeQ[#, _plusd],
0,
# /. Times[xx_?(! FreeQ[#, x] &), p_plusd, rest___] :> Times[xx /. x -> 1, p, rest]
]&
,
ExpandAll[expr]
]

cA Log[-(sa1/(-mgl^2-sa1))] plusd[1/(1-x)] +
1/2 cA Log[1+mgl^2/sa1] plusd[-(2/(-1+x))] +
1/2 cA cF plusd[(2 Log[-1+1/x])/(1-x)] +
-cF^2 plusd[(2 Log[-1+1/x])/(1-x)] +
cA plusd[Log[1-x]/(1-x)] +
-(1/2) cA plusd[(2 Log[1-x])/(1-x)] +
cF plusd[(2 Log[1-x])/(1-x)] +
1/2 cA plusd[2/(-1+x)+sa1^2/(2 mgl^4+4 mgl^2 sa1+2 sa1^2-4 mgl^2 sa1 x-4 sa1^2 x+2 sa1^2 x^2)-(sa1^2 x)/(2 mgl^4+4 mgl^2 sa1+2 sa1^2-4 mgl^2 sa1 x-4 sa1^2 x+2 sa1^2 x^2)+(2 Log[1+mgl^2/sa1-x])/(-1+x)]

• Hello, thanks for your reply. Yes, it works. But can you explain the code a bit more. For pattern matching the only thing I know is sth like x__ and x_ :D. And the code does not neglect all the other factors which are not multiplied by plusd[]. Or better it does neglect them, but I wanna set them to zero :D. Thanks in advance! – Marcel May 21 '15 at 6:34
• @Marcel Currently each term has plusd inside. What is term in your question then? Do you want to ExpandAll? About pattern matching, take a look at PatternTest and ReplaceAll. – Kuba May 21 '15 at 6:46
• ah, you are right. I wanna expand, yes:D. That I only have a sum of terms. So, expanding the expression beforehand will work, right? I will test :). Thanks a lot :D – Marcel May 21 '15 at 6:48
• hm, now I have all the terms expanded, but those terms are still in the expression after your replacement rule. – Marcel May 21 '15 at 6:50
• hm, I copied the code and replace expr with my expression. What I got now is th. like that 0[1/2 cA plusd[ 2/(-1 + x) + sa1^2/( 2 mgl^4 + 4 mgl^2 sa1 + 2 sa1^2 - 4 mgl^2 sa1 x - 4 sa1^2 x + 2 sa1^2 x^2) - (sa1^2 x)/( 2 mgl^4 + 4 mgl^2 sa1 + 2 sa1^2 - 4 mgl^2 sa1 x - 4 sa1^2 x + 2 sa1^2 x^2) + (2 Log[1 + mgl^2/sa1 - x])/(-1 + x)]] so, everywhere is now a zero as a factor. Sorry, I don't understand that much mathematica :) – Marcel May 21 '15 at 6:55