Skip to content

util

try_conjugate(x)

Return the complex conjugate.

\[ x^* \qquad \mathbb{K}\to\mathbb{K} \]

Tries to call a method conjugate. If not found, simply returns the element as is.

Source code in vector\util.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def try_conjugate(x):
    r"""Return the complex conjugate.

    $$
        x^* \qquad \mathbb{K}\to\mathbb{K}
    $$

    Tries to call a method `conjugate`.
    If not found, simply returns the element as is.
    """
    #try:
    #    return x.conjugate()
    #except AttributeError:
    #    return x
    #could throw an AttibuteError from somewhere deeper
    conj = getattr(x, 'conjugate', None)
    return conj() if callable(conj) else x