Skip to content

Commit

Permalink
Add flag to ignore linearity errors (#142)
Browse files Browse the repository at this point in the history
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ü <[email protected]>
  • Loading branch information
e-moral-sanchez and yguclu authored Dec 4, 2023
1 parent a74af02 commit 75df221
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 20 additions & 10 deletions sympde/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 75df221

Please sign in to comment.