|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | +# Add the parent directory (containing differintP) to the import path |
| 7 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 8 | + |
| 9 | +from differintP.core import * # type: ignore |
| 10 | +from differintP.functions import MittagLeffler |
| 11 | +from .__init__ import test_N |
| 12 | + |
| 13 | +# Define constants to be used in tests. |
| 14 | +size_coefficient_array = 20 |
| 15 | +sqrtpi2 = 0.88622692545275794 |
| 16 | +truevaluepoly = 0.94031597258 |
| 17 | +truevaluepoly_caputo = 1.50450555 # 8 / (3 * np.sqrt(np.pi)) |
| 18 | +truevaluepoly_caputo_higher = 2 / Gamma(1.5) |
| 19 | + |
| 20 | +# Get SQRT results for checking accuracy. |
| 21 | +GL_r = GL(0.5, lambda x: np.sqrt(x), 0, 1, test_N) |
| 22 | +GL_result = GL_r[-1] |
| 23 | +GL_length = len(GL_r) |
| 24 | + |
| 25 | +GLI_r = GLI(0.5, lambda x: np.sqrt(x), 0, 1, test_N) |
| 26 | +GLI_result = GLI_r[-1] |
| 27 | +GLI_length = len(GLI_r) |
| 28 | + |
| 29 | +RL_r = RL(0.5, lambda x: np.sqrt(x), 0, 1, test_N) |
| 30 | +RL_result = RL_r[-1] |
| 31 | +RL_length = len(RL_r) |
| 32 | + |
| 33 | +# --- Exponential function --- |
| 34 | +alpha_exp = 0.5 |
| 35 | +groundtruth_exp = MittagLeffler(1, 1 - 0.5, np.array([1]))[0] |
| 36 | + |
| 37 | +# --- Sine function --- |
| 38 | +alpha_sin = 0.5 |
| 39 | +groundtruth_sin = np.sin(1 + alpha_sin * np.pi / 2) |
| 40 | + |
| 41 | +####################### |
| 42 | +# GLpoint |
| 43 | +####################### |
| 44 | + |
| 45 | +def test_GLpoint_sqrt_accuracy(): |
| 46 | + assert abs(GLpoint(0.5, lambda x: x**0.5, 0.0, 1.0, 1024) - sqrtpi2) <= 1e-3 |
| 47 | + |
| 48 | +def test_GLpoint_accuracy_polynomial(): |
| 49 | + assert abs(GLpoint(0.5, lambda x: x**2 - 1, 0.0, 1.0, 1024) - truevaluepoly) <= 1e-3 |
| 50 | + |
| 51 | +def test_GLpoint_accuracy_exp(): |
| 52 | + val = GLpoint(alpha_exp, np.exp, 0, 1, 1024) |
| 53 | + assert abs(val - groundtruth_exp) < 1e-3 |
| 54 | + |
| 55 | +####################### |
| 56 | +# GL |
| 57 | +####################### |
| 58 | + |
| 59 | +def test_GL_accuracy_sqrt(): |
| 60 | + assert abs(GL_result - sqrtpi2) <= 1e-4 |
| 61 | + |
| 62 | +def test_GL_accuracy_polynomial(): |
| 63 | + assert abs(GL(0.5, lambda x: x**2 - 1, 0.0, 1.0, 1024)[-1] - truevaluepoly) <= 1e-3 |
| 64 | + |
| 65 | +def test_GL_accuracy_exp(): |
| 66 | + val = GL(alpha_exp, np.exp, 0, 1, 1024)[-1] |
| 67 | + assert abs(val - groundtruth_exp) < 1e-3 |
| 68 | + |
| 69 | +####################### |
| 70 | +# GLI |
| 71 | +####################### |
| 72 | + |
| 73 | +def test_GLI_accuracy_sqrt(): |
| 74 | + assert abs(GLI_result - sqrtpi2) <= 1e-4 |
| 75 | + |
| 76 | +def test_GLI_accuracy_polynomial(): |
| 77 | + assert abs(GLI(0.5, lambda x: x**2 - 1, 0.0, 1.0, 1024)[-1] - truevaluepoly) <= 6e-3 # low accuracy |
| 78 | + |
| 79 | +def test_GLI_accuracy_exp(): |
| 80 | + val = GLI(alpha_exp, np.exp, 0, 1, 1024)[-1] |
| 81 | + assert abs(val - groundtruth_exp) < 5e-3 # low accuracy |
| 82 | + |
| 83 | +####################### |
| 84 | +# RLpoint |
| 85 | +####################### |
| 86 | + |
| 87 | +def test_RLpoint_sqrt_accuracy(): |
| 88 | + assert abs(RLpoint(0.5, lambda x: x**0.5, 0.0, 1.0, 1024) - sqrtpi2) <= 1e-3 |
| 89 | + |
| 90 | +def test_RLpoint_accuracy_polynomial(): |
| 91 | + assert abs(RLpoint(0.5, lambda x: x**2 - 1, 0.0, 1.0, 1024) - truevaluepoly) <= 1e-2 |
| 92 | + |
| 93 | +def test_RLpoint_accuracy_exp(): |
| 94 | + val = RLpoint(alpha_exp, np.exp, 0, 1, 1024) |
| 95 | + assert abs(val - groundtruth_exp) < 1e-3 |
| 96 | + |
| 97 | +####################### |
| 98 | +# RL |
| 99 | +####################### |
| 100 | + |
| 101 | +def test_RL_accuracy_sqrt(): |
| 102 | + assert abs(RL_result - sqrtpi2) <= 1e-4 |
| 103 | + |
| 104 | +def test_RL_accuracy_polynomial(): |
| 105 | + assert abs(RL(0.5, lambda x: x**2 - 1, 0.0, 1.0, 1024)[-1] - truevaluepoly) <= 1e-3 |
| 106 | + |
| 107 | +def test_RL_accuracy_exp(): |
| 108 | + val = RL(alpha_exp, np.exp, 0, 1, 1024)[-1] |
| 109 | + assert abs(val - groundtruth_exp) < 1e-3 |
| 110 | + |
| 111 | +####################### |
| 112 | +# Caputo 1p |
| 113 | +####################### |
| 114 | + |
| 115 | +def test_CaputoL1point_accuracy_sqrt(): |
| 116 | + assert abs(CaputoL1point(0.5, lambda x: x**0.5, 0, 1.0, 1024) - sqrtpi2) <= 1e-2 |
| 117 | + |
| 118 | +def test_CaputoL1point_accuracy_polynomial(): |
| 119 | + assert abs(CaputoL1point(0.5, lambda x: x**2 - 1, 0, 1.0, 1024) - truevaluepoly_caputo) <= 1e-3 |
| 120 | + |
| 121 | +def test_CaputoL1point_accuracy_exp(): |
| 122 | + val = CaputoL1point(alpha_exp, np.exp, 0, 1, 1024) |
| 123 | + assert abs(val - groundtruth_exp) < 0.6 # really bad accuracy |
| 124 | + |
| 125 | +####################### |
| 126 | +# Caputo 2p |
| 127 | +####################### |
| 128 | + |
| 129 | +def test_CaputoL2point_accuracy_polynomial(): |
| 130 | + assert abs(CaputoL2point(1.5, lambda x: x**2, 0, 1.0, 1024) - truevaluepoly_caputo_higher) <= 1e-1 |
| 131 | + |
| 132 | +####################### |
| 133 | +# Caputo 2pC |
| 134 | +####################### |
| 135 | + |
| 136 | +def test_CaputoL2Cpoint_accuracy_polynomial_higher(): |
| 137 | + assert abs(CaputoL2Cpoint(1.5, lambda x: x**2, 0, 1.0, 1024) - truevaluepoly_caputo_higher) <= 1e-1 |
| 138 | + |
| 139 | +def test_CaputoL2Cpoint_accuracy_polynomial(): |
| 140 | + assert abs(CaputoL2Cpoint(0.5, lambda x: x**2, 0, 1.0, 1024) - truevaluepoly_caputo) <= 1e-3 |
| 141 | + |
| 142 | +def test_CaputoL2Cpoint_accuracy_exp(): |
| 143 | + val = CaputoL2Cpoint(alpha_exp, np.exp, 0, 1, 1024) |
| 144 | + assert abs(val - groundtruth_exp) < 2 # unusable |
| 145 | + |
| 146 | +####################### |
| 147 | +# General algorithm output checks |
| 148 | +####################### |
| 149 | + |
| 150 | +def test_GL_result_length(): |
| 151 | + assert GL_length == test_N |
| 152 | + |
| 153 | +def test_GLI_result_length(): |
| 154 | + assert GLI_length == test_N |
| 155 | + |
| 156 | +def test_RL_result_length(): |
| 157 | + assert RL_length == test_N |
| 158 | + |
| 159 | +def test_RL_matrix_shape(): |
| 160 | + assert np.shape(RLmatrix(0.4, test_N)) == (test_N, test_N) |
| 161 | + |
| 162 | +def test_GL_binomial_coefficient_array_size(): |
| 163 | + assert len(GLcoeffs(0.5, size_coefficient_array)) - 1 == size_coefficient_array |
| 164 | + |
| 165 | +# Optional: Run doctest if called directly |
| 166 | +if __name__ == "__main__": |
| 167 | + import doctest |
| 168 | + doctest.testmod() |
0 commit comments