2
votes

I am wondering about the simple way to strictly define a column vector in Julia, for example, I want a 3-row column vector B:

julia> columnVectorB
3×1 Array{Float64,2}:
1.0
2.0
3.0

While I think the normal way is:

julia> columnVectorB = [1.; 2.; 3.]

julia> columnVectorB
3-element Array{Float64,1}:
1.0
2.0
3.0

The reason I must do it is that there are annoying problems if column vectors are defined in the normal way when using matrix manipulation in JuMP. One of the problems is that:

julia> using JuMP
julia> using GLPKMathProgInterface
julia> n = 1
julia> model_mas = Model(solver = GLPKSolverLP())
julia> @variable(model, vec_y[1: n] >= 0, Int)

julia> vec_y
1-element Array{Variable,1}: 
vec_y[1]

The n indicates that vec_y can be a column vector of n variables. It is the number of column of B as well, so B is actually a matrix. When n > 1, there is no probelm. When n = 1, B becomes a column vector. Then, there will be a problem in:

julia> @constraint(model, (columnVectorB * vec_y)[1] <= 10)
ERROR: MethodError: no method matching *(::Array{Float64,1}, ::Array{Variable,1})
Closest candidates are:
*(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
*(::LinearAlgebra.Adjoint{#s45,#s44} where #s44<:SparseArrays.SparseMatrixCSC where #s45<:Union{AbstractJuMPScalar, GenericNormExpr{2,Float64,Variable}, NonlinearExpression, GenericNorm{P,Float64,Variable} where P}

Currently, I solve the problem by:

julia> columnVectorB = rand(3,1)
julia> columnVectorB[1] = 1.
julia> columnVectorB[2] = 2.
julia> columnVectorB[3] = 3.

julia> columnVectorB
3×1 Array{Float64,2}:
1.0
2.0
3.0

julia> columnVectorB * vec_y
3-element Array{JuMP.GenericAffExpr{Float64,Variable},1}:
1 vec_y[1]
2 vec_y[1]
3 vec_y[1]

But this is too stupid. Is there any better way to do this?

1

1 Answers

7
votes

Actually it seems you want a matrix that has exactly one column and n rows. You can transform a vector to a matrix in several ways. I give you several options for this:

julia> x = [1,2,3] # a vector
3-element Array{Int64,1}:
 1
 2
 3

julia> hcat(x) # a matrix
3×1 Array{Int64,2}:
 1
 2
 3

julia> reshape(x, :, 1) # a matrix
3×1 Array{Int64,2}:
 1
 2
 3

julia> x[:,:] # a matrix
3×1 Array{Int64,2}:
 1
 2
 3

EDIT Out of these three reshape will be the fastest as it does not perform a copy (it the result of reshape will share the underlying data with x). hcat and [:,:] will both perform a copy of the data.