Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
# CoCalc Examples Documentation File
2
# Copyright: SageMath Inc., 2016
3
# License: Creative Commons: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
4
---
5
language: sage
6
category: Mathematics / Intro
7
---
8
title: Introduction
9
descr: |
10
SageMath is an advanced Python-based environment for mathematics.
11
With that fundament, it is also suited for scientific computing,
12
statistics, and data analysis.
13
14
This introduction gives a quick overview,
15
while the individual sub-topics to into more detail.
16
code: ""
17
---
18
title: Pitfalls
19
descr: |
20
There is a blurry line between using SageMath and programming.
21
Don't be afraid about this,
22
since it only means that this opens up unlimited ways of expanding and enhancing of what you can do!
23
24
One detail to be aware of is, that expressions need to be explicit about their computational signs.
25
I.e. it is not ok to do `4 (x+1)` -- this should be `4 * (x+1)`.
26
Also, nested brackets are always round brackets like `((1+x) + y) / 2`.
27
28
Function calls are always `function_name(...)` with round brackets, too.
29
code: |
30
# TODO: fix this expression
31
%var x
32
sqrt [ (4 + x) (2 - x) ]
33
---
34
category: Mathematics / Calculus
35
---
36
title: Introduction
37
descr: |
38
Here are some examples of calculus symbolic computations using Sage.
39
The goal is to make calculus feel natural and intuitive,
40
while still being part of a modern programming language.
41
42
Read more about this here:
43
* [Constructions/Calculus](http://doc.sagemath.org/html/en/constructions/calculus.html)
44
* [Reference/Symbolic Calculus](http://doc.sagemath.org/html/en/reference/calculus/index.html)
45
* or download the book [Gregory Bard: Sage for Undergraduates](http://www.gregorybard.com/Sage.html)
46
code: |
47
%var x k w
48
f = x^3 * e^(k*x) * sin(w*x)
49
show(f)
50
print("Differentiate f w.r.t. x")
51
show(f.diff(x))
52
---
53
title: Functions
54
descr: |
55
SageMath's [preparser](http://doc.sagemath.org/html/en/reference/repl/sage/repl/preparse.html) makes it easy to define functions.
56
code: |
57
%var x
58
f(x) = 3 * x -1
59
f
60
g(x) = (1 - x)^2
61
g
62
# h is a composition of f and g
63
h = f(g)
64
h
65
# evaluate h
66
h(x = 3)
67
h.subs(x = pi)
68
---
69
title: Substitutions
70
descr: |
71
Evaluate symbolic expressions by substution of a variable.
72
code: |
73
%var x y
74
ex1 = 2*x+1
75
ex1
76
ex1.subs(x = 3)
77
ex1.subs(x = pi)
78
ex2 = ex1.subs(x = (x + y - 1) / 2)
79
ex2
80
---
81
title: Expressions
82
descr: |
83
Symbolic expressions are also Python objects.
84
This means, there are ways to inspect and take them apart.
85
86
In this example here, we start with an equation and "zoom in" to a small part of it.
87
88
Tipp: Combine this technique with substitutions to build new expressions out of existing ones.
89
code: |
90
%var x
91
ex = 2 * x^2 == (1 + x) / (1 - x)
92
print("full expression: %s " % ex)
93
ex2 = ex.right_hand_side()
94
print("right hand side: %s" % ex2)
95
print("operands of right hand side: %s" % ex2.operands())
96
ex3 = ex2.op[0] # nominator
97
print("the x inside the nominator: %s" % ex3.op[0])
98
---
99
title: Polynomials
100
descr: |
101
The `x` could also be a generator of a `PolynomialRing` over $\mathbb{Q}$
102
code: |
103
R.<x> = PolynomialRing(QQ, "x")
104
p = x^2 + 1
105
p
106
p.derivative()
107
p.integral()
108
type(p)
109
---
110
title: Limits
111
descr: |
112
Compute $lim$ with SageMath and Maxima. Note, that `oo` stands for $\infty$.
113
114
See [Reference/Limit](http://doc.sagemath.org/html/en/reference/calculus/sage/calculus/calculus.html#sage.calculus.calculus.limit) for more information.
115
code: |
116
%var x
117
lim((x+1)^(1/x), x = 0)
118
lim(sqrt(x^2+1) - x, x = oo)
119
---
120
title: Symbolic Sums
121
descr: |
122
The `sum` function is not Python's, but a special one by SageMath.
123
It allows you to compute [symbolic sums](http://doc.sagemath.org/html/en/reference/calculus/sage/calculus/calculus.html#sage.calculus.calculus.symbolic_sum)!
124
code: |
125
%var k u x
126
# known limits
127
sum(x^k, k, 1, 10)
128
# up to infinity
129
sum(1/k^4, k, 1, oo)
130
# unknown limits
131
sum(x^(2*k+1), k, -u, u)
132
---
133
title: Taylor Series
134
descr: >
135
Use `expr.taylor(...)` to obtain a taylor polynomial.
136
code: |
137
%var f0 k x
138
g = f0/sinh(k*x)^4
139
g
140
t = g.taylor(x, 0, 3)
141
t
142
---
143
title: Formal Power Series
144
descr: |
145
`expr.series(...)` gives formal power series expansions.
146
code: |
147
%var x
148
ex = 1/ (2 - cos(x))
149
ex
150
ex.series(x,7)
151
---
152
title: Fast Computations in Formal Power Series Ring
153
descr: |
154
You can use the formal power series rings for fast computation.
155
code: |
156
R.<w> = QQ[[]]
157
ps = w + 17/2*w^2 + 15/4*w^4 + O(w^6)
158
ps
159
(1+ps).log()
160
print(r"Coefficients of $ps^1000$")
161
(ps^1000).coefficients()
162
---
163
title: Symbolic Integrals
164
descr: |
165
SageMath can do symbolic integrals.
166
code: |
167
%var x
168
f = x^3
169
f.integral(x)
170
# or as a function
171
integral(x^3,x)
172
---
173
title: Definite Integrals
174
descr: |
175
It can also compute integrals involving limits.
176
code: |
177
%var x k w
178
f = k * sin(w*x)
179
f.integrate(x, 0, 1)
180
# or as a function
181
integrate(1/x^2, x, 1, infinity)
182
---
183
title: Laplace Transformations
184
descr: |
185
Compute laplace transformations
186
code: |
187
%var k s t
188
f = 1/exp(k*t)
189
f.laplace(t, s)
190
---
191
title: Inverse Laplace Transformations
192
descr: |
193
... and the inverse laplace transformations.
194
code: |
195
%var k s t
196
g = 1/(k + s)
197
g.inverse_laplace(s, t)
198
---
199
category: Mathematics / Differential Equations
200
---
201
title: Introduction
202
descr: |
203
Symbolically solving ODEs can be done using Sage interface with Maxima.
204
For more information, read the help of `desolvers`.
205
206
(Numerically solving ODEs is also possible via GSL or Octave.)
207
code: |
208
desolvers?
209
---
210
title: Function Objects
211
descr: |
212
To tell Sage that you want to work with a general function,
213
use the command `function` to create a named function in a specific variable.
214
215
That might look a bit strange first, but it is necessary to be very explicit about each variable's and expressions meaning!
216
code: |
217
%var x
218
f = function("f")(x)
219
type(f)
220
---
221
title: ODE Example
222
descr: |
223
An example, how to solve ODE’s symbolically in Sage using the Maxima interface under the hood.
224
225
`ics` are the initial conditions.
226
The second example is without them,
227
and therefore free constants named $\texttt{_K}_i$ appear.
228
code: |
229
%var x
230
y = function('y')(x)
231
desolve(diff(y,x,2) + 3*x == y, dvar = y, ics = [1,1,1])
232
desolve(diff(y,x,2) + 3*x == y, dvar = y)
233
---
234
title: Slope Field
235
descr: |
236
The following solves the differential equation
237
$$\frac{dy}{dx} + \frac{y}{x} + x = 2$$
238
with the initial conditions $y(2) = 4$.
239
code: |
240
x = var('x')
241
y = function('y')(x)
242
myequation = diff(y,x) + y/x + x == 2
243
h = desolve(myequation, y, [2, 4])
244
h.expand()
245
246