Commit ab4d4273 authored by Javier's avatar Javier

linalg implemented in juobs

juobs_linalg added, juobs deps extended (LinearAlgebra)
parent c6fd18b3
...@@ -7,5 +7,6 @@ version = "0.1.0" ...@@ -7,5 +7,6 @@ version = "0.1.0"
ADerrors = "5e92007d-7bf1-471c-8ceb-4591b8b567a9" ADerrors = "5e92007d-7bf1-471c-8ceb-4591b8b567a9"
BDIO = "375f315e-f2c4-11e9-2ef9-134f02f79e27" BDIO = "375f315e-f2c4-11e9-2ef9-134f02f79e27"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
...@@ -8,6 +8,7 @@ include("juobs_tools.jl") ...@@ -8,6 +8,7 @@ include("juobs_tools.jl")
include("juobs_obs.jl") include("juobs_obs.jl")
export read_mesons, read_ms1 export read_mesons, read_ms1
export get_matrix, uwgevp_tot, energies
export corr_obs, plat_av, lin_fit, x_lin_fit, y_lin_fit export corr_obs, plat_av, lin_fit, x_lin_fit, y_lin_fit
export meff, dec_const_pcvc export meff, dec_const_pcvc
......
...@@ -11,10 +11,8 @@ ...@@ -11,10 +11,8 @@
# #
# #
################################################################## ##################################################################
## Setting up packages
using PyPlot, ADerrors, LsqFit, Distributions, Statistics, DelimitedFiles, ForwardDiff, LinearAlgebra
""" @doc raw"""
get_matrix(corr_diag::Vector{Array}, corr_upper::Vector{Array} ) get_matrix(corr_diag::Vector{Array}, corr_upper::Vector{Array} )
This method returns an array of dim T where each element is a symmetrix matrix of dimension n of uwreal correlators This method returns an array of dim T where each element is a symmetrix matrix of dimension n of uwreal correlators
...@@ -24,7 +22,7 @@ at fixed time i=1..T. It takes as input: ...@@ -24,7 +22,7 @@ at fixed time i=1..T. It takes as input:
Each correlator is an vector of uwreal variables of dimension T. Each correlator is an vector of uwreal variables of dimension T.
Example: Example:
```@example
for i in 1:n for i in 1:n
a[i,i] = Vector{uwreal} # vector of uwreal variables of dimension T. They will constitute the diagonal elements of the matrices a[i,i] = Vector{uwreal} # vector of uwreal variables of dimension T. They will constitute the diagonal elements of the matrices
...@@ -51,16 +49,21 @@ Julia> (T,) ...@@ -51,16 +49,21 @@ Julia> (T,)
array_of_matrices[t] # t in 1:T array_of_matrices[t] # t in 1:T
Julia> 4*4 Array{uwreal,2} Julia> 4*4 Array{uwreal,2}
```
""" """
function get_matrix(corr_diag::Vector{Array}, corr_upper::Vector{Array} ) function get_matrix(corr_diag::Vector{Vector{uwreal}}, corr_upper::Vector{Vector{uwreal}})
time = length(corr_diag[1]) # total time slices time = length(corr_diag[1]) # total time slices
n = length(corr_diag) # matrix dimension n = length(corr_diag) # matrix dimension
d = length(corr_upper) # upper elements d = length(corr_upper) # upper elements
res = Vector{Array}() # array with all the matrices at each time step. if d != (n*(n-1) / 2)
for t in 1:time println("Error get_matrix")
return nothing
end
res = Vector{Matrix}(undef, time) # array with all the matrices at each time step.
aux = Matrix{uwreal}(undef, n, n) aux = Matrix{uwreal}(undef, n, n)
for t in 1:time
count=0 count=0
testing = corr_upper[1][1] #testing = corr_upper[1][1]
for i in range(n-1,1, step=-1) for i in range(n-1,1, step=-1)
for j in range(n, i+1, step=-1) for j in range(n, i+1, step=-1)
aux[i,j] = corr_upper[d-count][t] aux[i,j] = corr_upper[d-count][t]
...@@ -70,35 +73,35 @@ function get_matrix(corr_diag::Vector{Array}, corr_upper::Vector{Array} ) ...@@ -70,35 +73,35 @@ function get_matrix(corr_diag::Vector{Array}, corr_upper::Vector{Array} )
aux[i,i] = corr_diag[i][t] aux[i,i] = corr_diag[i][t]
end end
aux[n,n] = corr_diag[n][t] aux[n,n] = corr_diag[n][t]
push!(res, aux) res[t] = aux
end end
return res return res
end end
get_matrix(corr_diag::Vector{Corr}, corr_upper::Vector{Corr}) = get_matrix(getfield.(corr_diag, :obs), getfield.(corr_upper, :obs))
""" @doc raw"""
energies(evals::Vector{Array}) energies(evals::Vector{Array})
Given a vector where each entry evals[t] is a uwreal array of eigenvalues, this method computes the effective energies of the first N states, where N=dim(evals[t]). Given a vector where each entry evals[t] is a uwreal array of eigenvalues, this method computes the effective energies of the first N states, where N=dim(evals[t]).
The index t here runs from 1:T=lenght(evals), while the index i stands for the number of energy levels computed: i = length(evals[t]) The index t here runs from 1:T=lenght(evals), while the index i stands for the number of energy levels computed: i = length(evals[t])
It returns a vector array eff_en where each entry eff_en[t] contains the first N states energies as uwreal objects It returns a vector array eff_en where each entry eff_en[t] contains the first N states energies as uwreal objects
""" """
function energies(evals::Vector{Array}) function energies(evals::Vector{Vector{uwreal}})
time = length(evals) time = length(evals)
n = length(evals[1]) n = length(evals[1])
eff_en = Vector{Array}() eff_en = Vector{Vector}(undef, n)
aux_en = Vector{uwreal}(undef, time-1)
for i in 1:n for i in 1:n
aux_en = Vector{uwreal}()
for t in 1:time-1 for t in 1:time-1
ratio = evals[t][i] / evals[t+1][i] ratio = evals[t][i] / evals[t+1][i]
push!(aux_en, log(ratio*uwcopysign(uwreal(1.0),ratio))) aux_en[t] = log(sqrt(ratio * ratio))
end end
uwerr.(aux_en) #uwerr.(aux_en)
push!(eff_en, aux_en) eff_en[i] = aux_en
end end
return eff_en return eff_en
end end
""" @doc raw"""
uwgevp_tot(mat_list::Vector{Array}, tnot::Int64; iter = 30, evec = false) uwgevp_tot(mat_list::Vector{Array}, tnot::Int64; iter = 30, evec = false)
Given a list of uwreal matrices and an integer value tnot, this method solves the generalised eigenvalue problem Given a list of uwreal matrices and an integer value tnot, this method solves the generalised eigenvalue problem
...@@ -112,22 +115,15 @@ The columns of such matrix are the eigenvectors associated with eigenvalues eval ...@@ -112,22 +115,15 @@ The columns of such matrix are the eigenvectors associated with eigenvalues eval
The keyword iter, set by default to 30, selects the number of iterations of the qr algorithm before stopping. The keyword iter, set by default to 30, selects the number of iterations of the qr algorithm before stopping.
""" """
function uwgevp_tot(mat_list::Vector{Array}, tnot::Int64; iter = 30, evec = false) function uwgevp_tot(mat_list::Vector{Array{uwreal, 2}}, tnot::Int64; iter::Int64 = 30, evec::Bool = false)
n = length(mat_list) if !evec
evals = Vector{Array}() aux_evals = uwgevp.(mat_list, c_tnot=mat_list[tnot], iter = iter)
if evec == false evals = get_diag.(aux_evals)
for i in 1:n
aux_evals = uwgevp(mat_list[i], mat_list[tnot], iter = iter)
push!(evals, get_diag(aux_evals))
end
return evals return evals
else else
evecs = Vector{Array}() aux_evals, aux_evec = uwgevp.(mat_list, c_tnot=mat_list[tnot], iter = iter, evec= true)
for i in 1:n evals = get_diag.(aux_evals)
aux_evals, aux_evec= uwgevp(mat_list[i], mat_list[tnot], iter = iter, evec = true) evecs = aux_evec
push!(evals, get_diag(aux_evals))
push!(evecs, aux_evec)
end
return evals, evecs return evals, evecs
end end
end end
...@@ -142,14 +138,10 @@ C(t)V = C(t_0)D V, where V is the eigenvector matrix and D is the diagonal matri ...@@ -142,14 +138,10 @@ C(t)V = C(t_0)D V, where V is the eigenvector matrix and D is the diagonal matri
If evec = true also the eigenvectors are returned as columns of the orthogonal matrix u. If evec = true also the eigenvectors are returned as columns of the orthogonal matrix u.
The keyword iter, set by default to 30, selects the number of iterations of the qr algorithm before stopping. The keyword iter, set by default to 30, selects the number of iterations of the qr algorithm before stopping.
""" """
function uwgevp(c_t:: Matrix{uwreal}, c_tnot::Matrix{uwreal}; iter = 30, evec = false) function uwgevp(c_t:: Matrix{uwreal}; c_tnot::Matrix{uwreal}, iter::Int64 = 30, evec::Bool = false)
c_tnot_inv = invert(c_tnot) c_tnot_inv = invert(c_tnot)
c = uwdot(c_tnot_inv, c_t) #matrix to diagonalize c = uwdot(c_tnot_inv, c_t) #matrix to diagonalize
if evec == false return uwevp(c, iter = iter, evec = evec)
return uwevp(c, iter = iter)
else
return uwevp(c, iter = iter, evec = true)
end
end end
""" """
...@@ -161,7 +153,7 @@ The keyword iter, set by default to 30, selects the number of iterations of the ...@@ -161,7 +153,7 @@ The keyword iter, set by default to 30, selects the number of iterations of the
""" """
function uwevp(a::Matrix{uwreal}; iter = 30, evec = false) function uwevp(a::Matrix{uwreal}; iter = 30, evec = false)
n = size(a,1) n = size(a,1)
if evec == true if evec
u = idty(n) u = idty(n)
for k in 1:iter for k in 1:iter
q_k, r_k = qr(a) q_k, r_k = qr(a)
...@@ -181,10 +173,7 @@ end ...@@ -181,10 +173,7 @@ end
function get_diag(a::Matrix{uwreal}) function get_diag(a::Matrix{uwreal})
n = size(a,1) n = size(a,1)
res = Vector{uwreal}() res = [a[k, k] for k = 1:n]
for i in 1:n
push!(res, a[i,i])
end
return res return res
end end
...@@ -255,7 +244,7 @@ function make_householder(a::Vector{uwreal}) ...@@ -255,7 +244,7 @@ function make_householder(a::Vector{uwreal})
end end
v[1] = uwreal(1.0) v[1] = uwreal(1.0)
H = idty(n) H = idty(n)
H = H - (2.0 / uwdot(v,v)) * uwdot(reshape(v, :,1), reshape(v, 1 ,:)) H = H - (2.0 / uwdot(v,v)) * uwdot(v, v)
#H = H - (2.0 / dot(value.(v),value.(v))) * uwdot(reshape(v, :,1), reshape(v, 1 ,:)) #H = H - (2.0 / dot(value.(v),value.(v))) * uwdot(reshape(v, :,1), reshape(v, 1 ,:))
for i in 1:n for i in 1:n
for j in 1:n for j in 1:n
...@@ -276,7 +265,7 @@ function make_householder(a::Vector{Float64}) ...@@ -276,7 +265,7 @@ function make_householder(a::Vector{Float64})
println(typeof(v)) println(typeof(v))
y = similar(H) y = similar(H)
println(typeof(y)) println(typeof(y))
H = H .- (2.0 / dot(v,v)) * mul!( y, reshape(v, :,1), reshape(v, 1 ,:)) H = H .- (2.0 / dot(v,v)) * mul!(y, v, transpose(v))
return H return H
end end
...@@ -349,12 +338,9 @@ except for the k-th entry which is set to 1.0. ...@@ -349,12 +338,9 @@ except for the k-th entry which is set to 1.0.
This method is used as input vector in backward_sub to compute the inverse of an upper triangular matrix. This method is used as input vector in backward_sub to compute the inverse of an upper triangular matrix.
""" """
function canonical_basis(n::Int64, k::Int64) function canonical_basis(n::Int64, k::Int64)
e = Vector{uwreal}(undef,n) e = zeros(n)
for i in 1:n
e[i] = 0.0
end
e[k] = 1.0 e[k] = 1.0
return e return [uwreal(e[i]) for i=1:n]
end end
""" """
...@@ -366,30 +352,19 @@ Depending on the arguments, uwdot returns the vector vector or the matrix matrix ...@@ -366,30 +352,19 @@ Depending on the arguments, uwdot returns the vector vector or the matrix matrix
of uwreal data type. of uwreal data type.
""" """
function uwdot(a::Vector{uwreal}, b::Vector{uwreal}) uwdot(a::Vector{uwreal}, b::Vector{uwreal}) = sum(a .* b)
n = length(a)
c = uwreal(0) #Vector{uwreal}(undef, n)
for i in 1:n
c += a[i] * b[i]
end
#uwerr(c)
return c
end
function uwdot(a::Matrix{uwreal}, b::Matrix{uwreal}) function uwdot(a::Matrix{uwreal}, b::Matrix{uwreal})
n,m = size(a) n,m = size(a)
k,p = size(b) k,p = size(b)
c = Matrix{uwreal}(undef, n,p) c = Matrix{uwreal}(undef, n, p)
for i in 1:n if m != k
for j in 1:p println("Error uwdot")
temp = 0.0 # uwreal() return nothing
for l in 1:m
temp += a[i,l] * b[l,j]
end
c[i,j] = temp
if c[i,j] !=0 && c[i,j]!=1
#uwerr(c[i,j])
end end
for i = 1:n
for j = 1:p
c[i, j] = sum(a[i, :] .* b[:, j])
end end
end end
return c return c
...@@ -409,9 +384,9 @@ end ...@@ -409,9 +384,9 @@ end
""" """
Return the transpose of a matrix of uwreals Return the transpose of a matrix of uwreals
""" """
function Base.transpose(a::Matrix{uwreal}) function transpose(a::Matrix{uwreal})
res = similar(a) res = similar(a)
n = size(a, 1 ) n = size(a, 1)
for i in 1:n-1 for i in 1:n-1
for j in i+1:n for j in i+1:n
temp = a[i,j] temp = a[i,j]
...@@ -429,9 +404,10 @@ uwcopysign(a::uwreal, b::uwreal) ...@@ -429,9 +404,10 @@ uwcopysign(a::uwreal, b::uwreal)
It implements the copysign function for uwreal variables It implements the copysign function for uwreal variables
""" """
function uwcopysign(a::uwreal, b::uwreal) function uwcopysign(a::uwreal, b::uwreal)
c = deepcopy(a)
aux = copysign(value(a), value(b)) aux = copysign(value(a), value(b))
a.mean = aux c.mean = aux
return a return c
end end
...@@ -456,7 +432,7 @@ end ...@@ -456,7 +432,7 @@ end
# operator overloading # operator overloading
#=
""" """
Base.:*(x::uwreal, y::Matrix{uwreal}) Base.:*(x::uwreal, y::Matrix{uwreal})
...@@ -475,3 +451,4 @@ function Base.:*(x::uwreal, y::Matrix{uwreal}) ...@@ -475,3 +451,4 @@ function Base.:*(x::uwreal, y::Matrix{uwreal})
end end
return res return res
end end
=#
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment