# 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.

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.

make_symmetric_matrix_from_upper_tri(val)[source]

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]

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.

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.

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

• 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.