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.

angles_equivalent(first, second[, tolerance])

Check for angle equivalence in degrees.

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.

sign(x[, tolerance])

Sign function with specified tolerance.

solve_h_fixed_q(h, qval, B, a, b, c, d)

Find valid hkl for a given h and q value.

solve_k_fixed_q(k, qval, B, a, b, c, d)

Find valid hkl for a given k and q value.

solve_l_fixed_q(l, qval, B, a, b, c, d)

Find valid hkl for a given l and q value.

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 in degrees.

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.angles_equivalent(first: float, second: float, tolerance: float = 1e-07) bool[source]

Check for angle equivalence in degrees.

Parameters:
  • first (float) – First angle value in degrees.

  • second (float) – Second angle value in degrees.

  • 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

diffcalc.util.solve_h_fixed_q(h: float, qval: float, B: ndarray, a: float, b: float, c: float, d: float) List[Tuple[float, float, float]][source]

Find valid hkl for a given h and q value.

Coefficients are used to constrain solutions as:

a*h + b*k + c*l = d

Parameters:
  • h (float) – value of h to use

  • qval (float) – norm of the scattering vector squared

  • B (np.ndarray) – 3x3 matrix, usually the UB matrix

  • a (float) – a coefficient to constrain the resulting hkl

  • b (float) – a coefficient to constrain the resulting hkl

  • c (float) – a coefficient to constrain the resulting hkl

  • d (float) – a coefficient to constrain the resulting hkl

Returns:

list of possible hkl solutions

Return type:

List[Tuple[float, float, float]]

Raises:

DiffcalcException – If the divisor is 0, or the discriminant is negative. The first of these occurs if both b and c are equal to 0.

diffcalc.util.solve_k_fixed_q(k: float, qval: float, B: ndarray, a: float, b: float, c: float, d: float) List[Tuple[float, float, float]][source]

Find valid hkl for a given k and q value.

Coefficients are used to constrain solutions as:

a*h + b*k + c*l = d

Parameters:
  • k (float) – value of k to use

  • qval (float) – norm of the scattering vector squared

  • B (np.ndarray) – 3x3 matrix, usually the UB matrix

  • a (float) – a coefficient to constrain the resulting hkl

  • b (float) – a coefficient to constrain the resulting hkl

  • c (float) – a coefficient to constrain the resulting hkl

  • d (float) – a coefficient to constrain the resulting hkl

Returns:

list of possible hkl solutions

Return type:

List[Tuple[float, float, float]]

Raises:

DiffcalcException – If the divisor is 0, or the discriminant is negative. The first of these occurs if both a and c are equal to 0.

diffcalc.util.solve_l_fixed_q(l: float, qval: float, B: ndarray, a: float, b: float, c: float, d: float) List[Tuple[float, float, float]][source]

Find valid hkl for a given l and q value.

Coefficients are used to constrain solutions as:

a*h + b*k + c*l = d

Parameters:
  • l (float) – value of l to use

  • qval (float) – norm of the scattering vector squared

  • B (np.ndarray) – 3x3 matrix, usually the UB matrix

  • a (float) – a coefficient to constrain the resulting hkl

  • b (float) – a coefficient to constrain the resulting hkl

  • c (float) – a coefficient to constrain the resulting hkl

  • d (float) – a coefficient to constrain the resulting hkl

Returns:

list of possible hkl solutions

Return type:

List[Tuple[float, float, float]]

Raises:

DiffcalcException – If the divisor is 0, or the discriminant is negative. The first of these occurs if both a and b are equal to 0.