Skip to content

Commit d0c41b7

Browse files
committed
reshape fixes, copy of ShallowVec
1 parent 1d4eeb9 commit d0c41b7

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

docs/src/arrays.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ x = GBMatrix{Bool}(20_000_000, 50_000)
4040
x = GBMatrix([[1,2] [3,4]])
4141
x = GBMatrix(sprand(100, 100, 0.5); fill = 0.0)
4242
x = GBMatrix(
43-
rand(1:50_000, 5000), rand(1:500_000, 5000), 1;
44-
ncols = 500_000, nrows = 500_000
43+
rand(1:50_000, 5000), rand(1:500_000, 5000), 1,
44+
500_000, 500_000
4545
)
4646
```
4747

src/abstractgbarray.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ reshape!(A::AbstractGBMatrix, dims...; bycol = true) =
205205
reshape!(A, dims...; bycol)
206206
reshape!(A::AbstractGBMatrix, n; bycol = true) =
207207
reshape!(A, n, 1; bycol)
208-
function Base.reshape(
208+
209+
function _reshape(
209210
A::AbstractGBMatrix, nrows::Int, ncols::Int;
210211
bycol = true, desc = nothing)
211212
desc = _handledescriptor(desc)
@@ -214,10 +215,15 @@ function Base.reshape(
214215
C, A,
215216
bycol, nrows, ncols, desc
216217
)
217-
out = similar(A)
218-
out.p = finalizer(C) do ref
218+
return finalizer(C) do ref
219219
@wraperror LibGraphBLAS.GrB_Matrix_free(ref)
220220
end
221+
end
222+
function Base.reshape(
223+
A::AbstractGBMatrix, nrows::Int, ncols::Int;
224+
bycol = true, desc = nothing)
225+
out = similar(A)
226+
out.p = _reshape(A, nrows, ncols; bycol, desc)
221227
return out
222228
end
223229

@@ -238,7 +244,16 @@ Base.reshape(
238244
bycol = true
239245
) = reshape(A, dims...; bycol)
240246

241-
Base.reshape(A::AbstractGBMatrix, n::Union{Int, Colon}; bycol = true) = reshape(A, n, 1; bycol)
247+
function Base.reshape(A::AbstractGBMatrix, ::Colon; bycol = true)
248+
out = similar(A, length(A))
249+
out.p = _reshape(A, length(A), 1; bycol)
250+
return out
251+
end
252+
Base.reshape(A::AbstractGBMatrix, n::Int; bycol = true) = n == length(A) ? reshape(A, :; bycol) :
253+
throw(DimensionMismatch("new dimensions ($n,) must be consistent with array size $(length(A))"))
254+
Base.reshape(A::AbstractGBMatrix, n::Integer; bycol = true) = n == length(A) ? reshape(A, :; bycol) :
255+
throw(DimensionMismatch("new dimensions ($n,) must be consistent with array size $(length(A))"))
256+
242257

243258
function build!(A::AbstractGBMatrix{T}, I::AbstractVector, J::AbstractVector, x::T) where {T}
244259
nnz(A) == 0 || throw(OutputNotEmptyError())

src/mmread.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,6 @@ function mmread(filename, infoonly::Bool=false, retcoord::Bool=false)
123123
end
124124
(retcoord
125125
? (rr, cc, xx, rows, cols, entries, rep, field, symm)
126-
: symlabel(GBMatrix(rr, cc, xx, nrows=rows, ncols=cols)))
126+
: symlabel(GBMatrix(rr, cc, xx, rows, cols)))
127127
end
128128
end

src/shallowtypes.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ function Base.copy(A::GBShallowMatrix{T}) where {T}
4444
r = _copyGrBMat(A.p)
4545
return GBMatrix{T}(r; fill = A.fill)
4646
end
47-
47+
function Base.copy(A::GBShallowVector{T}) where {T}
48+
r = _copyGrBMat(A.p)
49+
return GBVector{T}(r; fill = A.fill)
50+
end
4851

4952
# similar functions:
5053
# Note well, these should *never* return a GBShallowArray.
@@ -94,3 +97,4 @@ Base.resize!(::GBShallowMatrix, nrows, ncols) = throw(ShallowException())
9497
Base.resize!(::GBShallowVector, nrows) = throw(ShallowException())
9598
build!(::GBShallowMatrix{T}, I::AbstractVector, J::AbstractVector, x::T) where T = throw(ShallowException())
9699

100+
LinearAlgebra.Diagonal(v::GBShallowVector) = Diagonal(copy(v))

test/abstractgbarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
@test size(reshape(A, 2, :)) == (2, 2)
3131
@test size(reshape(A, (:, 2))) == (2, 2)
3232
@test size(reshape(A, (2, 2))) == (2, 2)
33-
@test size(reshape(A, :)) == (4, 1)
33+
@test size(reshape(A, :)) == (4,)
3434
B = copy(A)
3535
@test resize!(B, 1, 4) == GBMatrix([1, 1], [1, 2], [1, 3], 1, 4)
3636
@test resize!(B, 1, 1) === B

test/issues.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,18 @@
5353
A = GBMatrix([1,1,2,2,3,4,4,5,6,7,7,7], [2,4,5,7,6,1,3,6,3,3,4,5], [1:12...])
5454
@test reduce(any, A, dims=2) == GBVector([1,3,5,6,8,9,10])
5555
end
56+
57+
@testset "#109ish" begin
58+
# test that masking works correctly:
59+
60+
end
61+
@testset "#106" begin
62+
a = rand(2, 4) |> GBMatrix
63+
@test reshape(a, :) isa AbstractVector
64+
@test reshape(a, :) == reshape(a, 8)
65+
end
66+
@testset "#98" begin
67+
x = GBMatrix([1,2], [2, 3], [1,2], fill=0)
68+
@test Array(x) .* [1,2] x .* [1,2]
69+
end
5670
end

0 commit comments

Comments
 (0)