Taylor series are a very useful tool in calculating values of analytic functions that cannot be expressed in terms of elementary functions, using only information about that function at a single point.
In this challenge, you won't be actually doing any math with them, but merely making string representations of taylor series of functions with periodic derivatives.
Specs
You will takes a input three things:
- A cycle of the derivatives of the function, including the original function as a list of strings, e.g.
["sin(x)", "cos(x)", "-sin(x)", "-cos(x)"]
. - A number
a
, the point around which to center the approximation. - A positive integer
n
, the number of terms of the series to output.
The general form for the nth term of a taylor series is:
f^(n)(a)/n!*(x-a)^n
where f^(n)
is the nth derivative of f
. Here is the taylor series of f
in a better formatted image from wikipedia:
.
The above example with sin(x)
, and a
of 1, and n
of 6, would have an output of:
sin(1) + cos(1)*(x-1) - sin(1)/2*(x-1)^2 - cos(1)/6*(x-1)^3 + sin(1)/24*(x-1)^4 + cos(1)/120*(x-1)^5
You can do the whitespace want. However, you have to replace + -
and - -
with -
and +
respectively. Also, if the series is a maclaurin series, and a
is 0
, you cannot have (x-0)
. (x)
and just x
is fine, but not (x-0)
. Niether can you have ^0
nor ^1
nor /1
. Additionally, you must expand out the factorials in the denominators. Multiplication by juxtaposition is not allowed.
Test cases
["e^x"], 0, 4 -> e^0 + e^0*x + e^0*x^2/2 + e^0*x^3/6
["sin(x)", "cos(x)", "-sin(x)", "-cos(x)"], 1, 6 -> sin(1) + cos(1)*(x-1) - sin(1)/2*(x-1)^2 - cos(1)/6*(x-1)^3 + sin(1)/24*(x-1)^4 + cos(1)/120*(x-1)^5
["e^(i*x)", "i * e^(i*x)", "-e^(i*x)", "-i*e^(i*x)"], 0, 7 -> e^(i*0) + i*e^(i*0)*x - e^(i*0)*x^2/2 - i*e^(i*0)*x^3/6 + e^(i*0)*x^4/24 + i*e^(i*0)*x^5/120 - e^(i*0)*x^6/720
This is code-golf, so shortest code in bytes wins!
**
be used in place of^
? \$\endgroup\$