Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils
Commits
7c0fc3e8
Commit
7c0fc3e8
authored
6 years ago
by
Martin Bauer
Browse files
Options
Downloads
Patches
Plain Diff
pygrandchem: approximate divs & sqrts
parent
0d12a2ac
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fast_approximation.py
+15
-17
15 additions, 17 deletions
fast_approximation.py
fd/finitedifferences.py
+1
-1
1 addition, 1 deletion
fd/finitedifferences.py
with
16 additions
and
18 deletions
fast_approximation.py
+
15
−
17
View file @
7c0fc3e8
...
...
@@ -9,15 +9,28 @@ from pystencils.simp import AssignmentCollection
class
fast_division
(
sp
.
Function
):
nargs
=
(
2
,)
# noinspection PyPep8Naming
class
fast_sqrt
(
sp
.
Function
):
nargs
=
(
1
,
)
# noinspection PyPep8Naming
class
fast_inv_sqrt
(
sp
.
Function
):
nargs
=
(
1
,
)
def
_run
(
term
,
visitor
):
if
isinstance
(
term
,
AssignmentCollection
):
new_main_assignments
=
_run
(
term
.
main_assignments
,
visitor
)
new_subexpressions
=
_run
(
term
.
subexpressions
,
visitor
)
return
term
.
copy
(
new_main_assignments
,
new_subexpressions
)
elif
isinstance
(
term
,
list
):
return
[
_run
(
e
,
visitor
)
for
e
in
term
]
else
:
return
visitor
(
term
)
def
insert_fast_sqrts
(
term
:
Union
[
sp
.
Expr
,
List
[
sp
.
Expr
],
AssignmentCollection
]):
def
visit
(
expr
):
if
isinstance
(
expr
,
Node
):
...
...
@@ -31,15 +44,7 @@ def insert_fast_sqrts(term: Union[sp.Expr, List[sp.Expr], AssignmentCollection])
else
:
new_args
=
[
visit
(
a
)
for
a
in
expr
.
args
]
return
expr
.
func
(
*
new_args
)
if
new_args
else
expr
if
isinstance
(
term
,
AssignmentCollection
):
new_main_assignments
=
insert_fast_sqrts
(
term
.
main_assignments
)
new_subexpressions
=
insert_fast_sqrts
(
term
.
subexpressions
)
return
term
.
copy
(
new_main_assignments
,
new_subexpressions
)
elif
isinstance
(
term
,
list
):
return
[
insert_fast_sqrts
(
e
)
for
e
in
term
]
else
:
return
visit
(
term
)
return
_run
(
term
,
visit
)
def
insert_fast_divisions
(
term
:
Union
[
sp
.
Expr
,
List
[
sp
.
Expr
],
AssignmentCollection
]):
...
...
@@ -65,11 +70,4 @@ def insert_fast_divisions(term: Union[sp.Expr, List[sp.Expr], AssignmentCollecti
new_args
=
[
visit
(
a
)
for
a
in
expr
.
args
]
return
expr
.
func
(
*
new_args
)
if
new_args
else
expr
if
isinstance
(
term
,
AssignmentCollection
):
new_main_assignments
=
insert_fast_divisions
(
term
.
main_assignments
)
new_subexpressions
=
insert_fast_divisions
(
term
.
subexpressions
)
return
term
.
copy
(
new_main_assignments
,
new_subexpressions
)
elif
isinstance
(
term
,
list
):
return
[
insert_fast_divisions
(
e
)
for
e
in
term
]
else
:
return
visit
(
term
)
return
_run
(
term
,
visit
)
This diff is collapsed.
Click to expand it.
fd/finitedifferences.py
+
1
−
1
View file @
7c0fc3e8
...
...
@@ -135,7 +135,7 @@ class Discretization2ndOrder:
def
__call__
(
self
,
expr
):
if
isinstance
(
expr
,
list
):
return
[
self
(
e
)
for
e
in
expr
]
elif
isinstance
(
expr
,
sp
.
Matrix
):
elif
isinstance
(
expr
,
sp
.
Matrix
)
or
isinstance
(
expr
,
sp
.
ImmutableDenseMatrix
)
:
return
expr
.
applyfunc
(
self
.
__call__
)
elif
isinstance
(
expr
,
AssignmentCollection
):
return
expr
.
copy
(
main_assignments
=
[
e
for
e
in
expr
.
main_assignments
],
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment