Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cpp/modmesh/buffer/SimpleArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,18 @@ class SimpleArrayMixinMatrix
return result;
}

value_type trace() const
{
auto athis = static_cast<A const *>(this);
validate_square("trace");
auto 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.

}
return result;
}

private:

static void validate_positive(const char * operation_name, ssize_t n)
Expand Down
1 change: 1 addition & 0 deletions cpp/modmesh/buffer/pymod/wrap_SimpleArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapSimpleArray
.def_static("scaled_eye", &wrapped_type::scaled_eye, py::arg("n"), py::arg("scale"), "Create a scaled identity matrix of size n x n")
.def("hermitian", &wrapped_type::hermitian, "Create hermitian (conjugate transpose) of the matrix")
.def("symmetrize", &wrapped_type::symmetrize, "Create symmetric matrix by averaging with its transpose")
.def("trace", &wrapped_type::trace, "Compute the trace (sum of the diagonal) of a square matrix")
//
;

Expand Down
36 changes: 36 additions & 0 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,42 @@ def test_symmetrize(self):
):
non_square.symmetrize()

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.

[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]], dtype='float64')
sarr = modmesh.SimpleArrayFloat64(array=ndarr)
self.assertEqual(sarr.trace(), 15.0)

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.

[0, 3, 0],
[4, 0, 5]], dtype='int32')
sarr_int = modmesh.SimpleArrayInt32(array=ndarr_int)
self.assertEqual(sarr_int.trace(), 10)

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.

[5.0 + 6.0j, 7.0 + 8.0j]],
dtype='complex128')
sarr_cplx = modmesh.SimpleArrayComplex128(array=ndarr_cplx)
self.assertEqual(complex(sarr_cplx.trace()), 8.0 + 10.0j)

vector = modmesh.SimpleArrayFloat64(5)
with self.assertRaisesRegex(
RuntimeError,
r"SimpleArray::trace\(\): operation requires 2D SimpleArray, "
r"but got 1D SimpleArray"
):
vector.trace()

non_square = modmesh.SimpleArrayFloat64((3, 4))
with self.assertRaisesRegex(
RuntimeError,
r"SimpleArray::trace\(\): operation requires square "
r"SimpleArray, but got 3x4 shape"
):
non_square.trace()


class SimpleArraySearchTC(unittest.TestCase):

Expand Down
Loading