Coverage for local/lib/python2.7/site-packages/sage/manifolds/calculus_method.py : 98%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" Calculus method control
The class :class:`CalculusMethod` governs the calculus methods (symbolic and numerical) to be used for coordinate computations on manifolds.
AUTHORS:
- Marco Mancini, (2017): initial version
"""
#****************************************************************************** # Copyright (C) 2015 Marco Mancini <marco.mancini@obspm.fr> # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #****************************************************************************** simplify_chain_generic, simplify_chain_real_sympy, simplify_chain_generic_sympy,)
# Conversion functions r""" Convert an expression from ``SR`` to ``sympy``.
In the case that the expression is already a ``sympy`` expression, then it is returned
INPUT:
- ``expression`` -- ``SR`` or ``sympy`` symbolic expression
OUTPUT:
- ``expression`` -- ``sympy`` symbolic expression
EXAMPLES::
sage: from sage.manifolds.calculus_method import _SR_to_Sympy sage: a = x^2+sin(x)^2; a x^2 + sin(x)^2 sage: type(a) <type 'sage.symbolic.expression.Expression'> sage: b = _SR_to_Sympy(a); b x**2 + sin(x)**2 sage: type(b) <class 'sympy.core.add.Add'>
The conversion of a ``sympy`` expression is not converted::
sage: _SR_to_Sympy(b) == b True
""" # Nothing to do if expression is already a SymPy object:
r""" Convert an expression from ``sympy`` to ``SR``.
In the case that the expression is already a ``SR`` expression, then it is returned.
INPUT:
- ``expression`` -- ``sympy`` symbolic expression
OUTPUT:
- ``expression`` -- SR or ``sympy`` symbolic expression
EXAMPLES::
sage: from sage.manifolds.calculus_method import _Sympy_to_SR, _SR_to_Sympy sage: a = x^2+sin(x)^2 sage: type(a) <type 'sage.symbolic.expression.Expression'> sage: b = _SR_to_Sympy(a); b x**2 + sin(x)**2 sage: type(b) <class 'sympy.core.add.Add'> sage: _Sympy_to_SR(b) == a x^2 + sin(x)^2 == x^2 + sin(x)^2 sage: bool(_Sympy_to_SR(b) == a) True
""" # If SR cannot transform a sympy expression is because it is a # sympy abstract function. # As all sage objects have a ._sage_ operator, they have to be # catched return a
r""" Control of calculus methods used on coordinate charts of manifolds.
This class stores the possible calculus methods and permits to select some basic operations working on them. For the moment, only two calculus methods are implemented:
- Sage's symbolic engine (Pynac/Maxima), implemented via the symbolic ring ``SR`` - SymPy engine, denoted ``sympy`` hereafter
INPUT:
- ``current`` -- (default: ``None``) current symbolic method
- ``base_field_type`` -- (default: ``'real'``) base field type of the manifold (cf. :meth:`~sage.manifolds.manifold.TopologicalManifold.base_field_type`)
EXAMPLES::
sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod()
In the display, the current method is pointed out by `*`::
sage: calc_meth Possible calculus methods: - SR (*) (default) - sympy
The current method is changed by :meth:`set`::
sage: calc_meth.set('sympy') sage: calc_meth Possible calculus methods: - SR (default) - sympy (*) sage: calc_meth.reset() sage: calc_meth Possible calculus methods: - SR (*) (default) - sympy
"""
r""" Initializes ``self``.
TESTS::
sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod(base_field_type='complex') sage: calc_meth Possible calculus methods: - SR (*) (default) - sympy
""" # initialization of the dict for simplifications else:
r""" Apply some simplification chain to a given symbolic expression.
INPUT:
- ``expression`` -- symbolic expression to simplify
- ``method`` -- (default: ``None``) string defining the calculus method to use; must be one of
- ``'SR'``: Sage's default symbolic engine (Symbolic Ring) - ``'sympy'``: SymPy - ``None``: the current calculus method of ``self`` is used.
EXAMPLES::
sage: M = Manifold(2, 'M', field='complex', structure='topological') sage: X = M.chart('x y'); sage: f = x^2+sin(x)^2+cos(x)^2 sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod(base_field_type='real') sage: calc_meth.simplify(f) x^2 + 1
Methods cannot be mixed::
sage: calc_meth.set('sympy') sage: calc_meth.simplify(f) Traceback (most recent call last): ... AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'combsimp'
In the present case, one should either transform ``f`` to a SymPy object::
sage: calc_meth.simplify(f._sympy_()) x**2 + 1
or force the calculus method to be ``'SR'``::
sage: calc_meth.simplify(f, method='SR') x^2 + 1
"""
r""" Check if an expression is trivially equal to zero without any simplification.
INPUT:
- ``expression`` -- expression
- ``method`` -- (default: ``None``) string defining the calculus method to use; if ``None`` the current calculus method of ``self`` is used.
OUTPUT:
- ``True`` is expression is trivially zero, ``False`` elsewhere.
EXAMPLES::
sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod(base_field_type='real') sage: f = sin(x) - sin(x) sage: calc_meth.is_trivial_zero(f) True sage: calc_meth.is_trivial_zero(f._sympy_(), method='sympy') True
::
sage: f = sin(x)^2 + cos(x)^2 - 1 sage: calc_meth.is_trivial_zero(f) False sage: calc_meth.is_trivial_zero(f._sympy_(), method='sympy') False
""" # we have to test SymPy's is_zero because it could be 'NoneType'
r""" Set the current calculus method.
- ``method`` -- string defining the calculus method
EXAMPLES::
sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod(base_field_type='complex') sage: calc_meth.set('sympy') sage: calc_meth Possible calculus methods: - SR (default) - sympy (*) sage: calc_meth.reset()
"""
r""" Test if a calculus method is available.
INPUT:
- ``method`` -- string defining the calculus method
TESTS::
sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod(base_field_type='complex') sage: calc_meth._test('lala') Traceback (most recent call last): ... NotImplementedError: method lala not implemented yet
""" "implemented yet")
r""" Set the current symbolic method to default one.
EXAMPLES::
sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod(base_field_type='complex') sage: calc_meth.set('sympy') sage: calc_meth Possible calculus methods: - SR (default) - sympy (*) sage: calc_meth.reset() sage: calc_meth Possible calculus methods: - SR (*) (default) - sympy
"""
r""" String representation of the object.
TESTS::
sage: from sage.manifolds.calculus_method import CalculusMethod sage: calc_meth = CalculusMethod(base_field_type='complex') sage: calc_meth._repr_() 'Possible calculus methods:\n - SR (*) (default)\n - sympy'
""" |