Kristoffer Carlsson
suggested an unsanctioned way of getting around this by using unsafe_wrap
. As an example he showed:
julia> a = rand(5,5,5);
julia> unsafe_wrap(Vector{Float64}, pointer(a), (15,))
15-element Array{Float64,1}:
0.39667876246883926
0.22245479132465285
with the understanding that one would have to ensure that a
is protected from the garbage collector.
He also linked to a related discussion on reshaping StaticArrays
.
https://github.com/JuliaArrays/StaticArrays.jl/pull/496#issuecomment-421806480
Moritz Schauer
suggested a clever alternative and safer approach which he attributed to Keno Fisher
and dubbed Keno’s obvious trick:
Fill the reshaped view of a full matrix of the shape you need, instead of reshaping the full matrix with a view into the shape you need.
This means that I start by declaring a Jacobian matrix, e.g. J = zeros(4*10,12)
and then create a reshaped view of the Jacobian matrix: Jv = reshape(reinterpret(Float64,J), 4, 10, 12)
.
I can then conveniently index into Jv
. Since Jv
is a view, I can pass J
into LsqFit.jl
because it contains the same data as Jv
and is also a Matrix
.