Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVector type yields ImmutableDiffResult #25

Open
RomeoV opened this issue Aug 15, 2023 · 3 comments · Fixed by #18
Open

MVector type yields ImmutableDiffResult #25

RomeoV opened this issue Aug 15, 2023 · 3 comments · Fixed by #18

Comments

@RomeoV
Copy link

RomeoV commented Aug 15, 2023

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.

"""
DiffResult(value::Union{Number,AbstractArray}, derivs::Tuple{Vararg{Number}})
DiffResult(value::Union{Number,AbstractArray}, derivs::Tuple{Vararg{AbstractArray}})
Return `r::DiffResult`, with output value storage provided by `value` and output derivative
storage provided by `derivs`.
In reality, `DiffResult` is an abstract supertype of two concrete types, `MutableDiffResult`
and `ImmutableDiffResult`. If all `value`/`derivs` are all `Number`s or `StaticArray`s,
then `r` will be immutable (i.e. `r::ImmutableDiffResult`). Otherwise, `r` will be mutable
(i.e. `r::MutableDiffResult`).
Note that `derivs` can be provide in splatted form, i.e. `DiffResult(value, derivs...)`.
"""
DiffResult
DiffResult(value::Number, derivs::Tuple{Vararg{Number}}) = ImmutableDiffResult(value, derivs)
DiffResult(value::Number, derivs::Tuple{Vararg{StaticArray}}) = ImmutableDiffResult(value, derivs)
DiffResult(value::StaticArray, derivs::Tuple{Vararg{StaticArray}}) = ImmutableDiffResult(value, derivs)
DiffResult(value::Number, derivs::Tuple{Vararg{AbstractArray}}) = MutableDiffResult(value, derivs)
DiffResult(value::AbstractArray, derivs::Tuple{Vararg{AbstractArray}}) = MutableDiffResult(value, derivs)
DiffResult(value::Union{Number,AbstractArray}, derivs::Union{Number,AbstractArray}...) = DiffResult(value, derivs)

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 pushed a commit to RomeoV/LsqFit.jl that referenced this issue Aug 15, 2023
This package is almost ready to be used straight up with StaticArrays.
Most variables are already written in a general way, there are just two
blockers:
The preallocated buffers `JJ` and `n_buffer`, aswell as the LMResults
type.

This commit fixes that, and almost allows usage of StaticArrays.
However, there's still
JuliaDiff/DiffResults.jl#25, but that can be
currently overcome by two typedefs.
@gdalle
Copy link
Member

gdalle commented Mar 15, 2024

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
Copy link
Member

gdalle commented Mar 15, 2024

I have opened #28 to keep track

@gdalle gdalle linked a pull request Mar 15, 2024 that will close this issue
@gdalle
Copy link
Member

gdalle commented Mar 19, 2024

Reopened cause the PR was temporarily reverted

@gdalle gdalle reopened this Mar 19, 2024
@gdalle gdalle mentioned this issue Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants