12
1
If we write a sequence of numbers as the coefficients of a power series, then that power series is called the (ordinary) generating function (or G.f.) of that sequence. That is, if for some function F(x)
and series of integers a(n)
we have:
a(0) + a(1)x + a(2)x^2 + a(3)x^3 + a(4)x^4 + ... = F(x)
Then F(x)
is the generating function of a
. For example, the geometric series tells us that:
1 + x + x^2 + x^3 + x^4 + ... = 1/(1-x)
So the generating function of 1, 1, 1, ...
is 1/(1-x)
. If we differentiate both sides of the equation above and multiply by x
we get the following equality:
x + 2x^2 + 3x^3 + 4x^4 + ... = x/(1-x)^2
So the generating function of 1, 2, 3, ...
is x/(1-x)^2
. Generating functions are a very powerful tool, and you can do many useful things with them. A short introduction can be found here, but for a really thorough explanation there is the amazing book generatingfunctionology.
In this challenge you will take a rational function (the quotient of two polynomials with integer coefficients) as input as two arrays of integer coefficients, first the numerator then the denominator. For example the function f(x) = x / (1 - x - x^2)
will be encoded as [0, 1], [1, -1, -1]
in the input.
Given this input your program must infinitely print the coefficients of the power series that equals the generating function, one per line, starting at the coefficient of x
, then x^2
, etc.
Examples:
[1], [1, -1] -> 1, 1, 1, 1, 1, 1, 1, ...
[1], [2, -2] -> 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, ...
[0, 1], [1, -2, 1] -> 1, 2, 3, 4, 5, 6, 7, 8, ...
[0, 1], [1, -1, -1] -> 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
[1], [1, -2] -> 1, 2, 4, 8, 16, 32, 64, 128, ...
[0, 1, 1], [1, -3, 3, -1] -> 1, 4, 9, 16, 25, 36, ...
Crap, my language is built for sequences this, but I can't really do multidimensional array input :( – Stephen – 2017-07-07T14:02:54.100
@StepHen What input would work for your language? – orlp – 2017-07-07T14:09:01.710
It's not a flaw in the challenge, it's a flaw in the language. I don't have the ability to process input of variable length yet :P – Stephen – 2017-07-07T14:10:58.783
2I'm really just not mathematically-minded enough for this spec, any chance you could post more of a layman's explanation for us common folk? – Skidsdev – 2017-07-07T14:15:21.737
@Mayube I added another general example at the top of my challenge. If it's still unclear I can suggest reading the linked introduction. Generating functions are a simple concept, but may seem magic to those unfamiliar. – orlp – 2017-07-07T14:32:20.823
Is it acceptable to use a descending degree order of coefficients? e.g.
x^3+2x^2+1
yields[1, 2, 0 ,1]
– Keyu Gan – 2017-07-07T18:03:48.980@KeyuGan That is fine, as long as you clearly state it in your answer. – orlp – 2017-07-07T18:45:02.973
2
Possible duplicate of Calculate Power Series Coefficients
– trichoplax – 2017-07-07T23:39:32.9031@trichoplax That always forces the numerator to be 1, which is not the same. For example it cannot express my last example, the squares. – orlp – 2017-07-08T05:53:34.973
1
An alternative way of phrasing this is that it evaluates a general linear recurence. In that way it generalises this question, and might serve as a dupe target for future recurrence questions.
– Peter Taylor – 2017-07-08T08:58:57.983@orlp thanks for explaining the difference - makes sense now. – trichoplax – 2017-07-08T11:37:00.447
Is it 1-indexed or 0 indexed? If it is 1-indexed, then the output for
[1], [1, -2]
should be2, 4, 8, 16, 32, 64, 128, ...
. If it is 0-indexed, then the output for[0, 1], [1, -2, 1]
should be0, 1, 2, 3, 4, 5, 6, 7, 8, ...
. – alephalpha – 2017-07-09T03:47:45.837