Initialize Packages

md"""
# Initialize Packages
"""
241 μs
begin
using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO
using PlutoUI
using HypertextLiteral
: @htl, @htl_str

using LinearAlgebra
using ForwardDiff
end
3.4 s
PlutoUI.TableOfContents()
4.4 ms

Functions in Math and Julia

Univariate Functions

md"""
# Functions in Math and Julia
## Univariate Functions
"""
277 μs
f₁ (generic function with 1 method)
f₁(x) = x^2
480 μs
25
f₁(5)
16.8 μs
f₂ (generic function with 1 method)
f₂(x) = sin(x)
427 μs
f₃ (generic function with 2 methods)
f₃(x, α=3) =x^α
699 μs
125
f₃(5)
17.8 μs
f₄ (generic function with 1 method)
f₄(x;α)=x^α
1.5 ms
32
f₄(2, α=5)
5.4 ms

Automatic Differentiation of Univariates

md"""
## Automatic Differentiation of Univariates
"""
229 μs
10
ForwardDiff.derivative(f₁, 5)
134 ms
75
ForwardDiff.derivative(x->f₃(x,3), 5)
95.4 ms
1
@bind γ Slider(1:7, show_value=true)
225 ms
ϵ
0.1
ϵ = (10.0)^(-γ)
23.2 μs
(sin(1+ϵ)-sin(1))/ϵ, cos(1), ForwardDiff.derivative(sin, 1)
78.5 ms

Scalar Valued Multivariate Function

md"""
## Scalar Valued Multivariate Function
"""
1.1 ms
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.1 ms
f₅(2,3,4), f₅([2,3,4])
26.5 μs
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
943 μs
f₆([2,3,4]), f₆(2,3,4)
25.7 μs
f₇ (generic function with 1 method)
f₇((x,y,z)) = 5sin(x*y)+2y/4z
1.8 ms
-1.0220774909946293
f₇((2,3,4))
19.6 μs
# f₇(2,3,4)
17.2 μs
ForwardDiff.gradient(f₅, [1,2,3])
1.2 s
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
76.9 μs

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.
"""
283 μs

Transformations: Vector Valued Multivariate Functions

md"""
## Transformations: Vector Valued Multivariate Functions
"""
234 μs
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
6.9 ms
rot(π/2)([4,5])
27.7 μs
scalex(2)([4,5])
22.4 μs
scaley(2)([4,5])
21.5 μs
xy (generic function with 1 method)
begin
function warp(α)
((x,y),) -> begin
r = √(x^2+y^2)
θ = α*r
rot(θ)([x,y])
end
end
(x) = (norm(x), atan(x[2], x[1]))
xy((r,θ)) = (r*cos(θ), r*sin(θ))
end
2.5 ms
warp₂ (generic function with 2 methods)
begin
warp₂(α,x,y) = rot(α*√(x^2+y^2))
warp₂(α) = ((x,y),) -> warp₂(α,x,y)([x,y])
end
2.6 ms
warp(1)([5,6])
24.0 μs
warp₂(1.0)([5.0, 6.0])
24.2 μs

Automatic Differention of Transformations

md"""
## Automatic Differention of Transformations
"""
230 μs
2×2 Matrix{Float64}:
   7.06684    8.0157
 -10.6677   -11.9586
ForwardDiff.jacobian(warp(3.0), [4,5])
1.2 s
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
30.4 ms
Enter cell code...
93.3 μs