Skip to content

Commit

Permalink
Improve Problem parametric types
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Bazdresch committed May 6, 2023
1 parent bd4a75d commit 3442673
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
27 changes: 13 additions & 14 deletions src/SinusoidalRegressions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SinusoidalRegressions

using LsqFit
using LsqFit: curve_fit, coef
using RecipesBase
using PrecompileTools
using Zygote
Expand All @@ -9,8 +9,7 @@ using LinearAlgebra
export rmse, mae, torect, sinfit

# export regression types
export SinusoidalFunctionParameters,
SinusoidP, MixedLinearSinusoidP
export SinusoidP, MixedLinearSinusoidP

# export problems
export Sin3Problem, Sin4Problem, MixedLinSin4Problem, MixedLinSin5Problem
Expand Down Expand Up @@ -69,6 +68,10 @@ function sinfit(p::Sin3Problem, ::IEEE1057)
ieee1057(p)
end

function sinfit(p::Sin3Problem, a::LevMar ; kwargs...)
levmar(p, a ; kwargs...)
end

function sinfit(p::Sin4Problem, a::IEEE1057)
ieee1057(p, a)
end
Expand All @@ -77,28 +80,24 @@ function sinfit(p::Sin4Problem, ::IntegralEquations)
jacquelin(p)
end

function sinfit(p::Sin3Problem, a::LevMar ; kwargs...)
levmar(p, a ; kwargs...)
end

function sinfit(p::Sin4Problem, a::LevMar ; kwargs...)
levmar(p, a ; kwargs...)
end

function sinfit(p::MixedLinSin5Problem, ::IntegralEquations)
jacquelin(p)
function sinfit(p::Sin4Problem, a::Liang)
liang(p, a)
end

function sinfit(p::MixedLinSin4Problem, a::LevMar ; kwargs...)
levmar(p, a ; kwargs...)
end

function sinfit(p::MixedLinSin5Problem, a::LevMar ; kwargs...)
levmar(p, a ; kwargs...)
function sinfit(p::MixedLinSin5Problem, ::IntegralEquations)
jacquelin(p)
end

function sinfit(p::Sin4Problem, a::Liang)
liang(p, a)
function sinfit(p::MixedLinSin5Problem, a::LevMar ; kwargs...)
levmar(p, a ; kwargs...)
end

"""
Expand Down Expand Up @@ -168,7 +167,7 @@ torect(M, θ) = (-M*sin(θ), M*cos(θ))
t8 = sinfit(p3, LevMar())

p4 = MixedLinSin5Problem(x, y)
t9 = sinfit(p3, IntegralEquations())
t9 = sinfit(p4, IntegralEquations())
end
end

Expand Down
78 changes: 44 additions & 34 deletions src/typedefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
# Algorithms
#

"""
Algorithm
Supported algorithm types are subtypes of the abstract type `Algorithm`.
Supertype for the different supported algorithms.
"""
abstract type Algorithm end

"""
Expand Down Expand Up @@ -64,8 +70,15 @@ end
# Problems
#

"""
Problem
Supertype for the different kinds of sinusoidal regression problems.
"""
abstract type Problem end

const MR = Union{Missing, Real}

"""
Sin3Problem(X, Y, f , [DC, Q, I, lb, ub]) <: Problem
Expand All @@ -81,16 +94,16 @@ may be specified in `lb` and `ub`, which must be vectors of length 3.
See also: [`Sin4Problem`](@ref)
"""
Base.@kwdef struct Sin3Problem{T1, T2, T3} <: Problem where
{T1 <: AbstractVector, T2 <: AbstractVector, T3 <: Real}
Base.@kwdef struct Sin3Problem{T1, T2, F<:Real, P1<:MR, P2<:MR, P3<:MR, LB, UB} <: Problem
X :: T1 # sampling times
Y :: T2 # sample values
f :: T3 # exact known frequency
DC = missing # initial estimates (may be missing)
Q = missing
I = missing
lb = missing # lower bounds
ub = missing # upper bounds
f :: F # exact known frequency
# initial estimates (may be missing)
DC :: P1 = missing
Q :: P2 = missing
I :: P3 = missing
lb :: LB = missing # lower bounds
ub :: UB = missing # upper bounds
end

Sin3Problem(X, Y, f ; kwargs...) = Sin3Problem(; X, Y, f, kwargs...)
Expand All @@ -109,16 +122,15 @@ bounds may be specified in `lb` and `ub`, which must be vectors of length 4.
See also: [`Sin3Problem`](@ref)
"""
Base.@kwdef struct Sin4Problem{T1, T2} <: Problem where
{T1 <: AbstractVector, T2 <: AbstractVector}
Base.@kwdef struct Sin4Problem{T1, T2, P1<:MR, P2<:MR, P3<:MR, P4<:MR, LB, UB} <: Problem
X :: T1 # sampling times
Y :: T2 # sample values
f = missing # initial estimates (may be missing)
DC = missing
Q = missing
I = missing
lb = missing # lower bounds
ub = missing # uppper bounds
f :: P1 = missing # initial estimates (may be missing)
DC :: P2 = missing
Q :: P3 = missing
I :: P4 = missing
lb :: LB = missing # lower bounds
ub :: UB = missing # uppper bounds
end

Sin4Problem(X, Y ; kwargs...) = Sin4Problem(; X, Y, kwargs...)
Expand All @@ -138,17 +150,16 @@ bounds may be specified in `lb` and `ub`, which must be vectors of length 4.
See also: [`MixedLinSin5Problem`](@ref)
"""
Base.@kwdef struct MixedLinSin4Problem{T1, T2, T3} <: Problem where
{T1 <: AbstractVector, T2 <: AbstractVector, T3 <: Real}
Base.@kwdef struct MixedLinSin4Problem{T1, T2, F<:Real, P1<:MR, P2<:MR, P3<:MR, P4<:MR, LB, UB}
X :: T1
Y :: T2
f :: T3
DC = missing
Q = missing
I = missing
m = missing
lb = missing
ub = missing
f :: F
DC :: P1= missing
Q :: P2 = missing
I :: P3 = missing
m :: P4 = missing
lb :: LB = missing
ub :: UB = missing
end

MixedLinSin4Problem(X, Y, f ; kwargs...) = MixedLinSin4Problem(; X, Y, f, kwargs...)
Expand All @@ -167,17 +178,16 @@ bounds may be specified in `lb` and `ub`, which must be vectors of length 5.
See also: [`MixedLinSin4Problem`](@ref)
"""
Base.@kwdef struct MixedLinSin5Problem{T1, T2} <: Problem where
{T1 <: AbstractVector, T2 <: AbstractVector, T3 <: Real}
Base.@kwdef struct MixedLinSin5Problem{T1, T2, P1<:MR, P2<:MR, P3<:MR, P4<:MR, P5<:MR, LB, UB}
X :: T1
Y :: T2
f = missing
DC = missing
Q = missing
I = missing
m = missing
lb = missing
ub = missing
f :: P1 = missing
DC :: P2 = missing
Q :: P3 = missing
I :: P4 = missing
m :: P5 = missing
lb :: LB = missing
ub :: UB = missing
end

MixedLinSin5Problem(X, Y ; kwargs...) = MixedLinSin5Problem(; X, Y, kwargs...)
Expand Down

0 comments on commit 3442673

Please sign in to comment.