Find and replace particular multiplication of factors in big multiplication

I have a sum of terms where each is a multiplication of several factors. For example my expression in FullForm is

expr = Plus[Times[Derivative[1][a][x], Derivative[0, 1][z][t, x]],
Times[R, z[t, x], Derivative[1][a][x], Derivative[0, 1][\[Sigma]][t, x]],
Times[2, R, a[x], Derivative[0, 1][z][t, x], Derivative[0, 1][\[Sigma]][t, x]],
Times[Power[R, 2], a[x], z[t, x], Power[Derivative[0, 1][\[Sigma]][t, x], 2]],
Times[a[x], Derivative[0, 2][z][t, x]], Times[R, a[x], z[t, x], Derivative[0, 2][\[Sigma]][t, x]], Derivative[1, 0][z][t, x],
Times[R, z[t, x], Derivative[1, 0][\[Sigma]][t, x]]]


I want to know if in any of the terms I have a multiplication of the form

Times[z[t, x],Derivative[1][a][x]]


and replace it with another expression in that case.

I am stuck even trying to find my particular multiplication. I have tried with

Cases[expr, Times[z[t, x],Derivative[1][a][x]],\[Infinite]]


But since the exact expression I am looking for does not appear in the full form I only get

out  = {}


As a start I would be happy if I could only find if my desired factor appears in any term. Then once I can do that it would be even better to replace it, but let us go one step at a time.

expr /. Times[p___, Derivative[1][a][x], z[t, x], q___] :> Times[p, newexpr, q]


Replace newexpr as desired.

Note the triple underscore on patterns p___ and q___, which means something like: "This is a pattern which may contain any number of elements in sequence, if it even appears at all." This allows us to match forms like Times[a'[x], z[t,x]], Times[..., a'[x], z[t,x]], and Times[a'[x], z[t,x], ...] simultaneously. This is largely just required because Times tags agglomerate together if they end up next to each other, so it's difficult to predict how large they are going to be. Because Mathematica does various things to put them into a canonical ordering and considers Times commutative, however, it shouldn't be an issue with what order you specify the replacement in.

You can also use Replace with simpler pattern:

Replace[expr, s_. Derivative[1][a][x] z[t, x] :> s newexpr, All]


(a^′)[x] (z^(0,1))[t,x]+newexpr R (σ^(0,1))[t,x]+2 R a[x] (z^(0,1))[t,x] (σ^(0,1))[t,x]+R^2 a[x] z[t,x] (σ^(0,1))[t,x]^2+a[x] (z^(0,2))[t,x]+R a[x] z[t,x] (σ^(0,2))[t,x]+(z^(1,0))[t,x]+R z[t,x] (σ^(1,0))[t,x]

 TeXForm @ %


$$a'(x) z^{(0,1)}(t,x)+R^2 a(x) \sigma ^{(0,1)}(t,x)^2 z(t,x)+2 R a(x) \sigma ^{(0,1)}(t,x) z^{(0,1)}(t,x)+R a(x) \sigma ^{(0,2)}(t,x) z(t,x)+a(x) z^{(0,2)}(t,x)+\text{newexpr} R \sigma ^{(0,1)}(t,x)+R \sigma ^{(1,0)}(t,x) z(t,x)+z^{(1,0)}(t,x)$$

• Can I just ask what the s_. stands for?
– I.C.
Jun 28 '19 at 21:03
• @I.C. s_. is the pattern that stands for the Default argument ( for Times it is 1). So s_. foo matches both 5 foo (where a multiplicative factor (5) is there) and foo (where the multiplicative factor is the default argument of Times, i.e. 1). Hope this helps.
– kglr
Jun 29 '19 at 0:01