---
language: sage
category: Mathematics / Intro
---
title: Introduction
descr: |
SageMath is an advanced Python-based environment for mathematics.
With that fundament, it is also suited for scientific computing,
statistics, and data analysis.
This introduction gives a quick overview,
while the individual sub-topics to into more detail.
code: ""
---
title: Pitfalls
descr: |
There is a blurry line between using SageMath and programming.
Don't be afraid about this,
since it only means that this opens up unlimited ways of expanding and enhancing of what you can do!
One detail to be aware of is, that expressions need to be explicit about their computational signs.
I.e. it is not ok to do `4 (x+1)` -- this should be `4 * (x+1)`.
Also, nested brackets are always round brackets like `((1+x) + y) / 2`.
Function calls are always `function_name(...)` with round brackets, too.
code: |
# TODO: fix this expression
%var x
sqrt [ (4 + x) (2 - x) ]
---
category: Mathematics / Calculus
---
title: Introduction
descr: |
Here are some examples of calculus symbolic computations using Sage.
The goal is to make calculus feel natural and intuitive,
while still being part of a modern programming language.
Read more about this here:
* [Constructions/Calculus](http://doc.sagemath.org/html/en/constructions/calculus.html)
* [Reference/Symbolic Calculus](http://doc.sagemath.org/html/en/reference/calculus/index.html)
* or download the book [Gregory Bard: Sage for Undergraduates](http://www.gregorybard.com/Sage.html)
code: |
%var x k w
f = x^3 * e^(k*x) * sin(w*x)
show(f)
print("Differentiate f w.r.t. x")
show(f.diff(x))
---
title: Functions
descr: |
SageMath's [preparser](http://doc.sagemath.org/html/en/reference/repl/sage/repl/preparse.html) makes it easy to define functions.
code: |
%var x
f(x) = 3 * x -1
f
g(x) = (1 - x)^2
g
# h is a composition of f and g
h = f(g)
h
# evaluate h
h(x = 3)
h.subs(x = pi)
---
title: Substitutions
descr: |
Evaluate symbolic expressions by substution of a variable.
code: |
%var x y
ex1 = 2*x+1
ex1
ex1.subs(x = 3)
ex1.subs(x = pi)
ex2 = ex1.subs(x = (x + y - 1) / 2)
ex2
---
title: Expressions
descr: |
Symbolic expressions are also Python objects.
This means, there are ways to inspect and take them apart.
In this example here, we start with an equation and "zoom in" to a small part of it.
Tipp: Combine this technique with substitutions to build new expressions out of existing ones.
code: |
%var x
ex = 2 * x^2 == (1 + x) / (1 - x)
print("full expression: %s " % ex)
ex2 = ex.right_hand_side()
print("right hand side: %s" % ex2)
print("operands of right hand side: %s" % ex2.operands())
ex3 = ex2.op[0] # nominator
print("the x inside the nominator: %s" % ex3.op[0])
---
title: Polynomials
descr: |
The `x` could also be a generator of a `PolynomialRing` over $\mathbb{Q}$
code: |
R.<x> = PolynomialRing(QQ, "x")
p = x^2 + 1
p
p.derivative()
p.integral()
type(p)
---
title: Limits
descr: |
Compute $lim$ with SageMath and Maxima. Note, that `oo` stands for $\infty$.
See [Reference/Limit](http://doc.sagemath.org/html/en/reference/calculus/sage/calculus/calculus.html#sage.calculus.calculus.limit) for more information.
code: |
%var x
lim((x+1)^(1/x), x = 0)
lim(sqrt(x^2+1) - x, x = oo)
---
title: Symbolic Sums
descr: |
The `sum` function is not Python's, but a special one by SageMath.
It allows you to compute [symbolic sums](http://doc.sagemath.org/html/en/reference/calculus/sage/calculus/calculus.html#sage.calculus.calculus.symbolic_sum)!
code: |
%var k u x
# known limits
sum(x^k, k, 1, 10)
# up to infinity
sum(1/k^4, k, 1, oo)
# unknown limits
sum(x^(2*k+1), k, -u, u)
---
title: Taylor Series
descr: >
Use `expr.taylor(...)` to obtain a taylor polynomial.
code: |
%var f0 k x
g = f0/sinh(k*x)^4
g
t = g.taylor(x, 0, 3)
t
---
title: Formal Power Series
descr: |
`expr.series(...)` gives formal power series expansions.
code: |
%var x
ex = 1/ (2 - cos(x))
ex
ex.series(x,7)
---
title: Fast Computations in Formal Power Series Ring
descr: |
You can use the formal power series rings for fast computation.
code: |
R.<w> = QQ[[]]
ps = w + 17/2*w^2 + 15/4*w^4 + O(w^6)
ps
(1+ps).log()
print(r"Coefficients of $ps^1000$")
(ps^1000).coefficients()
---
title: Symbolic Integrals
descr: |
SageMath can do symbolic integrals.
code: |
%var x
f = x^3
f.integral(x)
# or as a function
integral(x^3,x)
---
title: Definite Integrals
descr: |
It can also compute integrals involving limits.
code: |
%var x k w
f = k * sin(w*x)
f.integrate(x, 0, 1)
# or as a function
integrate(1/x^2, x, 1, infinity)
---
title: Laplace Transformations
descr: |
Compute laplace transformations
code: |
%var k s t
f = 1/exp(k*t)
f.laplace(t, s)
---
title: Inverse Laplace Transformations
descr: |
... and the inverse laplace transformations.
code: |
%var k s t
g = 1/(k + s)
g.inverse_laplace(s, t)
---
category: Mathematics / Differential Equations
---
title: Introduction
descr: |
Symbolically solving ODEs can be done using Sage interface with Maxima.
For more information, read the help of `desolvers`.
(Numerically solving ODEs is also possible via GSL or Octave.)
code: |
desolvers?
---
title: Function Objects
descr: |
To tell Sage that you want to work with a general function,
use the command `function` to create a named function in a specific variable.
That might look a bit strange first, but it is necessary to be very explicit about each variable's and expressions meaning!
code: |
%var x
f = function("f")(x)
type(f)
---
title: ODE Example
descr: |
An example, how to solve ODE’s symbolically in Sage using the Maxima interface under the hood.
`ics` are the initial conditions.
The second example is without them,
and therefore free constants named $\texttt{_K}_i$ appear.
code: |
%var x
y = function('y')(x)
desolve(diff(y,x,2) + 3*x == y, dvar = y, ics = [1,1,1])
desolve(diff(y,x,2) + 3*x == y, dvar = y)
---
title: Slope Field
descr: |
The following solves the differential equation
$$\frac{dy}{dx} + \frac{y}{x} + x = 2$$
with the initial conditions $y(2) = 4$.
code: |
x = var('x')
y = function('y')(x)
myequation = diff(y,x) + y/x + x == 2
h = desolve(myequation, y, [2, 4])
h.expand()