| Karim Belabas on Sun, 17 Aug 2014 09:20:56 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: Vec() and leading zeros of generating function |
* Kevin Ryde [2014-08-16 07:47]:
> Vec() can give terms from a polynomial generating function but it
> doesn't include leading zeros. Eg.
>
> Vec(x^2/(1-2*x) + O(x^5))
> =>
> [1, 2, 4]
>
> Is there a good way to start from the zero'th term so I would get
>
> [0, 0, 1, 2, 4]
>
> I have to confess to only doing this by cut-and-paste programming, so
> perhaps there's something completely different I don't know which is
> better. :-) My main use has been to compare against expected or desired
> values, so
>
> want = [1, -1, 1, -1]
> Vec(1/(1+x) + O(x^length(want))) == want || error("oops")
You can use Vec's optional "length" parameter (making it negative
prepends 0s instead of appending them):
want = [0,0,1,2,4];
s = x^2/(1-2*x) + O(x^5);
? Vec(s,5)
%3 = [1, 2, 4, 0, 0] \\ fix length = 5, append 0s
? Vec(s,-5)
%4 = [0, 0, 1, 2, 4] \\ prepend
? Vec(s, -#want) == want \\ note the '-' #want
%5 = 1
The is often marginally easier than concatenating 0s yourself, as others
already suggested. (As Loic's suggestion, this requires care if the series
has negative valuation: set the reference value accordingly...)
It might be simpler to just convert the desired value to generating function
form first (=> t_SER) instead
? want = Ser(want)
%5 = x^2 + 2*x^3 + 4*x^4 + O(x^5)
? s == want
%6 = 1
(use === instead of == if you insist on having the same accuracy, i.e.
"identical" series instead of merely "equal to given accyracy" ones)
Cheers,
K.B.
--
Karim Belabas, IMB (UMR 5251) Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation http://www.math.u-bordeaux1.fr/~kbelabas/
F-33405 Talence (France) http://pari.math.u-bordeaux1.fr/ [PARI/GP]
`