Julia usage in SF2526

The norm of matrices

In contrast to matlab, in Julia the norm(A)-command for a matrix A gives you the Frobenius norm, not the standard spectral norm. The spectral norm can be computed with the opnorm command, so the matlab command norm(A) is equivalent to opnorm(A) in julia. The design decision is motivated for example here Links to an external site. essentially because the Frobenius norm is cheaper.

Index vectors

How do I translate:

[~,I]=sort(sum(abs(Z1),1));

Answer:

I=sortperm(sum(abs.(Z1),dims=1))  

(or similar)

QR-factorization

The various variations of the QR-factorization can be obtained the qr command.

The default behavior of the QR-command is to return a factorization object:

julia> A=randn(4,3);
julia> fact=qr(A);
julia> size(fact.Q)
(4, 4)
julia> size(fact.R)
(3, 3)

In order to get a thin factorization one can do Matrix(fact.Q)

 

julia> Matrix(fact.Q)
4×3 Matrix{Float64}:
 -0.140912   0.916452    -0.274869
 -0.407868   0.224588     0.881087
 -0.659968  -0.331155    -0.159368
 -0.615004  -0.00356001  -0.350334

The CPQR factorization can be obtained by adding an argument Val(true):

julia> (Q,R,p)=qr(A,Val(true))
QRPivoted{Float64, Matrix{Float64}}
Q factor:
4×4 LinearAlgebra.QRPackedQ{Float64, Matrix{Float64}}:
 -0.140912   0.531061   0.795871  -0.254376
 -0.407868  -0.776772   0.472631   0.0830025
 -0.659968   0.055466  -0.363298  -0.655271
 -0.615004   0.333951  -0.10594    0.706416
R factor:
3×3 Matrix{Float64}:
 -2.7389  -0.426929  -2.06256
  0.0     -1.72956   -0.491979
  0.0      0.0       -1.60733
permutation:
3-element Vector{Int64}:
 1
 3
 2
julia> # p is a permutation index vector of the columns of A:
julia> norm(A[:,p]-Q*R)
3.7238012298709097e-16

 

Image processing

This is a summary of the page Video editing in Julia for SF2526 which covers video and image processing and has more examples.

The internal format for representing images in Julia differs from MATLAB. See documentation at https://juliaimages.org/latest/ Links to an external site..

When the Images package is loaded, it is possible to load images using simply

img = load("/Path/to/image")

Carefully study the typeof() the loaded values. To access red/green/blue color channel, use [color].(img), for example red.(img). To change the underlying format use [format].(img), for example Gray.(img) or RGB.(img). It is possible to show images using simply plot(img) (assuming using Plots), but there are faster alternatives. To build an image from the separate color channels, use img = colorview(RGB, redmatrix, greenmatrix, bluematrix).

How to read a .mat file

using MAT;
B=matread("Bwedge.mat")["B"];

Plotting using Gadfly

There are many packages to do plot figures in Julia. PyPlot and Plotly are nice packages, but have external dependencies (essentially wrappers for python graphics routines). Gadfly Links to an external site. has advanced functionality and little external dependencies, but syntax takes some time to learn. Here is how you make a semilogy plot

 


using Random,LinearAlgebra,BenchmarkTools, Gadfly, DataFrames
n1=70;
y1=(rand(n1)*0.1 .+0.5).^(0.3*((1:n1)));

n2=50;
y2=(rand(n2)*0.1 .+0.5).^(0.5*(1:n2));

data1=DataFrame(:s => y1,:x => 1:n1);

data2=DataFrame(:s => y2,:x => 1:n2);

p=plot(layer(data1, x=:x, y=:s, Geom.point, shape=[Shape.hexagon]),
       layer(data2, x=:x, y=:s, Geom.point, shape=[Shape.cross]),
       Scale.y_log10,
       Coord.cartesian(xmin=0, xmax=60,ymin=-10))


The last line creates a plot object p. You can plot it "manually" with the command display(p);. This is how you need to do it in a script (not REPL).

This generates this figure:

Screenshot_20220128_195645.png

 

 

Plotting graphs

The most mature graph plotting package to our knowledge is Graphs.jl Links to an external site. and GraphPlot.jl Links to an external site..

For example, this is the farmbot example from the lecture:

 

using Gadfly, Graphs, GraphPlot, LinearAlgebra;
# Farmbot data matrix. Each column is a "feature vector"
X=[2.9  0.5   0.9   0.5   0.5;
   3.3  0.5   0.8   0.4   0.5;
   1.0  0.4   0.2   0.8   0.4;
   2.6  0.4   0.9   0.5   0.4;
   2.9  0.7   0.8   0.4   0.5;
   1.6  0.4   0.3   0.8   0.4]


n=size(X,1);


ee=0.5;

S=zeros(n,n);
d=(x,y) -> norm(x-y); # inlined function

for i=1:n
    for j=1:n
        S[i,j]=d(X[i,:],X[j,:]);
    end
end
# Or with the Higher order functions
# map( (i,j) ->
# Build the graph object
g=SimpleGraph(n);

for i=1:n
    for j=1:n
        if (S[i,j] < ee && i!=j)
            add_edge!(g,i,j);
        end
    end
end
gplot(g,nodelabeldist=1.5, nodelabel=1:n, nodesize=0.2*ones(n), layout=circular_layout)

 

This plots the graph with a circular layout:

Screenshot_20220204_154941.png