pymatgen.util.num_utils module

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.

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

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

maxloc(seq)[source]

Return the index of the (first) maximum in seq

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

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

minloc(seq)[source]

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="<")
False
>>> monotonic(values, mode=">")
True
non_decreasing(values)[source]

True if values are not decreasing.

non_increasing(values)[source]

True if values are not increasing.

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

Sorts a dict by value.

Parameters:
  • 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.
Returns:

OrderedDict object whose keys are ordered according to their value.

strictly_decreasing(values)[source]

True if values are stricly decreasing.

strictly_increasing(values)[source]

True if values are stricly increasing.