magpylib.math package

This module includes several practical functions for working with axis-angle relations and genmeralized rotations.

magpylib.math.randomAxis()[source]

This function generates a random axis (3-vector of length 1) from an equal angular distribution using a MonteCarlo scheme.

Returns:axis – A random axis from an equal angular distribution of length 1
Return type:arr3

Example

>>> magpylib as magPy
>>> ax = magPy.math.randomAxis()
>>> print(ax)
  [-0.24834468  0.96858637  0.01285925]
magpylib.math.randomAxisV(N)[source]

This is the vectorized version of randomAxis(). It generates an N-sized vector of random axes (3-vector of length 1) from equal angular distributions using a MonteCarlo scheme.

Parameters:N (int) – Size of random axis vector.
Returns:axes – A vector of random axes from an equal angular distribution of length 1.
Return type:Nx3 arr

Example

>>> import magpylib as magpy
>>> import magpylib as magpy
>>> AXS = magpy.math.randomAxisV(3)
>>> print(AXS)
>>> # Output: [[ 0.39480364 -0.53600779 -0.74620757]
... [ 0.02974442  0.10916333  0.9935787 ]
... [-0.54639126  0.76659756 -0.33731997]]
magpylib.math.axisFromAngles(angles)[source]

This function generates an axis (3-vector of length 1) from two angles = [phi,th] that are defined as in spherical coordinates. phi = azimuth angle, th = polar angle. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:angles (vec2 [deg]) – The two angels [phi,th], azimuth and polar, in units of deg.
Returns:axis – An axis of length that is oriented as given by the input angles.
Return type:arr3

Example

>>> magpylib as magPy
>>> angles = [90,90]
>>> ax = magPy.math.axisFromAngles(angles)
>>> print(ax)
  [0.0  1.0  0.0]
magpylib.math.axisFromAnglesV(ANG)[source]

This is the vectorized version of axisFromAngles(). It generates an Nx3 array of axis vectors from the Nx2 array of input angle pairs angles. Each angle pair is (phi,theta) which are azimuth and polar angle of a spherical coordinate system respectively.

Parameters:ANG (arr Nx2 [deg]) – An N-sized array of angle pairs [phi th], azimuth and polar, in units of deg.
Returns:AXIS – An N-sized array of unit axis vectors oriented as given by the input ANG.
Return type:arr Nx3

Example

>>> import magpylib as magpy
>>> import numpy as np
>>> ANGS = np.array([[0,90],[90,180],[90,0]])
>>> AX = magpy.math.axisFromAnglesV(ANGS)
>>> print(np.around(AX,4))
>>> # Output: [[1.  0. 0.]  [0. 0. -1.]  [0. 0. 1.]]
magpylib.math.anglesFromAxis(axis)[source]

This function takes an arbitrary axis (3-vector) and returns the orientation given by the angles = [phi,th] that are defined as in spherical coordinates. phi = azimuth angle, th = polar angle. Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:axis (vec3) – Arbitrary input axis that defines an orientation.
Returns:angles – The angles [phi,th], azimuth and polar, that anchorrespond to the orientation given by the input axis.
Return type:arr2 [deg]

Example

>>> magpylib as magPy
>>> axis = [1,1,0]
>>> angles = magPy.math.anglesFromAxis(axis)
>>> print(angles)
  [45. 90.]
magpylib.math.anglesFromAxisV(AXIS)[source]

This is the vectorized version of anglesFromAxis(). It takes an Nx3 array of axis-vectors and returns an Nx2 array of angle pairs. Each angle pair is (phi,theta) which are azimuth and polar angle in a spherical coordinate system respectively.

Parameters:AXIS (arr Nx3) – N-sized array of axis-vectors (do not have to be not be normalized).
Returns:ANGLES – N-sized array of angle pairs [phi,th], azimuth and polar, that chorrespond to the orientations given by the input axis vectors in a spherical coordinate system.
Return type:arr Nx2 [deg]

Example

>>> import numpy as np
>>> import magpylib as magpy
>>> AX = np.array([[0,0,1],[0,0,1],[1,0,0]])
>>> ANGS = magpy.math.anglesFromAxisV(AX)
>>> print(ANGS)
>>> # Output: [[0. 0.]  [90. 90.]  [0. 90.]])
magpylib.math.angleAxisRotation(position, angle, axis, anchor=[0, 0, 0])[source]

This function uses angle-axis rotation to rotate the position vector by the angle argument about an axis defined by the axis vector which passes through the center of rotation anchor vector. Scalar input is either integer or float.Vector input format can be either list, tuple or array of any data type (float, int).

Parameters:
  • position (vec3) – Input position to be rotated.
  • angle (scalar [deg]) – Angle of rotation in untis of [deg]
  • axis (vec3) – Axis of rotation
  • anchor (vec3) – The Center of rotation which defines the position of the axis of rotation
Returns:

newPosition – Rotated position

Return type:

arr3

Example

>>> magpylib as magPy
>>> from numpy import pi
>>> position0 = [1,1,0]
>>> angle = -90
>>> axis = [0,0,1]
>>> centerOfRotation = [1,0,0]
>>> positionNew = magPy.math.angleAxisRotation(position0,angle,axis,anchor=centerOfRotation)
>>> print(positionNew)
  [2. 0. 0.]
magpylib.math.angleAxisRotationV(POS, ANG, AXIS, ANCHOR)[source]

This is the vectorized version of angleAxisRotation(). Each entry of POS (arrNx3) is rotated according to the angles ANG (arrN), about the axis vectors AXS (arrNx3) which pass throught the anchors ANCH (arrNx3) where N refers to the length of the input vectors.

Parameters:
  • POS (arrNx3) – The input vectors to be rotated.
  • ANG (arrN [deg]) – Rotation angles in units of [deg].
  • AXIS (arrNx3) – Vector of rotation axes.
  • anchor (arrNx3) – Vector of rotation anchors.
Returns:

  • newPOS (arrNx3) – Vector of rotated positions.
  • >>> import magpylib as magpy
  • >>> import numpy as np
  • >>> POS = np.array([[1,0,0]]*5) # avoid this slow Python loop
  • >>> ANG = np.linspace(0,180,5)
  • >>> AXS = np.array([[0,0,1]]*5) # avoid this slow Python loop
  • >>> ANCH = np.zeros((5,3))
  • >>> POSnew = magpy.math.angleAxisRotationV(POS,ANG,AXS,ANCH)
  • >>> print(np.around(POSnew,4))
  • >>> # Output ([[ 1. 0. 0. ])
  • … [ 0.7071 0.7071 0. ]
  • … [ 0. 1. 0. ]
  • … [-0.7071 0.7071 0. ]
  • … [-1. 0. 0. ]]