Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Lift of a Function is a Function #211

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/differential_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end
"""
$(TYPEDSIGNATURES)

Return the HamiltonianLift of a function.
Return the Lift of a function.
Dependencies are specified with boolean : autonomous and variable.

# Example
Expand All @@ -42,16 +42,19 @@ julia> H(1, 1, 1, 1)
2
```
"""
function Lift(X::Function; autonomous::Bool=true, variable::Bool=false)::HamiltonianLift
time_dependence = autonomous ? Autonomous : NonAutonomous
variable_dependence = variable ? NonFixed : Fixed
return Lift(VectorField(X, time_dependence, variable_dependence))
function Lift(X::Function; autonomous::Bool=true, variable::Bool=false)::Function
return @match (autonomous, variable) begin
(true , false) => ( x, p ) -> p' * X( x )
(true , true ) => ( x, p, v) -> p' * X( x, v)
(false, false) => (t, x, p ) -> p' * X(t, x )
_ => (t, x, p, v) -> p' * X(t, x, v)
end
end

"""
$(TYPEDSIGNATURES)

Return the HamiltonianLift of a VectorField or a function.
Return the Lift of a function.
Dependencies are specified with DataType : Autonomous, NonAutonomous and Fixed, NonFixed.

# Example
Expand All @@ -64,11 +67,11 @@ julia> H(1, 1, 1, 1)
2
```
"""
function Lift(X::Function, dependences::DataType...)::HamiltonianLift
function Lift(X::Function, dependences::DataType...)::Function
__check_dependencies(dependences)
variable_dependence = NonFixed ∈ dependences ? NonFixed : Fixed
time_dependence = NonAutonomous ∈ dependences ? NonAutonomous : Autonomous
return Lift(VectorField(X, time_dependence, variable_dependence))
autonomous = NonAutonomous ∈ dependences ? false : true
variable = NonFixed ∈ dependences ? true : false
return Lift(X; autonomous=autonomous, variable=variable)
end

# ---------------------------------------------------------------------------
Expand Down
26 changes: 16 additions & 10 deletions test/test_differential_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ function test_differential_geometry()
Test.@test H(1, 1, 1, 1) == 2
Test.@test H(1, [1, 2], [3, 4], 1) == 22

# overload
F::Function = x -> 2x
H_F(x, p) = Lift(F)(x, p)
H_F(y) = H_F(y, y)
Test.@test H_F(1) == 2

# exceptions
Test.@test_throws IncorrectArgument Lift(X, Int64)

Expand Down Expand Up @@ -386,12 +392,12 @@ function test_differential_geometry()
@testset "nonautonomous case" begin
f = (t, x) -> [t*x[1] + x[2]^2, x[1], 0]
g = (t, x) -> [0, x[2], t*x[1]^2 + 4*x[2]]
F = Lift(f,NonAutonomous)
G = Lift(g,NonAutonomous)
F = Lift(f, NonAutonomous)
G = Lift(g, NonAutonomous)
F_ = (t, x, p) -> p' * f(t, x)
G_ = (t, x, p) -> p' * g(t, x)
Test.@test Poisson(F, G)(2, [1, 2, 3], [4, 0, 4]) ≈ Poisson(F_, G_, NonAutonomous)(2, [1, 2, 3], [4, 0, 4]) atol=1e-6
Test.@test Poisson(F, G_)(2, [1, 2, 3], [4, 0, 4]) ≈ Poisson(F_, G)(2, [1, 2, 3], [4, 0, 4]) atol=1e-6
Test.@test Poisson(F, G, NonAutonomous)(2, [1, 2, 3], [4, 0, 4]) ≈ Poisson(F_, G_, NonAutonomous)(2, [1, 2, 3], [4, 0, 4]) atol=1e-6
Test.@test Poisson(F, G_, NonAutonomous)(2, [1, 2, 3], [4, 0, 4]) ≈ Poisson(F_, G, NonAutonomous)(2, [1, 2, 3], [4, 0, 4]) atol=1e-6
end

@testset "autonomous nonfixed case" begin
Expand All @@ -401,19 +407,19 @@ function test_differential_geometry()
G = Lift(g, NonFixed)
F_ = (x, p, v) -> p' * f(x, v)
G_ = (x, p, v) -> p' * g(x, v)
Test.@test Poisson(F, G)([1, 2, 3], [4, 0, 4], 1) ≈ Poisson(F_, G_, NonFixed)([1, 2, 3], [4, 0, 4], 1) atol=1e-6
Test.@test Poisson(F, G_)([1, 2, 3],[4, 0, 4], 1) ≈ Poisson(F_, G)([1, 2, 3],[4, 0, 4], 1) atol=1e-6
Test.@test Poisson(F, G, NonFixed)([1, 2, 3], [4, 0, 4], 1) ≈ Poisson(F_, G_, NonFixed)([1, 2, 3], [4, 0, 4], 1) atol=1e-6
Test.@test Poisson(F, G_, NonFixed)([1, 2, 3],[4, 0, 4], 1) ≈ Poisson(F_, G, NonFixed)([1, 2, 3],[4, 0, 4], 1) atol=1e-6
end

@testset "nonautonomous nonfixed case" begin
f = (t, x, v) -> [t*x[1] + v*x[2]^2, x[1], 0]
g = (t, x, v) -> [0, x[2], t*x[1]^2 + v*4*x[2]]
F = Lift(f,NonAutonomous, NonFixed)
G = Lift(g,NonAutonomous, NonFixed)
F = Lift(f, NonAutonomous, NonFixed)
G = Lift(g, NonAutonomous, NonFixed)
F_ = (t, x, p, v) -> p' * f(t, x, v)
G_ = (t, x, p, v) -> p' * g(t, x, v)
Test.@test Poisson(F, G)(2, [1, 2, 3], [4, 0, 4], 1) ≈ Poisson(F_, G_, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) atol=1e-6
Test.@test Poisson(F, G_)(2, [1, 2, 3], [4, 0, 4], 1) ≈ Poisson(F_, G)(2, [1, 2, 3], [4, 0, 4], 1) atol=1e-6
Test.@test Poisson(F, G, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) ≈ Poisson(F_, G_, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) atol=1e-6
Test.@test Poisson(F, G_, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) ≈ Poisson(F_, G, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) atol=1e-6
end

end
Expand Down
Loading