Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modules/vector_symbolic_dense.py
4057 views
1
"""
2
Vectors over the symbolic ring.
3
4
Implements vectors over the symbolic ring.
5
6
AUTHORS:
7
8
- Robert Bradshaw (2011-05-25): Added more element-wise simplification methods
9
10
- Joris Vankerschaver (2011-05-15): Initial version
11
12
EXAMPLES::
13
14
sage: x, y = var('x, y')
15
sage: u = vector([sin(x)^2 + cos(x)^2, log(2*y) + log(3*y)]); u
16
(sin(x)^2 + cos(x)^2, log(2*y) + log(3*y))
17
sage: type(u)
18
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
19
sage: u.simplify_full()
20
(1, log(6) + 2*log(y))
21
22
TESTS:
23
24
Check that the outcome of arithmetic with symbolic vectors is again
25
a symbolic vector (#11549)::
26
27
sage: v = vector(SR, [1, 2])
28
sage: w = vector(SR, [sin(x), 0])
29
sage: type(v)
30
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
31
sage: type(w)
32
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
33
sage: type(v + w)
34
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
35
sage: type(-v)
36
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
37
sage: type(5*w)
38
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
39
40
Test pickling/unpickling::
41
42
sage: u = vector(SR, [sin(x^2)])
43
sage: loads(dumps(u)) == u
44
True
45
46
"""
47
48
#*****************************************************************************
49
# Copyright (C) 2011 Joris Vankerschaver ([email protected])
50
#
51
# Distributed under the terms of the GNU General Public License (GPL)
52
# as published by the Free Software Foundation; either version 2 of
53
# the License, or (at your option) any later version.
54
# http://www.gnu.org/licenses/
55
#*****************************************************************************
56
57
import free_module_element
58
from sage.symbolic.all import Expression
59
60
61
def apply_map(phi):
62
"""
63
Returns a function that applies phi to its argument.
64
65
EXAMPLES::
66
67
sage: from sage.modules.vector_symbolic_dense import apply_map
68
sage: v = vector([1,2,3])
69
sage: f = apply_map(lambda x: x+1)
70
sage: f(v)
71
(2, 3, 4)
72
73
"""
74
def apply(self, *args, **kwds):
75
"""
76
Generic function used to implement common symbolic operations
77
elementwise as methods of a vector.
78
79
EXAMPLES::
80
81
sage: var('x,y')
82
(x, y)
83
sage: v = vector([sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), factorial(x+1)/factorial(x)])
84
sage: v.simplify_trig()
85
(1, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
86
sage: v.simplify_radical()
87
(sin(x)^2 + cos(x)^2, log(x) + log(y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
88
sage: v.simplify_rational()
89
(sin(x)^2 + cos(x)^2, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
90
sage: v.simplify_factorial()
91
(sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), x + 1)
92
sage: v.simplify_full()
93
(1, log(x*y), sin(1/(x + 1)), x + 1)
94
95
sage: v = vector([sin(2*x), sin(3*x)])
96
sage: v.simplify_trig()
97
(2*sin(x)*cos(x), (4*cos(x)^2 - 1)*sin(x))
98
sage: v.simplify_trig(False)
99
(sin(2*x), sin(3*x))
100
sage: v.simplify_trig(expand=False)
101
(sin(2*x), sin(3*x))
102
"""
103
return self.apply_map(lambda x: phi(x, *args, **kwds))
104
apply.__doc__ += "\nSee Expression." + phi.__name__ + "() for optional arguments."
105
return apply
106
107
108
class Vector_symbolic_dense(free_module_element.FreeModuleElement_generic_dense):
109
pass
110
111
# Add elementwise methods.
112
for method in ['simplify', 'simplify_exp', 'simplify_factorial',
113
'simplify_log', 'simplify_radical', 'simplify_rational',
114
'simplify_trig', 'simplify_full', 'trig_expand', 'trig_reduce']:
115
setattr(Vector_symbolic_dense, method, apply_map(getattr(Expression, method)))
116
117