Skip to content

Commit f194a97

Browse files
Merge pull request #45 from SciML/zeromatrix
add zeromatrix
2 parents 37f0e1e + d07ba92 commit f194a97

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.6.2"
3+
version = "2.7.0"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ cheaply.
6969

7070
Return an instance of the LU factorization object with the correct type cheaply.
7171

72+
## safevec(v)
73+
74+
Is a form of `vec` which is safe for all values in vector spaces, i.e. if
75+
is already a vector, like an AbstractVector or Number, it will return said
76+
AbstractVector or Number.
77+
78+
## zeromatrix(u::AbstractVector)
79+
80+
Creates the zero'd matrix version of `u`. Note that this is unique because
81+
`similar(u,length(u),length(u))` returns a mutable type, so is not type-matching,
82+
while `fill(zero(eltype(u)),length(u),length(u))` doesn't match the array type,
83+
i.e. you'll get a CPU array from a GPU array. The generic fallback is
84+
`u .* u' .* false` which works on a surprising number of types, but can be broken
85+
with weird (recursive) broadcast overloads. For higher order tensors, this
86+
returns the matrix linear operator type which acts on the `vec` of the array.
87+
7288
## List of things to add
7389

7490
- https://github.com/JuliaLang/julia/issues/22216

src/ArrayInterface.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,33 @@ Return the number.
420420
"""
421421
lu_instance(a::Number) = a
422422

423+
"""
424+
safevec(v)
425+
426+
Is a form of `vec` which is safe for all values in vector spaces, i.e. if
427+
is already a vector, like an AbstractVector or Number, it will return said
428+
AbstractVector or Number.
429+
"""
430+
safevec(v) = vec(v)
431+
safevec(v::Number) = v
432+
safevec(v::AbstractVector) = v
433+
434+
"""
435+
zeromatrix(u::AbstractVector)
436+
437+
Creates the zero'd matrix version of `u`. Note that this is unique because
438+
`similar(u,length(u),length(u))` returns a mutable type, so is not type-matching,
439+
while `fill(zero(eltype(u)),length(u),length(u))` doesn't match the array type,
440+
i.e. you'll get a CPU array from a GPU array. The generic fallback is
441+
`u .* u' .* false` which works on a surprising number of types, but can be broken
442+
with weird (recursive) broadcast overloads. For higher order tensors, this
443+
returns the matrix linear operator type which acts on the `vec` of the array.
444+
"""
445+
function zeromatrix(u)
446+
x = safevec(u)
447+
x .* x' .* false
448+
end
449+
423450
function __init__()
424451

425452
@require SuiteSparse="4607b0f0-06f3-5cda-b6b1-a6196a1729e9" begin

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,6 @@ using ArrayInterface: issingular
159159
@test all(!issingular, [UnitLowerTriangular(R), UnitUpperTriangular(R), UnitUpperTriangular(R)'])
160160
end
161161
end
162+
163+
using ArrayInterface: zeromatrix
164+
@test zeromatrix(rand(4,4,4)) == zeros(4*4*4,4*4*4)

0 commit comments

Comments
 (0)