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
Package registry
Model registry
Operate
Environments
Terraform modules
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
Christoph Alt
pystencils
Commits
f266bd7d
Commit
f266bd7d
authored
2 years ago
by
Frederik Hennig
Committed by
Markus Holzer
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix: `recursive_collect` now fails silently
parent
205d0a39
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pystencils/sympyextensions.py
+13
-3
13 additions, 3 deletions
pystencils/sympyextensions.py
with
13 additions
and
3 deletions
pystencils/sympyextensions.py
+
13
−
3
View file @
f266bd7d
...
@@ -6,6 +6,7 @@ from functools import partial, reduce
...
@@ -6,6 +6,7 @@ from functools import partial, reduce
from
typing
import
Callable
,
Dict
,
Iterable
,
List
,
Optional
,
Sequence
,
Tuple
,
TypeVar
,
Union
from
typing
import
Callable
,
Dict
,
Iterable
,
List
,
Optional
,
Sequence
,
Tuple
,
TypeVar
,
Union
import
sympy
as
sp
import
sympy
as
sp
from
sympy
import
PolynomialError
from
sympy.functions
import
Abs
from
sympy.functions
import
Abs
from
sympy.core.numbers
import
Zero
from
sympy.core.numbers
import
Zero
...
@@ -442,11 +443,14 @@ def extract_most_common_factor(term):
...
@@ -442,11 +443,14 @@ def extract_most_common_factor(term):
def
recursive_collect
(
expr
,
symbols
,
order_by_occurences
=
False
):
def
recursive_collect
(
expr
,
symbols
,
order_by_occurences
=
False
):
"""
Applies sympy.collect recursively for a list of symbols, collecting symbol 2 in the coefficients of symbol 1,
"""
Applies sympy.collect recursively for a list of symbols, collecting symbol 2 in the coefficients of symbol 1,
and so on.
and so on.
``expr`` must be rewritable as a polynomial in the given ``symbols``.
It it is not, ``recursive_collect`` will fail quietly, returning the original expression.
Args:
Args:
expr: A sympy expression
expr: A sympy expression
.
symbols: A sequence of symbols
symbols: A sequence of symbols
order_by_occurences: If True, during recursive descent, always collect the symbol occuring
order_by_occurences: If True, during recursive descent, always collect the symbol occuring
most often in the expression.
most often in the expression.
...
@@ -457,7 +461,13 @@ def recursive_collect(expr, symbols, order_by_occurences=False):
...
@@ -457,7 +461,13 @@ def recursive_collect(expr, symbols, order_by_occurences=False):
if
len
(
symbols
)
==
0
:
if
len
(
symbols
)
==
0
:
return
expr
return
expr
symbol
=
symbols
[
0
]
symbol
=
symbols
[
0
]
collected_poly
=
sp
.
Poly
(
expr
.
collect
(
symbol
),
symbol
)
collected
=
expr
.
collect
(
symbol
)
try
:
collected_poly
=
sp
.
Poly
(
collected
,
symbol
)
except
PolynomialError
:
return
expr
coeffs
=
collected_poly
.
all_coeffs
()[::
-
1
]
coeffs
=
collected_poly
.
all_coeffs
()[::
-
1
]
rec_sum
=
sum
(
symbol
**
i
*
recursive_collect
(
c
,
symbols
[
1
:],
order_by_occurences
)
for
i
,
c
in
enumerate
(
coeffs
))
rec_sum
=
sum
(
symbol
**
i
*
recursive_collect
(
c
,
symbols
[
1
:],
order_by_occurences
)
for
i
,
c
in
enumerate
(
coeffs
))
return
rec_sum
return
rec_sum
...
...
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