Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/curve_fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ function curve_fit(
end
end

curve_fit(
model::Function,
xdata::AbstractArray{T},
ydata::AbstractArray{Complex{T}},
p0::AbstractArray{T};
inplace=false,
kwargs...) where {T<:Real} =
curve_fit((x,p) -> reinterpret(T, model(x,p)),
xdata, reinterpret(T, ydata), p0; inplace=inplace, kwargs...)

function curve_fit(
model,
jacobian_model,
Expand Down
20 changes: 20 additions & 0 deletions test/curve_fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,23 @@ end
@test coef(fit)[1] ≈ 1
@test coef(fit_bounded)[1] ≈ 1.22727271
end


@testset "complex" begin
x = collect(1.0:10)
@. model(x, p) = p[1] * x + 1im*p[2]
p = [-2.0, 3]
y = model(x, p)
fit = curve_fit(model, x, y, [0.0, -5.0])
@test fit.converged
@test fit.param ≈ p atol=1e-9

x = range(-10, 10, length=101)
@. model(x, p) = p[1] / (1.0 + 1im * p[3] * (x - p[2]))
p = [2.0, 3, 0.5]
y = model(x, p)
fit = curve_fit(model, x, y, [1.0, -5.0, 0.05];
lower=[0.0, -Inf, 0.0])
@test fit.converged
@test fit.param ≈ p atol=1e-9
end