Skip to content

Commit e750596

Browse files
jishnubKristofferC
authored andcommitted
Specialize one for the SizedArray test helper (#58209)
Since the size of the array is encoded in the type, we may define `one` on the type. This is useful in certain linear algebra contexts. (cherry picked from commit d9fafab)
1 parent a291385 commit e750596

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

test/abstractarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,3 +1907,4 @@ end
19071907
@test r2[i] == z[j]
19081908
end
19091909
end
1910+

test/testhelpers/SizedArrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Base.size(a::SizedArray) = size(typeof(a))
2929
Base.size(::Type{<:SizedArray{SZ}}) where {SZ} = SZ
3030
Base.getindex(A::SizedArray, i...) = getindex(A.data, i...)
3131
Base.zero(::Type{T}) where T <: SizedArray = SizedArray{size(T)}(zeros(eltype(T), size(T)))
32+
function Base.one(::Type{SizedMatrix{SZ,T,A}}) where {SZ,T,A}
33+
allequal(SZ) || throw(DimensionMismatch("multiplicative identity defined only for square matrices"))
34+
D = diagm(fill(one(T), SZ[1]))
35+
SizedArray{SZ}(convert(A, D))
36+
end
37+
Base.parent(S::SizedArray) = S.data
3238
+(S1::SizedArray{SZ}, S2::SizedArray{SZ}) where {SZ} = SizedArray{SZ}(S1.data + S2.data)
3339
==(S1::SizedArray{SZ}, S2::SizedArray{SZ}) where {SZ} = S1.data == S2.data
3440
function *(S1::SizedArray, S2::SizedArray)

0 commit comments

Comments
 (0)