-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathUtils.jl
209 lines (185 loc) · 5.36 KB
/
MathUtils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
LogSpaced(min::Float64, max::Float64, n::Int64)
This function evaluates ``n`` points, logarithmically spaced between
``min`` and ``max``.
"""
function LogSpaced(min::T, max::T, n::I) where {T,I}
logmin = log10(min)
logmax = log10(max)
logarray = Array(LinRange(logmin, logmax, n))
return exp10.(logarray)
end
"""
ΔLogSpaced(min::Float64, max::Float64, n::Int64)
This function evaluates ``n`` points, logarithmically spaced between
``min`` and ``max``.
"""
function ΔLogSpaced(min::T, max::T, n::I) where {T,I}
λmin = log10(min)
λmax = log10(max)
Δℓ = zeros(n)
for i in 1:n
Δℓ[i] = 10^(λmin+i*(λmax-λmin)/n)-10^(λmin+(i-1)*(λmax-λmin)/n)
end
return Δℓ
end
"""
BinSearch(x::Float64, Array::Vector{Float64})
Given a value ``z`` and an Array, determines the couple of array elements where
``z`` lies and returns the index corresponding to the first value.
"""
function BinSearch(x::T, Array::AbstractArray{T}) where T
idx = 1
if x <= first(Array)
idx = 1
elseif x >= last(Array)
idx = length(Array)-1
else
while !(x >= Array[idx] && x <= Array[idx+1])
idx += 1
end
end
return idx
end
"""
CustomRegression(x::Vector{Float64}, y::Vector{Float64})
Given two arrays ``x`` and ``y``, performs the linear regression and returns the
coefficients ``c`` and ``m`` of the fitted line.
"""
function CustomRegression(x::Vector{T}, y::Vector{T}) where T
x_mean = mean(x)
y_mean = mean(y)
m = 0
den = 0
for i in 1:length(x)
m += (x[i]-x_mean)*(y[i]-y_mean)
den += (x[i]-x_mean)^2
end
m /= den
c = y_mean - m* x_mean
return c, m
end
"""
SimpsonWeightArray(n::Int64)
This function evaluates an array with the Simpson weight, for Simpson
Integration
"""
function SimpsonWeightArray(n::I) where I
number_intervals = floor((n-1)/2)
weight_array = zeros(n)
if n == number_intervals*2+1
for i in 1:number_intervals
weight_array[Int((i-1)*2+1)] += 1/3
weight_array[Int((i-1)*2+2)] += 4/3
weight_array[Int((i-1)*2+3)] += 1/3
end
else
weight_array[1] += 0.5
weight_array[2] += 0.5
for i in 1:number_intervals
weight_array[Int((i-1)*2+1)+1] += 1/3
weight_array[Int((i-1)*2+2)+1] += 4/3
weight_array[Int((i-1)*2+3)+1] += 1/3
end
weight_array[length(weight_array)] += 0.5
weight_array[length(weight_array)-1] += 0.5
for i in 1:number_intervals
weight_array[Int((i-1)*2+1)] += 1/3
weight_array[Int((i-1)*2+2)] += 4/3
weight_array[Int((i-1)*2+3)] += 1/3
end
weight_array ./= 2
end
return weight_array
end
"""
SimpsonWeightMatrix(n::Int64)
This function evaluates an array with the Simpson weight, for Simpson
Integration of the Lensing Efficiency
"""
function SimpsonWeightMatrix(n::I) where I
number_intervals = floor((n-1)/2)
weight_matrix = zeros(n, n)
for i in 1:n
weight_matrix[i,i:n] = SimpsonWeightArray(n-i+1)
end
return weight_matrix
end
"""
Difference(InputArray::Vector{Float64})
This function evaluates the n-th discrete difference of a given 1-D array.
"""
function Difference(InputArray::AbstractArray{T}) where T
OutputArray = zeros(length(InputArray)-1)
for i in 1:length(InputArray)-1
OutputArray[i] = InputArray[i+1] - InputArray[i]
end
return OutputArray
end
"""
UnevenTrapzWeightArray(InputArray::Vector{Float64})
This function evaluates the array of weights for the integration with uneven
spaced points and the Trapezoidal rule.
"""
function UnevenTrapzWeightArray(InputArray::AbstractArray{T}) where T
WeightArray = zeros(length(InputArray))
DifferenceArray = Difference(InputArray)
for idx in 2:length(WeightArray)-1
WeightArray[idx] = (DifferenceArray[idx]+DifferenceArray[idx-1])/2
end
WeightArray[1] = DifferenceArray[1]/2
WeightArray[length(InputArray)] = DifferenceArray[length(InputArray)-1]/2
return WeightArray
end
function UnevenTrapzWeightMatrix(InputMatrix::AbstractArray{T,2}) where T
WeightMatrix = zeros(size(InputMatrix))
for lidx in 1:length(WeightMatrix[:,1])
WeightMatrix[lidx,:] = UnevenTrapzWeightArray(InputMatrix[lidx,:])
end
return WeightMatrix
end
function uᵢⱼ(n::I, i::I, j::I) where I
u = zeros(floor(Int,0.5*n*(n+1)))
u[floor(Int64, (j-1)*n+i-0.5*j*(j-1))] = 1
return u
end
function eᵢ(n::I, i::I) where I
e = zeros(n)
e[i] = 1
return e
end
function Eᵢⱼ(n::I, i::I, j::I) where I
E = zeros((n,n))
E[i,j] = 1
return E
end
function Tᵢⱼ(n::I, i::I, j::I) where I
if i != j
T = Eᵢⱼ(n,i,j) + Eᵢⱼ(n,j,i)
else
T = Eᵢⱼ(n,i,i)
end
return T
end
function EliminationMatrix(n::I) where I
L = zeros((floor(Int64,0.5*n*(n+1)), n*n))
for i in 1:n
for j in 1:i
L += kron(uᵢⱼ(n,i,j), transpose(vec(Eᵢⱼ(n,i,j))))
end
end
return L
end
"""
DuplicationMatrix(n::Int)
Duplication matrix ``\\boldsymbol{D}_n``.
"""
function DuplicationMatrix(n::I) where I
D = zeros((n*n, floor(Int64,0.5*n*(n+1))))
for i in 1:n
for j in 1:i
D += transpose(kron(uᵢⱼ(n,i,j), transpose(vec(Tᵢⱼ(n,i,j)))))
end
end
return D
end