Arrays
An array is a multi-dimensional collection of objects. The elements of arrays do not need to be numbers or even from the same type. However, we are interested in numeric arrays.
using LinearAlgebra
import Random
Random.seed!(11)Random.TaskLocalRNG()Basic syntax
Vectors
x = [1, 3, 4]3-element Vector{Int64}:
1
3
4Julia allows to perform operations that are almost globally accepted on vectors. For example, let's get the transpose:
x'1×3 adjoint(::Vector{Int64}) with eltype Int64:
1 3 4Or multiply the vector by an scalar:
4x3-element Vector{Int64}:
4
12
16In cases where the operator is not clear, we need to use the dot operator to make element-by-element computations:
1 .+ x3-element Vector{Int64}:
2
4
5sqrt.(x)3-element Vector{Float64}:
1.0
1.7320508075688772
2.0The dot operator is atuomatically available for any function:
g(x) = 3 + 2x^2g (generic function with 1 method)g.(x)3-element Vector{Int64}:
5
21
35Matrices
A = [1 4 5;
3 4 5]2×3 Matrix{Int64}:
1 4 5
3 4 55A2×3 Matrix{Int64}:
5 20 25
15 20 255 .+ A2×3 Matrix{Int64}:
6 9 10
8 9 10A * x2-element Vector{Int64}:
33
35A .^ 22×3 Matrix{Int64}:
1 16 25
9 16 25g.(A)2×3 Matrix{Int64}:
5 35 53
21 35 53Constructors
zeros(5, 3)5×3 Matrix{Float64}:
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0ones(5, 3)5×3 Matrix{Float64}:
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0rand(5, 3)5×3 Matrix{Float64}:
0.498434 0.184079 0.258119
0.389721 0.46979 0.888894
0.26454 0.568002 0.723021
0.719424 0.677608 0.51986
0.676602 0.105514 0.0459551randn(5, 3)5×3 Matrix{Float64}:
-0.253313 0.910287 1.6276
1.90599 -0.180697 -0.474504
1.0981 -0.557519 0.713292
-0.490607 0.37708 -0.554788
-1.63391 0.559958 -0.760346ones(5, 5) + I5×5 Matrix{Float64}:
2.0 1.0 1.0 1.0 1.0
1.0 2.0 1.0 1.0 1.0
1.0 1.0 2.0 1.0 1.0
1.0 1.0 1.0 2.0 1.0
1.0 1.0 1.0 1.0 2.0