Python oneliner to differentiate polynomial list: Difference between revisions

From wikiluntti
(Created page with "== Introduction == Oneliners are powerful and often beautiful solutions to programming challenges (see [https://www.ee.columbia.edu/~marios/matlab/Matlab%20array%20manipulati...")
 
 
(4 intermediate revisions by the same user not shown)
Line 5: Line 5:
== Theory ==
== Theory ==


#D(poly) = 2*0 + 3*1 + 5*2 + 1*3
Let the polynomial be described in ascending order, thus <math>P(x) = 2 + 3x + 5x^2 + x^3</math> is written as a list <syntaxhighlight lang="Python" enclose="none">[2, 3, 5, 1]</syntaxhighlight >. The derivative <math>P'(x) = 3 + 10 x + 3x^2</math> equals a list <syntaxhighlight lang="Python" enclose="none"> [3, 10, 3]</syntaxhighlight>
#Dpoly = [3, 10, 3]
 


<syntaxhighlight lang="Python">
<syntaxhighlight lang="Python">
poly = [2, 3, 5, 1]  # Degrees starting from zero
poly = [2, 3, 5, 1]  # Degrees starting from zero
print( [(i+1)*j for i,j in enumerate( poly[1:] )] )
print( [(i+1)*j for i,j in enumerate( poly[1:] )] )
<syntaxhighlight>
</syntaxhighlight>

Latest revision as of 12:14, 5 August 2021

Introduction

Oneliners are powerful and often beautiful solutions to programming challenges (see Acklamization).

Theory

Let the polynomial be described in ascending order, thus is written as a list [2, 3, 5, 1]. The derivative equals a list [3, 10, 3]

poly = [2, 3, 5, 1]  # Degrees starting from zero
print( [(i+1)*j for i,j in enumerate( poly[1:] )] )