pymatgen.util.num module

This module provides utilities for basic math operations.

abs_cap(val, max_abs_val=1)[source]

Returns the value with its absolute value capped at max_abs_val. Particularly useful in passing values to trignometric functions where numerical errors may result in an argument > 1 being passed in.

  • val (float) – Input value.

  • max_abs_val (float) – The maximum absolute value for val. Defaults to 1.


val if abs(val) < 1 else sign of val * max_abs_val.


Given a symmetric matrix in upper triangular matrix form as flat array indexes as: [A_xx,A_yy,A_zz,A_xy,A_xz,A_yz] This will generate the full matrix: [[A_xx,A_xy,A_xz],[A_xy,A_yy,A_yz],[A_xz,A_yz,A_zz]


Return the index of the (first) maximum in seq

>>> assert maxloc([1,3,2,3]) == 1

Uses enumerate, max, and min to return the indices of the values in a list with the maximum and minimum value:


Return the index of the (first) minimum in seq

>>> assert minloc(range(3)) == 0
monotonic(values, mode='<', atol=1e-08)[source]

Returns False if values are not monotonic (decreasing|increasing). mode is “<” for a decreasing sequence, “>” for an increasing sequence. Two numbers are considered equal if they differ less that atol.

>>> values = [1.2, 1.3, 1.4]
>>> monotonic(values, mode="<")
>>> monotonic(values, mode=">")

True if values are not decreasing.


True if values are not increasing.

round_to_sigfigs(num, sigfigs)[source]

Rounds a number rounded to a specific number of significant figures instead of to a specific precision.

sort_dict(d, key=None, reverse=False)[source]

Sorts a dict by value.

  • d – Input dictionary

  • key – Function which takes an tuple (key, object) and returns a value to compare and sort by. By default, the function compares the values of the dict i.e. key = lambda t : t[1]

  • reverse – Allows to reverse sort order.


OrderedDict object whose keys are ordered according to their value.


True if values are stricly decreasing.


True if values are stricly increasing.