Skip to content

Instantly share code, notes, and snippets.

@mauro3

mauro3/#README Secret

Last active May 21, 2019 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauro3/6089af612380cb39ca00016d4f632cc2 to your computer and use it in GitHub Desktop.
Save mauro3/6089af612380cb39ca00016d4f632cc2 to your computer and use it in GitHub Desktop.
Julia-Intro
From
https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia/short-version
lightly adapted.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linear algebra in Julia\n",
"Based on work by Andreas Noack Jensen\n",
"\n",
"## Basic linalg ops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First let's define a random matrix"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"search: \u001b[0m\u001b[1mr\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mn\u001b[22m\u001b[0m\u001b[1md\u001b[22m \u001b[0m\u001b[1mr\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mn\u001b[22m\u001b[0m\u001b[1md\u001b[22mn t\u001b[0m\u001b[1mr\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mn\u001b[22msco\u001b[0m\u001b[1md\u001b[22me mac\u001b[0m\u001b[1mr\u001b[22moexp\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mn\u001b[22m\u001b[0m\u001b[1md\u001b[22m @mac\u001b[0m\u001b[1mr\u001b[22moexp\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mn\u001b[22m\u001b[0m\u001b[1md\u001b[22m @mac\u001b[0m\u001b[1mr\u001b[22moexp\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mn\u001b[22m\u001b[0m\u001b[1md\u001b[22m1\n",
"\n"
]
},
{
"data": {
"text/latex": [
"\\begin{verbatim}\n",
"rand([rng=GLOBAL_RNG], [S], [dims...])\n",
"\\end{verbatim}\n",
"Pick a random element or array of random elements from the set of values specified by \\texttt{S}; \\texttt{S} can be\n",
"\n",
"\\begin{itemize}\n",
"\\item an indexable collection (for example \\texttt{1:9} or \\texttt{('x', \"y\", :z)}),\n",
"\n",
"\n",
"\\item an \\texttt{AbstractDict} or \\texttt{AbstractSet} object,\n",
"\n",
"\n",
"\\item a string (considered as a collection of characters), or\n",
"\n",
"\n",
"\\item a type: the set of values to pick from is then equivalent to \\texttt{typemin(S):typemax(S)} for integers (this is not applicable to \\href{@ref}{\\texttt{BigInt}}), and to $[0, 1)$ for floating point numbers;\n",
"\n",
"\\end{itemize}\n",
"\\texttt{S} defaults to \\href{@ref}{\\texttt{Float64}}.\n",
"\n",
"\\begin{quote}\n",
"\\textbf{compat}\n",
"\n",
"Julia 1.1\n",
"\n",
"Support for \\texttt{S} as a tuple requires at least Julia 1.1.\n",
"\n",
"\\end{quote}\n",
"\\section{Examples}\n",
"\\begin{verbatim}\n",
"julia> rand(Int, 2)\n",
"2-element Array{Int64,1}:\n",
" 1339893410598768192\n",
" 1575814717733606317\n",
"\n",
"julia> rand(MersenneTwister(0), Dict(1=>2, 3=>4))\n",
"1=>2\n",
"\\end{verbatim}\n",
"\\begin{quote}\n",
"\\textbf{note}\n",
"\n",
"Note\n",
"\n",
"The complexity of \\texttt{rand(rng, s::Union\\{AbstractDict,AbstractSet\\})} is linear in the length of \\texttt{s}, unless an optimized method with constant complexity is available, which is the case for \\texttt{Dict}, \\texttt{Set} and \\texttt{BitSet}. For more than a few calls, use \\texttt{rand(rng, collect(s))} instead, or either \\texttt{rand(rng, Dict(s))} or \\texttt{rand(rng, Set(s))} as appropriate.\n",
"\n",
"\\end{quote}\n"
],
"text/markdown": [
"```\n",
"rand([rng=GLOBAL_RNG], [S], [dims...])\n",
"```\n",
"\n",
"Pick a random element or array of random elements from the set of values specified by `S`; `S` can be\n",
"\n",
" * an indexable collection (for example `1:9` or `('x', \"y\", :z)`),\n",
" * an `AbstractDict` or `AbstractSet` object,\n",
" * a string (considered as a collection of characters), or\n",
" * a type: the set of values to pick from is then equivalent to `typemin(S):typemax(S)` for integers (this is not applicable to [`BigInt`](@ref)), and to $[0, 1)$ for floating point numbers;\n",
"\n",
"`S` defaults to [`Float64`](@ref).\n",
"\n",
"!!! compat \"Julia 1.1\"\n",
" Support for `S` as a tuple requires at least Julia 1.1.\n",
"\n",
"\n",
"# Examples\n",
"\n",
"```julia-repl\n",
"julia> rand(Int, 2)\n",
"2-element Array{Int64,1}:\n",
" 1339893410598768192\n",
" 1575814717733606317\n",
"\n",
"julia> rand(MersenneTwister(0), Dict(1=>2, 3=>4))\n",
"1=>2\n",
"```\n",
"\n",
"!!! note\n",
" The complexity of `rand(rng, s::Union{AbstractDict,AbstractSet})` is linear in the length of `s`, unless an optimized method with constant complexity is available, which is the case for `Dict`, `Set` and `BitSet`. For more than a few calls, use `rand(rng, collect(s))` instead, or either `rand(rng, Dict(s))` or `rand(rng, Set(s))` as appropriate.\n",
"\n"
],
"text/plain": [
"\u001b[36m rand([rng=GLOBAL_RNG], [S], [dims...])\u001b[39m\n",
"\n",
" Pick a random element or array of random elements from the set of values\n",
" specified by \u001b[36mS\u001b[39m; \u001b[36mS\u001b[39m can be\n",
"\n",
" • an indexable collection (for example \u001b[36m1:9\u001b[39m or \u001b[36m('x', \"y\", :z)\u001b[39m),\n",
"\n",
" • an \u001b[36mAbstractDict\u001b[39m or \u001b[36mAbstractSet\u001b[39m object,\n",
"\n",
" • a string (considered as a collection of characters), or\n",
"\n",
" • a type: the set of values to pick from is then equivalent to\n",
" \u001b[36mtypemin(S):typemax(S)\u001b[39m for integers (this is not applicable to\n",
" \u001b[36mBigInt\u001b[39m), and to \u001b[35m[0, 1)\u001b[39m for floating point numbers;\n",
"\n",
" \u001b[36mS\u001b[39m defaults to \u001b[36mFloat64\u001b[39m.\n",
"\n",
"\u001b[39m\u001b[1m │ \u001b[22m\u001b[39m\u001b[1mJulia 1.1\u001b[22m\n",
"\u001b[39m\u001b[1m │\u001b[22m\n",
"\u001b[39m\u001b[1m │\u001b[22m Support for \u001b[36mS\u001b[39m as a tuple requires at least Julia 1.1.\n",
"\n",
"\u001b[1m Examples\u001b[22m\n",
"\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n",
"\n",
"\u001b[36m julia> rand(Int, 2)\u001b[39m\n",
"\u001b[36m 2-element Array{Int64,1}:\u001b[39m\n",
"\u001b[36m 1339893410598768192\u001b[39m\n",
"\u001b[36m 1575814717733606317\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> rand(MersenneTwister(0), Dict(1=>2, 3=>4))\u001b[39m\n",
"\u001b[36m 1=>2\u001b[39m\n",
"\n",
"\u001b[36m\u001b[1m │ \u001b[22m\u001b[39m\u001b[36m\u001b[1mNote\u001b[22m\u001b[39m\n",
"\u001b[36m\u001b[1m │\u001b[22m\u001b[39m\n",
"\u001b[36m\u001b[1m │\u001b[22m\u001b[39m The complexity of \u001b[36mrand(rng, s::Union{AbstractDict,AbstractSet})\u001b[39m is\n",
"\u001b[36m\u001b[1m │\u001b[22m\u001b[39m linear in the length of \u001b[36ms\u001b[39m, unless an optimized method with\n",
"\u001b[36m\u001b[1m │\u001b[22m\u001b[39m constant complexity is available, which is the case for \u001b[36mDict\u001b[39m, \u001b[36mSet\u001b[39m\n",
"\u001b[36m\u001b[1m │\u001b[22m\u001b[39m and \u001b[36mBitSet\u001b[39m. For more than a few calls, use \u001b[36mrand(rng, collect(s))\u001b[39m\n",
"\u001b[36m\u001b[1m │\u001b[22m\u001b[39m instead, or either \u001b[36mrand(rng, Dict(s))\u001b[39m or \u001b[36mrand(rng, Set(s))\u001b[39m as\n",
"\u001b[36m\u001b[1m │\u001b[22m\u001b[39m appropriate."
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"?rand"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = rand(1:4,3,3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a vector of ones"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = fill(1.0, (3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that $A$ has type Array{Int64,2} but $x$ has type Array{Float64,1}. Julia defines the aliases Vector{Type}=Array{Type,1} and Matrix{Type}=Array{Type,2}. \n",
"\n",
"Many of the basic operations are the same as in other languages\n",
"#### Multiplication"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"b = A*x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Transposition\n",
"As in other languages `A'` is the conjugate transpose"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and we can get the transpose with"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"transpose(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Transposed multiplication\n",
"Julia allows us to write this without *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A'A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Solving linear systems \n",
"The problem $Ax=b$ for ***square*** $A$ is solved by the \\ function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A\\b"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Special Matrix Structures\n",
"\n",
"Matrix structure is very important in linear algebra. To see *how* important it is, let's work with a larger linear system. Use the LinearAlgebra standard package to get access to structured matrices:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"using LinearAlgebra"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"n = 1000\n",
"A = randn(n,n);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Julia can often infer special matrix structure"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Asym = A + A'\n",
"issymmetric(Asym)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"but sometimes floating point error might get in the way."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.11399613068712722"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Asym_noisy = copy(Asym)\n",
"Asym_noisy[1,2] += 5eps()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6.283185307179586"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 2\n",
"2π"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"issymmetric(Asym_noisy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Luckily we can declare structure explicitly with, for example, `Diagonal`, `Triangular`, `Symmetric`, `Hermitian`, `Tridiagonal` and `SymTridiagonal`."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"Asym_explicit = Symmetric(Asym_noisy);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's compare how long it takes Julia to compute the eigenvalues of `Asym`, `Asym_noisy`, and `Asym_explicit`"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.496577 seconds (915.88 k allocations: 53.476 MiB, 2.31% gc time)\n"
]
}
],
"source": [
"@time eigvals(Asym);"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1.389342 seconds (18 allocations: 7.921 MiB, 0.41% gc time)\n"
]
}
],
"source": [
"@time eigvals(Asym_noisy);"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.130935 seconds (6.66 k allocations: 8.343 MiB)\n"
]
}
],
"source": [
"@time eigvals(Asym_explicit);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, using `Symmetric()` on `Asym_noisy` made our calculations about `5x` more efficient :)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### A big problem\n",
"Using the `Tridiagonal` and `SymTridiagonal` types to store tridiagonal matrices makes it possible to work with potentially very large tridiagonal problems. The following problem would not be possible to solve on a laptop if the matrix had to be stored as a (dense) `Matrix` type."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 0.903109 seconds (563.07 k allocations: 211.141 MiB, 8.28% gc time)\n"
]
},
{
"data": {
"text/plain": [
"6.14880480542946"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = 1_000_000;\n",
"A = SymTridiagonal(randn(n), randn(n-1));\n",
"@time eigmax(A)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"eigmax(A::<b>SymTridiagonal</b>) in LinearAlgebra at <a href=\"file:///home/mauro/julia/julia-1.1/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/tridiag.jl\" target=\"_blank\">/home/mauro/julia/julia-1.1/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/tridiag.jl:227</a>"
],
"text/plain": [
"eigmax(A::SymTridiagonal) in LinearAlgebra at /home/mauro/julia/julia-1.1/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/tridiag.jl:227"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@which eigmax(A)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.1.0",
"language": "julia",
"name": "julia-1.1"
},
"language": "Julia",
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment