LsqFit.jl icon indicating copy to clipboard operation
LsqFit.jl copied to clipboard

Allow passing in a scalar function instead of a vector function

Open dpsanders opened this issue 5 years ago • 4 comments

It should be possible to pass in a scalar function to fit instead of vectorising it.

dpsanders avatar Feb 12 '20 22:02 dpsanders

Vectorisation just confuses things and is unnecessary.

dpsanders avatar May 04 '20 02:05 dpsanders

I agree. Vectorization should be the package authors' job, instead of the users'.

Or we can recommend an always-multivariable API, say, the user can always assume that the model is a multivariable function, and user write the model as like f(x::Vector{Float64}, p::Vector{Float64})=p[1]*exp(p[2]*x[1])+p[3].

zhaiyusci avatar Jun 07 '21 08:06 zhaiyusci

I agree

pkofod avatar Jun 09 '21 11:06 pkofod

I've found a pretty convincing case for this (if it wasn't already):

function model(x, p)
    a = p[1:3:end]
    m = p[2:3:end]
    w = p[3:3:end]
    return exp.(-(x .- m').^2 ./ (2w' .^ 2))*a
end

i.e. a normal [no pun intended] sum-of-gaussians. This returns a vector just fine when x is input as a vector, but for scalar x it returns a 1 element Vector, which causes check_data_health to fail (because of isinf(::Vector{Float64})). The only way around this is to write a broadcastable version of the same function and doing Ref fu. That should not be on the user.

gustaphe avatar Sep 25 '21 09:09 gustaphe