How can I not change the matrix - julia

In Julia, if t is some matrix
t = [1 2; 3 4]
t1 = t
t1[1,1] = 0
t2 = t
t2 gives out same output as t1.
How can I make t2 to equal t?

They are all equal actually:
julia> t = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> t1 = t
2×2 Matrix{Int64}:
1 2
3 4
julia> t1[1,1] = 0
0
julia> t2 = t
2×2 Matrix{Int64}:
0 2
3 4
julia> t == t1 == t2
true
julia> t === t1 === t2
true
julia> pointer(t), pointer(t1), pointer(t2) # identical
(Ptr{Int64} #0x00007fd213d37000, Ptr{Int64} #0x00007fd213d37000, Ptr{Int64} #0x00007fd213d37000)
The variables t, t1, and t2 point to the same data, the same memory location. So when you mutate t1 you mutate all.
If you want them to be different, such that only one is modified, you have to use copy:
julia> t = [1 2; 3 4];
julia> t1 = copy(t);
julia> t1[1,1] = 0;
julia> t2 = copy(t); # or just t if you want to avoid the copy
julia> t == t1 == t2
false
julia> pointer(t), pointer(t1), pointer(t2)
(Ptr{Int64} #0x00007fd21220e900, Ptr{Int64} #0x00007fd2142f9630, Ptr{Int64} #0x00007fd2142f9a90)

Related

Pairwise distance matrix between two vectors

x1=[1,2,3]
x2=[2,3,4]
how to find the Pairwise distance matrix between x1 and x2 (distance matrix should be a 3 x 3 matrix)
This is not a Euclidean distance matrix, but it is 3 X 3. Is it what you want?
julia> x1 = [1,2,3]
3-element Vector{Int64}:
1
2
3
julia> x2 = [2,3,4]
3-element Vector{Int64}:
2
3
4
julia> [(a-b)^2 for a in x1, b in x2]
3×3 Matrix{Int64}:
1 4 9
0 1 4
1 0 1
With Distances.jl:
julia> pairwise(Euclidean(), x1, x2)
3×3 Matrix{Float64}:
1.0 2.0 3.0
0.0 1.0 2.0
1.0 0.0 1.0
(Although this will not return integers, as it uses BLAS stuff internally.)
Since you ask for Euclidean distance between all combinations of vector pairs from x1 and x2, that's, the distance between [1, 2] and [2, 3], [1, 2] and [2, 4], ..., [2, 3] and [3, 4], this can be done as follows:
Using Combinatorics.jl, construct all pairs from x1 by taking 2 elements at a time. Do the same for x2. Now you have c1 and c2, just loop over the two sequences applying the formal definition of Euclidean distance, sqrt(sum((x-y)^2)), to get the 3-by-3 matrix of pairwise distances that you want.
using Combinatorics
x1 = [1, 2, 3]
x2 = [2, 3, 4]
c1 = combinations(x1, 2)
c2 = combinations(x2, 2)
pairwise = [sqrt(sum((i.-j).^2)) for i in c1, j in c2]
3×3 Matrix{Float64}:
1.41421 2.23607 2.82843
1.0 1.41421 2.23607
0.0 1.0 1.41421
If you like higher-order index notations similar to math books, you can use Tullio.jl like this:
using Tullio
x = collect(combinations(x1, 2))
y = collect(combinations(x2, 2))
#tullio pairwise[i,j] := sqrt(sum((x[i].-y[j]).^2))
3×3 Matrix{Float64}:
1.41421 2.23607 2.82843
1.0 1.41421 2.23607
0.0 1.0 1.41421
You can try:
abs.(x1 .- x2')
#3×3 Array{Int64,2}:
# 1 2 3
# 0 1 2
# 1 0 1
Where x2' turns x2 in a column vector and .- and abs. makes element wise operations.
Or creating the desired pairs (1,2)and(2,3), (1,2)and(2,4), ... (2,3)and(3,4) and calculating the distance using norm.
using LinearAlgebra
#Create pairs
c1 = [(x1[i], x1[j]) for i in 1:lastindex(x1)-1 for j in i+1:lastindex(x1)]
c2 = [(x2[i], x2[j]) for i in 1:lastindex(x2)-1 for j in i+1:lastindex(x2)]
#Calc distance
[norm(i.-j) for i in c1, j in c2]
#3×3 Array{Float64,2}:
# 1.41421 2.23607 2.82843
# 1.0 1.41421 2.23607
# 0.0 1.0 1.41421
#Calc cityblock distance
[norm(i.-j, 1) for i in c1, j in c2]
#3×3 Array{Float64,2}:
# 2.0 3.0 4.0
# 1.0 2.0 3.0
# 0.0 1.0 2.0

How to iterate over Dict in Julia

How can I something like this (it is in Python) in Julia?
for key, value in dictionary.items(): # <-- I don't know how to iterate over keys and values of a dictionary in Julia
print(key, value)
Thank you.
You can just iterate, but you need a bracket around (key, value):
julia> dict = Dict(i => (i/10 + rand(1:99)) for i = 1:3)
Dict{Int64, Float64} with 3 entries:
2 => 24.2
3 => 29.3
1 => 41.1
julia> for (k,v) in dict
#show k v
end
k = 2
v = 24.2
k = 3
v = 29.3
k = 1
v = 41.1
julia> p = first(dict)
2 => 24.2
julia> typeof(p)
Pair{Int64, Float64}
julia> (a, b) = p;
julia> b
24.2

How can I determine if an array contains some element?

How can I tell if an array contains some element?
I have been manually checking with a loop:
for x in xs
if x == a
return true
end
end
return false
Is there a more idiomatic way?
The in operator will iterate over an array and check if some element exists:
julia> xs = [5, 9, 2, 3, 3, 8, 7]
julia> 8 in xs
true
julia> 1 in xs
false
It is important to remember that missing values can alter the behavior you might otherwise expect:
julia> 2 in [1, missing]
missing
in can be used on general collections. In particular, matrices:
julia> A = [1 4 7
2 5 8
3 6 9]
3×3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
julia> 7 in A
true
julia> 10 in A
false

newline sensitive interpretation of Arrays

When I add a newline to my array definition, the type of my array changes.
julia> a = [[1]]
1-element Array{Array{Int64,1},1}:
[1]
julia> a = [[1]
]
1-element Array{Int64,1}:
1
I thought they both should return the same result i.e. of type Array{Array{Int64,1},1}
In order to understand this see the following:
julia> :([[1]
])
:([[1];])
And you see that adding a newline is rewritten as vcat operation.
The reason for this is to allow writing something like this:
julia> x = [1 2
3 4]
2×2 Array{Int64,2}:
1 2
3 4
and your example is hitting a corner case of this syntax.
Note, however, that without an extra empty line vcat is not called:
julia> :([[1]
])
:([[1]])
Another use-case that is worth to know is:
julia> [[1, 2]
[3, 4]]
4-element Array{Int64,1}:
1
2
3
4
and the same with variables (can improve code readability in some cases):
julia> a = [1,2]
2-element Array{Int64,1}:
1
2
julia> b = [3, 4]
2-element Array{Int64,1}:
3
4
julia> [a
b]
4-element Array{Int64,1}:
1
2
3
4

Find the tail of array except first element

Is there a method to get the tail of a collection(e.g. vector) i.e. the array except the first element. Currently, I have
tail(arr) = selectdim(arr, 1, 2:length(arr))
julia> c
3-element Array{Int64,1}:
1
2
3
julia> tail(c)
2-element view(::Array{Int64,1}, 2:3) with eltype Int64:
2
3
Simply use the end keyword:
julia> c = [11,12,13]
3-element Array{Int64,1}:
11
12
13
julia> c[2:end]
2-element Array{Int64,1}:
12
13

Resources