Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added is_anti_hermitian.py #798

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions docs/refs.bib
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,13 @@ @misc{WikiHerm

}

@misc{WikiAntiHerm,
author = "Wikipedia",
title = "Skew-Hermitian matrix",
howpublished = {https://en.wikipedia.org/wiki/Skew-Hermitian_matrix}

}


@misc{WikiHilbSchOp,
author = "Wikipedia",
Expand Down
1 change: 1 addition & 0 deletions toqito/matrix_props/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from toqito.matrix_props.has_same_dimension import has_same_dimension
from toqito.matrix_props.is_square import is_square
from toqito.matrix_props.kp_norm import kp_norm
from toqito.matrix_props.is_anti_hermitian import is_anti_hermitian
from toqito.matrix_props.is_hermitian import is_hermitian
from toqito.matrix_props.is_positive_semidefinite import is_positive_semidefinite
from toqito.matrix_props.is_density import is_density
Expand Down
67 changes: 67 additions & 0 deletions toqito/matrix_props/is_anti_hermitian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Is matrix an anti-Hermitian matrix."""

import numpy as np

from toqito.matrix_props.is_hermitian import is_hermitian


def is_anti_hermitian(mat: np.ndarray, rtol: float = 1e-05, atol: float = 1e-08) -> bool:
r"""Check if matrix is anti-Hermitian (a.k.a. skew-Hermitian) :cite:`WikiAntiHerm`.

An anti-Hermitian matrix is a complex square matrix that is equal to the negative of its own
conjugate transpose.

Examples
==========

Consider the following matrix:

.. math::
A = \begin{pmatrix}
2j & -1 + 2j & 4j \\
1 + 2j & 3j & -1 \\
4j & 1 & 1j
\end{pmatrix}

our function indicates that this is indeed an anti-Hermitian matrix as it holds that

.. math::
A = -A^*.

>>> from toqito.matrix_props import is_anti_hermitian
>>> import numpy as np
>>> mat = np.array([[2j, -1 + 2j, 4j], [1 + 2j, 3j, -1], [4j, 1, 1j]])
>>> is_anti_hermitian(mat)
True

Alternatively, the following example matrix :math:`B` defined as

.. math::
B = \begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix}

is not anti-Hermitian.

>>> from toqito.matrix_props import is_anti_hermitian
>>> import numpy as np
>>> mat = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> is_anti_hermitian(mat)
False

References
==========
.. bibliography::
:filter: docname in docnames



:param mat: Matrix to check.
:param rtol: The relative tolerance parameter (default 1e-05).
:param atol: The absolute tolerance parameter (default 1e-08).
:return: Return True if matrix is anti-Hermitian, and False otherwise.

"""
return is_hermitian(mat * 1j, rtol, atol)
23 changes: 23 additions & 0 deletions toqito/matrix_props/tests/test_is_anti_hermitian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Test is_hermitian."""

import numpy as np

from toqito.matrix_props import is_anti_hermitian


def test_is_hermitian():
"""Test if matrix is Hermitian."""
mat = np.array([[2j, -1 + 2j, 4j], [1 + 2j, 3j, -1], [4j, 1, 1j]])
np.testing.assert_equal(is_anti_hermitian(mat), True)


def test_is_non_hermitian():
"""Test non-Hermitian matrix."""
mat = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.testing.assert_equal(is_anti_hermitian(mat), False)


def test_is_hermitian_not_square():
"""Input must be a square matrix."""
mat = np.array([[-1, 1, 1], [1, 2, 3]])
np.testing.assert_equal(is_anti_hermitian(mat), False)
tnemoz marked this conversation as resolved.
Show resolved Hide resolved