Coverage for /builds/alexhroom/ase/ase/dft/__init__.py: 94.44%
18 statements
« prev ^ index » next coverage.py v7.5.3, created at 2024-08-05 14:37 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2024-08-05 14:37 +0000
1import numpy as np
2try:
3 from numpy import trapezoid # NumPy 2.0.0
4except ImportError:
5 from numpy import trapz as trapezoid
7from ase.dft.dos import DOS
8from ase.dft.kpoints import monkhorst_pack
10__all__ = ['DOS', 'monkhorst_pack']
13def get_distribution_moment(x, y, order=0):
14 """Return the moment of nth order of distribution.
16 1st and 2nd order moments of a band correspond to the band's
17 center and width respectively.
19 For integration, the trapezoid rule is used.
20 """
22 x = np.asarray(x)
23 y = np.asarray(y)
25 if order == 0:
26 return trapezoid(y, x)
27 elif isinstance(order, int):
28 return trapezoid(x**order * y, x) / trapezoid(y, x)
29 elif hasattr(order, '__iter__'):
30 return [get_distribution_moment(x, y, n) for n in order]
31 else:
32 raise ValueError(f'Illegal order: {order}')