# Find the rate of change at a point on a polynomial

Given the equation of a polynomial and an x-coordinate find the rate of change of the point at that x-coord on the curve.

A polynomial is in the form: axn + axn-1 + ... + ax1 + a, where a ϵ Q and n ϵ W. For this challenge, n can also be 0 if you don't want to have to deal with special cases (constants) where there is no x.

To find the rate of change at that x-coord, we can get the derivative of the polynomial and plug in the x-coord.

### Input

The polynomial can be taken in any reasonable form, but you must state what that format is explicitly. For example, an array of the form [..[coefficient, exponent]..] is acceptable.

### Output

The rate of change of the point at the x-coord given.

### Examples

[[4, 3], [-2, 4], [5, 10]]   19    ->   16134384838410
[[0, 4]]  400    ->   0
[[4, 0], [5,1]]  -13    ->   5
[[4.14, 4], [48, 2]]   -3    ->   -735.12
[[1, 3], [-5, 0]]    5.4  ->   87.48

• Algorithm for anyone who doesn't have the appropriate math background: The derivative of Ax^B+Cx^D+... is (AB)*x^(B-1)+(CD)*x^(D-1)+... – Sparr Nov 10 '16 at 22:43
• I'm not familiar with the set W. Is that the natural numbers union 0? – Alex A. Nov 10 '16 at 23:16
• @AlexA., yes, it is. – Daniel Nov 10 '16 at 23:17
• Borderline dupe – Peter Taylor Nov 10 '16 at 23:27
• @PeterTaylor I think they share a similar idea but I don't think any answer from there could be posted here without very, very significant modification. – Alex A. Nov 10 '16 at 23:32

# Mathematica, 6 bytes

#'@#2&


(Beat THAT, MATL and 05AB1E)

The first argument must be a polynomial, with # as its variable and with & at the end (i.e. a pure function polynomial; e.g. 3 #^2 + # - 7 &). The second argument is the x-coordinate of the point of interest.

Explanation

#'


Take the derivative of the first argument (1 is implied).

... @#2&


Plug in the second argument.

Usage

#'@#2&[4 #^3 - 2 #^4 + 5 #^10 &, 19] (* The first test case *)


16134384838410

• You win by 0 bytes now :-P – Luis Mendo Nov 11 '16 at 1:38
• @LuisMendo When a guy with a chef's knife can tie with a mandoline in a slicing competition, I'll give the point to the guy using the knife. ;) – J... Nov 12 '16 at 10:57

# MATL, 8 6 bytes

yq^**s


Input is: array of exponents, number, array of coefficients.

Try it online! Or verify all test cases: 1, 2 3, 4, 5.

### Explanation

Consider example inputs [3 4 10], 19, [4 -2 5].

y    % Take first two inputs implicitly and duplicate the first
%   STACK: [3 4 10], 19, [3 4 10]
q    % Subtract 1, element-wise
%   STACK: [3 4 10], 19, [2 3 9]
^    % Power, element-wise
%   STACK: [3 4 10], [361 6859 322687697779]
*    % Multiply, element-wise
%   STACK: [1083 27436 3226876977790]
*    % Take third input implicitly and multiply element-wise
%   STACK: [4332 -54872 16134384888950]
s    % Sum of array
%   STACK: 16134384838410


# Julia, 454240 37 bytes

f(p,x)=sum(i->prod(i)x^abs(i-1),p)


This is a function that acceps a vector of tuples and a number and returns a number. The absolute value is to ensure that the exponent isn't negative, which necessary because Julia annoying throws a DomainError when raising an integer to a negative exponent.

Try it online! (includes all test cases)

Thanks to Glen O for a couple of corrections and bytes.

• I feared that @AlexA. and Julia broke up, but here they are again, together in harmony <3 – flawr Nov 10 '16 at 23:17
• You can save an extra three bytes if, instead of using i>0&& to deal with the constant case, you use abs(i-1) in the exponent of x. And a slightly less clean trick to save another three bytes is to use p%x instead of f(p,x) - note that you can call it as %(p,x) if you want to use it in function form... unfortunately, it seems it doesn't work on TIO (which apparently is running Julia 0.4.6), although it works on my Julia 0.5.0. – Glen O Nov 12 '16 at 4:49
• @GlenO Nice, thanks for the suggestions. I went with the abs part, but redefining infix operators physically pains me... – Alex A. Nov 12 '16 at 23:19

# 05AB1E, 12 11 bytes

Saved one byte thanks to Adnan.

vy¤<²smsP*O

v          For each [coefficient, power] in the input array
y         Push [coefficient, power]
¤<       Compute (power-1)
²       Push x value (second input entry)
sms    Push pow(x, power-1)
P   Push coefficient * power ( = coefficient of derivative)
*  Push coefficient * power * pow(x, power-1)
O Sum everything and implicitly display the result


Try it online!

Floating point precision is Python's. I currently swap stack values twice, maybe there is a way to avoid it and save some bytes.

• I believe you can leave out the } :). – Adnan Nov 10 '16 at 23:21
• DIs<m**O is 8 bytes, following the MATL answer that @Luis Mendo provided. – Magic Octopus Urn Nov 12 '16 at 16:19
• Even better, s¹<m**O is 7 bytes. (05ab1e.tryitonline.net/…) – Magic Octopus Urn Nov 12 '16 at 16:21
• It changes substantially the input format while I kept the original one. But I agree that manipulating the input format enables shorter answers. – Osable Nov 12 '16 at 18:18
• @Osable true, but others have used that loophole ;) – Magic Octopus Urn Nov 14 '16 at 18:00

# Python 3, 41 bytes

6 bytes removed thanks to @AndrasDeak! In fact, this answer is now more his than mine...

Thanks also to @1Darco1 for two corrections!

lambda A,x:sum(a*b*x**(b-1) for a,b in A)


Anonymous function that accepts a list of lists with coefficients and exponents (same format as described in the challenge) and a number.

• Why can you sum a*x**(b-1) instead of a*b*x**(b-1) ? And further, what if $x=0$ ? – 1Darco1 Nov 11 '16 at 10:59
• @1Darco1 You are right on both. I'll change it in a little while – Luis Mendo Nov 11 '16 at 11:04

# R, 31 bytes

function(a,n,x)sum(a*n*x^(n-1))


Anonymous function that takes a vector of coefficients a, a vector of exponents n, and an x value.

• Nice! I added another answer with the same byte count. It uses a completely different approach though. Isn't R amazing? – Billywob Nov 11 '16 at 8:23
• Edit: No longer the same byte count :) – Billywob Nov 11 '16 at 8:44

# Matlab, 27 bytes

This is an anonymous function that accepts a value x and a polyonmial p in the form of a list of coefficients, e.g. x^2 + 2 can be represented as [1,0,2].

@(x,p)polyval(polyder(p),x)


## JavaScript (ES7), 40 bytes

(a,n)=>a.reduce((t,c,i)=>t+i*c*n**--i,0)


a is an array of the coefficients in ascending exponent order with zeros included e.g. x³-5 would be represented by [-5, 0, 0, 1].

# MATLAB with Symbolic Math Toolbox, 26 bytes

@(p,x)subs(diff(sym(p)),x)


This defines an anonymous function. Inputs are:

• a string p defining the polynomial, in the format '4*x^3-2*x^4+5*x^10'
• a number x

Example use:

>> f = @(p,x)subs(diff(sym(p)),x)
f =
@(p,x)subs(diff(sym(p)),x)

>> f('4*x^3-2*x^4+5*x^10', 19)
ans =
16134384838410

• You could use @(x,p)polyval(polyder(p),x) in order to gain a byte. – flawr Nov 10 '16 at 23:09
• @flawr Well, he shouldn't now because you just posted that as an answer ;P – Alex A. Nov 10 '16 at 23:10
• @flawr Thanks, but that's too different, you should post it! – Luis Mendo Nov 10 '16 at 23:11
• Well I think you wouldn't have done it anyway, 'cause you'd gain a byte =D – flawr Nov 10 '16 at 23:12
• @flawr Aww. I completely misunderstood, haha – Luis Mendo Nov 10 '16 at 23:14

## R, 31 27 bytes

Unnamed function taking two inputs p and x. p is assumed to be an R-expression of the polynomial (see example below) and x is simply the point of evaluation.

function(p,x)eval(D(p,"x"))


It works by calling the D which computes the symbolic derivative w.r.t. x and the evaluates the expression at x.

Example output

Assuming that the function is now named f it can be called in the following way:

f(expression(4*x^3-2*x^4+5*x^10),19)
f(expression(0*x^4),400)
f(expression(4*x^0+5*x^1),-13)
f(expression(4.14*x^4+48*x^2),-3)
f(expression(1*x^3-5*x^0),5.4)


which respectively produces:

 1.613438e+13
 0
 5
 -735.12
 87.48

• Thanks for showing me this! I hadn't considered the possibility of having the input as an expression - this is a really elegant solution. – rturnbull Nov 12 '16 at 17:45

# PARI/GP, 20 bytes

a(f,n)=subst(f',x,n)


For example, a(4*x^3-2*x^4+5*x^10,19) yields 16134384838410.

• How the heck does that work? – cat Nov 12 '16 at 1:42
• @cat It calculates the derivative f' of f, and then substitutes n for x. – Paŭlo Ebermann Nov 12 '16 at 13:57

# C++14, 165138133112 110 bytes

Generic Variadic Lambda saves a lot. -2 bytes for #import and deleting the space before <

#import<cmath>
#define A auto
A f(A x){return 0;}A f(A x,A a,A b,A...p){return a*b*std::pow(x,b-1)+f(x,p...);}


Ungolfed:

#include <cmath>

auto f(auto x){return 0;}

auto f(auto x,auto a,auto b,auto...p){
return a*b*std::pow(x,b-1)+f(x,p...);
}


Usage:

int main() {
std::cout << f(19,4,3,-2,4,5,10) << std::endl;
std::cout << f(400,0,4) << std::endl;
std::cout << f(-13,4,0,5,1) << std::endl;
std::cout << f(-3,4.14,4,48,2) << std::endl;
std::cout << f(5.4,1,3,-5,0) << std::endl;
}

• You seem to have crossed out all your byte counts. What's the actual byte count, then? – numbermaniac May 15 '17 at 8:13
• @numbermaniac thank you, done. – Karl Napf May 22 '17 at 4:07

# Haskell, 33 bytes

f x=sum.map(\[c,e]->c*e*x**(e-1))


Usage:

> f 5.4 [[1, 3], [-5, 0]]
87.48000000000002


## dc, 31 bytes

??sx0[snd1-lxr^**ln+z2<r]srlrxp


Usage:

\$ dc -e "??sx0[snd1-lxr^**ln+z2<r]srlrxp"
4.14 4 48 2
_3
-735.12


# DASH, 33 bytes

@@sum(->@* ^#1- :1#0 1(sS *)#0)#1


Usage:

(
(
@@sum(->@* ^#1- :1#0 1(sS *)#0)#1
) [[4;3];[_2;4];[5;10]]
) 19


# Explanation

@@                             #. Curried 2-arg lambda
#. 1st arg -> X, 2nd arg -> Y
sum                          #. Sum the following list:
(map @                     #. Map over X
#. item list -> [A;B]
* ^ #1 - :1#0 1(sS *)#0  #. This mess is just A*B*Y^(B-1)
)#1                        #. X


# Scala, 46 bytes

s=>i=>s map{case(c,e)=>c*e*math.pow(i,e-1)}sum


Usage:

val f:(Seq[(Double,Double)]=>Double=>Double)=
s=>i=>s map{case(c,e)=>c*e*math.pow(i,e-1)}sum
print(f(Seq(4.0 → 3, -2.0 → 4, 5.0 → 10))(19))


Explanation:

s=>                        //define an anonymous function with a parameter s returning
i=>                        //an anonymous function taking a paramater i and returning
s map{                   //map each element of s:
case(c,e)=>              //unpack the tuple and call the values c and e
c*e*math.pow(i,e-1)    //calculate the value of the first derivate
}sum                      //take the sum


# Axiom 31 bytes

h(q,y)==eval(D(q,x),x,y)::Float


results

 -> h(4*x^3-2*x^4+5*x^10, 19)
161343 84838410.0

-> h(4.14*x^4+48*x^2, -3)
- 735.12


## Python 2, 39 bytes

lambda p,x:sum(c*e*x**~-e for c,e in p)


lambda function takes two inputs, p and x. p is the polynomial, given in the example format given in the question. x is the x-value at which to find the rate of change.

# Pari/GP, 14 bytes

p->x->eval(p')


### Usage:

? (p->x->eval(p'))(4*x^3 - 2*x^4 + 5*x^10)(19)
%1 = 16134384838410


Try it online!

# C, 78 bytes

f(int*Q,int*W,int S,int x){return Q[--S]*W[S]*pow(x,W[S]-1)+(S?f(Q,W,S,x):0);}


## Clojure, 53 bytes

#(apply +(for[[c e]%](apply * c e(repeat(dec e)%2))))


The polynomial is expressed as a hash-map, keys being coefficients and values are exponents.

# Casio Basic, 16 bytes

diff(a,x)|x=b


Input should be the polynomial in terms of x. 13 bytes for the code, +3 bytes to enter a,b as parameters.

Simply derives expression a in respect to x, then subs in x=b.

# Dyalog APL, 2625 23 bytes

{a←⍺⋄+/{×/⍵×a*2⌷⍵-1}¨⍵}


Takes polynomial as right argument and value as left argument.