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 code-golf, 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
-
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\$Sparr– Sparr2016年11月10日 22:43:40 +00:00Commented Nov 10, 2016 at 22:43
-
\$\begingroup\$ I'm not familiar with the set W. Is that the natural numbers union 0? \$\endgroup\$Alex A.– Alex A.2016年11月10日 23:16:43 +00:00Commented Nov 10, 2016 at 23:16
-
\$\begingroup\$ @AlexA., yes, it is. \$\endgroup\$Daniel– Daniel2016年11月10日 23:17:27 +00:00Commented Nov 10, 2016 at 23:17
-
1\$\begingroup\$ Borderline dupe \$\endgroup\$Peter Taylor– Peter Taylor2016年11月10日 23:27:50 +00:00Commented 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\$Alex A.– Alex A.2016年11月10日 23:32:44 +00:00Commented Nov 10, 2016 at 23:32
24 Answers 24
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
-
3\$\begingroup\$ You win by 0 bytes now :-P \$\endgroup\$Luis Mendo– Luis Mendo2016年11月11日 01:38:39 +00:00Commented 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\$J...– J...2016年11月12日 10:57:03 +00:00Commented Nov 12, 2016 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, (削除) 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.
-
3\$\begingroup\$ I feared that @AlexA. and Julia broke up, but here they are again, together in harmony <3 \$\endgroup\$flawr– flawr2016年11月10日 23:17:19 +00:00Commented 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 useabs(i[2]-1)in the exponent ofx. And a slightly less clean trick to save another three bytes is to usep%xinstead off(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\$Glen O– Glen O2016年11月12日 04:49:06 +00:00Commented Nov 12, 2016 at 4:49 -
\$\begingroup\$ @GlenO Nice, thanks for the suggestions. I went with the
abspart, but redefining infix operators physically pains me... \$\endgroup\$Alex A.– Alex A.2016年11月12日 23:19:21 +00:00Commented Nov 12, 2016 at 23:19
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
Floating point precision is Python's. I currently swap stack values twice, maybe there is a way to avoid it and save some bytes.
-
1\$\begingroup\$ I believe you can leave out the
}:). \$\endgroup\$Adnan– Adnan2016年11月10日 23:21:32 +00:00Commented Nov 10, 2016 at 23:21 -
\$\begingroup\$
DIs<m**Ois 8 bytes, following the MATL answer that @Luis Mendo provided. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2016年11月12日 16:19:46 +00:00Commented Nov 12, 2016 at 16:19 -
\$\begingroup\$ Even better,
s¹<m**Ois 7 bytes. (05ab1e.tryitonline.net/…) \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2016年11月12日 16:21:09 +00:00Commented 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\$Osable– Osable2016年11月12日 18:18:44 +00:00Commented Nov 12, 2016 at 18:18
-
\$\begingroup\$ @Osable true, but others have used that loophole ;) \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2016年11月14日 18:00:45 +00:00Commented Nov 14, 2016 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.
-
\$\begingroup\$ Why can you sum
a*x**(b-1)instead ofa*b*x**(b-1)? And further, what if $x=0$ ? \$\endgroup\$Marco– Marco2016年11月11日 10:59:55 +00:00Commented Nov 11, 2016 at 10:59 -
\$\begingroup\$ @1Darco1 You are right on both. I'll change it in a little while \$\endgroup\$Luis Mendo– Luis Mendo2016年11月11日 11:04:56 +00:00Commented Nov 11, 2016 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.
-
1\$\begingroup\$ Nice! I added another answer with the same byte count. It uses a completely different approach though. Isn't R amazing? \$\endgroup\$Billywob– Billywob2016年11月11日 08:23:46 +00:00Commented Nov 11, 2016 at 8:23
-
1\$\begingroup\$ Edit: No longer the same byte count :) \$\endgroup\$Billywob– Billywob2016年11月11日 08:44:18 +00:00Commented Nov 11, 2016 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. x3-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
pdefining 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
-
\$\begingroup\$ You could use
@(x,p)polyval(polyder(p),x)in order to gain a byte. \$\endgroup\$flawr– flawr2016年11月10日 23:09:46 +00:00Commented Nov 10, 2016 at 23:09 -
\$\begingroup\$ @flawr Well, he shouldn't now because you just posted that as an answer ;P \$\endgroup\$Alex A.– Alex A.2016年11月10日 23:10:58 +00:00Commented Nov 10, 2016 at 23:10
-
\$\begingroup\$ @flawr Thanks, but that's too different, you should post it! \$\endgroup\$Luis Mendo– Luis Mendo2016年11月10日 23:11:01 +00:00Commented 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\$flawr– flawr2016年11月10日 23:12:24 +00:00Commented Nov 10, 2016 at 23:12
-
\$\begingroup\$ @flawr Aww. I completely misunderstood, haha \$\endgroup\$Luis Mendo– Luis Mendo2016年11月10日 23:14:25 +00:00Commented Nov 10, 2016 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] 1.613438e+13
[1] 0
[1] 5
[1] -735.12
[1] 87.48
-
\$\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\$rturnbull– rturnbull2016年11月12日 17:45:42 +00:00Commented Nov 12, 2016 at 17:45
-
\$\begingroup\$ How the heck does that work? \$\endgroup\$cat– cat2016年11月12日 01:42:04 +00:00Commented Nov 12, 2016 at 1:42
-
\$\begingroup\$ @cat It calculates the derivative
f'off, and then substitutesnforx. \$\endgroup\$Paŭlo Ebermann– Paŭlo Ebermann2016年11月12日 13:57:45 +00:00Commented Nov 12, 2016 at 13:57
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;
}
-
\$\begingroup\$ You seem to have crossed out all your byte counts. What's the actual byte count, then? \$\endgroup\$numbermaniac– numbermaniac2017年05月15日 08:13:47 +00:00Commented May 15, 2017 at 8:13
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
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, (削除) 26 (削除ここまで) (削除) 25 (削除ここまで) 23 bytes
{a×ばつa*2⌷⍵-1} ̈⍵}
Takes polynomial as right argument and value as left argument.
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
-
\$\begingroup\$ for say the true i dont know why the function polynom is build before derivate \$\endgroup\$Rosario– Rosario2024年01月11日 09:13:57 +00:00Commented Jan 11, 2024 at 9:13