Skip to content

Matrix doesn't support negative indices #835

@xlucn

Description

@xlucn
from mpmath import matrix

# Create a 3x3 matrix
A = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Access the last element of the first row
print(A[0, -1])

I expect negative index -n to indicate last n element, like in Python list or numpy array. But I got 0.

After some digging, I found the related code:

# single element extraction
if key[0] >= self._rows or key[1] >= self._cols:
raise IndexError('matrix index out of range')
if key in self._data:
return self._data[key]
else:
return self.ctx.zero

The same goes for index of columns, such as A[-1,:] returns [0.0 0.0 0.0].

Are negative indices for other purposes? Can Python-like negative indices be supported?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementnew feature requests (or implementation)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions