Ian Jauslin
summaryrefslogtreecommitdiff
blob: 223d6ccf1cf5d27ab36ccd007c636945e6e2cd17 (plain)
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
## Copyright 2021-2023 Ian Jauslin
## 
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
## 
##     http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.

# approximate \int_a^b f using Gauss-Legendre quadratures
@everywhere function integrate_legendre(
  f::Function,
  a::Float64,
  b::Float64,
  weights::Tuple{Array{Float64,1},Array{Float64,1}}
)
  out=0
  for i in 1:length(weights[1])
    out+=(b-a)/2*weights[2][i]*f((b-a)/2*weights[1][i]+(b+a)/2)
  end
  return out
end
# \int f*g where g is sampled at the Legendre nodes
@everywhere function integrate_legendre_sampled(
  f::Function,
  g::Array{Float64,1},
  a::Float64,
  b::Float64,
  weights::Tuple{Array{Float64,1},Array{Float64,1}}
)
  out=0
  for i in 1:length(weights[1])
    out+=(b-a)/2*weights[2][i]*f((b-a)/2*weights[1][i]+(b+a)/2)*g[i]
  end
  return out
end


# approximate \int_a^b f/sqrt((b-x)(x-a)) using Gauss-Chebyshev quadratures
@everywhere function integrate_chebyshev(
  f::Function,
  a::Float64,
  b::Float64,
  N::Int64
)
  out=0
  for i in 1:N
    out=out+pi/N*f((b-a)/2*cos((2*i-1)/(2*N)*pi)+(b+a)/2)
  end
  return out
end

# approximate \int_0^\infty dr f(r)*exp(-a*r) using Gauss-Chebyshev quadratures
@everywhere function integrate_laguerre(
  f::Function,
  a::Float64,
  weights_gL::Tuple{Array{Float64,1},Array{Float64,1}}
)
  out=0.
  for i in 1:length(weights_gL[1])
    out+=1/a*f(weights_gL[1][i]/a)*weights_gL[2][i]
  end
  return out
end

# Hann window
@everywhere function hann(
  x::Float64,
  L::Float64
)
  if abs(x)<L/2
    return cos(pi*x/L)^2
  else
    return 0.
  end
end
# Fourier transform (in 3d)
@everywhere function hann_fourier(
  k::Float64,
  L::Float64
)
  return L^2*4*pi^3/k*(((k*L)^3-4*k*L*pi^2)*cos(k*L/2)-2*(3*(k*L)^2-4*pi^2)*sin(k*L/2))/((k*L)^3-4*k*L*pi^2)^2
end

# normalized Gaussian (in 3d)
@everywhere function gaussian(
  k::Float64,
  L::Float64
)
  return exp(-k^2/(2*L))/sqrt(8*pi^3*L^3)
end