magnon.linalg

Functions

normalised_vector(vec)

Return a normalised vector in the direction of vec.

generate_orthonormal_vectors(vec)

Generate an orthonormal set of vectors including the given input vector.

rotation_matrix_pair(axis, angle)

Return the generalized rotation matrix describing a rotation of [angle] radians around [axis], and a rotation of -[angle] radians around [axis]

paraidentity(size[, dtype])

Create a paraidentity (\(\sigma_3\)) matrix of shape (2*size, 2*size).

is_purely_real(arr[, zero_tol])

Test whether all elements of an array are real within a tolerance.

Module Contents

magnon.linalg.normalised_vector(vec)

Return a normalised vector in the direction of vec.

Parameters:

vec (array_like) – The vector to normalize.

Returns:

out – Normalised vector with the same direction as vec. If the input norm is zero, returns a vector of zeros with the same shape as vec.

Return type:

ndarray

Examples

>>> normalised_vector([3, 4])
array([0.6, 0.8])
>>> normalised_vector([0, 0])
array([0., 0.])
magnon.linalg.generate_orthonormal_vectors(vec)

Generate an orthonormal set of vectors including the given input vector.

Given a 2D or 3D input vector, this function returns a set of orthonormal vectors where the first vector is the normalised input vector, and the remaining vectors are mutually orthogonal and normalised.

Parameters:

vec (array_like) – Input vector of length 2 or 3.

Returns:

orthonormals – Tuple containing orthonormal vectors:

For 2D input: (v0, v1), where v0 is the normalised input vector, and v1 is perpendicular to v0.

For 3D input: (v0, v1, v2), where v0 is the normalised input vector, and v1, v2 are mutually orthogonal and normalised.

Return type:

tuple of ndarray

Raises:

AssertionError – If the length of the input vector is not 2 or 3.

Examples

>>> generate_orthonormal_vectors([1, 0, 0])
(array([1., 0., 0.]), array([0., 0., 1.]), array([0., 1., 0.]))
>>> generate_orthonormal_vectors([0, 1])
(array([0., 1.]), array([1., 0.]))
magnon.linalg.rotation_matrix_pair(axis, angle)

Return the generalized rotation matrix describing a rotation of [angle] radians around [axis], and a rotation of -[angle] radians around [axis]

This is an implementation of Rodrigues’ rotation formula [1].

Parameters:
  • axis (array_like) – The axis around which the rotation is to occur

  • angle (float) – The angle of the rotation in radians

Returns:

R – The rotation matrix

Return type:

numpy.ndarray

References

magnon.linalg.paraidentity(size, dtype=np.float64)

Create a paraidentity (\(\sigma_3\)) matrix of shape (2*size, 2*size).

The paraidentity matrix, also known as the generalized Pauli \(\sigma_3\) matrix, is a diagonal matrix with the first size diagonal entries set to 1, and the next size diagonal entries set to -1.

Parameters:
  • size (int) – The block size. The output matrix will have shape (2*size, 2*size).

  • dtype (data-type, optional) – Desired output data type (default is np.float64).

Returns:

out – Paraidentity matrix of shape (2*size, 2*size) and specified dtype.

Return type:

ndarray

Examples

>>> paraidentity(2)
array([[ 1.,  0.,  0.,  0.],
[ 0.,  1.,  0.,  0.],
[ 0.,  0., -1.,  0.],
[ 0.,  0.,  0., -1.]])
magnon.linalg.is_purely_real(arr, zero_tol=magnon.default_numerical_tol)

Test whether all elements of an array are real within a tolerance.

Parameters:
  • arr (array_like) – Input array to be tested.

  • zero_tol (float, optional) – Tolerance for considering the imaginary part as zero (default is 1e-8).

Returns:

is_real – True if all elements have imaginary part less than tol in magnitude, False otherwise.

Return type:

bool

Examples

>>> import numpy as np
>>> is_purely_real([1, 2, 3])
True
>>> is_purely_real([1+1e-9j, 2, 3])
True
>>> is_purely_real([1+1e-6j, 2, 3], zero_tol=1e-7)
False
>>> is_purely_real([1+0j, 2+0j, 3+0j])
True