sleeve chen
sleeve chen

Reputation: 241

How to convert "1-D int array" to "int" in Julia (Jupyter notebook)

There is a similar question in C How to convert int array to int?; however, I cannot find such question in Julia.

My question is the following:

There is an error about v[1] = x'*y enter image description here

So I try to find the reason and it show that:
enter image description here

However, x^Ty=2 so if you directly type 2, it is Int64 and there is no error as shown in the following:

enter image description here

So how to transfrom Array{Int64,1} to Int 64,1?

Upvotes: 1

Views: 956

Answers (2)

Gnimuc
Gnimuc

Reputation: 8566

The reason under the hood is that Julia-0.5 still doesn't take vector transposes seriously, actually, x' is a 1x2 matrix:

julia> x'
1×2 Array{Int64,2}:
 1  1 

Apparently, you would like to get the dot product of x and y, but technically speaking x'*y is not the right syntax, you should use dot(x,y) or \cdot[tab]:

julia> x ⋅ y
2

This issue has already been fixed on Julia-0.6 master by introducing a new type RowVector:

julia> x'
1×2 RowVector{Int64,Array{Int64,1}}:
 1  1

julia> x'*y
2

Upvotes: 5

novatena
novatena

Reputation: 196

You have to define the corresponding convert method:

Base.convert{T}(Float64, x::Array{T, 1}) = float(x[1])

Or in general

Base.convert{T,K}(::Type{K}, x::Array{T, 1}) = Base.convert(K, x[1])

Example:

v = zeros(Complex{Float64}, 3)
x = [1, 1]
y = [1, 1]
v1 = x'*y
v[1] = v1
v[2] = 45
v[3] = 100
v

Result:

3-element Array{Complex{Float64},1}:
   2.0+0.0im
  45.0+0.0im
 100.0+0.0im

Upvotes: 1

Related Questions