#5185: [with patch, needs review] is_zero is broken for sparse vectors
------------------------+---------------------------------------------------
Reporter: jhpalmieri | Owner: tbd
Type: defect | Status: new
Priority: major | Milestone: sage-3.4.1
Component: algebra | Keywords:
------------------------+---------------------------------------------------
Consider this:
{{{
sage: v = vector({1: 1, 3: -1})
sage: w = vector({1: -1, 3: 1})
sage: v+w
(0, 0, 0, 0)
sage: (v+w).is_zero()
False
}}}
I see two things wrong with the source code:
1. in modules/free_module_element.pyx, it says
{{{
def __nonzero__(self):
"""
EXAMPLES:
sage: V = vector(ZZ, [0, 0, 0, 0])
sage: bool(V)
False
sage: V = vector(ZZ, [1, 2, 3, 5])
sage: bool(V)
True
"""
return self != 0
}}}
I don't understand the relevance of the doctest at all, and the actual
code should probably say something like {{{self != self.parent()(0)}}}.
In fact, this is completely unnecessary, because this class inherits from
ModuleElement, which has {{{__nonzero__}}} defined in precisely this way
-- see structure/element.pyx.
2. in structure/element.pyx, it says
{{{
def is_zero(self):
"""
Return True if self equals self.parent()(0). The default
implementation is to fall back to 'not self.__nonzero__'.
NOTE: Do not re-implement this method in your subclass but
implement __nonzero__ instead.
"""
return not self
}}}
The code {{{return not self}}} looks like a typo: it should be {{{return
not self.__nonzero__()}}} -- read the docstring!
The patch deals with both of these issues by fixing the code in
element.pyx and by deleting the code in free_module_element.pyx. It also
adds a doctest to element.pyx, verifying that the above vector example now
works.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/5185>
Sage <http://sagemath.org/>
Sage - Open Source Mathematical Software: Building the Car Instead of
Reinventing the Wheel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en
-~----------~----~----~----~------~----~------~--~---