From 807ee8b1fd393b05bdd814760f34003ce9d3de41 Mon Sep 17 00:00:00 2001 From: Martin Bauer <martin.bauer@fau.de> Date: Tue, 7 Aug 2018 14:15:30 +0200 Subject: [PATCH] Generalized N phases model: bulk functions f1 and f2 can be changed from outside --- sympyextensions.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sympyextensions.py b/sympyextensions.py index 71fc975a8..51a2f77e2 100644 --- a/sympyextensions.py +++ b/sympyextensions.py @@ -55,6 +55,30 @@ def kronecker_delta(*args): return 1 +def tanh_step_function_approximation(x, step_location, kind='right', steepness=0.0001): + """Approximation of step function by a tanh function + + >>> tanh_step_function_approximation(1.2, step_location=1.0, kind='right') + 1.00000000000000 + >>> tanh_step_function_approximation(0.9, step_location=1.0, kind='right') + 0 + >>> tanh_step_function_approximation(1.1, step_location=1.0, kind='left') + 0 + >>> tanh_step_function_approximation(0.9, step_location=1.0, kind='left') + 1.00000000000000 + >>> tanh_step_function_approximation(0.5, step_location=(0, 1), kind='middle') + 1 + """ + if kind == 'left': + return (1 - sp.tanh((x - step_location) / steepness)) / 2 + elif kind == 'right': + return (1 + sp.tanh((x - step_location) / steepness)) / 2 + elif kind == 'middle': + x1, x2 = step_location + return 1 - (tanh_step_function_approximation(x, x1, 'left', steepness) + \ + tanh_step_function_approximation(x, x2, 'right', steepness)) + + def multidimensional_sum(i, dim): """Multidimensional summation -- GitLab