- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking βSign up for GitHubβ, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add typing to chart and calculus method #35188
Draft
tobiasdiez
wants to merge
8
commits into
sagemath:develop
Choose a base branch
from
tobiasdiez:typing_charts
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
β114
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4d8f6dc
Add typing to chart and calculus method
tobiasdiez f8d4648
Update python version
tobiasdiez a30565a
Minor fixes
tobiasdiez c5e1080
Fix import
tobiasdiez 820440f
Fix support for python 3.8
tobiasdiez b5886e1
add debug info
tobiasdiez 71bdece
add more logging
tobiasdiez c930dca
Merge branch 'develop' into typing_charts
tobiasdiez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,18 +20,27 @@ | |
# the License, or (at your option) any later version. | ||
# https://www.gnu.org/licenses/ | ||
# ***************************************************************************** | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Callable, Literal, Optional, Union | ||
|
||
import sympy | ||
|
||
from sage.manifolds.utilities import ( | ||
simplify_chain_generic, | ||
simplify_chain_generic_sympy, | ||
simplify_chain_real, | ||
simplify_chain_real_sympy, | ||
) | ||
from sage.misc.latex import latex | ||
from sage.structure.sage_object import SageObject | ||
from sage.symbolic.ring import SR | ||
from sage.manifolds.utilities import (simplify_chain_real, | ||
simplify_chain_generic, | ||
simplify_chain_real_sympy, | ||
simplify_chain_generic_sympy,) | ||
from sage.misc.latex import latex | ||
import sympy | ||
|
||
if TYPE_CHECKING: | ||
from sage.symbolic.expression import Expression | ||
|
||
# Conversion functions | ||
def _SR_to_Sympy(expression): | ||
def _SR_to_Sympy(expression: Expression) -> Expression: | ||
r""" | ||
Convert an expression from ``SR`` to ``sympy``. | ||
|
||
|
@@ -70,7 +79,7 @@ | |
return SR(expression)._sympy_() | ||
|
||
|
||
def _Sympy_to_SR(expression): | ||
def _Sympy_to_SR(expression: Expression) -> Expression: | ||
r""" | ||
Convert an expression from ``sympy`` to ``SR``. | ||
|
||
|
@@ -113,6 +122,9 @@ | |
return a | ||
|
||
|
||
CalculusMethodName = Literal["SR", "sympy"] | ||
|
||
|
||
class CalculusMethod(SageObject): | ||
r""" | ||
Control of calculus backends used on coordinate charts of manifolds. | ||
|
@@ -173,11 +185,16 @@ | |
introducing a new simplification algorithm. | ||
|
||
""" | ||
_default = 'SR' # default calculus method | ||
_methods = ('SR', 'sympy') # implemented methods | ||
_tranf = {'SR': _Sympy_to_SR, 'sympy': _SR_to_Sympy} # translators | ||
|
||
def __init__(self, current=None, base_field_type='real'): | ||
# default calculus method | ||
_default: CalculusMethodName = "SR" | ||
# implemented methods | ||
_methods: tuple[CalculusMethodName, ...] = ("SR", "sympy") | ||
# translators | ||
_tranf = {"SR": _Sympy_to_SR, "sympy": _SR_to_Sympy} | ||
_simplify_dict: dict[CalculusMethodName, Callable[[Expression], Expression]] | ||
_current: CalculusMethodName | ||
|
||
def __init__(self, current: Optional[CalculusMethodName] = None, base_field_type: str = "real"): | ||
r""" | ||
Initializes ``self``. | ||
|
||
|
@@ -204,7 +221,9 @@ | |
self._simplify_dict_default = self._simplify_dict.copy() | ||
self._latex_dict = {'sympy': sympy.latex, 'SR': latex} | ||
|
||
def simplify(self, expression, method=None): | ||
def simplify( | ||
self, expression: Expression, method: Optional[CalculusMethodName] = None | ||
) -> Expression: | ||
r""" | ||
Apply the simplifying function associated with a given calculus method | ||
to a symbolic expression. | ||
|
@@ -275,7 +294,9 @@ | |
method = self._current | ||
return self._simplify_dict[method](expression) | ||
|
||
def is_trivial_zero(self, expression, method=None): | ||
def is_trivial_zero( | ||
self, expression: Expression, method: Optional[CalculusMethodName] = None | ||
) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here, the type declaration |
||
r""" | ||
Check if an expression is trivially equal to zero without any | ||
simplification. | ||
|
@@ -320,7 +341,7 @@ | |
return True | ||
return False | ||
|
||
def set(self, method): | ||
def set(self, method: CalculusMethodName): | ||
r""" | ||
Set the currently active calculus method. | ||
|
||
|
@@ -350,7 +371,7 @@ | |
"implemented") | ||
self._current = method | ||
|
||
def current(self): | ||
def current(self) -> CalculusMethodName: | ||
r""" | ||
Return the active calculus method as a string. | ||
|
||
|
@@ -377,7 +398,11 @@ | |
""" | ||
return self._current | ||
|
||
def set_simplify_function(self, simplifying_func, method=None): | ||
def set_simplify_function( | ||
self, | ||
simplifying_func: Union[Callable[[Expression], Expression], Literal["default"]], | ||
method: Optional[CalculusMethodName] = None, | ||
): | ||
r""" | ||
Set the simplifying function associated to a given calculus method. | ||
|
||
|
@@ -447,7 +472,9 @@ | |
else: | ||
self._simplify_dict[method] = simplifying_func | ||
|
||
def simplify_function(self, method=None): | ||
def simplify_function( | ||
self, method: Optional[CalculusMethodName] = None | ||
) -> Callable[[Expression], Expression]: | ||
r""" | ||
Return the simplifying function associated to a given calculus method. | ||
|
||
|
@@ -547,7 +574,7 @@ | |
""" | ||
self._current = self._default | ||
|
||
def _repr_(self): | ||
def _repr_(self) -> str: | ||
r""" | ||
String representation of the object. | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The declared return type is wrong. It returns a sympy object, not an Expression.
And also the declared input is wrong because this function first marshals the input into SR. It is allowed to be all kinds of things.