Find the rate of change at a point on a polynomial
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
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.