Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/calculus/test_sympy.py
8815 views
1
# -*- coding: utf-8 -*-
2
r"""
3
A Sample Session using SymPy
4
5
In this first part, we do all of the examples in the SymPy tutorial
6
(https://github.com/sympy/sympy/wiki/Tutorial), but using Sage
7
instead of SymPy.
8
9
::
10
11
sage: a = Rational((1,2))
12
sage: a
13
1/2
14
sage: a*2
15
1
16
sage: Rational(2)^50 / Rational(10)^50
17
1/88817841970012523233890533447265625
18
sage: 1.0/2
19
0.500000000000000
20
sage: 1/2
21
1/2
22
sage: pi^2
23
pi^2
24
sage: float(pi)
25
3.141592653589793
26
sage: RealField(200)(pi)
27
3.1415926535897932384626433832795028841971693993751058209749
28
sage: float(pi + exp(1))
29
5.85987448204883...
30
sage: oo != 2
31
True
32
33
::
34
35
sage: var('x y')
36
(x, y)
37
sage: x + y + x - y
38
2*x
39
sage: (x+y)^2
40
(x + y)^2
41
sage: ((x+y)^2).expand()
42
x^2 + 2*x*y + y^2
43
sage: ((x+y)^2).subs(x=1)
44
(y + 1)^2
45
sage: ((x+y)^2).subs(x=y)
46
4*y^2
47
48
::
49
50
sage: limit(sin(x)/x, x=0)
51
1
52
sage: limit(x, x=oo)
53
+Infinity
54
sage: limit((5^x + 3^x)^(1/x), x=oo)
55
5
56
57
::
58
59
sage: diff(sin(x), x)
60
cos(x)
61
sage: diff(sin(2*x), x)
62
2*cos(2*x)
63
sage: diff(tan(x), x)
64
tan(x)^2 + 1
65
sage: limit((tan(x+y) - tan(x))/y, y=0)
66
cos(x)^(-2)
67
sage: diff(sin(2*x), x, 1)
68
2*cos(2*x)
69
sage: diff(sin(2*x), x, 2)
70
-4*sin(2*x)
71
sage: diff(sin(2*x), x, 3)
72
-8*cos(2*x)
73
74
::
75
76
sage: cos(x).taylor(x,0,10)
77
-1/3628800*x^10 + 1/40320*x^8 - 1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1
78
sage: (1/cos(x)).taylor(x,0,10)
79
50521/3628800*x^10 + 277/8064*x^8 + 61/720*x^6 + 5/24*x^4 + 1/2*x^2 + 1
80
81
::
82
83
sage: matrix([[1,0], [0,1]])
84
[1 0]
85
[0 1]
86
sage: var('x y')
87
(x, y)
88
sage: A = matrix([[1,x], [y,1]])
89
sage: A
90
[1 x]
91
[y 1]
92
sage: A^2
93
[x*y + 1 2*x]
94
[ 2*y x*y + 1]
95
sage: R.<x,y> = QQ[]
96
sage: A = matrix([[1,x], [y,1]])
97
sage: print A^10
98
[x^5*y^5 + 45*x^4*y^4 + 210*x^3*y^3 + 210*x^2*y^2 + 45*x*y + 1 10*x^5*y^4 + 120*x^4*y^3 + 252*x^3*y^2 + 120*x^2*y + 10*x]
99
[ 10*x^4*y^5 + 120*x^3*y^4 + 252*x^2*y^3 + 120*x*y^2 + 10*y x^5*y^5 + 45*x^4*y^4 + 210*x^3*y^3 + 210*x^2*y^2 + 45*x*y + 1]
100
sage: var('x y')
101
(x, y)
102
103
And here are some actual tests of sympy::
104
105
sage: from sympy import Symbol, cos, sympify, pprint
106
sage: from sympy.abc import x
107
108
::
109
110
sage: e = sympify(1)/cos(x)**3; e
111
cos(x)**(-3)
112
sage: f = e.series(x, 0, 10); f
113
1 + 3*x**2/2 + 11*x**4/8 + 241*x**6/240 + 8651*x**8/13440 + O(x**10)
114
115
And the pretty-printer. Since unicode characters aren't working on
116
some archictures, we disable it::
117
118
sage: from sympy.printing import pprint_use_unicode
119
sage: prev_use = pprint_use_unicode(False)
120
sage: pprint(e)
121
1
122
-------
123
3
124
cos (x)
125
126
sage: pprint(f)
127
2 4 6 8
128
3*x 11*x 241*x 8651*x / 10\
129
1 + ---- + ----- + ------ + ------- + O\x /
130
2 8 240 13440
131
sage: pprint_use_unicode(prev_use)
132
False
133
134
And the functionality to convert from sympy format to Sage format::
135
136
sage: e._sage_()
137
cos(x)^(-3)
138
sage: e._sage_().taylor(x._sage_(), 0, 8)
139
8651/13440*x^8 + 241/240*x^6 + 11/8*x^4 + 3/2*x^2 + 1
140
sage: f._sage_()
141
8651/13440*x^8 + 241/240*x^6 + 11/8*x^4 + 3/2*x^2 + 1
142
143
Mixing SymPy with Sage::
144
145
sage: import sympy
146
sage: sympy.sympify(var("y"))+sympy.Symbol("x")
147
x + y
148
sage: o = var("omega")
149
sage: s = sympy.Symbol("x")
150
sage: t1 = s + o
151
sage: t2 = o + s
152
sage: type(t1)
153
<class 'sympy.core.add.Add'>
154
sage: type(t2)
155
<type 'sage.symbolic.expression.Expression'>
156
sage: t1, t2
157
(omega + x, omega + x)
158
sage: e=sympy.sin(var("y"))+sage.all.cos(sympy.Symbol("x"))
159
sage: type(e)
160
<class 'sympy.core.add.Add'>
161
sage: e
162
sin(y) + cos(x)
163
sage: e=e._sage_()
164
sage: type(e)
165
<type 'sage.symbolic.expression.Expression'>
166
sage: e
167
cos(x) + sin(y)
168
sage: e = sage.all.cos(var("y")**3)**4+var("x")**2
169
sage: e = e._sympy_()
170
sage: e
171
x**2 + cos(y**3)**4
172
173
::
174
175
sage: a = sympy.Matrix([1, 2, 3])
176
sage: a[1]
177
2
178
179
::
180
181
sage: sympify(1.5)
182
1.50000000000000
183
sage: sympify(2)
184
2
185
sage: sympify(-2)
186
-2
187
"""
188
189