{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using LinearAlgebra" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Square Case first" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Float64,2}:\n", " 0.330869 0.843845 0.0623888 0.398208\n", " 0.8661 0.204488 0.138221 0.218923\n", " 0.741009 0.775278 0.576722 0.9775 \n", " 0.87276 0.139498 0.072938 0.983904" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = rand(4,4)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "LinearAlgebra.QRCompactWY{Float64,Array{Float64,2}}\n", "Q factor:\n", "4×4 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n", " -0.224588 0.76522 0.603058 -0.017861 \n", " -0.587892 -0.292547 0.130273 -0.742852 \n", " -0.502982 0.437253 -0.739298 0.0962124\n", " -0.592413 -0.371031 0.269792 0.662266 \n", "R factor:\n", "4×4 Array{Float64,2}:\n", " -1.47323 -0.782326 -0.428561 -1.29268 \n", " 0.0 0.873139 0.232416 0.303028\n", " 0.0 0.0 -0.351061 -0.188552\n", " 0.0 0.0 0.0 0.575913" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q,R = qr(A)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Float64,2}:\n", " 1.0 0.0 0.0 -0.0\n", " 0.0 1.0 -0.0 -0.0\n", " 0.0 -0.0 1.0 0.0\n", " -0.0 -0.0 0.0 1.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round.(Q'Q, digits=0)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Float64,2}:\n", " 1.0 0.0 -0.0 -0.0\n", " 0.0 1.0 0.0 -0.0\n", " -0.0 0.0 1.0 0.0\n", " -0.0 -0.0 0.0 1.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round.(Q*Q', digits=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## I'm happy, I see Q is orthogonal" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A ≈ Q * R" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Float64,2}:\n", " 0.330869 0.843845 0.0623888 0.398208\n", " 0.8661 0.204488 0.138221 0.218923\n", " 0.741009 0.775278 0.576722 0.9775 \n", " 0.87276 0.139498 0.072938 0.983904" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q * R" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4×4 Array{Float64,2}:\n", " 0.330869 0.843845 0.0623888 0.398208\n", " 0.8661 0.204488 0.138221 0.218923\n", " 0.741009 0.775278 0.576722 0.9775 \n", " 0.87276 0.139498 0.072938 0.983904" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now a tall skinny example" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " 0.657045 0.214426 0.0462177\n", " 0.268263 0.208357 0.269215 \n", " 0.410459 0.948475 0.756601 \n", " 0.391947 0.683485 0.28925 \n", " 0.949405 0.999374 0.570501 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = rand(5,3)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " -0.499955 -0.62267 0.00434913\n", " -0.204125 -0.0946497 -0.628421 \n", " -0.312324 0.686918 -0.424575 \n", " -0.298239 0.362414 0.645285 \n", " -0.722416 0.0110753 0.0917176 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q,R = qr(A)\n", "Q = Q[:,1:3] # make sure we have the first three columns" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " -0.499955 -0.62267 0.00434913\n", " -0.204125 -0.0946497 -0.628421 \n", " -0.312324 0.686918 -0.424575 \n", " -0.298239 0.362414 0.645285 \n", " -0.722416 0.0110753 0.0917176 " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " -1.31421 -1.37177 -0.81277 \n", " 0.0 0.75706 0.57661 \n", " 0.0 0.0 -0.251239" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " 0.657045 0.214426 0.0462177\n", " 0.268263 0.208357 0.269215 \n", " 0.410459 0.948475 0.756601 \n", " 0.391947 0.683485 0.28925 \n", " 0.949405 0.999374 0.570501 " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q * R" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×3 Array{Float64,2}:\n", " 0.657045 0.214426 0.0462177\n", " 0.268263 0.208357 0.269215 \n", " 0.410459 0.948475 0.756601 \n", " 0.391947 0.683485 0.28925 \n", " 0.949405 0.999374 0.570501 " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A ≈ Q * R" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Array{Float64,2}:\n", " 1.0 -0.0 -0.0\n", " -0.0 1.0 -0.0\n", " -0.0 -0.0 1.0" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round.(Q'Q, digits=0)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5×5 Array{Float64,2}:\n", " 0.637693 0.158256 -0.273422 -0.0737523 0.354678 \n", " 0.158256 0.445538 0.265548 -0.378935 0.0887777\n", " -0.273422 0.265548 0.749666 0.0681243 0.194295 \n", " -0.0737523 -0.378935 0.0681243 0.636683 0.27865 \n", " 0.354678 0.0887777 0.194295 0.27865 0.53042 " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q * Q'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.3.1", "language": "julia", "name": "julia-1.3" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.3.1" } }, "nbformat": 4, "nbformat_minor": 2 }