Skip to content

Commit 90744c4

Browse files
generalize the zeromatrix to higher order tensors as well
1 parent f684f66 commit 90744c4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ Creates the zero'd matrix version of `u`. Note that this is unique because
7676
while `fill(zero(eltype(u)),length(u),length(u))` doesn't match the array type,
7777
i.e. you'll get a CPU array from a GPU array. The generic fallback is
7878
`u .* u' .* false` which works on a surprising number of types, but can be broken
79-
with weird (recursive) broadcast overloads.
79+
with weird (recursive) broadcast overloads. For higher order tensors, this
80+
returns the matrix linear operator type which acts on the `vec` of the array.
8081

8182
## List of things to add
8283

src/ArrayInterface.jl

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

423+
_vec(v) = vec(v)
424+
_vec(v::Number) = v
425+
_vec(v::AbstractVector) = v
426+
423427
"""
424428
zeromatrix(u::AbstractVector)
425429
@@ -428,9 +432,13 @@ Creates the zero'd matrix version of `u`. Note that this is unique because
428432
while `fill(zero(eltype(u)),length(u),length(u))` doesn't match the array type,
429433
i.e. you'll get a CPU array from a GPU array. The generic fallback is
430434
`u .* u' .* false` which works on a surprising number of types, but can be broken
431-
with weird (recursive) broadcast overloads.
435+
with weird (recursive) broadcast overloads. For higher order tensors, this
436+
returns the matrix linear operator type which acts on the `vec` of the array.
432437
"""
433-
zeromatrix(u::AbstractVector) = u .* u' .* false
438+
function zeromatrix(u)
439+
x = _vec(u)
440+
x .* x' .* false
441+
end
434442

435443
function __init__()
436444

0 commit comments

Comments
 (0)