Why does this work,
tst{T <: AbstractFloat} = SMatrix{3,3,T}
tst(rand(3,3))
3×3 SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
0.901624 0.966323 0.397229
0.716175 0.758242 0.791737
0.839526 0.798434 0.311178
but this does not?
tst2{T <: AbstractFloat} = UpperTriangular{T,SMatrix{3,3,T}}
tst2(rand(3,3))
MethodError: no method matching (UpperTriangular{T, SMatrix{3, 3, T}} where T<:AbstractFloat)(::Matrix{Float64})
Stacktrace:
[1] top-level scope
@ In[8]:1
[2] eval
@ ./boot.jl:368 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1428
Thanks!
jling
October 22, 2022, 12:17am
2
nothing to do with alias:
julia> convert((SMatrix{3, 3, T} where T<:AbstractFloat), rand(3,3))
3×3 SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
0.60079 0.0957968 0.160429
0.0510449 0.0892437 0.435343
0.897627 0.389427 0.964163
julia> convert((UpperTriangular{T,SMatrix{3,3,T}} where T<:AbstractFloat), rand(3,3))
ERROR: MethodError: Cannot `convert` an object of type
Matrix{Float64} to an object of type
UpperTriangular{T, SMatrix{3, 3, T}} where T<:AbstractFloat
# this covers most conversions and "statically-sized reshapes"
@inline function convert(::Type{SA}, sa::StaticArray{S}) where {SA<:StaticArray,S<:Tuple}
SA′ = construct_type(SA, sa)
# `SA′((sa,))` is not valid. As we want `SA′(sa...)`
need_rewrap(SA′, sa) && _no_precise_size(SA, sa)
return SA′(Tuple(sa))
end
there’s no equivalent for UpperTriangular
This case works because the eltype is known,
UpperTriangular{Float32,SMatrix{3,3,Float32}}(rand(3,3))
3×3 UpperTriangular{Float32, SMatrix{3, 3, Float32}}:
0.784383 0.599959 0.838959
⋅ 0.0565993 0.620415
⋅ ⋅ 0.118366
But I don’t see how it relates to your comment.
jling
October 22, 2022, 1:18pm
4
Because that’s a contractor and is defined here:
function $t{T,S}(data) where {T,S<:AbstractMatrix{T}}
require_one_based_indexing(data)
checksquare(data)
new{T,S}(data)
end
what I’m saying is simple, if there’s no definition for a convert
then there’s no conversion that can take place.
When the outermost container is a StaticArray, that convert
is implemented in StaticArrays.jl
package.
1 Like