Skip to content
Open
50 changes: 28 additions & 22 deletions src/sage/modules/free_module_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1920,15 +1920,20 @@

def get(self, i):
"""
Like ``__getitem__`` but without bounds checking:
`i` must satisfy ``0 <= i < self.degree``.
Return the `i`-th entry of ``self``.

This is equivalent to ``self[i]``.

EXAMPLES::

sage: vector(SR, [1/2,2/5,0]).get(0) # needs sage.symbolic
1/2
sage: zero_vector(3).get(5)
Traceback (most recent call last):
...
IndexError: vector index out of range
"""
return self.get_unsafe(i)
return self[i]

def __setitem__(self, i, value):
"""
Expand Down Expand Up @@ -1982,19 +1987,23 @@

def set(self, i, value):
"""
Like ``__setitem__`` but without type or bounds checking:
`i` must satisfy ``0 <= i < self.degree`` and ``value`` must be
an element of the coordinate ring.
Set the `i`-th entry of ``self`` to ``value``.

This is equivalent to ``self[i] = value``.

EXAMPLES::

sage: v = vector(SR, [1/2,2/5,0]); v # needs sage.symbolic
sage: # needs sage.symbolic
sage: v = vector(SR, [1/2,2/5,0]); v
(1/2, 2/5, 0)
sage: v.set(2, pi); v # needs sage.symbolic
sage: v.set(2, pi); v
(1/2, 2/5, pi)
sage: v.set(5, 1)
Traceback (most recent call last):
...
IndexError: vector index out of range
"""
assert value.parent() is self.coordinate_ring()
self.set_unsafe(i, value)
self[i] = value

def __invert__(self):
"""
Expand Down Expand Up @@ -5302,16 +5311,15 @@
"""
EXAMPLES::

sage: v = vector([-1,0,2/3,pi], sparse=True) # needs sage.symbolic

Check warning on line 5314 in src/sage/modules/free_module_element.pyx

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, new)

Warning: Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Check warning on line 5314 in src/sage/modules/free_module_element.pyx

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.13, all)

Warning: Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Check warning on line 5314 in src/sage/modules/free_module_element.pyx

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.13, all)

Warning: Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Check warning on line 5314 in src/sage/modules/free_module_element.pyx

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all)

Warning: Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Check warning on line 5314 in src/sage/modules/free_module_element.pyx

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all, editable)

Warning: Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Check warning on line 5314 in src/sage/modules/free_module_element.pyx

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.12, all)

Warning: Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times

Consider using a block-scoped tag by inserting the line 'sage: # needs sage.symbolic' just before this line to avoid repeating the tag 4 times
sage: v.get(1) # needs sage.symbolic
0
sage: v.get(2) # needs sage.symbolic
2/3

For this class, 0 is returned if the access is out of bounds::

sage: v.get(10) # needs sage.symbolic
0
Traceback (most recent call last):
...
IndexError: vector index out of range
"""
try:
return self._entries[i]
Expand All @@ -5336,7 +5344,7 @@
...
TypeError: self must be a numeric expression

::
TESTS::

sage: # needs sage.symbolic
sage: v = vector([1,2/3,pi], sparse=True)
Expand All @@ -5349,13 +5357,11 @@

This assignment is illegal::

sage: v.set(10, pi) # needs sage.symbolic

This lack of bounds checking causes trouble later::

sage: v # needs sage.symbolic
<repr(<sage.modules.free_module.FreeModule_ambient_field_with_category.element_class at 0x...>) failed:
IndexError: list assignment index out of range>
sage: # needs sage.symbolic
sage: v.set(10, pi)
Traceback (most recent call last):
...
IndexError: vector index out of range
"""
if value:
self._entries[i] = value
Expand Down
Loading