15
\$\begingroup\$

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.

This is , so shortest code in bytes wins.

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
Alex A.
24.8k5 gold badges39 silver badges120 bronze badges
asked Nov 10, 2016 at 22:37
\$\endgroup\$
5
  • 8
    \$\begingroup\$ 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)+... \$\endgroup\$ Commented Nov 10, 2016 at 22:43
  • \$\begingroup\$ I'm not familiar with the set W. Is that the natural numbers union 0? \$\endgroup\$ Commented Nov 10, 2016 at 23:16
  • \$\begingroup\$ @AlexA., yes, it is. \$\endgroup\$ Commented Nov 10, 2016 at 23:17
  • 1
    \$\begingroup\$ Borderline dupe \$\endgroup\$ Commented Nov 10, 2016 at 23:27
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ Commented Nov 10, 2016 at 23:32

24 Answers 24

23
\$\begingroup\$

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

answered Nov 10, 2016 at 23:38
\$\endgroup\$
2
  • 3
    \$\begingroup\$ You win by 0 bytes now :-P \$\endgroup\$ Commented Nov 11, 2016 at 1:38
  • \$\begingroup\$ @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. ;) \$\endgroup\$ Commented Nov 12, 2016 at 10:57
8
\$\begingroup\$

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
answered Nov 10, 2016 at 23:14
\$\endgroup\$
7
\$\begingroup\$

Julia, (削除) 45 (削除ここまで) (削除) 42 (削除ここまで) (削除) 40 (削除ここまで) 37 bytes

f(p,x)=sum(i->prod(i)x^abs(i[2]-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.

answered Nov 10, 2016 at 23:06
\$\endgroup\$
3
  • 3
    \$\begingroup\$ I feared that @AlexA. and Julia broke up, but here they are again, together in harmony <3 \$\endgroup\$ Commented Nov 10, 2016 at 23:17
  • \$\begingroup\$ You can save an extra three bytes if, instead of using i[2]>0&& to deal with the constant case, you use abs(i[2]-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. \$\endgroup\$ Commented Nov 12, 2016 at 4:49
  • \$\begingroup\$ @GlenO Nice, thanks for the suggestions. I went with the abs part, but redefining infix operators physically pains me... \$\endgroup\$ Commented Nov 12, 2016 at 23:19
5
\$\begingroup\$

05AB1E, (削除) 12 (削除ここまで) 11 bytes

Saved one byte thanks to Adnan.

vy¤<2smsP*O
v For each [coefficient, power] in the input array
 y Push [coefficient, power]
 ¤< Compute (power-1)
 2 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.

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

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.

Try it here.

answered Nov 10, 2016 at 23:43
\$\endgroup\$
2
  • \$\begingroup\$ Why can you sum a*x**(b-1) instead of a*b*x**(b-1) ? And further, what if $x=0$ ? \$\endgroup\$ Commented Nov 11, 2016 at 10:59
  • \$\begingroup\$ @1Darco1 You are right on both. I'll change it in a little while \$\endgroup\$ Commented Nov 11, 2016 at 11:04
3
\$\begingroup\$

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.

answered Nov 10, 2016 at 23:31
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nice! I added another answer with the same byte count. It uses a completely different approach though. Isn't R amazing? \$\endgroup\$ Commented Nov 11, 2016 at 8:23
  • 1
    \$\begingroup\$ Edit: No longer the same byte count :) \$\endgroup\$ Commented Nov 11, 2016 at 8:44
2
\$\begingroup\$

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)
answered Nov 10, 2016 at 23:09
\$\endgroup\$
2
\$\begingroup\$

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. x3-5 would be represented by [-5, 0, 0, 1].

answered Nov 11, 2016 at 1:00
\$\endgroup\$
2
\$\begingroup\$

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
answered Nov 10, 2016 at 23:06
\$\endgroup\$
5
  • \$\begingroup\$ You could use @(x,p)polyval(polyder(p),x) in order to gain a byte. \$\endgroup\$ Commented Nov 10, 2016 at 23:09
  • \$\begingroup\$ @flawr Well, he shouldn't now because you just posted that as an answer ;P \$\endgroup\$ Commented Nov 10, 2016 at 23:10
  • \$\begingroup\$ @flawr Thanks, but that's too different, you should post it! \$\endgroup\$ Commented Nov 10, 2016 at 23:11
  • 1
    \$\begingroup\$ Well I think you wouldn't have done it anyway, 'cause you'd gain a byte =D \$\endgroup\$ Commented Nov 10, 2016 at 23:12
  • \$\begingroup\$ @flawr Aww. I completely misunderstood, haha \$\endgroup\$ Commented Nov 10, 2016 at 23:14
2
\$\begingroup\$

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] 1.613438e+13
[1] 0
[1] 5
[1] -735.12
[1] 87.48
answered Nov 11, 2016 at 8:23
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for showing me this! I hadn't considered the possibility of having the input as an expression - this is a really elegant solution. \$\endgroup\$ Commented Nov 12, 2016 at 17:45
2
\$\begingroup\$

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.

answered Nov 11, 2016 at 16:02
\$\endgroup\$
2
  • \$\begingroup\$ How the heck does that work? \$\endgroup\$ Commented Nov 12, 2016 at 1:42
  • \$\begingroup\$ @cat It calculates the derivative f' of f, and then substitutes n for x. \$\endgroup\$ Commented Nov 12, 2016 at 13:57
2
\$\begingroup\$

C++14, (削除) 165 (削除ここまで) (削除) 138 (削除ここまで) (削除) 133 (削除ここまで) (削除) 112 (削除ここまで) 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;
}
answered Nov 11, 2016 at 9:51
\$\endgroup\$
1
  • \$\begingroup\$ You seem to have crossed out all your byte counts. What's the actual byte count, then? \$\endgroup\$ Commented May 15, 2017 at 8:13
1
\$\begingroup\$

Haskell, 33 bytes

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

Usage:

> f 5.4 [[1, 3], [-5, 0]]
87.48000000000002
answered Nov 11, 2016 at 8:14
\$\endgroup\$
1
\$\begingroup\$

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
answered Nov 12, 2016 at 15:34
\$\endgroup\$
0
\$\begingroup\$

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
answered Nov 11, 2016 at 2:45
\$\endgroup\$
0
\$\begingroup\$

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
answered Nov 11, 2016 at 7:40
\$\endgroup\$
0
\$\begingroup\$

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
answered Nov 28, 2016 at 23:42
\$\endgroup\$
0
\$\begingroup\$

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.

answered May 21, 2017 at 4:14
\$\endgroup\$
0
\$\begingroup\$

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!

answered May 22, 2017 at 4:21
\$\endgroup\$
0
\$\begingroup\$

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);}
answered Jun 11, 2017 at 8:02
\$\endgroup\$
0
\$\begingroup\$

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.

answered Jun 11, 2017 at 21:00
\$\endgroup\$
0
\$\begingroup\$

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.

answered May 15, 2017 at 8:38
\$\endgroup\$
0
\$\begingroup\$

Dyalog APL, (削除) 26 (削除ここまで) (削除) 25 (削除ここまで) 23 bytes

{a×ばつa*2⌷⍵-1} ̈⍵}

Takes polynomial as right argument and value as left argument.

answered Nov 11, 2016 at 2:02
\$\endgroup\$
0
\$\begingroup\$

APL(NARS), 6 chars

{⍵⊥⍺}∂

For the input of polynom the list

$$ a_n a_{n-1} ... a_1 a_0 $$

means polynom

$$ a_n X^n+a_{n-1} X^{n-1} ... a_1 X+a_0 $$

 f←{⍵⊥⍺}∂
 5 4 f ̄13
5
 5 0 0 0 0 0 ̄2 4 0 0 0 f 19
1.613438484E13
 0 0 0 0 0 f 400
0
 0 f 400
0
 4.14 0 48 0 0 f ̄3
 ̄735.12
 1 0 0 ̄5 f 5.4
87.48
answered Jan 11, 2024 at 8:53
\$\endgroup\$
1
  • \$\begingroup\$ for say the true i dont know why the function polynom is build before derivate \$\endgroup\$ Commented Jan 11, 2024 at 9:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.