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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
pub use native::*;

// For use in Rust
//
// - Collect into a module to improve code organization, but immediately re-export.
//
// - Each function simply calls its inline analogue with the sole objective of not
//   inlining code when not necessary. The inline functions are still available to
//   the crate for more specialized use.
mod native {
    use super::inlines;
    use crate::{Num, R};

    /// The relativistic fermi momentum `sqrt(mu^2-m^2)` as a function of
    /// the mass `m` and chemical potential `mu`.
    pub fn fermi_momentum(m: R, mu: R) -> R {
        inlines::fermi_momentum(m, mu)
    }

    /// The [Fermi-Dirac distribution](https://en.wikipedia.org/wiki/Fermi%E2%80%93Dirac_statistics).
    ///
    /// As a function of the energy `en`, inverse temperature `beta` and
    /// chemical potential `mu`.
    pub fn fermi_distribution<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        beta: R,
        mu: T2,
    ) -> T1 {
        inlines::fermi_distribution(en, beta, mu)
    }

    /// The [Fermi-Dirac distribution](https://en.wikipedia.org/wiki/Fermi%E2%80%93Dirac_statistics)
    /// at zero temperature.
    ///
    /// As a function of the energy `en` and chemical potential `mu`.
    pub fn fermi_distribution_zero_temp<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        mu: T2,
    ) -> T1 {
        inlines::fermi_distribution_zero_temp(en, mu)
    }

    /// The [Fermi-Dirac distribution](https://en.wikipedia.org/wiki/Fermi%E2%80%93Dirac_statistics),
    /// summed over positive and negative chemical potential.
    ///
    /// As a function of the energy `en`, inverse temperature `beta` and
    /// chemical potential `mu`. Equal to
    /// `fermi_distribution(en, beta, mu) + fermi_distribution(en, beta, -mu)`.
    pub fn fermi_distribution_double<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        beta: R,
        mu: T2,
    ) -> T1 {
        inlines::fermi_distribution_double(en, beta, mu)
    }

    /// The [Fermi-Dirac distribution](https://en.wikipedia.org/wiki/Fermi%E2%80%93Dirac_statistics),
    /// summed over positive and negative chemical potential, at zero temperature.
    ///
    /// As a function of the energy `en` and chemical potential `mu`. Equal to
    /// `fermi_distribution_zero_temp(en, mu) + fermi_distribution_zero_temp(en, -mu)`.
    pub fn fermi_distribution_double_zero_temp<
        T1: Num + std::ops::Sub<T2, Output = T1>,
        T2: Num,
    >(
        en: T1,
        mu: T2,
    ) -> T1 {
        inlines::fermi_distribution_double_zero_temp(en, mu)
    }

    /// The [Fermi-Dirac distribution](https://en.wikipedia.org/wiki/Fermi%E2%80%93Dirac_statistics),
    /// at zero chemical potential.
    ///
    /// As a function of the energy `en` and inverse temperature `beta`.
    pub fn fermi_distribution_zero_chempot<T: Num>(en: T, beta: R) -> T {
        inlines::fermi_distribution_zero_chempot(en, beta)
    }

    /// The [Bose-Einstein distribution](https://en.wikipedia.org/wiki/Bose%E2%80%93Einstein_statistics).
    ///
    /// As a function of the energy `en`, inverse temperature `beta` and
    /// chemical potential `mu`.
    pub fn bose_distribution<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        beta: R,
        mu: T2,
    ) -> T1 {
        inlines::bose_distribution(en, beta, mu)
    }

    /// The [Bose-Einstein distribution](https://en.wikipedia.org/wiki/Bose%E2%80%93Einstein_statistics)
    /// at zero chemical potential.
    ///
    /// As a function of the energy `en` and inverse temperature `beta`.
    pub fn bose_distribution_zero_chempot<T: Num>(en: T, beta: R) -> T {
        inlines::bose_distribution_zero_chempot(en, beta)
    }
}

// For use in other languages, e.g. C/C++/Python
//
// - Re-export at crate::ffi, since symbols need to be unmangled anyway and the
//   namespace will not be preserved.
//
// - Here too each function calls its inline analogue, but the objective is to
//   switch from generic to concrete argument types so that the functions can
//   be compiled into a C dynamic library. To do so, we need to double their
//   number (one function for real arguments, another for complex arguments).
pub(crate) mod ffi {
    use super::inlines;
    use crate::R;

    #[no_mangle]
    pub extern "C" fn fermi_momentum(m: R, mu: R) -> R {
        inlines::fermi_momentum(m, mu)
    }

    #[no_mangle]
    pub extern "C" fn fermi_distribution(en: R, beta: R, mu: R) -> R {
        inlines::fermi_distribution(en, beta, mu)
    }

    #[no_mangle]
    pub extern "C" fn fermi_distribution_zero_temp(en: R, mu: R) -> R {
        inlines::fermi_distribution_zero_temp(en, mu)
    }

    #[no_mangle]
    pub extern "C" fn fermi_distribution_double(en: R, beta: R, mu: R) -> R {
        inlines::fermi_distribution_double(en, beta, mu)
    }

    #[no_mangle]
    pub extern "C" fn fermi_distribution_double_zero_temp(en: R, mu: R) -> R {
        inlines::fermi_distribution_double_zero_temp(en, mu)
    }

    #[no_mangle]
    pub extern "C" fn fermi_distribution_zero_chempot(en: R, beta: R) -> R {
        inlines::fermi_distribution_zero_chempot(en, beta)
    }

    #[no_mangle]
    pub extern "C" fn bose_distribution(en: R, beta: R, mu: R) -> R {
        inlines::bose_distribution(en, beta, mu)
    }

    #[no_mangle]
    pub extern "C" fn bose_distribution_zero_chempot(en: R, beta: R) -> R {
        inlines::bose_distribution_zero_chempot(en, beta)
    }
}

// For internal use only
//
// - Here we define the building blocks for the other functions. This module
//   serves two purposes: to hold inlined functions and to provide a single
//   source of truth for the actual mathematical expressions
pub(crate) mod inlines {
    use crate::low_level::common::inlines::{
        statistic_distribution_exponential, statistic_distribution_exponential_zero_chempot,
    };
    use crate::{Num, R};

    #[inline(always)]
    pub fn fermi_momentum(m: R, mu: R) -> R {
        (mu * mu - m * m).sqrt()
    }

    #[inline(always)]
    pub fn fermi_distribution<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        beta: R,
        mu: T2,
    ) -> T1 {
        (statistic_distribution_exponential(en, beta, mu) + 1.).inv()
    }

    #[inline(always)]
    pub fn fermi_distribution_zero_temp<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        mu: T2,
    ) -> T1 {
        if en.re() > mu.re() {
            T1::zero()
        } else {
            T1::one()
        }
    }

    #[inline(always)]
    pub fn fermi_distribution_double<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        beta: R,
        mu: T2,
    ) -> T1 {
        (statistic_distribution_exponential(en, beta, mu) + 1.).inv()
            + (statistic_distribution_exponential(en, beta, -mu) + 1.).inv()
    }

    #[inline(always)]
    pub fn fermi_distribution_double_zero_temp<
        T1: Num + std::ops::Sub<T2, Output = T1>,
        T2: Num,
    >(
        en: T1,
        mu: T2,
    ) -> T1 {
        fermi_distribution_zero_temp(en, mu) + fermi_distribution_zero_temp(en, -mu)
    }

    #[inline(always)]
    pub fn fermi_distribution_zero_chempot<T: Num>(en: T, beta: R) -> T {
        (statistic_distribution_exponential_zero_chempot(en, beta) + 1.).inv()
    }

    #[inline(always)]
    pub fn bose_distribution<T1: Num + std::ops::Sub<T2, Output = T1>, T2: Num>(
        en: T1,
        beta: R,
        mu: T2,
    ) -> T1 {
        (statistic_distribution_exponential(en, beta, mu) - 1.).inv()
    }

    #[inline(always)]
    pub fn bose_distribution_zero_chempot<T: Num>(en: T, beta: R) -> T {
        (statistic_distribution_exponential_zero_chempot(en, beta) - 1.).inv()
    }
}