Initialize Packages
md"""
# Initialize Packages
"""
begin
using Colors , ColorVectorSpace , ImageShow , FileIO , ImageIO
using PlutoUI
using HypertextLiteral
: @htl, @htl_str
using LinearAlgebra
using ForwardDiff
end
Functions in Math and Julia
Univariate Functions
md"""
# Functions in Math and Julia
## Univariate Functions
"""
f₁ (generic function with 1 method)
f₁(x) = x ^2
25
f₂ (generic function with 1 method)
f₂(x) = sin(x )
f₃ (generic function with 2 methods)
f₃(x, α=3) =x ^α
125
f₄ (generic function with 1 method)
f₄(x;α)=x ^α
32
Automatic Differentiation of Univariates
md"""
## Automatic Differentiation of Univariates
"""
10
75
@bind γ Slider(1:7, show_value=true)
0.1
ϵ = (10.0)^(-γ )
0.497364
0.540302
0.540302
(sin(1+ϵ )-sin(1))/ϵ , cos(1), ForwardDiff .derivative(sin, 1)
Scalar Valued Multivariate Function
md"""
## Scalar Valued Multivariate Function
"""
f₅ (generic function with 2 methods)
begin
f₅(v) = 5sin(v [1]*v [2]) + 2*v [2]/4v [3]
f₅(x,y,z) = 5sin(x *y )+2*y /4z
end
-1.02208
-1.02208
f₆ (generic function with 2 methods)
begin
f₆(x,y,z) = 5sin(x *y )+2y /4z
f₆(v) = f₆ (v [1],v [2],v [3])
end
-1.02208
-1.02208
f₇ (generic function with 1 method)
f₇((x,y,z)) = 5sin(x*y)+2y/4z
-1.0220774909946293
# f₇(2,3,4)
-4.16147
-1.91407
-0.111111
-5.04005
-2.13774
-0.107527
begin
∂f₅∂x = (f₅ (1+ϵ ,2,3) -f₅ (1,2,3))/ϵ
∂f₅∂y = (f₅ (1,2+ϵ ,3) -f₅ (1,2,3))/ϵ
∂f₅∂z = (f₅ (1,2,3+ϵ ) -f₅ (1,2,3))/ϵ
∇f = [∂f₅∂x , ∂f₅∂y , ∂f₅∂z ]
end
Important Remark: In machine learning, and other optimization contexts, we want to minimize a scalar function of many parameters known as a "loss function." Following the negative gradient is a standard technique for minimizing functions especially when there are many variables. When there are only a few variables, there are better techniques.
md"""
Important Remark: In machine learning, and other optimization contexts, we want to minimize a scalar function of many parameters known as a "loss function." Following the negative gradient is a standard technique for minimizing functions especially when there are many variables. When there are only a few variables, there are better techniques.
"""
Transformations: Vector Valued Multivariate Functions
md"""
## Transformations: Vector Valued Multivariate Functions
"""
genlin (generic function with 1 method)
begin
idy((x,y)) = [x,y]
lin1((x,y)) = [2x+3y, -5x+4y]
scalex(α) = ((x,y),) -> (α *x, y)
scaley(α) = ((x,y),) -> (x, α *y)
rot(θ) = ((x,y),) -> [cos(θ )*x+sin(θ )*y, -sin(θ )*x+cos(θ )*y]
shear(α)=((x,y),) -> [x+α *y, y]
genlin(a,b,c,d) = ((x,y),) -> [a *x+b *y; c *x+d *y]
end
5.0
-4.0
8
5
4
10
xy (generic function with 1 method)
begin
function warp(α)
((x,y),) -> begin
r = √(x^2+y^2)
θ = α *r
end
end
rθ(x) = (norm(x ), atan(x [2], x [1]))
xy((r,θ)) = (r*cos(θ), r*sin(θ))
end
warp₂ (generic function with 2 methods)
begin
warp₂(α,x,y) = rot (α *√(x ^2+y ^2))
warp₂(α) = ((x,y),) -> warp₂ (α ,x,y)([x,y])
end
6.21285
-4.73291
6.21285
-4.73291
Automatic Differention of Transformations
md"""
## Automatic Differention of Transformations
"""
2×2 Matrix{Float64}:
7.06684 8.0157
-10.6677 -11.9586
2×2 Matrix{Float64}:
6.01539 6.66791
-11.4348 -12.8975
begin
∂w∂x = (warp (3.0)([4+ϵ , 5]) - warp (3.0)([4,5]))/ϵ # This is a vector, right?
∂w∂y = (warp (3.0)([4, 5+ϵ ]) - warp (3.0)([4,5]))/ϵ # This too
[∂w∂x ∂w∂y ]
end
Enter cell code...