Skip to content

Commit e69eb08

Browse files
committed
Add convert() methods for Matrix from Vector and RowVector
1 parent a72aad4 commit e69eb08

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

base/array.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x)
320320

321321
convert(::Type{Vector}, x::AbstractVector{T}) where {T} = convert(Vector{T}, x)
322322
convert(::Type{Matrix}, x::AbstractMatrix{T}) where {T} = convert(Matrix{T}, x)
323+
convert(::Type{Matrix}, x::Vector) = Matrix(reshape(x, :, 1))
323324

324325
convert(::Type{Array{T}}, x::Array{T,n}) where {T,n} = x
325326
convert(::Type{Array{T,n}}, x::Array{T,n}) where {T,n} = x

base/linalg/rowvector.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const ConjRowVector{T,CV<:ConjVector} = RowVector{T,CV}
4444
RowVector{T}(Vector{transpose_type(T)}(n[2])) :
4545
error("RowVector expects 1×N size, got $n")
4646

47+
# Convenience constructor to Matrix
48+
convert(::Type{Matrix}, x::RowVector) = Matrix(reshape(x, 1, :))
49+
4750
# Conversion of underlying storage
4851
convert(::Type{RowVector{T,V}}, rowvec::RowVector) where {T,V<:AbstractVector} =
4952
RowVector{T,V}(convert(V,rowvec.vec))

test/arrayops.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ end
191191
@test convert(Vector{Float64}, b) == b
192192
end
193193

194+
@testset "conversion from Vector/RowVector to Matrix (#21977)" begin
195+
a = Vector([1, 2, 3])
196+
am = Matrix([1; 2; 3])
197+
@test convert(Matrix, a) == am
198+
@test convert(Matrix, a') == am'
199+
end
200+
194201
@testset "operations with IndexLinear ReshapedArray" begin
195202
b = collect(1:12)
196203
a = Base.ReshapedArray(b, (4,3), ())

0 commit comments

Comments
 (0)