{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a88398a0-03e1-458f-ba48-518fb151b1b3", "metadata": {}, "outputs": [], "source": [ "using Plots\n", "using LinearAlgebra" ] }, { "cell_type": "code", "execution_count": 2, "id": "deac13e6-b3dd-4c8d-9c15-504052e60c6d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Matrix{Float64}:\n", " 0.693988 0.0373642 0.306196\n", " 0.475249 0.0523086 0.525352\n", " 0.275428 0.0212671 0.431897" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = rand(3,3)" ] }, { "cell_type": "code", "execution_count": 3, "id": "a7cfa41c-b406-475b-8a8e-bc56cab68cfa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "SVD{Float64, Float64, Matrix{Float64}, Vector{Float64}}\n", "U factor:\n", "3×3 Matrix{Float64}:\n", " -0.649854 0.735055 0.193349\n", " -0.624454 -0.371326 -0.687149\n", " -0.433297 -0.567284 0.700316\n", "singular values:\n", "3-element Vector{Float64}:\n", " 1.1253009779161582\n", " 0.27878010490969024\n", " 0.013851237498536341\n", "Vt factor:\n", "3×3 Matrix{Float64}:\n", " -0.770554 -0.0587937 -0.634658\n", " 0.636347 -0.0144316 -0.771268\n", " 0.0361865 -0.998166 0.0485336" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "U, S, V = svd(A) #singular decomposition" ] }, { "cell_type": "code", "execution_count": 4, "id": "c6fdc076-bfc8-46e9-97c2-5427f4c49395", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 BitMatrix:\n", " 1 0 0\n", " 0 1 0\n", " 0 0 1" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "3×3 BitMatrix:\n", " 1 0 0\n", " 0 1 0\n", " 0 0 1" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "3×3 BitMatrix:\n", " 0 0 0\n", " 0 0 0\n", " 0 0 0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(U'*U .> 0.00001)\n", "display(V'*V .> 0.00001)\n", "display(A - (U * Diagonal(S) * V') .> 0.00001)" ] }, { "cell_type": "code", "execution_count": 5, "id": "37b7a415-d7ea-4d00-b704-7d79e562d899", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Vector{Float64}:\n", " 0.019506810588402184\n", " 0.2433655332706062\n", " 0.9153216003851385" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "3-element Vector{Float64}:\n", " 1.1253009779161582\n", " 0.27878010490969024\n", " 0.013851237498536341" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(eigvals(A))\n", "display(svd(A).S)\n", "# singular values are more spread out" ] }, { "cell_type": "code", "execution_count": 39, "id": "5a4893c6-074c-4479-ac8a-9ad8db295d0a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((3, 3), (3,), (5, 3))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "((5, 3), (3,), (3, 3))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# svd exists also for rectangular matrices\n", "U, S, V = svd(rand(3, 5))\n", "display((size(U), size(S), size(V)))\n", "U, S, V = svd(rand(5, 3))\n", "display((size(U), size(S), size(V)))" ] }, { "cell_type": "code", "execution_count": 9, "id": "380565b8-f15d-4f3f-9a0f-85fc82809bdd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3-element Vector{Float64}:\n", " 2.302775637731995\n", " 1.3027756377319948\n", " 0.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "svdvals([2 0 1;0 1 1; 0 0 0; 0 0 0]) # rank is 2 -> so 2 non zero values" ] }, { "cell_type": "code", "execution_count": 23, "id": "deb05175-6a64-4c40-ab5c-e614ef85797f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}}\n", "values:\n", "3-element Vector{Float64}:\n", " 4.440892098500626e-15\n", " 1.697224362268007\n", " 5.302775637731994\n", "vectors:\n", "3×3 Matrix{Float64}:\n", " -0.333333 0.444872 -0.831251\n", " -0.666667 -0.734656 -0.125841\n", " 0.666667 -0.51222 -0.541467" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "3×3 Matrix{Float64}:\n", " 4.0 0.0 2.0\n", " 0.0 1.0 1.0\n", " 2.0 1.0 2.0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "A = [2 0 1;0 1 1; 0 0 0];\n", "display(eigen(A' * A))\n", "U, S, V = svd(A);\n", "display(V * Diagonal(S)' * Diagonal(S) * V')" ] }, { "cell_type": "code", "execution_count": 32, "id": "60516aff-16aa-41e6-a56d-6cad04547e55", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3×3 Matrix{Float64}:\n", " 1.0 -1.0 0.0\n", " 0.0 1.0 0.0\n", " 0.0 0.0 0.5" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "3×3 Matrix{Float64}:\n", " 1.0 -1.0 0.0\n", " -1.11022e-16 1.0 0.0\n", " 0.0 0.0 0.5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "A = [1 1 0; 0 1 0; 0 0 2];\n", "display(inv(A))\n", "F = svd(A);\n", "display(F.V * Diagonal(F.S .^ -1) * F.U') # we can get the inverse from the svd" ] }, { "cell_type": "code", "execution_count": 46, "id": "cd9e606f-957d-4803-9ab8-f7b23c455e13", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2.0, false)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "\"frobenius norm: 2.6457513110645907\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display((opnorm(A), opnorm(A) == norm(A))) # use opnorm for the matrix norm\n", "display(\"frobenius norm: $(norm(A))\")" ] }, { "cell_type": "code", "execution_count": 57, "id": "9c469ac2-4d2e-4eba-9c6e-c63ec1fc06ae", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Matrix{Float64}:\n", " 1.27357 1.80721\n", " 2.87898 4.08529" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Eckhart-Young theorem\n", "# the min X with respect to opnorm(A-X) with rank less than or equal to k is:\n", "A = [1 2;3 4];\n", "F = svd(A);\n", "X = F.U[:,1] .* F.S[1, 1] * F.V[:, 1]';\n", "display(X)\n", "display(rank(X))" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.9.3", "language": "julia", "name": "julia-1.9" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.9.3" } }, "nbformat": 4, "nbformat_minor": 5 }