{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Is Q mxn or nxn? It is both! This is useful! " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n", " -0.36166 0.916348 -0.135025 -0.106193 \n", " -0.875369 -0.396235 -0.176529 -0.21346 \n", " -0.216 0.0556349 0.974526 -0.0234073\n", " -0.237226 0.0144527 -0.0300856 0.970881 " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using LinearAlgebra\n", "A = rand(4,2)\n", "Q,R = qr(A)\n", "Q" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## It looks 4x4 !!" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 4)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "size(Q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## but wait! I can multiply by a vector of size 2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 1.4710364530489386 \n", " -1.6678387032016588 \n", " -0.10473038358204545\n", " -0.2083203318415276 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q * [1,2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## what about size 3? !! (answer: no)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "ename": "DimensionMismatch", "evalue": "DimensionMismatch(\"vector must have length either 4 or 2\")", "output_type": "error", "traceback": [ "DimensionMismatch(\"vector must have length either 4 or 2\")", "", "Stacktrace:", " [1] *(::LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}, ::Array{Int64,1}) at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/qr.jl:563", " [2] top-level scope at In[16]:1" ] } ], "source": [ "Q * [1,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## what about size 4? (answer: yes)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Array{Float64,1}:\n", " 0.6411891565050274\n", " -3.051264992118778 \n", " 2.7252182423170517\n", " 3.5849468538868363" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q * [1,2,3,4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's going on??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q is not stored as elements, it is stored in a more compact form known as a [WY representation](https://www.researchgate.net/publication/23844885_A_Storage-Efficient_WY_Representation_for_Products_of_Householder_Transformations) which we do not cover in 18.06.\n", "This form not only saves memory, but allows us to complete the tall-skinny mxn Q into a full square orthogonal Q." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The \"extra\" vectors are an orthonormal set of vectors that are orthogonal to the column space of A. This is associated with the left nullspace of A." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Float64,2}:\n", " 0.0486726 0.422254 0.0384858 0.324084\n", " 0.700173 0.479445 0.570882 0.340941\n", " 0.868803 0.791772 0.5067 0.294249\n", " 0.550927 0.680756 0.133569 0.127481" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = rand(4,4)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 0.422254 0.0384858 0.324084\n", " 0.479445 0.570882 0.340941\n", " 0.791772 0.5067 0.294249\n", " 0.680756 0.133569 0.127481" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:,2:end]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 0.422254 0.0384858 0.324084\n", " 0.479445 0.570882 0.340941\n", " 0.791772 0.5067 0.294249\n", " 0.680756 0.133569 0.127481" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:,2:4]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×3 Array{Float64,2}:\n", " 0.422254 0.0384858 0.324084\n", " 0.479445 0.570882 0.340941\n", " 0.791772 0.5067 0.294249\n", " 0.680756 0.133569 0.127481" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:,[2,3,4]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Six Cases (kind of)\n", "1. Square (rank=n rank