Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modules/vector_callable_symbolic_dense.py
4057 views
1
"""
2
Vectors over callable symbolic rings
3
4
AUTHOR:
5
-- Jason Grout (2010)
6
7
EXAMPLES::
8
9
sage: f(r, theta, z) = (r*cos(theta), r*sin(theta), z)
10
sage: f.parent()
11
Vector space of dimension 3 over Callable function ring with arguments (r, theta, z)
12
sage: f
13
(r, theta, z) |--> (r*cos(theta), r*sin(theta), z)
14
sage: f[0]
15
(r, theta, z) |--> r*cos(theta)
16
sage: f+f
17
(r, theta, z) |--> (2*r*cos(theta), 2*r*sin(theta), 2*z)
18
sage: 3*f
19
(r, theta, z) |--> (3*r*cos(theta), 3*r*sin(theta), 3*z)
20
sage: f*f # dot product
21
(r, theta, z) |--> r^2*sin(theta)^2 + r^2*cos(theta)^2 + z^2
22
sage: f.diff()(0,1,2) # the matrix derivative
23
[cos(1) 0 0]
24
[sin(1) 0 0]
25
[ 0 0 1]
26
27
28
TESTS::
29
30
sage: f(u,v,w) = (2*u+v,u-w,w^2+u)
31
sage: loads(dumps(f)) == f
32
True
33
34
35
"""
36
37
#*****************************************************************************
38
# Copyright (C) 2010 Jason Grout <[email protected]>
39
#
40
# Distributed under the terms of the GNU General Public License (GPL)
41
#
42
# This code is distributed in the hope that it will be useful,
43
# but WITHOUT ANY WARRANTY; without even the implied warranty of
44
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45
# General Public License for more details.
46
#
47
# The full text of the GPL is available at:
48
#
49
# http://www.gnu.org/licenses/
50
#*****************************************************************************
51
52
import free_module_element
53
from sage.symbolic.ring import SR
54
55
56
class Vector_callable_symbolic_dense(free_module_element.FreeModuleElement_generic_dense):
57
58
59
60
def _repr_(self):
61
"""
62
Returns the string representation of the vector
63
64
EXAMPLES::
65
sage: f(u,v,w) = (2*u+v,u-w,w^2+u)
66
sage: f
67
(u, v, w) |--> (2*u + v, u - w, w^2 + u)
68
sage: r(t) = (cos(t), sin(t))
69
sage: r
70
t |--> (cos(t), sin(t))
71
"""
72
ring=self.base_ring()
73
args = ring.arguments()
74
repr_x=self.change_ring(SR)._repr_()
75
if len(args) == 1:
76
return "%s |--> %s" % (args[0], repr_x)
77
else:
78
args = ", ".join(map(str, args))
79
return "(%s) |--> %s" % (args, repr_x)
80
81
82
def _latex_(self):
83
"""
84
Returns the latex representation of the vector
85
86
EXAMPLES::
87
sage: f(u,v,w) = (2*u+v,u-w,w^2+u)
88
sage: f
89
(u, v, w) |--> (2*u + v, u - w, w^2 + u)
90
sage: latex(f)
91
\left( u, v, w \right) \ {\mapsto} \ \left(2 \, u + v,\,u - w,\,w^{2} + u\right)
92
sage: r(t) = (cos(t), sin(t))
93
sage: r
94
t |--> (cos(t), sin(t))
95
sage: latex(r)
96
t \ {\mapsto}\ \left(\cos\left(t\right),\,\sin\left(t\right)\right)
97
"""
98
from sage.misc.latex import latex
99
ring=self.base_ring()
100
args = ring.arguments()
101
args = [latex(arg) for arg in args]
102
latex_x = self.change_ring(SR)._latex_()
103
if len(args) == 1:
104
return r"%s \ {\mapsto}\ %s" % (args[0], latex_x)
105
else:
106
vars = ", ".join(args)
107
# the weird TeX is to workaround an apparent JsMath bug
108
return r"\left( %s \right) \ {\mapsto} \ %s" % (vars, latex_x)
109
110