Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Stephan Seitz
pystencils
Commits
d8e498fa
Commit
d8e498fa
authored
Apr 16, 2019
by
Martin Bauer
Browse files
Workaround for sympy bug in placeholder_function
see
https://github.com/sympy/sympy/issues/16662
parent
27a131fb
Changes
2
Hide whitespace changes
Inline
Side-by-side
pystencils/placeholder_function.py
View file @
d8e498fa
...
...
@@ -2,6 +2,7 @@ import sympy as sp
from
typing
import
List
from
pystencils.assignment
import
Assignment
from
pystencils.astnodes
import
Node
from
pystencils.sympyextensions
import
is_constant
from
pystencils.transformations
import
generic_visit
...
...
@@ -37,11 +38,11 @@ def to_placeholder_function(expr, name):
assignments
=
[
Assignment
(
sp
.
Symbol
(
name
),
expr
)]
assignments
+=
[
Assignment
(
symbol
,
derivative
)
for
symbol
,
derivative
in
zip
(
derivative_symbols
,
derivatives
)
if
not
derivative
.
is_constant
()]
if
not
is_constant
(
derivative
)]
def
fdiff
(
_
,
index
):
result
=
derivatives
[
index
-
1
]
return
result
if
result
.
is_constant
()
else
derivative_symbols
[
index
-
1
]
return
result
if
is_constant
(
result
)
else
derivative_symbols
[
index
-
1
]
func
=
type
(
name
,
(
sp
.
Function
,
PlaceholderFunction
),
{
'fdiff'
:
fdiff
,
...
...
pystencils/sympyextensions.py
View file @
d8e498fa
...
...
@@ -172,6 +172,13 @@ def fast_subs(expression: T, substitutions: Dict,
return
visit
(
expression
)
def
is_constant
(
expr
):
"""Simple version of checking if a sympy expression is constant.
Works also for piecewise defined functions - sympy's is_constant() has a problem there, see:
https://github.com/sympy/sympy/issues/16662
"""
return
len
(
expr
.
free_symbols
)
==
0
def
subs_additive
(
expr
:
sp
.
Expr
,
replacement
:
sp
.
Expr
,
subexpression
:
sp
.
Expr
,
required_match_replacement
:
Optional
[
Union
[
int
,
float
]]
=
0.5
,
required_match_original
:
Optional
[
Union
[
int
,
float
]]
=
None
)
->
sp
.
Expr
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment