7.5. Data Types of Arrays#
So far we have worked with arrays of integers and floats. Arrays of complex and rational types can be defined easily, and the array functions work as expected:
x = [ complex(cos(θ),sin(θ)) for θ in 2π*(0:10)/10 ]
11-element Vector{ComplexF64}:
1.0 + 0.0im
0.8090169943749475 + 0.5877852522924731im
0.30901699437494745 + 0.9510565162951535im
-0.30901699437494734 + 0.9510565162951536im
-0.8090169943749473 + 0.5877852522924732im
-1.0 + 1.2246467991473532e-16im
-0.8090169943749475 - 0.587785252292473im
-0.30901699437494756 - 0.9510565162951535im
0.30901699437494723 - 0.9510565162951536im
0.8090169943749473 - 0.5877852522924734im
1.0 - 2.4492935982947064e-16im
sum(x.^2)
0.9999999999999996 - 6.008810221214569e-16im
y = [ a//(a+1) for a = 1:10 ]
10-element Vector{Rational{Int64}}:
1//2
2//3
3//4
4//5
5//6
6//7
7//8
8//9
9//10
10//11
prod(y.^2)
1//121
7.5.1. Conversion#
If you wish to change the data type of your array, use Julia’s convert
function. For example, suppose you wish that elements of y
were floats instead of rationals.
convert(Array{Float64}, y)
10-element Vector{Float64}:
0.5
0.6666666666666666
0.75
0.8
0.8333333333333334
0.8571428571428571
0.875
0.8888888888888888
0.9
0.9090909090909091