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

`MVector` type yields `ImmutableDiffResult`

Open RomeoV opened this issue 2 years ago • 3 comments

Hello, I'm using this package through LsqFit.jl. I have a 3d parameter vector, and would like to speed up the code by leveraging StaticArrays.jl, which basically come for free by passing a StaticVector instead of a Vector. In order to support in-place operations, we can further pass an MVector, i.e. a modifiable statically sized array. However, as seen below, Static Arrays always lead to ImmutableDiffResults.

https://github.com/JuliaDiff/DiffResults.jl/blob/80f085ebddbe2cabd691a61cb56cc1fd7fccd309/src/DiffResults.jl#L31-L52

I've done the following overloads to make it work for my case:

DiffResult(value::MArray, derivs::Tuple{Vararg{MArray}}) = MutableDiffResult(value, derivs)
DiffResult(value::Union{Number, AbstractArray}, derivs::Tuple{Vararg{MVector}}) = MutableDiffResult(value, derivs)

I think this is a useful feature and currently can be considered a bug. If there's interest in merging this I can also open a PR.

RomeoV avatar Aug 15 '23 21:08 RomeoV

I agree that this behavior is not wanted, and I am actually questioning the utility of the mutable/immutable split (see #26)

If we do keep it, perhaps we should base the distinction on the output of ismutable?

gdalle avatar Mar 15 '24 18:03 gdalle

I have opened #28 to keep track

gdalle avatar Mar 15 '24 18:03 gdalle

Reopened cause the PR was temporarily reverted

gdalle avatar Mar 19 '24 09:03 gdalle