4

Consider the following expression:

expr = a[x]*da[x,t]*db[x,t]c[x] + db[x,z]^2*a[x]^2*d[x] +d[x]^2*da[x,t]db[x,t]+ dc[x,z]^2 + c[x]*dc[x,z]^2*db[x,t]^6

Is there any way in Mathematica to leave only the terms with the total power of objects #[x] and d#[x,#] being $\leq 4$? The final expression should be

exprfinal = a[x]*da[x,t]*db[x,t]c[x] + d[x]^2*da[x,t]db[x,t] + dc[x,z]^2 
John Taylor
  • 5,701
  • 2
  • 12
  • 33
  • 6
    Do you mean something like Normal@Series[expr /. {factor : _[x] | _[x, t] | _[x, z] :> factor*$T}, {$T, 0, 4}] /. $T -> 1? – Michael E2 Aug 05 '23 at 13:18
  • That trick is from here: https://mathematica.stackexchange.com/a/15035/4999 – Michael E2 Aug 05 '23 at 13:23
  • @MichaelE2 : exactly, thanks! – John Taylor Aug 05 '23 at 13:27
  • Please include the desired output from your expression as well. – MarcoB Aug 05 '23 at 13:29
  • 2
    I see this forum is starting to get the Math Stack Exchange downvoting disease. – 1729taxi Aug 05 '23 at 14:18
  • 2
    @1729taxi This Q seems well-asked to me, but some of the other ones are not that good. This one's arguably a duplicate. But it took me a long time to find it, and I knew what I was looking for. – Michael E2 Aug 05 '23 at 14:52
  • 2
    @MichaelE2 true but some people get downvoting disease. I don't know if you hang around Math Stack Exchange but it is a cesspit - with a core group of about ten people who basically feed off downvoting and sarcastic comments to brand new posters. This place isn't too bad for it - though I have seen an increase recently in such behaviour. – 1729taxi Aug 05 '23 at 16:44
  • 1
    I’ve also noticed, with dismay, the uptick in downvotes. No idea why a question like this would warrant one. It’s a duplicate, but (i) that’s far from obvious and (ii) there is a better way than downvoting to handle duplicates (those people at StackExchange, they think of everything). – Daniel Lichtblau Aug 06 '23 at 16:02

2 Answers2

8

I learned this trick from this answer:

Normal@Series[
  expr /. {factor : _[x] | _[x, t] | _[x, z] :> factor*$T},
  {$T, 0, 4}
  ] /. $T -> 1

The present question is framed in a different context than Multivariable Taylor expansion does not work as expected, but the answer is the same to each.

On the other hand, if expr is a polynomial (as it is here), then the following approach works:

vars = Variables@expr;
deg = 4;
Fold[#1 . vars + #2 &, 
 Reverse@Take[CoefficientArrays[expr, vars], UpTo[deg + 1]]]

In many cases, such as if expr has a combination of parameters and variables, the user must supply an explicit list of variables.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
5

There is now a resource function for this.

expr = a[x]*da[x, t]*db[x, t] c[x] + db[x, z]^2*a[x]^2*d[x] + 
   d[x]^2*da[x, t] db[x, t] + dc[x, z]^2 + 
   c[x]*dc[x, z]^2*db[x, t]^6;
vars = Variables[expr];

In[160]:= ResourceFunction["MultivariateTaylorPolynomial"][expr, vars, 4]

(* Out[160]= a[x] c[x] da[x, t] db[x, t] + d[x]^2 da[x, t] db[x, t] + dc[x, z]^2 *)

Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199