Re: sparsearray bug?
- To: mathgroup at smc.vnet.net
- Subject: [mg101497] Re: [mg101477] sparsearray bug?
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 9 Jul 2009 01:52:27 -0400 (EDT)
- References: <200907081111.HAA12330@smc.vnet.net>
Hi,
This seems indeed to be a bug. Here is what I think is the reason.
In[1] = SparseArray[{1}] // FullForm
Out[1] =
SparseArray[Automatic,List[1],0,List[1,List[List[0,1],List[List[1]]],List[1]]]
Look at the third element (zero). This is a default element, which is filled
for any position missed in explicit rules (when SparseArray is defined from
a set of rules position->value).
Now, in that latter case (when at least one default element is present),
it makes sense to apply whatever Listable operation is applied to an entire
array (Power[x,-1] in this case) also to the default element - but this
should
not be done in cases when all elements are indicated explicitly (and so the
default element substitution does not actually take place). Let us change a
default element to 1 in the FullForm:
In[2] =
SparseArray[Automatic,List[1],1,List[1,List[List[0,1],List[List[1]]],List[1]]]
Out[2] = SparseArray[<1>,{1},1]
In this case, no error message:
In[3] =
1/SparseArray[Automatic,List[1],1,List[1,List[List[0,1],List[List[1]]],List[1]]]
Out[3] = SparseArray[<1>,{1},1]
In[4] = Normal[%]
Out[4] ={1}
Now, we can convince ourselves that the bug is indeed related to applying
an inverse (in this case, but this may be a more general problem) to the
default element:
In[5]:= Normal[1/SparseArray[{1->1,2->2,4->5}]]
During evaluation of In[5]:= Power::infy: Infinite expression 1/0
encountered. >>
Out[5]= {1,1/2,ComplexInfinity,1/5}
Here the behavior was correct, since the default is present. However, I
would bet that the actual place where this message was generated isn't
correct - I am almost certain that it was generated before default element
substitution actually took place (but this only WRI could confirm). Now:
In[6]:= Normal[1/SparseArray[{1->1,2->2,3->4,4->5}]]
During evaluation of In[6]:= Power::infy: Infinite expression 1/0
encountered. >>
Out[6]= {1,1/2,1/4,1/5}
Here it was incorrect (in terms of error message generation at least), since
the default wasn't ever used.
Now we change the default to 1:
In[7]:= Normal[1/SparseArray[{1->1,2->2,3->4,4->5,{_}->1}]]
Out[7]= {1,1/2,1/4,1/5}
and get no error message. Here also the default wasn't actually used.
This seems to support my conjecture.
Hope this helps.
Regards,
Leonid
On Wed, Jul 8, 2009 at 4:11 AM, wpb <w.p.bergsma at lse.ac.uk> wrote:
> In[494]:= SparseArray[{1}]/SparseArray[{1}]
>
> During evaluation of In[494]:= Power::infy: Infinite expression 1/0
> encountered. >>
> During evaluation of In[494]:= \[Infinity]::indet: Indeterminate expression
> 0 ComplexInfinity encountered. >>
>
> Out[494]= SparseArray[<1>,{1},Indeterminate]
>
>
- References:
- sparsearray bug?
- From: wpb <w.p.bergsma@lse.ac.uk>
- sparsearray bug?