Matrix

Matrix utils

twiss.matrix.is_symplectic(m: torch.Tensor, *, epsilon: float = 1e-12) bool[source]

Test symplectic condition for a given input matrix elementwise

Parameters:
  • matrix (Tensor) – input matrix

  • epsilon (float) – tolerance epsilon

Return type:

bool

Examples

>>> from math import pi
>>> import torch
>>> is_symplectic(rotation_block(torch.tensor(pi, dtype=torch.float64)))
True
twiss.matrix.projection(d: int, p: int, *, dtype=torch.float64, device=torch.device) torch.Tensor[source]

Plane projection matrix

Parameters:
  • d (int) – total number of planes

  • p (int) – plane id (1, 2, …, n)

  • dtype (torch.dtype, default=torch.float64) – data type

  • device (torch.device, torch.device=torch.device('cpu')) – data device

Return type:

Tensor

Examples

>>> projection(2, 1)
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]], dtype=torch.float64)
>>> projection(2, 2)
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]], dtype=torch.float64)
twiss.matrix.rotation(*angles: torch.Tensor) torch.Tensor[source]

Generate rotation matrix using given angles.

Parameters:

angles (Tensor) – rotation angles

Return type:

Tensor

Note

Block rotation is [[cos(angle), sin(angle)], [-sin(angle), cos(angle)]]

Examples

>>> from math import pi
>>> import torch
>>> rotation(*torch.tensor([pi, -pi], dtype=torch.float64))
tensor([[-1.0000e+00,  1.2246e-16,  0.0000e+00,  0.0000e+00],
        [-1.2246e-16, -1.0000e+00,  0.0000e+00,  0.0000e+00],
        [ 0.0000e+00,  0.0000e+00, -1.0000e+00, -1.2246e-16],
        [ 0.0000e+00,  0.0000e+00,  1.2246e-16, -1.0000e+00]],
       dtype=torch.float64)
twiss.matrix.rotation_block(angle: torch.Tensor) torch.Tensor[source]

Generate 2D rotation block [[cos(angle), sin(angle)], [-sin(angle), cos(angle)]] matrix

Parameters:

angle (Tensor) – rotation angle

Return type:

Tensor

Examples

>>> from math import pi
>>> import torch
>>> rotation_block(torch.tensor(pi, dtype=torch.float64))
tensor([[-1.0000e+00,  1.2246e-16],
        [-1.2246e-16, -1.0000e+00]], dtype=torch.float64)
twiss.matrix.symplectic_block(*, dtype: torch.dtype = torch.float64, device: torch.device = torch.device) torch.Tensor[source]

Generate 2D symplectic block [[0, 1], [-1, 0]] matrix

Parameters:
  • dtype (torch.dtype, default=torch.float64) – output type

  • device (torch.device, torch.device=torch.device('cpu')) – output device

Return type:

Tensor

Examples

>>> symplectic_block()
tensor([[ 0.,  1.],
        [-1.,  0.]], dtype=torch.float64)
twiss.matrix.symplectic_conjugate(m: torch.Tensor) torch.Tensor[source]

Compute symplectic conjugate of a given input matrix

Parameters:

m (Tensor, even-dimension) – input matrix

Return type:

Tensor

Examples

>> import torch >> symplectic_conjugate(torch.tensor([[1.0, 0.1], [0.0, 1.0]], dtype=torch.float64)) tensor([[ 1.0000, -0.1000],

[ 0.0000, 1.0000]], dtype=torch.float64)

twiss.matrix.symplectic_identity(d: int, *, dtype: torch.dtype = torch.float64, device: torch.device = torch.device) torch.Tensor[source]

Generate symplectic identity matrix for a given (configuration space) dimension

Parameters:
  • d (int, positive) – configuration space dimension

  • dtype (torch.dtype, default=torch.float64) – data type

  • device (torch.device, torch.device=torch.device('cpu')) – data device

Return type:

Tensor

Note

Symplectic block is [[0, 1], [-1, 0]], i.e. canonical variables are grouped by pairs

Examples

>>> symplectic_identity(1)
tensor([[ 0.,  1.],
        [-1.,  0.]], dtype=torch.float64)
>>> symplectic_identity(2)
tensor([[ 0.,  1.,  0.,  0.],
        [-1.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  1.],
        [ 0.,  0., -1.,  0.]], dtype=torch.float64)
twiss.matrix.symplectic_inverse(m: torch.Tensor) torch.Tensor[source]

Compute inverse of a given input symplectic matrix

Parameters:

m (Tensor, even-dimension, symplectic) – input matrix

Return type:

Tensor

Examples

>>> import torch
>>> symplectic_inverse(torch.tensor([[1.0, 0.1], [0.0, 1.0]], dtype=torch.float64))
tensor([[ 1.0000, -0.1000],
        [ 0.0000,  1.0000]], dtype=torch.float64)
twiss.matrix.symplectify(m: torch.Tensor) torch.Tensor[source]

Perform symplectification of a given input matrix

Parameters:

m (Tensor, even-dimension) – input matrix to symplectify

Return type:

Tensor

Examples

>>> import torch
>>> symplectify(torch.tensor([[1.0, 0.1], [0.0, 0.99]], dtype=torch.float64))
tensor([[1.0050, 0.1005],
        [0.0000, 0.9950]], dtype=torch.float64)