From 75df221ca14ab209aab9c1e14d9e8296a81b9af0 Mon Sep 17 00:00:00 2001 From: e-moral-sanchez <88042165+e-moral-sanchez@users.noreply.github.com> Date: Mon, 4 Dec 2023 19:46:41 +0100 Subject: [PATCH] Add flag to ignore linearity errors (#142) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the flag `ignore_linearity_errors` to the constructors of `BilinearForm` and `LinearForm`. Such a flag converts the error of the linearity check into a warning. This is useful because the linearity check sometimes fails due to small roundoff errors. Update version to 0.18.2. --------- Co-authored-by: Yaman Güçlü --- pyproject.toml | 2 +- sympde/expr/expr.py | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fee5180..1a44d71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "sympde" -version = "0.18.1" +version = "0.18.2" description = "Symbolic calculus for partial differential equations (and variational forms)" readme = "README.rst" requires-python = ">= 3.8, < 3.12" diff --git a/sympde/expr/expr.py b/sympde/expr/expr.py index f0ae68f..bd87dfb 100644 --- a/sympde/expr/expr.py +++ b/sympde/expr/expr.py @@ -334,7 +334,7 @@ def space(self): class LinearForm(BasicForm): is_linear = True - def __new__(cls, arguments, expr, **options): + def __new__(cls, arguments, expr, ignore_linearity_errors=False, **options): # Trivial case: null expression if expr == 0: @@ -349,8 +349,13 @@ def __new__(cls, arguments, expr, **options): # Check linearity with respect to the given arguments if not is_linear_expression(expr, args): - msg = 'Expression is not linear w.r.t [{}]'.format(args) - raise UnconsistentLinearExpressionError(msg) + print(expr) + print(args) + msg = f'Expression is not linear w.r.t. [{args}]' + if ignore_linearity_errors: + print(msg) + else: + raise UnconsistentLinearExpressionError(msg) # Create new object of type LinearForm obj = Basic.__new__(cls, args, expr) @@ -414,7 +419,7 @@ class BilinearForm(BasicForm): is_bilinear = True _is_symmetric = None - def __new__(cls, arguments, expr, **options): + def __new__(cls, arguments, expr, ignore_linearity_errors=False, **options): # Trivial case: null expression if expr == 0: @@ -433,15 +438,20 @@ def __new__(cls, arguments, expr, **options): # Check linearity with respect to trial functions if not is_linear_expression(expr, trial_functions): - msg = ' Expression is not linear w.r.t trial functions {}'\ - .format(trial_functions) - raise UnconsistentLinearExpressionError(msg) + msg = f'Expression is not linear w.r.t. trial functions [{trial_functions}]' + if ignore_linearity_errors: + print(msg) + else: + raise UnconsistentLinearExpressionError(msg) + # Check linearity with respect to test functions if not is_linear_expression(expr, test_functions): - msg = ' Expression is not linear w.r.t test functions {}'\ - .format(test_functions) - raise UnconsistentLinearExpressionError(msg) + msg = f'Expression is not linear w.r.t. test functions [{test_functions}]' + if ignore_linearity_errors: + print(msg) + else: + raise UnconsistentLinearExpressionError(msg) # Create new object of type BilinearForm obj = Basic.__new__(cls, args, expr)