{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic linear algebra in Julia\n", "Author: Andreas Noack Jensen (MIT) (http://www.econ.ku.dk/phdstudent/noack/)\n", "(with edits from Jane Herriman)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First let's define a random matrix" ] }, { "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,)) # = 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, or adjoint" ] }, { "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": {}, "source": [ "`A\\b` gives us the *least squares solution* if we have an overdetermined linear system (a \"tall\" matrix)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Atall = rand(3, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Atall\\b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the *minimum norm least squares solution* if we have a rank-deficient least squares problem" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v = rand(3)\n", "rankdef = hcat(v, v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rankdef\\b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia also gives us the minimum norm solution when we have an underdetermined solution (a \"short\" matrix)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bshort = rand(2)\n", "Ashort = rand(2, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Ashort\\bshort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The LinearAlgebra library\n", "\n", "While much of linear algebra is available in Julia by default (as shown above), there's a standard library named `LinearAlgebra` that brings in many more relevant names and functions. In particular, it provides factorizations and some structured matrix types. As with all packages, you can bring these additional features into your session with a `using LinearAlgebra`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises\n", "\n", "#### 10.1 \n", "Take the inner product (or \"dot\" product) of a vector `v` with itself." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 10.2 \n", "Take the outer product of a vector v with itself." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.0.0", "language": "julia", "name": "julia-1.0" }, "language": "Julia", "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.0.0" } }, "nbformat": 4, "nbformat_minor": 1 }