Skip to content

Add member function SimpleArray::trace()#842

Merged
yungyuc merged 1 commit into
solvcon:masterfrom
yungyuc:feat/trace
May 30, 2026
Merged

Add member function SimpleArray::trace()#842
yungyuc merged 1 commit into
solvcon:masterfrom
yungyuc:feat/trace

Conversation

@yungyuc
Copy link
Copy Markdown
Member

@yungyuc yungyuc commented May 30, 2026

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 cover it in tests/test_buffer.py with float, int, and complex cases plus the 2D and square validation error paths.

For issue #822.

@yungyuc yungyuc self-assigned this May 30, 2026
@yungyuc yungyuc added the array Multi-dimensional array implementation label May 30, 2026
@yungyuc yungyuc moved this to In Progress in tensor operations May 30, 2026
Comment thread tests/test_buffer.py

def test_trace(self):
"""Test trace() method for summing the diagonal of a matrix"""
ndarr = np.array([[1.0, 2.0, 3.0],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the trace for real number.

Comment thread tests/test_buffer.py
sarr = modmesh.SimpleArrayFloat64(array=ndarr)
self.assertEqual(sarr.trace(), np.trace(ndarr))

ndarr_int = np.array([[2, 0, 1],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the trace for integer.

Comment thread tests/test_buffer.py
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],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sum over the diagonal.

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.
@yungyuc yungyuc marked this pull request as ready for review May 30, 2026 07:49
@yungyuc yungyuc requested review from KHLee529 and ThreeMonth03 May 30, 2026 07:49
@yungyuc yungyuc merged commit 33c1168 into solvcon:master May 30, 2026
16 of 17 checks passed
@yungyuc yungyuc deleted the feat/trace branch May 30, 2026 07:49
@github-project-automation github-project-automation Bot moved this from In Progress to Done in tensor operations May 30, 2026
@yungyuc yungyuc linked an issue May 30, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

array Multi-dimensional array implementation

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Implement matrix trace SimpleArray::trace()

1 participant