Add member function SimpleArray::trace()#842
Merged
Merged
Conversation
yungyuc
commented
May 30, 2026
|
|
||
| def test_trace(self): | ||
| """Test trace() method for summing the diagonal of a matrix""" | ||
| ndarr = np.array([[1.0, 2.0, 3.0], |
Member
Author
There was a problem hiding this comment.
Test the trace for real number.
| sarr = modmesh.SimpleArrayFloat64(array=ndarr) | ||
| self.assertEqual(sarr.trace(), np.trace(ndarr)) | ||
|
|
||
| ndarr_int = np.array([[2, 0, 1], |
Member
Author
There was a problem hiding this comment.
Test the trace for integer.
| sarr_int = modmesh.SimpleArrayInt32(array=ndarr_int) | ||
| self.assertEqual(sarr_int.trace(), np.trace(ndarr_int)) | ||
|
|
||
| ndarr_cplx = np.array([[1.0 + 2.0j, 3.0 + 4.0j], |
Member
Author
There was a problem hiding this comment.
Test the trace for complex number.
| value_type result = static_cast<value_type>(0); | ||
| for (ssize_t i = 0; i < athis->shape(0); ++i) | ||
| { | ||
| result += (*athis)(i, i); |
Add `SimpleArray::trace()` to `SimpleArrayMixinMatrix` to compute the trace (sum of the diagonal) of a square matrix, alongside the existing eye, scaled_eye, hermitian, and symmetrize operations. It works for real, integer, and complex dtypes. Expose it through the wrap_matrix() pybind11 section and test it in Python. `tests/test_buffer.py` covers float, int, and complex cases, as well as the 2D and square validation error paths. For issue solvcon#822.
This was referenced May 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
SimpleArray::trace()toSimpleArrayMixinMatrixto compute the trace (sum of the diagonal) of a square matrix, alongside the existing eye, scaled_eye, hermitian, and symmetrize operations. It works for real, integer, and complex dtypes.Expose it through the wrap_matrix() pybind11 section and cover it in tests/test_buffer.py with float, int, and complex cases plus the 2D and square validation error paths.
For issue #822.