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., 2015
3
# License: Creative Commons: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
4
---
5
language: sage
6
---
7
category: Tutorial / Tour
8
---
9
title: SageMath is Python with Extras
10
descr: >
11
The language of SageMath is built on top of [Python](https://docs.python.org/2/tutorial/).
12
There are only some minor differences and hence you can apply all Python knowledge for working with Sage.
13
14
The only real difference is, that a few expressions are *pre-parsed*.
15
For example, `f(x) = 2*x + 1` is valid SageMath for defining a function `f`,
16
but that's not Python!
17
code: |
18
f(x) = 2*x + 1
19
print f(3)
20
---
21
title: Assignments
22
descr: >
23
Assignments are one of the most basic concepts of programming.
24
An object is born -- as a result of a creation or as an evaluation of an expression --
25
and assigned to (variable-) name for later use.
26
27
This assignment happens with the `=` sign on the left,
28
which is exactly like giving the created object on the right hand side a "name".
29
Do not get confused with a mathematical equation!
30
code: |
31
a = 5 # giving "5" the name "a"
32
a
33
---
34
title: Expressions
35
descr: >
36
Expressions are where computations happen.
37
These could be arithmetic operations, function calls, or other binary operations like comparisons.
38
code: |
39
2 + 2 # sum of 2 plus 2
40
2 == 2 # testing if 2 equals 2
41
2 < 4 # is 2 smaller than 4?
42
sqrt(4) # square root of 4
43
a = 5 # assigning 5 to a
44
a + 4 # should give 9
45
a == 5 # not an assignment, but a test if a is still 5
46
---
47
title: Mathematical Operations
48
descr: >
49
Sage uses standard operators to express mathematical operations.
50
In contrast to Python, the `^` is for exponentiation!
51
code: |
52
2 ** 3
53
2^3
54
10 % 3 # modulo
55
8 / 7 # this gives a fraction
56
9//2 # integer quotient
57
---
58
title: Mathematical Functions
59
descr: >
60
Many standard functions are directly available.
61
They produce either numerical approximations or give a symbolic expression.
62
code: |
63
sqrt(9.81)
64
sin(6.28)
65
cos(pi)
66
tan(pi/4)
67
---
68
title: Types
69
descr: >
70
A great deal of programming is about organizing data and information.
71
A "type" is a specific pattern, defining how data is structured and it a descriptive name.
72
Use Pythons `type(...)` function any time,
73
to learn more about what you are currently dealing with.
74
code: |
75
a = 5
76
type(a)
77
a = 9/82
78
type(a)
79
a = "I am a string in quotes"
80
type(a)
81
# ... and finally a complicated type
82
e11a = EllipticCurve('11a')
83
type(e11a)
84
---
85
title: Classes
86
descr: >
87
A common way to create types are classes.
88
Understanding their construction is not necessary at this point,
89
but mentioned for completeness.
90
code: |
91
class Fraction(object):
92
def __init__(self, nom, denom):
93
self.nominator = nom
94
self.denominator = denom
95
f1 = Fraction(1, 2)
96
f1.nominator
97
f1.denominator
98
---
99
title: Functions
100
descr: >
101
In Sage, there are (at least) two types of functions.
102
A Python-function is defined via the `def` keyword and maps optional arguments to a returned value.
103
For more information, [consult the Sage Tour](http://doc.sagemath.org/html/en/a_tour_of_sage/index.html).
104
105
Symbolic-functions are defined via `f(x) = ...`.
106
They are special purpose in the context of symbolic computations.
107
108
Hint: always make sure to define `x` prior to constructing such a function or symbolic expression,
109
as a symbolic variable and not accidentally as a number!
110
code: |
111
def f1(x):
112
return 2*x
113
f1(11)
114
115
# this defines x to be a symbolic variable
116
%var x
117
f2(x) = 2 * x + 1
118
f2(21)
119
---
120
title: Symbolic Expressions
121
descr: >
122
A great deal of mathematics concerns symbolic expressions.
123
The fundamental building block is a single variable, `x`, `y`, etc.
124
It can be defined via `var('x')` or the magic command `%var y`.
125
Then, symbolic expressions are constructed by using them as building blocks.
126
It is important to define the variables as being symblic.
127
Otherwise, the constructed expression is something completely different!
128
code: |
129
%var x, y
130
type(y)
131
ex1 = (x + y)^2 == 1
132
ex1
133
ex2 = x - y == 1
134
ex2
135
---
136
title: Approximate Symbolic Expressions
137
descr: >
138
To approximate a symbolic expression (without a free variable), use `n(<expr>)` or `expr.n()`.
139
code: |
140
exp(2)
141
n(exp(2))
142
sin(31.41592).n(digits=50) # number of digits
143
numerical_approx(pi, prec=200) # number of bits
144
---
145
title: LaTeX Formulas
146
descr: "To see a LaTeX rendering of such a symbolic expression, use Sage's `show(...)` function."
147
code: |
148
%var x, y
149
ex1 = (x + y)^2 == 1
150
show(ex1)
151
---
152
title: Solving Symbolic Equations
153
descr: >
154
Sage's `solve(...)` function allows you to solve symbolic equations exactly.
155
Expressons are automatically assumed to be equal to zero.
156
code: |
157
%var x, y
158
solve(2*x^2 + 1, x)
159
# second example, two equations:
160
ex1 = (x + y)^2 - 1
161
ex2 = x - y == 1
162
solve([ex1, ex2], [x, y])
163
---
164
title: Numerical Solutions
165
descr: >
166
Sage's `find_root(...)` solves equations numerically.
167
The optional second and third argument constrain the variable to an interval.
168
code: |
169
%var phi
170
find_root(cos(phi)==sin(phi), 0, pi/2)
171
---
172
title: Symbolic Differentiation
173
descr: >
174
Symbolic expressions can be differentiated.
175
This works either with Sage's `diff(...)` function or the expression's `.diff(...)` method.
176
You can also compute the n-th derivative directly.
177
code: |
178
%var x, y
179
f = sin(x^2) + 17*y^2
180
diff(f, x)
181
f.diff(y, 3)
182
---
183
title: Symbolic Integration
184
descr: >
185
If the algorithm is able to find it, the `integral(...)` function computes a symbolic integral.
186
code: |
187
%var x
188
integral(x*sin(x^2), x)
189
190