Consider the following polynomial:
expr = (1 + a[x] + c[x, aa] + c[x, aa] da[x, aa] +
da[x, aa]*da[x, aa] +
dc[x, aa, bb]*da[x, cc]*da[x, bb]*eps[aa, bb, cc, dd])^3 //
Expand
Among all the terms, I want to leave only those which contain exactly three a[x] or da[x,yy_] and one c or dc[x,yy_,zz_]; it is okay if they have eps[aa,bb,cc,dd]. At least one of the terms from expr matching these requirements is 3 a[x]^2 c[x, aa] da[x, aa].
Could you please tell me if this is possible in a more or less efficient way in Mathematica (in my realistic example, I deal with thousands of terms)?
Edit
This is my ugly realization. It uses the following answer:
ruleFixedPower[expr_, field_, dfield_, n_] := (Normal@
Series[expr /. {factor :
field[x] | field[x, a_] | dfield[x, a_] |
dfield[x, a_, b_] :> factor*$T}, {$T, 0, n}] /. {$T ->
1}) - (Normal@
Series[expr /. {factor :
field[x] | field[x, a_] | dfield[x, a_] |
dfield[x, a_, b_] :> factor*$T}, {$T, 0,
Max[0, n - 1]}] /. {$T -> 1})
Then,
expr1 = ruleFixedPower[expr, a, da, 3];
expr2 = ruleFixedPower[expr1, c, dc, 1]
6 a(x) da(x,bb) da(x,cc) dc(x,aa,bb) eps(aa,bb,cc,dd)+6 a(x) c(x,aa) da(x,aa)^2+3 a(x)^2 c(x,aa) da(x,aa)+6 c(x,aa) da(x,aa)^3
Unfortunately, it is slow as hell for my realistic tasks with many thousands of terms and more than two different structures, the powers of which have to be fixed. I may pre-select to have only the terms with the total degree equal to n, where n is the total degree of the desired output, but still, it is slow.
The derivative expansion would work much faster than this construction with Series, but it obviously introduces wrong expansion coefficients.
To illustrate the slowness, consider the following expansion:
expr = (1 + a[x] + c[x, aa] + a[x]*b[aa] + c[x, aa]* da[x, aa] +
dc[x, bb] dx[x, dd] + da[x, aa]*da[x, aa] +
dc[x, aa, bb]*da[x, cc]*da[x, bb]*eps[aa, bb, cc, dd])^16 //
Expand;
expr // Length
167637
(*Selecting terms not with the common power not higher than n*)
ruleCommonPower[expr_,
n_] := (Normal@
Series[expr /. {factor :
a[x] | ca[x, a_] | da[x, a_] | dc[x, a_, b_] :>
factor*$T}, {$T, 0, 14}] /. {$T -> 1})
expr0 = ruleCommonPower[expr, 14]; // AbsoluteTiming
expr1 = ruleFixedPower[expr0, a, da, 10]; // AbsoluteTiming
expr2 = ruleFixedPower[expr1, c, dc, 4] // AbsoluteTiming
This code gets stuck.
ruleFixedPower, you ought to be able to useSeriesCoefficientand avoid subtracting two series. – Michael E2 Aug 10 '23 at 23:28a[x], da[x,yy_] , dc[x,yy_,zz_]but the putative term then given,3 a[x]^2 c[x, aa] da[x, aa], has nodc[...], contrary to the requirement that there be exactly one (I assume this counting is by multiplicity). Before tackling this I'd want to see the actual objective in unambiguous terms. – Daniel Lichtblau Aug 11 '23 at 00:14