Skip to content

Commit d07ba92

Browse files
add safevec and test
1 parent 90744c4 commit d07ba92

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ 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+
7278
## zeromatrix(u::AbstractVector)
7379

7480
Creates the zero'd matrix version of `u`. Note that this is unique because

src/ArrayInterface.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,16 @@ 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
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
426433

427434
"""
428435
zeromatrix(u::AbstractVector)
@@ -436,7 +443,7 @@ with weird (recursive) broadcast overloads. For higher order tensors, this
436443
returns the matrix linear operator type which acts on the `vec` of the array.
437444
"""
438445
function zeromatrix(u)
439-
x = _vec(u)
446+
x = safevec(u)
440447
x .* x' .* false
441448
end
442449

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)