From 47ac3eb8fdee498761c11bc88f91a558acbfd656 Mon Sep 17 00:00:00 2001 From: Aron T Date: Fri, 11 Oct 2024 12:46:44 +0300 Subject: [PATCH] Squashed commit of the following: commit 628e98b7c108c3f0440e8c2a77fdbcaa74cd679b Author: Aron T Date: Fri Oct 11 12:16:19 2024 +0300 add reflection matrix commit c47ab365b93ff28199d798895bf0bb55f51d1e25 Author: Aron T Date: Thu Oct 3 17:33:56 2024 +0300 ritation matrix commit af9b40b694a1a716410fb73f45644f205835aee7 Author: Aron T Date: Tue Oct 1 22:35:30 2024 +0300 typo --- .vscode/settings.json | 5 + ...ebra_Basics.ipynb => Linear_Algebra.ipynb} | 244 ++++++++++++++++-- src/linear_algebra_basic.jl | 14 +- src/linear_algebra_transform.jl | 77 +++++- 4 files changed, 309 insertions(+), 31 deletions(-) create mode 100644 .vscode/settings.json rename notebooks/{Linear_Algebra_Basics.ipynb => Linear_Algebra.ipynb} (66%) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0267bb4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "editor.codeLensFontFamily": "Fira Code", + "editor.fontFamily": "Fira Code", + "editor.fontLigatures": true +} \ No newline at end of file diff --git a/notebooks/Linear_Algebra_Basics.ipynb b/notebooks/Linear_Algebra.ipynb similarity index 66% rename from notebooks/Linear_Algebra_Basics.ipynb rename to notebooks/Linear_Algebra.ipynb index 3d3effb..562c2e2 100644 --- a/notebooks/Linear_Algebra_Basics.ipynb +++ b/notebooks/Linear_Algebra.ipynb @@ -2,13 +2,13 @@ "cells": [ { "cell_type": "code", - "execution_count": 6, + "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "intersection_2_implicit_lines" + "rotation_matrix_ns" ] }, "metadata": {}, @@ -19,7 +19,7 @@ "using Plots\n", "using LinearAlgebra\n", "using Symbolics\n", - "include(\"/Users/aron/Code/Study/Julia/Math/Linear_Algebra//src/linear_algebra.jl\")" + "include(\"/Users/aron/Code/Study/Julia/Math/Linear_Algebra/src/linear_algebra_transform.jl\")" ] }, { @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -112,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -147,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -182,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -217,7 +217,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -253,7 +253,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -297,7 +297,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -316,19 +316,26 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\begin{equation}\n", - "a t x + b t y\n", + "\\left[\n", + "\\begin{array}{c}\n", + "a t x + b t y \\\\\n", + "c t x + d t y \\\\\n", + "\\end{array}\n", + "\\right]\n", "\\end{equation}\n", " $$" ], "text/plain": [ - "a*t*x + b*t*y" + "2-element Vector{Num}:\n", + " a*t*x + b*t*y\n", + " c*t*x + d*t*y" ] }, "metadata": {}, @@ -342,7 +349,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -381,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -407,15 +414,58 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$$ \\begin{equation}\n", + "\\left[\n", + "\\begin{array}{cc}\n", + "\\frac{u^{2}}{u^{2} + v^{2}} & \\frac{u v}{u^{2} + v^{2}} \\\\\n", + "\\frac{u v}{u^{2} + v^{2}} & \\frac{v^{2}}{u^{2} + v^{2}} \\\\\n", + "\\end{array}\n", + "\\right]\n", + "\\end{equation}\n", + " $$" + ], + "text/plain": [ + "2×2 Matrix{Num}:\n", + " (u^2) / (u^2 + v^2) (u*v) / (u^2 + v^2)\n", + " (u*v) / (u^2 + v^2) (v^2) / (u^2 + v^2)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "@variables u v\n", + "x = [u, v]\n", + "e₁ = [1, 0]\n", + "e₂ = [0, 1]\n", + "P = [permutedims(orthproj(x, e₁)); permutedims(orthproj(x, e₂))]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Rotation of 45 degrees around x axis" + ] + }, + { + "cell_type": "code", + "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Matrix{Float64}:\n", - " 0.137931 0.344828\n", - " 0.344828 0.862069" + " 0.707107 -0.707107\n", + " 0.707107 0.707107" ] }, "metadata": {}, @@ -423,19 +473,163 @@ } ], "source": [ - "v = [2, 5]\n", "e₁ = [1, 0]\n", "e₂ = [0, 1]\n", - "P = [permutedims(orthproj(v, e₁)); permutedims(orthproj(v, e₂))]" + "x = [1, 0]\n", + "R = [rotation(45, e₁) rotation(45, e₂)]\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$$ \\begin{equation}\n", + "\\left[\n", + "\\begin{array}{cc}\n", + "\\cos\\left( \\theta \\right) & - \\sin\\left( \\theta \\right) \\\\\n", + "\\sin\\left( \\theta \\right) & \\cos\\left( \\theta \\right) \\\\\n", + "\\end{array}\n", + "\\right]\n", + "\\end{equation}\n", + " $$" + ], + "text/plain": [ + "2×2 Matrix{Num}:\n", + " cos(θ) -sin(θ)\n", + " sin(θ) cos(θ)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rotation_matrix_symbolic()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Rotation matrix rotates vector by θ degrees" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2×2 Matrix{Float64}:\n", + " 0.707107 -0.707107\n", + " 0.707107 0.707107" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "R = rotation_matrix(45)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2-element Vector{Float64}:\n", + " 0.7071067811865476\n", + " 0.7071067811865475" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "R * [1, 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2×2 Matrix{Float64}:\n", + " 0.0 -1.0\n", + " 1.0 0.0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "R = round.(rotation_matrix(90))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If rotate x-axis by 90 degrees we get to y-axis" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2-element Vector{Float64}:\n", + " 0.0\n", + " 1.0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "R * [1, 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2-element Vector{Float64}:\n", + " 0.0\n", + " 5.0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ - "v = (cos(θ), sin(θ))" + "R * [5, 0]" ] } ], diff --git a/src/linear_algebra_basic.jl b/src/linear_algebra_basic.jl index e767bd5..bae167b 100644 --- a/src/linear_algebra_basic.jl +++ b/src/linear_algebra_basic.jl @@ -81,11 +81,11 @@ end """ function reflection(v::Vector, w::Vector) -> Vector -The midpoint of the segment from `v` to the reflection of `v` is the projection P from 'v' to the line along 'w' +The midpoint of the segment from `v` to the reflection of `v` around 'w', is the projection P from 'v' to the line along 'w' """ function reflection(v::Vector,w::Vector) P = orthproj(w,v) - (2 * P ) - w + (2 * P) - v end """ @@ -95,7 +95,7 @@ function rotation(θ::Number, v::Vector) -> Vector function rotation(θ::Number, v::Vector) x′ = (cos(deg2rad(θ)) * v[1]) - (sin(deg2rad(θ)) * v[2]) y′ = (sin(deg2rad(θ)) * v[1]) + (cos(deg2rad(θ)) * v[2]) - [round(x′), round(y′)] + [x′, y′] end """ @@ -279,4 +279,10 @@ function intersection_2_implicit_lines(a₁::Number, b₁::Number, c₁::Number, # solve the system using left division of the matrix by the vector A\b end - +""" + function rationalize(x::Number; sigdigits=16) -> Rational(Number) +Rationalize a number to a rational number +""" +function rationalize(x; sigdigits=16) + return Int(round(x * 10^(sigdigits - 1), digits=0)) // 10^(sigdigits - 1) +end \ No newline at end of file diff --git a/src/linear_algebra_transform.jl b/src/linear_algebra_transform.jl index be17d9e..9ccc481 100644 --- a/src/linear_algebra_transform.jl +++ b/src/linear_algebra_transform.jl @@ -7,7 +7,8 @@ Create symbolic matrix for projection on [u, v] using E₁ = [1,0] and E₂ =[0, """ function projection_matrix_symbolic() @variables u v - [orthproj([u, v], [1, 0])[1] orthproj([u, v], [0, 1])[1]; orthproj([u, v], [1, 0])[2] orthproj([u, v], [0, 1])[2]] +# [orthproj([u, v], [1, 0])[1] orthproj([u, v], [0, 1])[1]; orthproj([u, v], [1, 0])[2] orthproj([u, v], [0, 1])[2]] + [permutedims(orthproj([u,v], [1,0])); permutedims(orthproj([u,v], [0,1]))] end """ function projection_matrix_symbolic_polar() -> Matrix @@ -27,9 +28,81 @@ function projection_matrix(x::Vector) end """ function projection_matrix_polar(θ::Number) -> Matrix -value of Matrix with an actual value for the angle of the vector +value of projection polar matrix with an actual value for the angle of the vector """ function projection_matrix_polar(n::Number) @variables u v θ Symbolics.value.(substitute.(projection_matrix_symbolic_polar(), θ => deg2rad(n),)) end + +""" + function rotation_matrix_symbolic() -> Matrix +Create symbolic matrix for rotation θ degrees, using E₁ = [1,0] and E₂ =[0,1] to get first and second column +""" +function rotation_matrix_symbolic() + @variables θ + # calculate for e₁ = [1, 0] + x′ = (cos(θ) * 1) - (sin(θ) * 0) + y′ = (sin(θ) * 1) + (cos(θ) * 0) + # calculate for e₂ = [0, 1] + x′′ = (cos(θ) * 0) - (sin(θ) * 1) + y′′ = (sin(θ) * 0) + (cos(θ) * 1) + [x′ x′′; y′ y′′] +end + +""" + function rotation_matrix(d::Number) -> Matrix +value of rotation Matrix with an actual value for the rotation angle +""" +function rotation_matrix(d::Number) + @variables θ + Symbolics.value.(substitute.(rotation_matrix_symbolic(), θ => deg2rad(d),)) +end + +""" + function rotation_matrix_ns(θ::Number) -> Matrix +value of rotation Matrix with an actual value for the rotation angle - non symbolic version +""" +function rotation_matrix_ns(θ::Number) + [rotation(θ, e₁) rotation(θ, e₂)] +end + +""" + function stretch_matrix_symbolic() -> Matrix +Create symbolic matrix for stretch λ₁, using E₁ = [1,0] and E₂ =[0,1] to get first and second column +""" +function stretch_matrix_symbolic() + @variables λ₁ + [permutedims(λ₁ * [1,0]); permutedims(λ₁ * [0,1])] +end +""" + function stretch_matrix_symbolic(n::Number) -> Matrix +value of stretch Matrix with an actual value for the stretch factor +""" +function stretch_matrix(n::Number) + @variables λ₁ + Symbolics.value.(substitute.(stretch_matrix_symbolic(), λ₁ => n)) +end +""" + function reflection_matrix_symbolic() -> Matrix +Create symbolic matrix for reflection on [u, v] using E₁ = [1,0] and E₂ =[0,1] to get first and second colomn +""" +function reflection_matrix_symbolic() + @variables u v + simplify.([permutedims(reflection([1,0],[u,v])); permutedims(reflection([0,1], [u,v]))]) +end +""" + function reflection_matrix(U::Vector) -> Matrix +value of reflection Matrix with an actual vector U +""" +function reflection_matrix(U::Vector) + @variables u v + Symbolics.value.(substitute.(reflection_matrix_symbolic(), (Dict(u => U[1], v => U[2]), ))) +end +""" + function reflection_matrix_rational(U::Vector) -> Matrix +value of reflection Matrix with an actual vector U in rational form +""" +function reflection_matrix_rational(U::Vector) + rationalize.(reflection_matrix(U)) +end