Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Added sympy symbolic method for manifolds.
Browse files Browse the repository at this point in the history
The passed tests for coord_func_sympy.py are 389/411.
This is due to the fact that generic fuction are not defined for sympy.

Tests also failed in src/sage/manifolds/differentiable and src/sage/manifolds/coord_func_generic.py
  • Loading branch information
man74cio committed Apr 12, 2017
1 parent fd5f71a commit b494a4f
Show file tree
Hide file tree
Showing 7 changed files with 2,049 additions and 117 deletions.
25 changes: 17 additions & 8 deletions src/sage/manifolds/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class Chart(UniqueRepresentation, SageObject):
manifolds over `\RR`.
"""
def __init__(self, domain, coordinates='', names=None):
def __init__(self, domain, coordinates='', names=None, symb_method='sage'):
r"""
Construct a chart.
Expand All @@ -264,6 +264,8 @@ def __init__(self, domain, coordinates='', names=None):
coordinates = coordinates[:-1]
self._manifold = domain.manifold()
self._domain = domain
self._symb_method = symb_method

# Treatment of the coordinates:
if ' ' in coordinates:
coord_list = coordinates.split()
Expand Down Expand Up @@ -323,6 +325,7 @@ def __init__(self, domain, coordinates='', names=None):
# dom is an open set
dom._one_scalar_field._express[self] = self.function_ring().one()


def _init_coordinates(self, coord_list):
r"""
Initialization of the coordinates as symbolic variables.
Expand Down Expand Up @@ -838,7 +841,7 @@ def transition_map(self, other, transformations, intersection_name=None,
transformations = [transformations]
return CoordChange(chart1, chart2, *transformations)

def function_ring(self):
def function_ring(self,symb_method=None):
"""
Return the ring of coordinate functions on ``self``.
Expand All @@ -849,8 +852,15 @@ def function_ring(self):
sage: X.function_ring()
Ring of coordinate functions on Chart (M, (x, y))
"""
from sage.manifolds.coord_func_symb import CoordFunctionSymbRing
return CoordFunctionSymbRing(self)
if symb_method is None :
symb_method = self._symb_method

if symb_method == 'sage' :
from sage.manifolds.coord_func_symb import CoordFunctionSymbRing
return CoordFunctionSymbRing(self)
elif symb_method == 'sympy':
from sage.manifolds.coord_func_sympy import CoordFunctionSympyRing
return CoordFunctionSympyRing(self)

def function(self, expression):
r"""
Expand Down Expand Up @@ -915,7 +925,7 @@ def function(self, expression):
raise NotImplementedError("numerical coordinate function not " +
"implemented yet")
else:
return self.function_ring()(expression)
return self.function_ring(self._symb_method)(expression)

def zero_function(self):
r"""
Expand Down Expand Up @@ -1314,7 +1324,7 @@ class RealChart(Chart):
:meth:`plot`.
"""
def __init__(self, domain, coordinates='', names=None):
def __init__(self, domain, coordinates='', names=None, symb_method='sage'):
r"""
Construct a chart on a real topological manifold.
Expand All @@ -1332,7 +1342,7 @@ def __init__(self, domain, coordinates='', names=None):
sage: TestSuite(X).run()
"""
Chart.__init__(self, domain, coordinates=coordinates, names=names)
Chart.__init__(self, domain, coordinates=coordinates, names=names, symb_method=symb_method)

def _init_coordinates(self, coord_list):
r"""
Expand Down Expand Up @@ -3008,4 +3018,3 @@ def display(self):
return FormattedExpansion(rtxt, rlatex)

disp = display

1 change: 0 additions & 1 deletion src/sage/manifolds/coord_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -1448,4 +1448,3 @@ def simple_determinant(aa):
J = self.jacobian()
J = [[J[i,j] for i in range(self._nc)] for j in range(self._nc)]
return simple_determinant(J)

267 changes: 267 additions & 0 deletions src/sage/manifolds/coord_func_generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
r"""
Coordinate Functions
In the context of a topological manifold `M` over a topological field `K`,
a *coordinate function* is a function from a chart codomain
to `K`. In other words, a coordinate function is a `K`-valued function of
the coordinates associated to some chart.
More precisely, let `(U, \varphi)` be a chart on `M`, i.e. `U` is an open
subset of `M` and `\varphi: U \to V \subset K^n` is a homeomorphism
from `U` to an open subset `V` of `K^n`. A *coordinate function associated
to the chart* `(U, \varphi)` is a function
.. MATH::
\begin{array}{cccc}
f:& V\subset K^n & \longrightarrow & K \\
& (x^1, \ldots, x^n) & \longmapsto & f(x^1, \ldots, x^n)
\end{array}
Coordinate functions are implemented by derived classes of the abstract base
class :class:`CoordFunction`.
The class :class:`MultiCoordFunction` implements `K^m`-valued functions of
the coordinates of a chart, with `m` a positive integer.
AUTHORS:
- Eric Gourgoulhon, Michal Bejger (2013-2015) : initial version
- Travis Scrimshaw (2016) : make :class:`CoordFunction` inheritate from
:class:`~sage.structure.element.AlgebraElement`
"""
#*****************************************************************************
# Copyright (C) 2015 Eric Gourgoulhon <eric.gourgoulhon@obspm.fr>
# Copyright (C) 2015 Michal Bejger <bejger@camk.edu.pl>
# Copyright (C) 2016 Travis Scrimshaw <tscrimsh@umn.edu>
#
# 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/
#*****************************************************************************

from sage.misc.abstract_method import abstract_method
from sage.manifolds.coord_func import CoordFunction
from sage.manifolds.utilities import ExpressionNice

class CoordFunctionGeneric(CoordFunction):
r"""
Abstract base class for coordinate functions.
If `(U, \varphi)` is a chart on a topological manifold `M` of
dimension `n` over a topological field `K`, a *coordinate function*
associated to `(U, \varphi)` is a map `f: V \subset K^n \to K`, where
`V` is the codomain of `\varphi`. In other words, `f` is a `K`-valued
function of the coordinates associated to the chart `(U, \varphi)`.
The class :class:`CoordFunction` is an abstract one. Specific
coordinate functions must be implemented by derived classes, like
:class:`~sage.manifolds.coord_func_symb.CoordFunctionSymb` for
symbolic coordinate functions.
INPUT:
- ``parent`` -- the algebra of coordinate functions on a given chart
"""
def __init__(self, parent):
r"""
Initialize ``self``.
TEST::
sage: M = Manifold(2, 'M', structure='topological')
sage: X.<x,y> = M.chart()
sage: f = CoordFunctionGeneric(X.function_ring())
"""
CoordFunction.__init__(self, parent)

# symbolic expression enforced :
self._express = None

# Derived quantities:
self._der = None # list of partial derivatives (to be set by diff()
# and unset by del_derived())


# ----------------------------------------------------------------
# Methods that do not need to be re-implemented by derived classes
# but can be overloaded
# ----------------------------------------------------------------
def _repr_(self):
r"""
String representation of ``self``.
TESTS::
sage: M = Manifold(2, 'M', structure='topological')
sage: X.<x,y> = M.chart()
sage: f = CoordFunctionGeneric(X.function_ring())
sage: f = X.function(1+x*y)
sage: f._repr_()
'x*y + 1'
sage: repr(f) # indirect doctest
'x*y + 1'
sage: f # indirect doctest
x*y + 1
"""
return str(self._express)

def _latex_(self):
r"""
LaTeX representation of ``self``.
TESTS::
sage: M = Manifold(2, 'M', structure='topological')
sage: X.<x,y> = M.chart()
sage: f = X.function(cos(x*y/2))
sage: f._latex_()
\cos\left(\frac{1}{2} \, x y\right)
sage: latex(f) # indirect doctest
\cos\left(\frac{1}{2} \, x y\right)
"""
from sage.misc.latex import latex
return latex(self._express)


def display(self):
r"""
Display ``self`` in arrow notation.
The output is either text-formatted (console mode) or
LaTeX-formatted (notebook mode).
EXAMPLES:
Coordinate function on a 2-dimensional manifold::
sage: M = Manifold(2, 'M', structure='topological')
sage: X.<x,y> = M.chart()
sage: f = X.function(cos(x*y/2))
sage: f.display()
(x, y) |--> cos(1/2*x*y)
sage: latex(f.display())
\left(x, y\right) \mapsto \cos\left(\frac{1}{2} \, x y\right)
A shortcut is ``disp()``::
sage: f.disp()
(x, y) |--> cos(1/2*x*y)
Display of the zero function::
sage: X.zero_function().display()
(x, y) |--> 0
"""
from sage.tensor.modules.format_utilities import FormattedExpansion
from sage.misc.latex import latex
resu_txt = str(self.parent()._chart[:]) + ' |--> ' + \
str(ExpressionNice(self._express))
resu_latex = latex(self.parent()._chart[:]) + r' \mapsto' + \
latex(ExpressionNice(self._express))
return FormattedExpansion(resu_txt, resu_latex)

disp = display


def expr(self):
r"""
Return the symbolic expression representing the image of ``self``.
OUTPUT:
- :class:`symbolic expression <sage.symbolic.expression.Expression>`
involving the chart coordinates
EXAMPLES:
Coordinate function of a 2-dimensional manifold::
sage: M = Manifold(2, 'M', structure='topological')
sage: X.<x,y> = M.chart()
sage: f = X.function(x^2+3*y+1)
sage: f.expr()
x^2 + 3*y + 1
sage: type(f.expr())
<type 'sage.symbolic.expression.Expression'>
For a symbolic coordinate function, one shall always have::
sage: bool( f.expr() == f(*(f.chart()[:])) )
True
The method :meth:`expr` is useful for accessing to all the
symbolic expression functionalities in Sage; for instance::
sage: var('a')
a
sage: f = X.function(a*x*y); f.display()
(x, y) |--> a*x*y
sage: f.expr()
a*x*y
sage: f.expr().subs(a=2)
2*x*y
Note that for substituting the value of a coordinate, the function
call can be used as well::
sage: f(x,3)
3*a*x
sage: bool( f(x,3) == f.expr().subs(y=3) )
True
"""
return self._express

# --------------------------------------------
# Methods to be implemented by derived classes
# --------------------------------------------

@abstract_method
def _simplify(self,expression):
r"""
Simplification chain.
TESTS:
This method must be implemented by derived classes; it is not
implemented here::
sage: M = Manifold(2, 'M', structure='topological')
sage: X.<x,y> = M.chart()
sage: from sage.manifolds.coord_func_generic import CoordFunctionGeneric
sage: f = CoordFunctionGeneric(X.function_ring())
sage: f._simplify()
Traceback (most recent call last):
...
NotImplementedError: <abstract method _simplify at 0x...>
"""

@abstract_method
def _symb_method(self,expression):
r"""
Method to select the symbolic representation method.
TESTS:
This method must be implemented by derived classes; it is not
implemented here::
sage: M = Manifold(2, 'M', structure='topological')
sage: X.<x,y> = M.chart()
sage: from sage.manifolds.coord_func_generic import CoordFunctionGeneric
sage: f = CoordFunctionGeneric(X.function_ring())
sage: f._symb_method()
Traceback (most recent call last):
...
NotImplementedError: <abstract method _symb_method at 0x...>
"""
Loading

0 comments on commit b494a4f

Please sign in to comment.