Skip to content

Commit a36ee82

Browse files
authored
norm and zero! based on MemoryLayout (#73)
* norm and zero! based on MemoryLayout * Update test_layoutarray.jl
1 parent 869363f commit a36ee82

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/ArrayLayouts.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,20 @@ Base.map(::typeof(copy), D::Diagonal{<:LayoutArray}) = Diagonal(map(copy, D.diag
232232
Base.permutedims(D::Diagonal{<:Any,<:LayoutVector}) = D
233233

234234

235-
zero!(A::AbstractArray{T}) where T = fill!(A,zero(T))
236-
function zero!(A::AbstractArray{<:AbstractArray})
235+
zero!(A) = zero!(MemoryLayout(A), A)
236+
zero!(_, A) = fill!(A,zero(eltype(A)))
237+
function zero!(_, A::AbstractArray{<:AbstractArray})
237238
for a in A
238239
zero!(a)
239240
end
240241
A
241242
end
242243

244+
_norm(_, A, p) = Base.invoke(norm, Tuple{Any,Real}, A, p)
245+
LinearAlgebra.norm(A::LayoutArray, p::Real=2) = _norm(MemoryLayout(A), A, p)
246+
LinearAlgebra.norm(A::SubArray{<:Any,N,<:LayoutArray}, p::Real=2) where N = _norm(MemoryLayout(A), A, p)
247+
248+
243249
_fill_lmul!(β, A::AbstractArray{T}) where T = iszero(β) ? zero!(A) : lmul!(β, A)
244250

245251

test/test_layoutarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
5050
v = view(a,1:3)
5151
@test copy(v) == sub_materialize(v) == a[1:3]
5252
@test dot(v,a) == dot(v,a.A) == dot(a,v) == dot(a.A,v) == dot(v,v) == 14
53+
@test norm(v) == norm(a) == norm([1,2,3])
5354

5455
V = view(a',:,1:3)
5556
@test copy(V) == sub_materialize(V) == (a')[:,1:3]

0 commit comments

Comments
 (0)