fe_test.py ダウンロード/コピー
fe_test.py
fe_test.py
1"""
2fermi_dirac_e_npvector, fermi_dirac_e_if, fermi_dirac_eを
3ライブラリtkfermi_integral.pyから読み込み、x = [-100, -50, -40, -20, -10, -5, -2, -1, -0.5, -0.1, 0.0, 0.1, 0.5, 1.0, 2, 5, 10, 20, 40, 50, 100]の値と相対誤差を計算し、
4tol=1e-20を基準にしてテストして、FAILかPASSかを出力(出力には3関数の値とdiff値もいれる)するとともに、
5ベンチマークするプログラムを作ってください
6"""
7
8
9import numpy as np
10import time
11from tkfermi_integral import (
12 fermi_dirac_e_npvector,
13 fermi_dirac_e_if,
14 fermi_dirac_e
15)
16
17# ------------------------------------------------------------
18# Test points
19# ------------------------------------------------------------
20x_values = np.array([
21 -1000,
22 -100, -50, -40, -20, -10, -5, -2, -1, -0.5, -0.1,
23 0.0, 0.1, 0.5, 1.0, 2, 5, 10, 20, 40, 50, 100,
24 300, 500, 600, 700, 800, 900, 1000
25], dtype=float)
26
27eta = 0.0
28g = 1.0
29tol = 1e-20
30
31# ------------------------------------------------------------
32# Helper: relative error
33# ------------------------------------------------------------
34def relerr(a, b):
35 return np.abs(a - b) / np.maximum(np.abs(b), 1e-300)
36
37# ------------------------------------------------------------
38# Wrappers to unify interface
39# ------------------------------------------------------------
40def fd_high_precision_scalar(x, eta, g):
41 return fermi_dirac_e_npvector(np.array([x]))[0]
42
43def fd_if_scalar(x, eta, g):
44 return fermi_dirac_e_if(x, eta, g)
45
46def fd_expit_scalar(x, eta, g):
47 return fermi_dirac_e(x, eta, g)
48
49# ------------------------------------------------------------
50# Test each function
51# ------------------------------------------------------------
52def test_function(name, func, x_values, ref_values):
53 print(f"\n=== Testing {name} ===")
54 for x, ref in zip(x_values, ref_values):
55 val = func(x, eta, g)
56 diff = relerr(val, ref)
57 status = "PASS" if diff < tol else "FAIL"
58 print(f"x={x:8.2f} val={val:.8e} ref={ref:.8e} diff={diff:.3e} {status}")
59
60# ------------------------------------------------------------
61# Compute reference (high precision)
62# ------------------------------------------------------------
63ref_values = fermi_dirac_e_npvector(x_values)
64
65# ------------------------------------------------------------
66# Run tests
67# ------------------------------------------------------------
68#test_function("high_precision", fd_high_precision_scalar, x_values, ref_values)
69test_function("scalar_if", fd_if_scalar, x_values, ref_values)
70test_function("expit", fd_expit_scalar, x_values, ref_values)
71
72# ------------------------------------------------------------
73# Benchmark
74# ------------------------------------------------------------
75def benchmark(name, func, x_values, repeat=20000):
76 t0 = time.time()
77 for _ in range(repeat):
78 # vectorized version for speed test
79 if name == "npvector":
80 fermi_dirac_e_npvector(x_values)
81 elif name == "scalar_if":
82 np.array([fermi_dirac_e_if(x, eta, g) for x in x_values])
83 else:
84 fermi_dirac_e(x_values, eta, g)
85 t1 = time.time()
86 return t1 - t0
87
88print("\n=== Benchmark (lower is faster) ===")
89t_np = benchmark("npvector", fd_high_precision_scalar, x_values)
90t_if = benchmark("scalar_if", fd_if_scalar, x_values)
91t_ex = benchmark("expit", fd_expit_scalar, x_values)
92
93print(f"npvector : {t_np:.4f} sec")
94print(f"scalar_if: {t_if:.4f} sec")
95print(f"expit : {t_ex:.4f} sec")