Re: Re: Possible Bug Report: CoefficientList
- To: mathgroup at smc.vnet.net
- Subject: [mg88868] Re: [mg88861] Re: [mg88844] Possible Bug Report: CoefficientList
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 20 May 2008 02:23:15 -0400 (EDT)
- References: <200805190915.FAA12556@smc.vnet.net> <200805191045.GAA14919@smc.vnet.net>
Craig Carter correctly pointed out that I commited the capital crime not reading the code I was commenting on and giving some thought to what it is intended to do. Of course the problem is enitrely different form what I had mindlessly assumed and arises from using x into two different meanings (without localizing). In fact it is enough to simply use different names for different variables: hances[dice_, sides_] := CoefficientList[ Expand[Sum[x^i, {i, sides}]^dice], x]; Table[Chances[p, 6], {p, 3, 5}] {{0, 0, 0, 1, 3, 6, 10, 15, 21, 25, 27, 27, 25, 21, 15, 10, 6, 3, 1}, {0, 0, 0, 0, 1, 4, 10, 20, 35, 56, 80, 104, 125, 140, 146, 140, 125, 104, 80, 56, 35, 20, 10, 4, 1}, {0, 0, 0, 0, 0, 1, 5, 15, 35, 70, 126, 205, 305, 420, 540, 651, 735, 780, 780, 735, 651, 540, 420, 305, 205, 126, 70, 35, 15, 5, 1}} although localizing x is naturally better. Andrzej Kozlowski On 19 May 2008, at 19:45, Andrzej Kozlowski wrote: > > On 19 May 2008, at 18:15, anglewyrm at yahoo.com wrote: > >> Here is a function using CoefficientList[] that produces a list: >> Chances[dice_, sides_] := CoefficientList[Expand[(Sum[x^i, {i, >> sides}])^dice], x]; >> >> (* This works: *) >> Chances[3, 6] >> Chances[4,6] >> Chances[5,6] >> >> (* This fails: *) >> Table[ Chances[x, 6], {x, 3, 5}] >> >> CoefficientList::ivar: 1 is not a valid variable. >> >> CoefficientList::ivar: 2 is not a valid variable. >> >> CoefficientList::ivar: 3 is not a valid variable. >> >> General::stop: Further output of CoefficientList::ivar will be >> suppressed during this calculation. >> >> > > This is not a bug but a question of understanding the way Mathematica > evaluates certain expressions; in particular case Table. > > > Chances[dice_, sides_] := CoefficientList[Expand[Sum[x^i, {i, > sides}]^dice], x]; > > Table[Evaluate[Chances[x, 6]], {x, 3, 5}] > > {{1302170688}, {888731494560000}, {2841261564746499300000}} > > Note the need for Evaluate. > > Andrzej Kozlowski > > PS. Did it not seem to you rather remarkable that a bug as basic as > this would survive till version 6 of Mathematica? > > > >
- References:
- Possible Bug Report: CoefficientList
- From: anglewyrm@yahoo.com
- Re: Possible Bug Report: CoefficientList
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Possible Bug Report: CoefficientList