diff --git a/src/differential_geometry.jl b/src/differential_geometry.jl index 75b858f..f1acb0e 100644 --- a/src/differential_geometry.jl +++ b/src/differential_geometry.jl @@ -51,29 +51,6 @@ function Lift(X::Function; autonomous::Bool = true, variable::Bool = false)::Fun end end -""" -$(TYPEDSIGNATURES) - -Return the Lift of a function. -Dependencies are specified with DataType : Autonomous, NonAutonomous and Fixed, NonFixed. - -# Example -```@example -julia> H = Lift(x -> 2x) -julia> H(1, 1) -2 -julia> H = Lift((t, x, v) -> 2x + t - v, NonAutonomous, NonFixed) -julia> H(1, 1, 1, 1) -2 -``` -""" -function Lift(X::Function, dependences::DataType...)::Function - __check_dependencies(dependences) - autonomous = NonAutonomous ∈ dependences ? false : true - variable = NonFixed ∈ dependences ? true : false - return Lift(X; autonomous = autonomous, variable = variable) -end - # --------------------------------------------------------------------------- # Lie derivative of a scalar function along a vector field or a function: L_X(f) = X⋅f @@ -179,34 +156,7 @@ julia> Lie(φ, f, autonomous=false, variable=true)(1, [1, 2], [2, 1]) ``` """ function Lie(X::Function, f::Function; autonomous::Bool = true, variable::Bool = false)::Function - time_dependence = autonomous ? Autonomous : NonAutonomous - variable_dependence = variable ? NonFixed : Fixed - return Lie(VectorField(X, time_dependence, variable_dependence), f) -end - -""" -$(TYPEDSIGNATURES) - -Lie derivative of a scalar function along a vector field or a function. -Dependencies are specified with DataType : Autonomous, NonAutonomous and Fixed, NonFixed. - -# Example -```@example -julia> φ = x -> [x[2], -x[1]] -julia> f = x -> x[1]^2 + x[2]^2 -julia> Lie(φ,f)([1, 2]) -0 -julia> φ = (t, x, v) -> [t + x[2] + v[1], -x[1] + v[2]] -julia> f = (t, x, v) -> t + x[1]^2 + x[2]^2 -julia> Lie(φ, f, NonAutonomous, NonFixed)(1, [1, 2], [2, 1]) -10 -``` -""" -function Lie(X::Function, f::Function, dependences::DataType...)::Function - __check_dependencies(dependences) - time_dependence = NonAutonomous ∈ dependences ? NonAutonomous : Autonomous - variable_dependence = NonFixed ∈ dependences ? NonFixed : Fixed - return Lie(VectorField(X, time_dependence, variable_dependence), f) + return Lie(VectorField(X; autonomous=autonomous, variable=variable), f) end # --------------------------------------------------------------------------- @@ -468,39 +418,9 @@ function Poisson( autonomous::Bool = true, variable::Bool = false, )::Hamiltonian - TD = autonomous ? Autonomous : NonAutonomous - VD = variable ? NonFixed : Fixed return Poisson( - Hamiltonian(f, TD, VD), - Hamiltonian(g, TD, VD), - ) -end - -""" -$(TYPEDSIGNATURES) - -Poisson bracket of two functions : {f, g} = Poisson(f, g) -Dependencies are specified with DataType : Autonomous, NonAutonomous and Fixed, NonFixed. - -# Example -```@example -julia> f = (x, p) -> x[2]^2 + 2x[1]^2 + p[1]^2 -julia> g = (x, p) -> 3x[2]^2 + -x[1]^2 + p[2]^2 + p[1] -julia> Poisson(f, g)([1, 2], [2, 1]) --20 -julia> f = (t, x, p, v) -> t*v[1]*x[2]^2 + 2x[1]^2 + p[1]^2 + v[2] -julia> g = (t, x, p, v) -> 3x[2]^2 + -x[1]^2 + p[2]^2 + p[1] + t - v[2] -julia> Poisson(f, g, NonAutonomous, NonFixed)(2, [1, 2], [2, 1], [4, 4]) --76 -``` -""" -function Poisson(f::Function, g::Function, dependences::DataType...)::Hamiltonian - __check_dependencies(dependences) - TD = NonAutonomous ∈ dependences ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependences ? NonFixed : Fixed - return Poisson( - Hamiltonian(f, TD, VD), - Hamiltonian(g, TD, VD), + Hamiltonian(f, autonomous=autonomous, variable=variable), + Hamiltonian(g, autonomous=autonomous, variable=variable), ) end @@ -644,21 +564,9 @@ macro Lie(expr::Expr, arg1, arg2) local variable = false @match arg1 begin - :(Autonomous) => begin - autonomous = true - end - :(NonAutonomous) => begin - autonomous = false - end :(autonomous = $a) => begin autonomous = a end - :(NonFixed) => begin - variable = true - end - :(Fixed) => begin - variable = false - end :(variable = $a) => begin variable = a end @@ -666,21 +574,9 @@ macro Lie(expr::Expr, arg1, arg2) end @match arg2 begin - :(Autonomous) => begin - autonomous = true - end - :(NonAutonomous) => begin - autonomous = false - end :(autonomous = $a) => begin autonomous = a end - :(NonFixed) => begin - variable = true - end - :(Fixed) => begin - variable = false - end :(variable = $a) => begin variable = a end @@ -723,21 +619,9 @@ macro Lie(expr::Expr, arg) local variable = false @match arg begin - :(Autonomous) => begin - autonomous = true - end - :(NonAutonomous) => begin - autonomous = false - end :(autonomous = $a) => begin autonomous = a end - :(NonFixed) => begin - variable = true - end - :(Fixed) => begin - variable = false - end :(variable = $a) => begin variable = a end diff --git a/src/functions.jl b/src/functions.jl index ede1698..4c4e02e 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -35,17 +35,8 @@ julia> B = BoundaryConstraint((x0, xf, v) -> [v[3]+xf[2]-x0[1], v[1]-v[2]+2xf[1] ``` """ -function BoundaryConstraint(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return BoundaryConstraint{typeof(f), VD}(f) -end - -function BoundaryConstraint!(f!::Function, dependencies::DataType...) - __check_dependencies(dependencies) - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return BoundaryConstraint!{typeof(f!), VD}(f!) -end +BoundaryConstraint(f::Function, VD::Type{<:VariableDependence}) = BoundaryConstraint{typeof(f), VD}(f) +BoundaryConstraint!(f!::Function, VD::Type{<:VariableDependence}) = BoundaryConstraint!{typeof(f!), VD}(f!) """ @@ -126,17 +117,8 @@ julia> G = Mayer((x0, xf, v) -> v[3]+xf[2]-x0[1], NonFixed) ``` """ -function Mayer(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Mayer{typeof(f),VD}(f) -end - -function Mayer!(f!::Function, dependencies::DataType...) - __check_dependencies(dependencies) - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Mayer!{typeof(f!), VD}(f!) -end +Mayer(f::Function, VD::Type{<:VariableDependence}) = Mayer{typeof(f),VD}(f) +Mayer!(f!::Function, VD::Type{<:VariableDependence}) = Mayer!{typeof(f!), VD}(f!) """ @@ -213,12 +195,7 @@ julia> H = Hamiltonian((t, x, p, v) -> [t+x[1]^2+2p[2]+v[3]], NonAutonomous, Non ``` """ -function Hamiltonian(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Hamiltonian{typeof(f), TD, VD}(f) -end +Hamiltonian(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = Hamiltonian{typeof(f), TD, VD}(f) """ @@ -227,10 +204,6 @@ $(TYPEDSIGNATURES) Return the value of the Hamiltonian. ```@example -julia> Hamiltonian((x, p) -> x + p, Int64) -IncorrectArgument -julia> Hamiltonian((x, p) -> x + p, Int64) -IncorrectArgument julia> H = Hamiltonian((x, p) -> [x[1]^2+2p[2]]) # autonomous=true, variable=false julia> H([1, 0], [0, 1]) MethodError # H must return a scalar @@ -333,20 +306,13 @@ Return an ```HamiltonianLift``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> HamiltonianLift(HamiltonianLift(VectorField(x -> [x[1]^2, 2x[2]], Int64)) -IncorrectArgument julia> HL = HamiltonianLift(x -> [x[1]^2,x[2]^2], Autonomous, Fixed) julia> HL = HamiltonianLift((x, v) -> [x[1]^2,x[2]^2+v], Autonomous, NonFixed) julia> HL = HamiltonianLift((t, x) -> [t+x[1]^2,x[2]^2], NonAutonomous, Fixed) julia> HL = HamiltonianLift((t, x, v) -> [t+x[1]^2,x[2]^2+v], NonAutonomous, NonFixed) ``` """ -function HamiltonianLift(f::Function, dependences::DataType...) - __check_dependencies(dependences) - TD = NonAutonomous ∈ dependences ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependences ? NonFixed : Fixed - return HamiltonianLift(VectorField(f, TD, VD)) -end +HamiltonianLift(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = HamiltonianLift(VectorField(f, TD, VD)) """ @@ -357,8 +323,6 @@ Return the value of the HamiltonianLift. ## Examples ```@example -julia> HamiltonianLift(HamiltonianLift(VectorField(x -> [x[1]^2, 2x[2]], Int64)) -IncorrectArgument julia> H = HamiltonianLift(VectorField(x -> [x[1]^2, 2x[2]])) julia> H([1, 2], [1, 1]) 5 @@ -463,10 +427,6 @@ Return an ```HamiltonianVectorField``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> HamiltonianVectorField((x, p) -> [x[1]^2+2p[2], x[2]-3p[2]^2], Int64) -IncorrectArgument -julia> HamiltonianVectorField((x, p) -> [x[1]^2+2p[2], x[2]-3p[2]^2], Int64) -IncorrectArgument julia> Hv = HamiltonianVectorField((x, p) -> [x[1]^2+2p[2], x[2]-3p[2]^2]) # autonomous=true, variable=false julia> Hv = HamiltonianVectorField((x, p, v) -> [x[1]^2+2p[2]+v[3], x[2]-3p[2]^2+v[4]], NonFixed) julia> Hv = HamiltonianVectorField((t, x, p) -> [t+x[1]^2+2p[2], x[2]-3p[2]^2], NonAutonomous) @@ -474,12 +434,7 @@ julia> Hv = HamiltonianVectorField((t, x, p, v) -> [t+x[1]^2+2p[2]+v[3], x[2]-3p ``` """ -function HamiltonianVectorField(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return HamiltonianVectorField{typeof(f), TD, VD}(f) -end +HamiltonianVectorField(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = HamiltonianVectorField{typeof(f), TD, VD}(f) """ @@ -490,10 +445,6 @@ Return the value of the HamiltonianVectorField. ## Examples ```@example -julia> HamiltonianVectorField((x, p) -> [x[1]^2+2p[2], x[2]-3p[2]^2], Int64) -IncorrectArgument -julia> HamiltonianVectorField((x, p) -> [x[1]^2+2p[2], x[2]-3p[2]^2], Int64) -IncorrectArgument julia> Hv = HamiltonianVectorField((x, p) -> [x[1]^2+2p[2], x[2]-3p[2]^2]) # autonomous=true, variable=false julia> Hv([1, 0], [0, 1]) [3, -3] @@ -609,10 +560,6 @@ Return a ```VectorField``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> VectorField(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument -julia> VectorField(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument julia> V = VectorField(x -> [x[1]^2, 2x[2]]) # autonomous=true, variable=false julia> V = VectorField((x, v) -> [x[1]^2, 2x[2]+v[3]], NonFixed) julia> V = VectorField((t, x) -> [t+x[1]^2, 2x[2]], NonAutonomous) @@ -620,12 +567,7 @@ julia> V = VectorField((t, x, v) -> [t+x[1]^2, 2x[2]+v[3]], NonAutonomous, NonFi ``` """ -function VectorField(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return VectorField{typeof(f), TD, VD}(f) -end +VectorField(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = VectorField{typeof(f), TD, VD}(f) """ @@ -636,10 +578,6 @@ Return the value of the VectorField. ## Examples ```@example -julia> VectorField(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument -julia> VectorField(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument julia> V = VectorField(x -> [x[1]^2, 2x[2]]) # autonomous=true, variable=false julia> V([1, -1]) [1, -2] @@ -732,10 +670,6 @@ Return a ```Lagrange``` cost of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> Lagrange((x, u) -> 2x[2]-u[1]^2, Int64) -IncorrectArgument -julia> Lagrange((x, u) -> 2x[2]-u[1]^2, Int64) -IncorrectArgument julia> L = Lagrange((x, u) -> [2x[2]-u[1]^2], autonomous=true, variable=false) julia> L = Lagrange((x, u) -> 2x[2]-u[1]^2, autonomous=true, variable=false) julia> L = Lagrange((x, u, v) -> 2x[2]-u[1]^2+v[3], autonomous=true, variable=true) @@ -745,19 +679,8 @@ julia> L = Lagrange((t, x, u, v) -> t+2x[2]-u[1]^2+v[3], autonomous=false, varia ``` """ -function Lagrange(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Lagrange{typeof(f), TD, VD}(f) -end - -function Lagrange!(f!::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Lagrange!{typeof(f!), TD, VD}(f!) -end +Lagrange(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = Lagrange{typeof(f), TD, VD}(f) +Lagrange!(f!::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = Lagrange!{typeof(f!), TD, VD}(f!) """ @@ -768,10 +691,6 @@ Return the value of the Lagrange function. ## Examples ```@example -julia> Lagrange((x, u) -> 2x[2]-u[1]^2, Int64) -IncorrectArgument -julia> Lagrange((x, u) -> 2x[2]-u[1]^2, Int64) -IncorrectArgument julia> L = Lagrange((x, u) -> [2x[2]-u[1]^2], autonomous=true, variable=false) julia> L([1, 0], [1]) MethodError @@ -903,29 +822,14 @@ Return the ```Dynamics``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> Dynamics((x, u) -> [2x[2]-u^2, x[1]], Int64) -IncorrectArgument -julia> Dynamics((x, u) -> [2x[2]-u^2, x[1]], Int64) -IncorrectArgument julia> D = Dynamics((x, u) -> [2x[2]-u^2, x[1]], Autonomous, Fixed) julia> D = Dynamics((x, u, v) -> [2x[2]-u^2+v[3], x[1]], Autonomous, NonFixed) julia> D = Dynamics((t, x, u) -> [t+2x[2]-u^2, x[1]], NonAutonomous, Fixed) julia> D = Dynamics((t, x, u, v) -> [t+2x[2]-u^2+v[3], x[1]], NonAutonomous, NonFixed) ``` """ -function Dynamics(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Dynamics{typeof(f), TD, VD}(f) -end - -function Dynamics!(f!::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Dynamics!{typeof(f!), TD, VD}(f!) -end +Dynamics(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = Dynamics{typeof(f), TD, VD}(f) +Dynamics!(f!::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = Dynamics!{typeof(f!), TD, VD}(f!) """ @@ -1058,29 +962,14 @@ Return the ```StateConstraint``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> StateConstraint(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument -julia> StateConstraint(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument julia> S = StateConstraint(x -> [x[1]^2, 2x[2]], Autonomous, Fixed) julia> S = StateConstraint((x, v) -> [x[1]^2, 2x[2]+v[3]], Autonomous, NonFixed) julia> S = StateConstraint((t, x) -> [t+x[1]^2, 2x[2]], NonAutonomous, Fixed) julia> S = StateConstraint((t, x, v) -> [t+x[1]^2, 2x[2]+v[3]], NonAutonomous, NonFixed) ``` """ -function StateConstraint(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return StateConstraint{typeof(f), TD, VD}(f) -end - -function StateConstraint!(f!::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return StateConstraint!{typeof(f!), TD, VD}(f!) -end +StateConstraint(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = StateConstraint{typeof(f), TD, VD}(f) +StateConstraint!(f!::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = StateConstraint!{typeof(f!), TD, VD}(f!) """ @@ -1089,10 +978,6 @@ $(TYPEDSIGNATURES) Return the value of the StateConstraint function. ```@example -julia> StateConstraint(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument -julia> StateConstraint(x -> [x[1]^2, 2x[2]], Int64) -IncorrectArgument julia> S = StateConstraint(x -> [x[1]^2, 2x[2]], autonomous=true, variable=false) julia> S([1, -1]) [1, -2] @@ -1207,27 +1092,14 @@ Return the ```StateConstraint``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> IncorrectArgument ControlConstraint(u -> [u[1]^2, 2u[2]], Int64) -julia> IncorrectArgument ControlConstraint(u -> [u[1]^2, 2u[2]], Int64) julia> C = ControlConstraint(u -> [u[1]^2, 2u[2]], Autonomous, Fixed) julia> C = ControlConstraint((u, v) -> [u[1]^2, 2u[2]+v[3]], Autonomous, NonFixed) julia> C = ControlConstraint((t, u) -> [t+u[1]^2, 2u[2]], NonAutonomous, Fixed) julia> C = ControlConstraint((t, u, v) -> [t+u[1]^2, 2u[2]+v[3]], NonAutonomous, NonFixed) ``` """ -function ControlConstraint(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return ControlConstraint{typeof(f), TD, VD}(f) -end - -function ControlConstraint!(f!::Function, dependencies::DataType...) # todo: useless? only use version above? - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return ControlConstraint!{typeof(f!), TD, VD}(f!) -end +ControlConstraint(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = ControlConstraint{typeof(f), TD, VD}(f) +ControlConstraint!(f!::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = ControlConstraint!{typeof(f!), TD, VD}(f!) """ @@ -1236,8 +1108,6 @@ $(TYPEDSIGNATURES) Return the value of the ControlConstraint function. ```@example -julia> IncorrectArgument ControlConstraint(u -> [u[1]^2, 2u[2]], Int64) -julia> IncorrectArgument ControlConstraint(u -> [u[1]^2, 2u[2]], Int64) julia> C = ControlConstraint(u -> [u[1]^2, 2u[2]], autonomous=true, variable=false) julia> C([1, -1]) [1, -2] @@ -1351,29 +1221,14 @@ Return the ```MixedConstraint``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> MixedConstraint((x, u) -> [2x[2]-u^2, x[1]], Int64) -IncorrectArgument -julia> MixedConstraint((x, u) -> [2x[2]-u^2, x[1]], Int64) -IncorrectArgument julia> M = MixedConstraint((x, u) -> [2x[2]-u^2, x[1]], Autonomous, Fixed) julia> M = MixedConstraint((x, u, v) -> [2x[2]-u^2+v[3], x[1]], Autonomous, NonFixed) julia> M = MixedConstraint((t, x, u) -> [t+2x[2]-u^2, x[1]], NonAutonomous, Fixed) julia> M = MixedConstraint((t, x, u, v) -> [t+2x[2]-u^2+v[3], x[1]], NonAutonomous, NonFixed) ``` """ -function MixedConstraint(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return MixedConstraint{typeof(f), TD, VD}(f) -end - -function MixedConstraint!(f!::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return MixedConstraint!{typeof(f!), TD, VD}(f!) -end +MixedConstraint(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = MixedConstraint{typeof(f), TD, VD}(f) +MixedConstraint!(f!::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = MixedConstraint!{typeof(f!), TD, VD}(f!) """ @@ -1382,10 +1237,6 @@ $(TYPEDSIGNATURES) Return the value of the MixedConstraint function. ```@example -julia> MixedConstraint((x, u) -> [2x[2]-u^2, x[1]], Int64) -IncorrectArgument -julia> MixedConstraint((x, u) -> [2x[2]-u^2, x[1]], Int64) -IncorrectArgument julia> M = MixedConstraint((x, u) -> [2x[2]-u^2, x[1]], autonomous=true, variable=false) julia> M([1, 0], 1) [-1, 1] @@ -1560,22 +1411,13 @@ Return the ```FeedbackControl``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> FeedbackControl(x -> x[1]^2+2x[2], Int64) -IncorrectArgument -julia> FeedbackControl(x -> x[1]^2+2x[2], Int64) -IncorrectArgument julia> u = FeedbackControl(x -> x[1]^2+2x[2], Autonomous, Fixed) julia> u = FeedbackControl((x, v) -> x[1]^2+2x[2]+v[3], Autonomous, NonFixed) julia> u = FeedbackControl((t, x) -> t+x[1]^2+2x[2], NonAutonomous, Fixed) julia> u = FeedbackControl((t, x, v) -> t+x[1]^2+2x[2]+v[3], NonAutonomous, NonFixed) ``` """ -function FeedbackControl(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return FeedbackControl{typeof(f), TD, VD}(f) -end +FeedbackControl(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = FeedbackControl{typeof(f), TD, VD}(f) """ @@ -1584,10 +1426,6 @@ $(TYPEDSIGNATURES) Return the value of the FeedbackControl function. ```@example -julia> FeedbackControl(x -> x[1]^2+2x[2], Int64) -IncorrectArgument -julia> FeedbackControl(x -> x[1]^2+2x[2], Int64) -IncorrectArgument julia> u = FeedbackControl(x -> x[1]^2+2x[2], autonomous=true, variable=false) julia> u([1, 0]) 1 @@ -1671,22 +1509,13 @@ Return the ```ControlLaw``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> ControlLaw((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument -julia> ControlLaw((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument julia> u = ControlLaw((x, p) -> x[1]^2+2p[2], Autonomous, Fixed) julia> u = ControlLaw((x, p, v) -> x[1]^2+2p[2]+v[3], Autonomous, NonFixed) julia> u = ControlLaw((t, x, p) -> t+x[1]^2+2p[2], NonAutonomous, Fixed) julia> u = ControlLaw((t, x, p, v) -> t+x[1]^2+2p[2]+v[3], NonAutonomous, NonFixed) ``` """ -function ControlLaw(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return ControlLaw{typeof(f), TD, VD}(f) -end +ControlLaw(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = ControlLaw{typeof(f), TD, VD}(f) """ @@ -1695,10 +1524,6 @@ $(TYPEDSIGNATURES) Return the value of the ControlLaw function. ```@example -julia> ControlLaw((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument -julia> ControlLaw((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument julia> u = ControlLaw((x, p) -> x[1]^2+2p[2], autonomous=true, variable=false) julia> u([1, 0], [0, 1]) 3 @@ -1787,22 +1612,13 @@ Return the ```Multiplier``` of a function. Dependencies are specified with DataType, Autonomous, NonAutonomous and Fixed, NonFixed. ```@example -julia> Multiplier((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument -julia> Multiplier((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument julia> μ = Multiplier((x, p) -> x[1]^2+2p[2], Autonomous, Fixed) julia> μ = Multiplier((x, p, v) -> x[1]^2+2p[2]+v[3], Autonomous, NonFixed) julia> μ = Multiplier((t, x, p) -> t+x[1]^2+2p[2], NonAutonomous, Fixed) julia> μ = Multiplier((t, x, p, v) -> t+x[1]^2+2p[2]+v[3], NonAutonomous, NonFixed) ``` """ -function Multiplier(f::Function, dependencies::DataType...) - __check_dependencies(dependencies) - TD = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous - VD = NonFixed ∈ dependencies ? NonFixed : Fixed - return Multiplier{typeof(f), TD, VD}(f) -end +Multiplier(f::Function, TD::Type{<:TimeDependence}, VD::Type{<:VariableDependence}) = Multiplier{typeof(f), TD, VD}(f) """ @@ -1811,10 +1627,6 @@ $(TYPEDSIGNATURES) Return the value of the Multiplier function. ```@example -julia> Multiplier((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument -julia> Multiplier((x, p) -> x[1]^2+2p[2], Int64) -IncorrectArgument julia> μ = Multiplier((x, p) -> x[1]^2+2p[2], autonomous=true, variable=false) julia> μ([1, 0], [0, 1]) 3 diff --git a/test/test_differential_geometry.jl b/test/test_differential_geometry.jl index d1913c9..22cfb28 100644 --- a/test/test_differential_geometry.jl +++ b/test/test_differential_geometry.jl @@ -49,16 +49,16 @@ function test_differential_geometry() variable = true, ) @test HL(1, [1, 0], [0, 1], 1) == 1 - HL = HamiltonianLift(x -> [x[1]^2, x[2]^2], Autonomous, Fixed) + HL = HamiltonianLift(x -> [x[1]^2, x[2]^2], autonomous=true, variable=false) @test HL([1, 0], [0, 1]) == 0 @test HL(1, [1, 0], [0, 1], 1) == 0 - HL = HamiltonianLift((x, v) -> [x[1]^2, x[2]^2 + v], Autonomous, NonFixed) + HL = HamiltonianLift((x, v) -> [x[1]^2, x[2]^2 + v], autonomous=true, variable=true) @test HL([1, 0], [0, 1], 1) == 1 @test HL(1, [1, 0], [0, 1], 1) == 1 - HL = HamiltonianLift((t, x) -> [t + x[1]^2, x[2]^2], NonAutonomous, Fixed) + HL = HamiltonianLift((t, x) -> [t + x[1]^2, x[2]^2], autonomous=false, variable=false) @test HL(1, [1, 0], [0, 1]) == 0 @test HL(1, [1, 0], [0, 1], 1) == 0 - HL = HamiltonianLift((t, x, v) -> [t + x[1]^2, x[2]^2 + v], NonAutonomous, NonFixed) + HL = HamiltonianLift((t, x, v) -> [t + x[1]^2, x[2]^2 + v], autonomous=false, variable=true) @test HL(1, [1, 0], [0, 1], 1) == 1 end @@ -73,13 +73,13 @@ function test_differential_geometry() Test.@test_throws MethodError H(1, [1, 2]) # nonautonomous case - X = VectorField((t, x) -> 2x, NonAutonomous) + X = VectorField((t, x) -> 2x, autonomous=false, variable=false) H = Lift(X) Test.@test H(1, 1, 1) == 2 Test.@test H(1, [1, 2], [3, 4]) == 22 # autonomous nonfixed case - X = VectorField((x, v) -> 2x, NonFixed) + X = VectorField((x, v) -> 2x, autonomous=true, variable=true) H = Lift(X) Test.@test H(1, 1, 1) == 2 Test.@test H([1, 2], [3, 4], 1) == 22 @@ -87,7 +87,7 @@ function test_differential_geometry() Test.@test_throws MethodError H(1, [1, 2]) # nonautonomous nonfixed case - X = VectorField((t, x, v) -> 2x, NonAutonomous, NonFixed) + X = VectorField((t, x, v) -> 2x, autonomous=false, variable=true) H = Lift(X) Test.@test H(1, 1, 1, 1) == 2 Test.@test H(1, [1, 2], [3, 4], 1) == 22 @@ -124,9 +124,6 @@ function test_differential_geometry() 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) end end # tests for Lift @@ -150,45 +147,45 @@ function test_differential_geometry() # nonautonomous, dim 2 φ = (t, x) -> [t + x[2], -x[1]] - X = VectorField(φ, NonAutonomous) + X = VectorField(φ, autonomous=false, variable=false) f = (t, x) -> t + x[1]^2 + x[2]^2 Test.@test (X ⋅ f)(1, [1, 2]) == 2 - Test.@test Lie(φ, f, NonAutonomous)(1, [1, 2]) == 2 + Test.@test Lie(φ, f, autonomous=false, variable=false)(1, [1, 2]) == 2 # nonautonomous, dim 1 φ = (t, x) -> 2x + t - X = VectorField(φ, NonAutonomous) + X = VectorField(φ, autonomous=false, variable=false) f = (t, x) -> t + x^2 Test.@test (X ⋅ f)(1, 1) == 6 - Test.@test Lie(φ, f, NonAutonomous)(1, 1) == 6 + Test.@test Lie(φ, f, autonomous=false, variable=false)(1, 1) == 6 # autonomous, nonfixed, dim 2 φ = (x, v) -> [x[2] + v[1], -x[1] + v[2]] - X = VectorField(φ, NonFixed) + X = VectorField(φ, autonomous=true, variable=true) f = (x, v) -> x[1]^2 + x[2]^2 Test.@test (X ⋅ f)([1, 2], [2, 1]) == 8 - Test.@test Lie(φ, f, NonFixed)([1, 2], [2, 1]) == 8 + Test.@test Lie(φ, f, autonomous=true, variable=true)([1, 2], [2, 1]) == 8 # autonomous, nonfixed, dim 1 φ = (x, v) -> 2x + v - X = VectorField(φ, NonFixed) + X = VectorField(φ, autonomous=true, variable=true) f = (x, v) -> x^2 Test.@test (X ⋅ f)(1, 1) == 6 - Test.@test Lie(φ, f, NonFixed)(1, 1) == 6 + Test.@test Lie(φ, f, autonomous=true, variable=true)(1, 1) == 6 # nonautonomous, nonfixed, dim 2 φ = (t, x, v) -> [t + x[2] + v[1], -x[1] + v[2]] - X = VectorField(φ, NonAutonomous, NonFixed) + X = VectorField(φ, autonomous=false, variable=true) f = (t, x, v) -> t + x[1]^2 + x[2]^2 Test.@test (X ⋅ f)(1, [1, 2], [2, 1]) == 10 - Test.@test Lie(φ, f, NonAutonomous, NonFixed)(1, [1, 2], [2, 1]) == 10 + Test.@test Lie(φ, f, autonomous=false, variable=true)(1, [1, 2], [2, 1]) == 10 # nonautonomous, nonfixed, dim 1 φ = (t, x, v) -> 2x + t + v - X = VectorField(φ, NonAutonomous, NonFixed) + X = VectorField(φ, autonomous=false, variable=true) f = (t, x, v) -> t + x^2 Test.@test (X ⋅ f)(1, 1, 1) == 8 - Test.@test Lie(φ, f, NonAutonomous, NonFixed)(1, 1, 1) == 8 + Test.@test Lie(φ, f, autonomous=false, variable=true)(1, 1, 1) == 8 end @testset "Directional derivative of a vector field" begin @@ -204,33 +201,33 @@ function test_differential_geometry() Test.@test CTBase.:(⅋)(X, Y)(1) == 6 # nonautonomous, dim 2 - X = VectorField((t, x) -> [t + x[2], -x[1]], NonAutonomous) - Y = VectorField((t, x) -> [t + x[1], x[2]], NonAutonomous) + X = VectorField((t, x) -> [t + x[2], -x[1]], autonomous=false, variable=false) + Y = VectorField((t, x) -> [t + x[1], x[2]], autonomous=false, variable=false) Test.@test CTBase.:(⅋)(X, Y)(1, [1, 2]) == [3, -1] # nonautonomous, dim 1 - X = VectorField((t, x) -> 2x + t, NonAutonomous) - Y = VectorField((t, x) -> 3x + t, NonAutonomous) + X = VectorField((t, x) -> 2x + t, autonomous=false, variable=false) + Y = VectorField((t, x) -> 3x + t, autonomous=false, variable=false) Test.@test CTBase.:(⅋)(X, Y)(1, 1) == 9 # autonomous, nonfixed, dim 1 - X = VectorField((x, v) -> 2x + v, NonFixed) - Y = VectorField((x, v) -> 3x + v, NonFixed) + X = VectorField((x, v) -> 2x + v, autonomous=true, variable=true) + Y = VectorField((x, v) -> 3x + v, autonomous=true, variable=true) Test.@test CTBase.:(⅋)(X, Y)(1, 1) == 9 # nonautonomous, nonfixed, dim 1 - X = VectorField((t, x, v) -> t + 2x + v, NonAutonomous, NonFixed) - Y = VectorField((t, x, v) -> t + 3x + v, NonAutonomous, NonFixed) + X = VectorField((t, x, v) -> t + 2x + v, autonomous=false, variable=true) + Y = VectorField((t, x, v) -> t + 3x + v, autonomous=false, variable=true) Test.@test CTBase.:(⅋)(X, Y)(1, 1, 1) == 12 # autonomous, nonfixed, dim 2 - X = VectorField((x, v) -> [v[1] + v[2] + x[2], -x[1]], NonFixed) - Y = VectorField((x, v) -> [v[1] + v[2] + x[1], x[2]], NonFixed) + X = VectorField((x, v) -> [v[1] + v[2] + x[2], -x[1]], autonomous=true, variable=true) + Y = VectorField((x, v) -> [v[1] + v[2] + x[1], x[2]], autonomous=true, variable=true) Test.@test CTBase.:(⅋)(X, Y)([1, 2], [2, 3]) == [7, -1] # nonautonomous, nonfixed, dim 2 - X = VectorField((t, x, v) -> [t + v[1] + v[2] + x[2], -x[1]], NonFixed, NonAutonomous) - Y = VectorField((t, x, v) -> [v[1] + v[2] + x[1], x[2]], NonFixed, NonAutonomous) + X = VectorField((t, x, v) -> [t + v[1] + v[2] + x[2], -x[1]], autonomous=false, variable=true) + Y = VectorField((t, x, v) -> [v[1] + v[2] + x[1], x[2]], autonomous=false, variable=true) Test.@test CTBase.:(⅋)(X, Y)(1, [1, 2], [2, 3]) == [8, -1] end @@ -246,15 +243,15 @@ function test_differential_geometry() @testset "nonautonomous case" begin f = (t, x) -> [t + x[2], -2x[1]] g = (t, x) -> [t + 3x[2], -x[1]] - X = VectorField(f, NonAutonomous) - Y = VectorField(g, NonAutonomous) + X = VectorField(f, autonomous=false, variable=false) + Y = VectorField(g, autonomous=false, variable=false) Test.@test Lie(X, Y)(1, [1, 2]) == [-5, 11] end @testset "autonomous nonfixed case" begin f = (x, v) -> [x[2] + v, 2x[1]] g = (x, v) -> [3x[2], v - x[1]] - X = VectorField(f, NonFixed) + X = VectorField(f, autonomous=true, variable=true) Y = VectorField(g, variable = true) Test.@test Lie(X, Y)([1, 2], 1) == [6, -15] end @@ -262,8 +259,8 @@ function test_differential_geometry() @testset "nonautonomous nonfixed case" begin f = (t, x, v) -> [t + x[2] + v, -2x[1] - v] g = (t, x, v) -> [t + 3x[2] + v, -x[1] - v] - X = VectorField(f, NonAutonomous, NonFixed) - Y = VectorField(g, NonAutonomous, NonFixed) + X = VectorField(f, autonomous=false, variable=true) + Y = VectorField(g, autonomous=false, variable=true) Test.@test Lie(X, Y)(1, [1, 2], 1) == [-7, 12] end @@ -448,41 +445,41 @@ 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, autonomous=false, variable=false) + G = Lift(g, autonomous=false, variable=false) F_ = (t, x, p) -> p' * f(t, x) G_ = (t, x, p) -> p' * g(t, x) - 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 + Test.@test Poisson(F, G, autonomous=false, variable=false)(2, [1, 2, 3], [4, 0, 4]) ≈ + Poisson(F_, G_, autonomous=false, variable=false)(2, [1, 2, 3], [4, 0, 4]) atol = 1e-6 + Test.@test Poisson(F, G_, autonomous=false, variable=false)(2, [1, 2, 3], [4, 0, 4]) ≈ + Poisson(F_, G, autonomous=false, variable=false)(2, [1, 2, 3], [4, 0, 4]) atol = 1e-6 end @testset "autonomous nonfixed case" begin f = (x, v) -> [x[1] + v * x[2]^2, x[1], 0] g = (x, v) -> [0, x[2], x[1]^2 + v * 4 * x[2]] - F = Lift(f, NonFixed) - G = Lift(g, NonFixed) + F = Lift(f, autonomous=true, variable=true) + G = Lift(g, autonomous=true, variable=true) F_ = (x, p, v) -> p' * f(x, v) G_ = (x, p, v) -> p' * g(x, v) - 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 + Test.@test Poisson(F, G, autonomous=true, variable=true)([1, 2, 3], [4, 0, 4], 1) ≈ + Poisson(F_, G_, autonomous=true, variable=true)([1, 2, 3], [4, 0, 4], 1) atol = 1e-6 + Test.@test Poisson(F, G_, autonomous=true, variable=true)([1, 2, 3], [4, 0, 4], 1) ≈ + Poisson(F_, G, autonomous=true, variable=true)([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, autonomous=false, variable=true) + G = Lift(g, autonomous=false, variable=true) 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, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) ≈ - Poisson(F_, G_, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) atol = + Test.@test Poisson(F, G, autonomous=false, variable=true)(2, [1, 2, 3], [4, 0, 4], 1) ≈ + Poisson(F_, G_, autonomous=false, variable=true)(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 = + Test.@test Poisson(F, G_, autonomous=false, variable=true)(2, [1, 2, 3], [4, 0, 4], 1) ≈ + Poisson(F_, G, autonomous=false, variable=true)(2, [1, 2, 3], [4, 0, 4], 1) atol = 1e-6 end end @@ -516,9 +513,9 @@ function test_differential_geometry() Test.@test F011_(x) ≈ F011___(x) atol = 1e-6 # nonautonomous - F0 = VectorField((t, x) -> [-Γ * x[1], -Γ * x[2], γ * (1 - x[3])], NonAutonomous) - F1 = VectorField((t, x) -> [0, -x[3], x[2]], NonAutonomous) - F2 = VectorField((t, x) -> [x[3], 0, -x[1]], NonAutonomous) + F0 = VectorField((t, x) -> [-Γ * x[1], -Γ * x[2], γ * (1 - x[3])], autonomous=false, variable=false) + F1 = VectorField((t, x) -> [0, -x[3], x[2]], autonomous=false, variable=false) + F2 = VectorField((t, x) -> [x[3], 0, -x[1]], autonomous=false, variable=false) F01_ = Lie(F0, F1) F011_ = Lie(F01_, F1) F01__ = @Lie [F0, F1] @@ -532,9 +529,9 @@ function test_differential_geometry() Test.@test F011_(t, x) ≈ F011___(t, x) atol = 1e-6 # autonomous nonfixed - F0 = VectorField((x, v) -> [-Γ * x[1], -Γ * x[2], γ * (1 - x[3])], NonFixed) - F1 = VectorField((x, v) -> [0, -x[3], x[2]], NonFixed) - F2 = VectorField((x, v) -> [x[3], 0, -x[1]], NonFixed) + F0 = VectorField((x, v) -> [-Γ * x[1], -Γ * x[2], γ * (1 - x[3])], autonomous=true, variable=true) + F1 = VectorField((x, v) -> [0, -x[3], x[2]], autonomous=true, variable=true) + F2 = VectorField((x, v) -> [x[3], 0, -x[1]], autonomous=true, variable=true) F01_ = Lie(F0, F1) F011_ = Lie(F01_, F1) F01__ = @Lie [F0, F1] @@ -550,11 +547,11 @@ function test_differential_geometry() # nonautonomous nonfixed F0 = VectorField( (t, x, v) -> [-Γ * x[1], -Γ * x[2], γ * (1 - x[3])], - NonAutonomous, - NonFixed, + autonomous=false, + variable=true, ) - F1 = VectorField((t, x, v) -> [0, -x[3], x[2]], NonAutonomous, NonFixed) - F2 = VectorField((t, x, v) -> [x[3], 0, -x[1]], NonAutonomous, NonFixed) + F1 = VectorField((t, x, v) -> [0, -x[3], x[2]], autonomous=false, variable=true) + F2 = VectorField((t, x, v) -> [x[3], 0, -x[1]], autonomous=false, variable=true) F01_ = Lie(F0, F1) F011_ = Lie(F01_, F1) F01__ = @Lie [F0, F1] @@ -593,7 +590,7 @@ function test_differential_geometry() # nonautonomous H0 = Hamiltonian((t, x, p) -> 0.5 * (x[1]^2 + x[2]^2 + p[1]^2), autonomous = false) - H1 = Hamiltonian((t, x, p) -> 0.5 * (x[1]^2 + x[2]^2 + p[2]^2), NonAutonomous) + H1 = Hamiltonian((t, x, p) -> 0.5 * (x[1]^2 + x[2]^2 + p[2]^2), autonomous=false, variable=false) P01 = Poisson(H0, H1) P011 = Poisson(P01, H1) P01_ = @Lie {H0, H1} @@ -625,8 +622,8 @@ function test_differential_geometry() ) H1 = Hamiltonian( (t, x, p, v) -> 0.5 * (x[1]^2 + x[2]^2 + p[2]^2 + v), - NonAutonomous, - NonFixed, + autonomous=false, + variable=true, ) P01 = Poisson(H0, H1) P011 = Poisson(P01, H1) @@ -674,7 +671,7 @@ function test_differential_geometry() Test.@test P01_val ≈ P01_(t, x, p) atol = 1e-6 Test.@test P011(t, x, p) ≈ P011_(t, x, p) atol = 1e-6 get_H0 = () -> H0 - P011__ = @Lie {{get_H0(), H1}, H1} NonAutonomous + P011__ = @Lie {{get_H0(), H1}, H1} autonomous=false variable=false Test.@test P011_(t, x, p) ≈ P011__(t, x, p) atol = 1e-6 # autonomous nonfixed @@ -687,7 +684,7 @@ function test_differential_geometry() Test.@test P01(x, p, v) ≈ P01_(x, p, v) atol = 1e-6 Test.@test P011(x, p, v) ≈ P011_(x, p, v) atol = 1e-6 get_H0 = () -> H0 - P011__ = @Lie {{get_H0(), H1}, H1} NonFixed + P011__ = @Lie {{get_H0(), H1}, H1} autonomous=true variable=true Test.@test P011_(x, p, v) ≈ P011__(x, p, v) atol = 1e-6 # nonautonomous nonfixed @@ -696,11 +693,11 @@ function test_differential_geometry() P01 = Poisson(H0, H1; autonomous = false, variable = true) P011 = Poisson(P01, H1) P01_ = @Lie {H0, H1} autonomous = false variable = true - P011_ = @Lie {{H0, H1}, H1} NonAutonomous NonFixed + P011_ = @Lie {{H0, H1}, H1} autonomous=false variable=true Test.@test P01(t, x, p, v) ≈ P01_(t, x, p, v) atol = 1e-6 Test.@test P011(t, x, p, v) ≈ P011_(t, x, p, v) atol = 1e-6 get_H0 = () -> H0 - P011__ = @Lie {{get_H0(), H1}, H1} NonAutonomous NonFixed + P011__ = @Lie {{get_H0(), H1}, H1} autonomous=false variable=true Test.@test P011_(t, x, p, v) ≈ P011__(t, x, p, v) atol = 1e-6 end @@ -727,11 +724,11 @@ function test_differential_geometry() # nonautonomous nonfixed F0 = VectorField( (t, x, v) -> [-Γ * x[1], -Γ * x[2], γ * (1 - x[3])], - NonAutonomous, - NonFixed, + autonomous=false, + variable=true, ) - F1 = VectorField((t, x, v) -> [0, -x[3], x[2]], NonAutonomous, NonFixed) - F2 = VectorField((t, x, v) -> [x[3], 0, -x[1]], NonAutonomous, NonFixed) + F1 = VectorField((t, x, v) -> [0, -x[3], x[2]], autonomous=false, variable=true) + F2 = VectorField((t, x, v) -> [x[3], 0, -x[1]], autonomous=false, variable=true) Test.@test @Lie [F0, F1](t, x, v) + 4 * [F1, F2](t, x, v) == [8, -8, -2] Test.@test @Lie [F0, F1](t, x, v) - [F1, F2](t, x, v) == [-2, -3, -2] Test.@test @Lie [F0, F1](t, x, v) .* [F1, F2](t, x, v) == [0, 4, 0] diff --git a/test/test_function.jl b/test/test_function.jl index fb69407..d7e217e 100644 --- a/test/test_function.jl +++ b/test/test_function.jl @@ -8,10 +8,6 @@ function test_function() @test BoundaryConstraint(dummy_function, NonFixed) == BoundaryConstraint(dummy_function, variable = true) - @test_throws IncorrectArgument BoundaryConstraint( - (x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], - Int64, - ) B = BoundaryConstraint((x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], variable = false) @test B([0, 0], [1, 1]) == [1, 2] @test B([0, 0], [1, 1], ∅) == [1, 2] @@ -26,7 +22,6 @@ function test_function() @test Mayer(dummy_function, Fixed) == Mayer(dummy_function, variable = false) @test Mayer(dummy_function, NonFixed) == Mayer(dummy_function, variable = true) - @test_throws IncorrectArgument Mayer((x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], Int64) G = Mayer((x0, xf) -> [xf[2] - x0[1]], variable = false) @test_throws MethodError G([0, 0], [1, 1]) G = Mayer((x0, xf) -> xf[2] - x0[1], variable = false) @@ -46,8 +41,6 @@ function test_function() @test Hamiltonian(dummy_function, NonAutonomous, NonFixed) == Hamiltonian(dummy_function, autonomous = false, variable = true) - @test_throws IncorrectArgument Hamiltonian((x, p) -> x + p, Int64) - @test_throws IncorrectArgument Hamiltonian((x, p) -> x + p, Int64) H = Hamiltonian((x, p) -> [x[1]^2 + 2p[2]], autonomous = true, variable = false) @test_throws MethodError H([1, 0], [0, 1]) H = Hamiltonian((x, p) -> x[1]^2 + 2p[2], autonomous = true, variable = false) @@ -81,14 +74,6 @@ function test_function() @test HamiltonianVectorField(dummy_function, NonAutonomous, NonFixed) == HamiltonianVectorField(dummy_function, autonomous = false, variable = true) - @test_throws IncorrectArgument HamiltonianVectorField( - (x, p) -> ([x[1]^2 + 2p[2]], [x[2] - 3p[2]^2]), - Int64, - ) - @test_throws IncorrectArgument HamiltonianVectorField( - (x, p) -> ([x[1]^2 + 2p[2]], [x[2] - 3p[2]^2]), - Int64, - ) Hv = HamiltonianVectorField( (x, p) -> ([x[1]^2 + 2p[2]], [x[2] - 3p[2]^2]), autonomous = true, @@ -131,9 +116,6 @@ function test_function() VectorField(dummy_function, autonomous = true, variable = true) @test VectorField(dummy_function, NonAutonomous, NonFixed) == VectorField(dummy_function, autonomous = false, variable = true) - - @test_throws IncorrectArgument VectorField(x -> [x[1]^2, 2x[2]], Int64) - @test_throws IncorrectArgument VectorField(x -> [x[1]^2, 2x[2]], Int64) V = VectorField(x -> [x[1]^2, 2x[2]], autonomous = true, variable = false) @test V([1, -1]) == [1, -2] t = 1 @@ -165,8 +147,6 @@ function test_function() @test Lagrange(dummy_function, NonAutonomous, NonFixed) == Lagrange(dummy_function, autonomous = false, variable = true) - @test_throws IncorrectArgument Lagrange((x, u) -> 2x[2] - u[1]^2, Int64) - @test_throws IncorrectArgument Lagrange((x, u) -> 2x[2] - u[1]^2, Int64) L = Lagrange((x, u) -> [2x[2] - u[1]^2], autonomous = true, variable = false) @test_throws MethodError L([1, 0], [1]) L = Lagrange((x, u) -> 2x[2] - u[1]^2, autonomous = true, variable = false) @@ -199,8 +179,6 @@ function test_function() # Similar to Lagrange, but the function Dynamics is assumed to return a vector of the same dimension as the state x. # dim x = 2, dim u = 1 # when a dim is 1, consider the element as a scalar - @test_throws IncorrectArgument Dynamics((x, u) -> [2x[2] - u^2, x[1]], Int64) - @test_throws IncorrectArgument Dynamics((x, u) -> [2x[2] - u^2, x[1]], Int64) D = Dynamics((x, u) -> [2x[2] - u^2, x[1]], autonomous = true, variable = false) @test D([1, 0], 1) == [-1, 1] t = 1 @@ -234,8 +212,6 @@ function test_function() # Similar to `VectorField` in the usage, but the dimension of the output of the function StateConstraint is arbitrary. # dim x = 2 - @test_throws IncorrectArgument StateConstraint(x -> [x[1]^2, 2x[2]], Int64) - @test_throws IncorrectArgument StateConstraint(x -> [x[1]^2, 2x[2]], Int64) S = StateConstraint(x -> [x[1]^2, 2x[2]], autonomous = true, variable = false) @test S([1, -1]) == [1, -2] t = 1 @@ -269,8 +245,6 @@ function test_function() # Similar to `VectorField` in the usage, but the dimension of the output of the function ControlConstraint is arbitrary. # dim u = 2 - @test_throws IncorrectArgument ControlConstraint(u -> [u[1]^2, 2u[2]], Int64) - @test_throws IncorrectArgument ControlConstraint(u -> [u[1]^2, 2u[2]], Int64) C = ControlConstraint(u -> [u[1]^2, 2u[2]], autonomous = true, variable = false) @test C([1, -1]) == [1, -2] t = 1 @@ -304,8 +278,6 @@ function test_function() # Similar to `Lagrange` in the usage, but the dimension of the output of the function MixedConstraint is arbitrary. # dim x = 2, dim u = 1, dim output = 2 - @test_throws IncorrectArgument MixedConstraint((x, u) -> [2x[2] - u^2, x[1]], Int64) - @test_throws IncorrectArgument MixedConstraint((x, u) -> [2x[2] - u^2, x[1]], Int64) M = MixedConstraint((x, u) -> [2x[2] - u^2, x[1]], autonomous = true, variable = false) @test M([1, 0], 1) == [-1, 1] t = 1 @@ -352,8 +324,6 @@ function test_function() # Similar to `VectorField` in the usage, but the dimension of the output of the function `f` is arbitrary. # dim x = 2, dim output = 1 - @test_throws IncorrectArgument FeedbackControl(x -> x[1]^2 + 2x[2], Int64) - @test_throws IncorrectArgument FeedbackControl(x -> x[1]^2 + 2x[2], Int64) u = FeedbackControl(x -> x[1]^2 + 2x[2], autonomous = true, variable = false) @test u([1, 0]) == 1 t = 1 @@ -387,8 +357,6 @@ function test_function() # Similar to `Hamiltonian` in the usage, but the dimension of the output of the function `ControlLaw` is arbitrary. # dim x = 2, dim p =2, dim output = 1 - @test_throws IncorrectArgument ControlLaw((x, p) -> x[1]^2 + 2p[2], Int64) - @test_throws IncorrectArgument ControlLaw((x, p) -> x[1]^2 + 2p[2], Int64) u = ControlLaw((x, p) -> x[1]^2 + 2p[2], autonomous = true, variable = false) @test u([1, 0], [0, 1]) == 3 t = 1 @@ -422,8 +390,6 @@ function test_function() # Similar to `ControlLaw` in the usage. # dim x = 2, dim p =2, dim output = 1 - @test_throws IncorrectArgument Multiplier((x, p) -> x[1]^2 + 2p[2], Int64) - @test_throws IncorrectArgument Multiplier((x, p) -> x[1]^2 + 2p[2], Int64) μ = Multiplier((x, p) -> x[1]^2 + 2p[2], autonomous = true, variable = false) @test μ([1, 0], [0, 1]) == 3 t = 1