diffcalc.util

Collection of auxiliary mathematical methods.

Functions

allnum(lst)

Check if all object types in the input sequence are either int or float.

angle_between_vectors(x, y)

Angle between two column vectors.

bound(x)

Check the input value is in [-1, 1] range.

cross3(x, y)

Cross product of column vectors.

dot3(x, y)

Dot product of column vectors.

is_small(x[, tolerance])

Check if input value is 0 within tolerance.

isnum(o)

Check if the input object type is either int or float.

normalised(vector)

Normalise vector array.

radians_equivalent(first, second[, tolerance])

Check for angle equivalence.

sign(x[, tolerance])

Sign function with specified tolerance.

x_rotation(th)

Rotation matrix over x axis.

xyz_rotation(axis, angle)

Rotation matrix over arbitrary axis.

y_rotation(th)

Rotation matrix over y axis.

z_rotation(th)

Rotation matrix over z axis.

zero_round(num)

Round to zero if small.

Exceptions

DiffcalcException

Error caused by user misuse of diffraction calculator.

diffcalc.util.x_rotation(th: float) ndarray[source]

Rotation matrix over x axis.

Parameters

th (float) – Rotation angle.

Returns

Rotation matrix.

Return type

np.ndarray

diffcalc.util.y_rotation(th: float) ndarray[source]

Rotation matrix over y axis.

Parameters

th (float) – Rotation angle.

Returns

Rotation matrix.

Return type

np.ndarray

diffcalc.util.z_rotation(th: float) ndarray[source]

Rotation matrix over z axis.

Parameters

th (float) – Rotation angle.

Returns

Rotation matrix.

Return type

np.ndarray

diffcalc.util.xyz_rotation(axis: Tuple[float, float, float], angle: float) ndarray[source]

Rotation matrix over arbitrary axis.

Parameters
  • axis (Tuple[float, float, float]) – Rotation axis coordinates

  • angle (float) – Rotation angle.

Returns

Rotation matrix.

Return type

np.ndarray

exception diffcalc.util.DiffcalcException[source]

Error caused by user misuse of diffraction calculator.

diffcalc.util.cross3(x: ndarray, y: ndarray) ndarray[source]

Cross product of column vectors.

Parameters
  • x (np.ndarray) – Column vector represented as NumPy (3,1) array.

  • y (np.ndarray) – Column vector represented as NumPy (3,1) array.

Returns

Cross product column vector as as NumPy (3,1) array.

Return type

np.ndarray

diffcalc.util.dot3(x: ndarray, y: ndarray) float[source]

Dot product of column vectors.

Parameters
  • x (np.ndarray) – Column vector represented as NumPy (3,1) array.

  • y (np.ndarray) – Column vector represented as NumPy (3,1) array.

Returns

Dot product of column vectors.

Return type

float

diffcalc.util.angle_between_vectors(x: ndarray, y: ndarray) float[source]

Angle between two column vectors.

Parameters
  • x (np.ndarray) – Column vector represented as NumPy (3,1) array.

  • y (np.ndarray) – Column vector represented as NumPy (3,1) array.

Returns

Angle between the vectors.

Return type

float

diffcalc.util.bound(x: float) float[source]

Check the input value is in [-1, 1] range.

Rounds input value to +/-1 if |x| - 1 < SMALL.

Parameters

x (float) – Input value to be checked

Returns

Value in [-1, 1] range.

Return type

float

Raises

AssertionError – Input value outside [-1, 1] range.

diffcalc.util.radians_equivalent(first: float, second: float, tolerance: float = 1e-07) bool[source]

Check for angle equivalence.

Parameters
  • first (float) – First angle value.

  • second (float) – Second angle value.

  • tolerance (float, default = SMALL) – Absolute tolerance for the angle difference.

Returns

True is angles are equivalent.

Return type

bool

diffcalc.util.isnum(o: Any) bool[source]

Check if the input object type is either int or float.

Parameters

o (Any) – Input object to be checked.

Returns

If object type is either int or float.

Return type

bool

diffcalc.util.allnum(lst: Sequence[Any]) bool[source]

Check if all object types in the input sequence are either int or float.

Parameters

o (Sequence[Any]) – Input object sequence to be checked.

Returns

If all object types in th sequence are either int or float.

Return type

bool

diffcalc.util.is_small(x, tolerance=1e-07) bool[source]

Check if input value is 0 within tolerance.

Parameters
  • x (float) – Input value to be checked.

  • tolerance (float, default = SMALL) – Absolute tolerance.

Returns

True is the value is 0 within tolerance.

Return type

bool

diffcalc.util.sign(x: float, tolerance: float = 1e-07) int[source]

Sign function with specified tolerance.

Parameters
  • x (float) – Function argument.

  • tolerance (float, default = SMALL) – Absolute tolerance.

Returns

1 for positive, -1 for negative values and 0 if argument equals 0 within tolerance.

Return type

int

diffcalc.util.normalised(vector: ndarray) ndarray[source]

Normalise vector array.

Return normalised vector coordinates.

Parameters

vector (ndarray) – The vector to be normalised.

Returns

Normalised vector.

Return type

ndarray

diffcalc.util.zero_round(num)[source]

Round to zero if small.

This is useful to get rid of erroneous minus signs resulting from float representation close to zero.

Parameters

num (number) – The value to be checked for rounding.

Returns

The rounded input value.

Return type

number