---
language: sage
---
category: Tutorial / Tour
---
title: SageMath is Python with Extras
descr: >
The language of SageMath is built on top of [Python](https://docs.python.org/2/tutorial/).
There are only some minor differences and hence you can apply all Python knowledge for working with Sage.
The only real difference is, that a few expressions are *pre-parsed*.
For example, `f(x) = 2*x + 1` is valid SageMath for defining a function `f`,
but that's not Python!
code: |
f(x) = 2*x + 1
print f(3)
---
title: Assignments
descr: >
Assignments are one of the most basic concepts of programming.
An object is born -- as a result of a creation or as an evaluation of an expression --
and assigned to (variable-) name for later use.
This assignment happens with the `=` sign on the left,
which is exactly like giving the created object on the right hand side a "name".
Do not get confused with a mathematical equation!
code: |
a = 5 # giving "5" the name "a"
a
---
title: Expressions
descr: >
Expressions are where computations happen.
These could be arithmetic operations, function calls, or other binary operations like comparisons.
code: |
2 + 2 # sum of 2 plus 2
2 == 2 # testing if 2 equals 2
2 < 4 # is 2 smaller than 4?
sqrt(4) # square root of 4
a = 5 # assigning 5 to a
a + 4 # should give 9
a == 5 # not an assignment, but a test if a is still 5
---
title: Mathematical Operations
descr: >
Sage uses standard operators to express mathematical operations.
In contrast to Python, the `^` is for exponentiation!
code: |
2 ** 3
2^3
10 % 3 # modulo
8 / 7 # this gives a fraction
9//2 # integer quotient
---
title: Mathematical Functions
descr: >
Many standard functions are directly available.
They produce either numerical approximations or give a symbolic expression.
code: |
sqrt(9.81)
sin(6.28)
cos(pi)
tan(pi/4)
---
title: Types
descr: >
A great deal of programming is about organizing data and information.
A "type" is a specific pattern, defining how data is structured and it a descriptive name.
Use Pythons `type(...)` function any time,
to learn more about what you are currently dealing with.
code: |
a = 5
type(a)
a = 9/82
type(a)
a = "I am a string in quotes"
type(a)
# ... and finally a complicated type
e11a = EllipticCurve('11a')
type(e11a)
---
title: Classes
descr: >
A common way to create types are classes.
Understanding their construction is not necessary at this point,
but mentioned for completeness.
code: |
class Fraction(object):
def __init__(self, nom, denom):
self.nominator = nom
self.denominator = denom
f1 = Fraction(1, 2)
f1.nominator
f1.denominator
---
title: Functions
descr: >
In Sage, there are (at least) two types of functions.
A Python-function is defined via the `def` keyword and maps optional arguments to a returned value.
For more information, [consult the Sage Tour](http://doc.sagemath.org/html/en/a_tour_of_sage/index.html).
Symbolic-functions are defined via `f(x) = ...`.
They are special purpose in the context of symbolic computations.
Hint: always make sure to define `x` prior to constructing such a function or symbolic expression,
as a symbolic variable and not accidentally as a number!
code: |
def f1(x):
return 2*x
f1(11)
# this defines x to be a symbolic variable
%var x
f2(x) = 2 * x + 1
f2(21)
---
title: Symbolic Expressions
descr: >
A great deal of mathematics concerns symbolic expressions.
The fundamental building block is a single variable, `x`, `y`, etc.
It can be defined via `var('x')` or the magic command `%var y`.
Then, symbolic expressions are constructed by using them as building blocks.
It is important to define the variables as being symblic.
Otherwise, the constructed expression is something completely different!
code: |
%var x, y
type(y)
ex1 = (x + y)^2 == 1
ex1
ex2 = x - y == 1
ex2
---
title: Approximate Symbolic Expressions
descr: >
To approximate a symbolic expression (without a free variable), use `n(<expr>)` or `expr.n()`.
code: |
exp(2)
n(exp(2))
sin(31.41592).n(digits=50) # number of digits
numerical_approx(pi, prec=200) # number of bits
---
title: LaTeX Formulas
descr: "To see a LaTeX rendering of such a symbolic expression, use Sage's `show(...)` function."
code: |
%var x, y
ex1 = (x + y)^2 == 1
show(ex1)
---
title: Solving Symbolic Equations
descr: >
Sage's `solve(...)` function allows you to solve symbolic equations exactly.
Expressons are automatically assumed to be equal to zero.
code: |
%var x, y
solve(2*x^2 + 1, x)
# second example, two equations:
ex1 = (x + y)^2 - 1
ex2 = x - y == 1
solve([ex1, ex2], [x, y])
---
title: Numerical Solutions
descr: >
Sage's `find_root(...)` solves equations numerically.
The optional second and third argument constrain the variable to an interval.
code: |
%var phi
find_root(cos(phi)==sin(phi), 0, pi/2)
---
title: Symbolic Differentiation
descr: >
Symbolic expressions can be differentiated.
This works either with Sage's `diff(...)` function or the expression's `.diff(...)` method.
You can also compute the n-th derivative directly.
code: |
%var x, y
f = sin(x^2) + 17*y^2
diff(f, x)
f.diff(y, 3)
---
title: Symbolic Integration
descr: >
If the algorithm is able to find it, the `integral(...)` function computes a symbolic integral.
code: |
%var x
integral(x*sin(x^2), x)