Skip to content

Commit 8e8b29c

Browse files
committed
add reshape, partially fix #81
1 parent 19b09b7 commit 8e8b29c

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

src/abstractgbarray.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,58 @@ function SparseArrays.SparseVector(v::GBVectorOrTranspose)
4343
return unpack!(T, SparseVector)
4444
end
4545

46+
function reshape!(
47+
A::AbstractGBMatrix, nrows, ncols;
48+
bycol::Bool = true, desc = nothing
49+
)
50+
desc = _handledescriptor(desc)
51+
lenA = length(A)
52+
nrows isa Colon && ncols isa Colon && throw(
53+
ArgumentError("nrows and ncols may not both be Colon"))
54+
nrows isa Colon && (nrows = lenA ÷ ncols)
55+
ncols isa Colon && (ncols = lenA ÷ nrows)
56+
@wraperror LibGraphBLAS.GxB_Matrix_reshape(
57+
gbpointer(A), bycol, nrows, ncols, desc
58+
)
59+
return A
60+
end
61+
reshape!(A::AbstractGBMatrix, dims...; bycol = true) =
62+
reshape!(A, dims...; bycol)
63+
reshape!(A::AbstractGBMatrix, n; bycol = true) =
64+
reshape!(A, n, 1; bycol)
65+
function Base.reshape(
66+
A::AbstractGBMatrix, nrows, ncols;
67+
bycol = true, desc = nothing)
68+
desc = _handledescriptor(desc)
69+
lenA = length(A)
70+
nrows isa Colon && ncols isa Colon && throw(
71+
ArgumentError("nrows and ncols may not both be Colon"))
72+
nrows isa Colon && (nrows = lenA ÷ ncols)
73+
ncols isa Colon && (ncols = lenA ÷ nrows)
74+
C = Ref{LibGraphBLAS.GrB_Matrix}()
75+
@wraperror LibGraphBLAS.GxB_Matrix_reshapeDup(
76+
C, gbpointer(A),
77+
bycol, nrows, ncols, desc
78+
)
79+
# TODO, do better. This is ugly and allocates twice.
80+
out = similar(A)
81+
out.p = finalizer(C) do ref
82+
@wraperror LibGraphBLAS.GrB_Matrix_free(ref)
83+
end
84+
return out
85+
end
86+
Base.reshape(A::AbstractGBMatrix, dims::Tuple{Vararg{Int64, N}}; bycol = true) where N =
87+
reshape(A, dims...; bycol)
88+
Base.reshape(A::AbstractGBMatrix, dims::Tuple{Vararg{Union{Colon, Int64}}}; bycol = true) =
89+
reshape(A, dims...; bycol)
90+
Base.reshape(
91+
A::AbstractGBMatrix,
92+
dims::Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}};
93+
bycol = true
94+
) = reshape(A, dims...; bycol)
95+
96+
Base.reshape(A::AbstractGBMatrix, n; bycol = true) = reshape(A, n, 1; bycol)
97+
4698
# AbstractGBMatrix functions:
4799
#############################
48100

src/operations/extract.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,18 @@ function extract(
116116
return extract!(C, A, :, J; mask, accum, desc)
117117
end
118118

119-
# TODO: FINISH THIS WITH GxB_Matrix_reshape!!!
120-
# function extract(
121-
# A::GBMatrixOrTranspose, I::Number, ::Colon;
122-
# mask = nothing, accum = nothing, desc = nothing
123-
# )
124-
# _, Jlen = _outlength(A, I, :)
125-
# C = similar(A, 1, Jlen)
126-
# return extract!(C, A, I, :; mask, accum, desc)
127-
# end
119+
function extract(
120+
A::GBMatrixOrTranspose{T}, I::Number, ::Colon;
121+
mask = nothing, accum = nothing, desc = nothing
122+
) where {T}
123+
_, Jlen = _outlength(A, I, :)
124+
C = similar(A, Jlen)
125+
# TODO, better abstractions here.
126+
@wraperror LibGraphBLAS.GxB_Matrix_reshape(C, true, 1, Jlen, C_NULL)
127+
extract!(C, A, I, :; mask, accum, desc)
128+
@wraperror LibGraphBLAS.GxB_Matrix_reshape(C, true, Jlen, 1, C_NULL)
129+
return C
130+
end
128131

129132
function extract!(
130133
w::AbstractGBVector, u::AbstractGBVector, I;

test/gbarray.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#Indexing tests
1717
x = sprand(Int64, 100, 100, 0.05)
1818
m = GBMatrix(x)
19+
deleteat!(m, 1, 2)
1920
@test m[1, 2] === nothing
2021
@test m[:, 2] == GBVector(x[:, 2])
21-
@test m[2, :] == copy(GBVector(x[2, :])')
22+
@test m[2, :] == GBVector(x[2, :])
2223
@test m[:, :] == m
2324
@test m[1:2:5, 1:2] == GBMatrix(x[1:2:5, 1:2])
2425
@test m[1:2:5, :] == GBMatrix(x[1:2:5, :])

0 commit comments

Comments
 (0)