Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Possible Bug Report: CoefficientList


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?
>
>
>
>



  • Prev by Date: Re: Mathematica 6: No more memory available problem with
  • Next by Date: Re: Re: Problem with cell brackets and Manipulate
  • Previous by thread: Re: Possible Bug Report: CoefficientList
  • Next by thread: Re: Possible Bug Report: CoefficientList