2
$\begingroup$

I've got the following monomial list:

monlist={c x^3, x^5, 4};

I need to compute the successive derivatives of that list of monomials such that when the derivative of all elements in the list is zero, the Do loop stops. For a single monomial it is easy to implement the Do loop with the If statement, but for a monomial list I don't understand how to do it.

My goal is to get the following list structure:

{{3 c x^2, 5 x^4, 0}, {6 c x, 20 x^3, 0}, {6 c, 60 x^2, 0}, {0, 120 x,0}, {0, 120, 0}, {0, 0, 0}}

I thank you in advance for any suggestion.

$\endgroup$

2 Answers 2

6
$\begingroup$
Most@FixedPointList[D[#, x] &, monlist]
{{c x^3, x^5, 4}, {3 c x^2, 5 x^4, 0}, {6 c x, 20 x^3, 0}, {6 c, 
  60 x^2, 0}, {0, 120 x, 0}, {0, 120, 0}, {0, 0, 0}}

EDIT

Using While and for the sake of comparison with the procedural approach:

monlist = {c x^3, x^5, 4};
dlist = {monlist};
While[Last@dlist =!= {0, 0, 0},
 AppendTo[dlist, D[Last@dlist, x]]
 ]
dlist
$\endgroup$
3
  • $\begingroup$ Nice answer, Syed! $\endgroup$ Commented Feb 13, 2022 at 4:29
  • $\begingroup$ With Rest and Most commands. It's perfect, thanks a lot, Syed! $\endgroup$ Commented Feb 13, 2022 at 4:33
  • 2
    $\begingroup$ Thanks for the accept and the feedback; Best Regards. Try: FixedPointList[D[#, x] &, monlist][[2 ;; -2]] as a variation. $\endgroup$
    – Syed
    Commented Feb 13, 2022 at 4:39
6
$\begingroup$

Another possibility

 monlist = {c x^3, x^5, 4};
 NestWhileList[D[#, x] &, monlist, Total[#] =!= 0 &]

Mathematica graphics

Edit

And in the spirit of procedural like programming, here is my version

monlist = {c x^3, x^5, 4};
First@Last@Reap@While[True, Sow[monlist = D[monlist, x]]; 
    If[Total[monlist] === 0, Break[]]]

Mathematica graphics

I am sure the NestWhileList will be faster for very long list but I did not do any measurements.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.