Skip to content

Commit 2d91aaf

Browse files
committed
Make one() use similar_type()
Fixes one part of #765 Reverts "Fix accidential API breakage in one() (#763)" commit 93ec354
1 parent 0e13954 commit 2d91aaf

File tree

2 files changed

+5
-37
lines changed

2 files changed

+5
-37
lines changed

src/linalg.jl

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,49 +87,30 @@ end
8787
@inline Base.zero(a::SA) where {SA <: StaticArray} = zeros(SA)
8888
@inline Base.zero(a::Type{SA}) where {SA <: StaticArray} = zeros(SA)
8989

90-
@inline _construct_sametype(a::Type, elements) = a(elements)
91-
@inline _construct_sametype(a, elements) = typeof(a)(elements)
92-
9390
@inline one(m::StaticMatrixLike) = _one(Size(m), m)
9491
@inline one(::Type{SM}) where {SM<:StaticMatrixLike}= _one(Size(SM), SM)
9592
function _one(s::Size, m_or_SM)
9693
if (length(s) != 2) || (s[1] != s[2])
9794
throw(DimensionMismatch("multiplicative identity defined only for square matrices"))
9895
end
99-
λ = one(_eltype_or(m_or_SM, Float64))
100-
_construct_sametype(m_or_SM, _scalar_matrix_elements(s, λ))
101-
# TODO: Bring back the use of _construct_similar here:
102-
# _construct_similar(m_or_SM, s, _scalar_matrix_elements(s, λ))
103-
#
104-
# However, because _construct_similar uses `similar_type`, it will be
105-
# breaking for some StaticMatrix types (in particular Rotations.RotMatrix)
106-
# which must have similar_type return a general type able to hold any
107-
# matrix in the full general linear group.
108-
#
109-
# (Generally we're stuck here and things like RotMatrix really need to
110-
# override one() and zero() themselves: on the one hand, one(RotMatrix)
111-
# should return a RotMatrix, but zero(RotMatrix) can not be a RotMatrix.
112-
# The best StaticArrays can do is to use similar_type to return an SMatrix
113-
# for both of these, and let the more specialized library define the
114-
# correct algebraic properties.)
96+
_scalar_matrix(s, m_or_SM, one(_eltype_or(m_or_SM, Float64)))
11597
end
11698

11799
# StaticMatrix(I::UniformScaling)
118-
(::Type{SM})(I::UniformScaling) where {SM<:StaticMatrix} =
119-
SM(_scalar_matrix_elements(Size(SM), I.λ))
100+
(::Type{SM})(I::UniformScaling) where {SM<:StaticMatrix} = _scalar_matrix(Size(SM), SM, I.λ)
120101
# The following oddity is needed if we want `SArray{Tuple{2,3}}(I)` to work
121102
# because we do not have `SArray{Tuple{2,3}} <: StaticMatrix`.
122103
(::Type{SM})(I::UniformScaling) where {SM<:(StaticArray{Tuple{N,M}} where {N,M})} =
123-
SM(_scalar_matrix_elements(Size(SM), I.λ))
104+
_scalar_matrix(Size(SM), SM, I.λ)
124105

125106
# Construct a matrix with the scalar λ on the diagonal and zeros off the
126107
# diagonal. The matrix can be non-square.
127-
@generated function _scalar_matrix_elements(s::Size{S}, λ) where {S}
108+
@generated function _scalar_matrix(s::Size{S}, m_or_SM, λ) where {S}
128109
elements = Symbol[i == j ? : :λzero for i in 1:S[1], j in 1:S[2]]
129110
return quote
130111
$(Expr(:meta, :inline))
131112
λzero = zero(λ)
132-
tuple($(elements...))
113+
_construct_similar(m_or_SM, s, tuple($(elements...)))
133114
end
134115
end
135116

test/linalg.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ using StaticArrays, Test, LinearAlgebra
22

33
using LinearAlgebra: checksquare
44

5-
# For one() test
6-
struct RotMat2 <: StaticMatrix{2,2,Float64}
7-
elements::NTuple{4,Float64}
8-
end
9-
Base.getindex(m::RotMat2, i::Int) = getindex(m.elements, i)
10-
# Rotation matrices must be unitary so `similar_type` has to return an SMatrix.
11-
StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,4}
12-
135
@testset "Linear algebra" begin
146

157
@testset "SArray as a (mathematical) vector space" begin
@@ -111,11 +103,6 @@ StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,
111103
@test @inferred(one(MMatrix{2}))::MMatrix == @MMatrix [1.0 0.0; 0.0 1.0]
112104

113105
@test_throws DimensionMismatch one(MMatrix{2,4})
114-
115-
@test one(RotMat2) isa RotMat2
116-
@test one(RotMat2) == SA[1 0; 0 1]
117-
# TODO: See comment in _one.
118-
@test_broken one(RotMat2) isa SMatrix{2,2,Float64}
119106
end
120107

121108
@testset "cross()" begin

0 commit comments

Comments
 (0)