laddu 0.20.0

Amplitude analysis tools for Rust
Documentation
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use laddu_amplitudes::{
    blatt_weisskopf_barriers as rust_blatt_weisskopf_barriers, breit_wigner as rust_breit_wigner,
    f_vector as rust_f_vector, k_matrix as rust_k_matrix,
    k_matrix_with_background as rust_k_matrix_with_background, kopf_pi1 as rust_kopf_pi1,
    kopf_rho as rust_kopf_rho, p_vector as rust_p_vector,
    p_vector_with_background as rust_p_vector_with_background,
    relativistic_breit_wigner as rust_relativistic_breit_wigner,
    relativistic_breit_wigner_custom as rust_relativistic_breit_wigner_custom,
};
use pyo3::{prelude::*, types::PyAny};

use super::{
    error::to_py_err,
    expr::{PyExpr, extract_expr},
    quantum::extract_l,
};

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    mass: "Expr | int | float",
    width: "Expr | int | float"
))]
/// Construct a constant-width Breit-Wigner amplitude.
///
/// Parameters
/// ----------
/// s : Expr or number
///     Squared invariant mass.
/// mass : Expr or number
///     Resonance pole mass.
/// width : Expr or number
///     Resonance width.
///
/// Returns
/// -------
/// Expr
///     Complex amplitude ``1 / (mass**2 - s - 1j * mass * width)``.
///
/// Raises
/// ------
/// TypeError
///     If an argument cannot be converted to an expression.
pub fn breit_wigner(
    s: &Bound<'_, PyAny>,
    mass: &Bound<'_, PyAny>,
    width: &Bound<'_, PyAny>,
) -> PyResult<PyExpr> {
    Ok(rust_breit_wigner(extract_expr(s)?, extract_expr(mass)?, extract_expr(width)?).into())
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    mass: "Expr | int | float",
    width: "Expr | int | float",
    mass1: "Expr | int | float",
    mass2: "Expr | int | float",
    *,
    l: "int | float | None" = None
))]
/// Construct a relativistic two-body Breit-Wigner amplitude.
///
/// Parameters
/// ----------
/// s, mass, width : Expr or number
///     Squared invariant mass, pole mass, and nominal width.
/// mass1, mass2 : Expr or number
///     Daughter masses.
/// l : L or int, optional
///     Orbital angular momentum; the default is S-wave (``0``).
///
/// Returns
/// -------
/// Expr
///     Complex amplitude with a mass-dependent width and barrier factors.
///
/// Raises
/// ------
/// TypeError
///     If an expression or angular momentum cannot be converted.
/// LadduError
///     If the physical inputs are inconsistent.
pub fn relativistic_breit_wigner(
    s: &Bound<'_, PyAny>,
    mass: &Bound<'_, PyAny>,
    width: &Bound<'_, PyAny>,
    mass1: &Bound<'_, PyAny>,
    mass2: &Bound<'_, PyAny>,
    l: Option<&Bound<'_, PyAny>>,
) -> PyResult<PyExpr> {
    let l = match l {
        Some(l) => extract_l(l)?,
        None => laddu_physics::quantum::L::try_from(0).map_err(to_py_err)?,
    };
    rust_relativistic_breit_wigner(
        extract_expr(s)?,
        extract_expr(mass)?,
        extract_expr(width)?,
        extract_expr(mass1)?,
        extract_expr(mass2)?,
        l,
    )
    .map(PyExpr::from)
    .map_err(to_py_err)
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    mass: "Expr | int | float",
    width: "Expr | int | float",
    mass1: "Expr | int | float",
    mass2: "Expr | int | float",
    l: "int | float",
    *,
    barrier_factors=true,
    q_r=1.0
))]
#[allow(clippy::too_many_arguments)]
/// Construct a configurable relativistic Breit-Wigner amplitude.
///
/// Parameters
/// ----------
/// s, mass, width : Expr or number
///     Squared invariant mass, pole mass, and nominal width.
/// mass1, mass2 : Expr or number
///     Daughter masses.
/// l : L or int
///     Orbital angular momentum.
/// barrier_factors : bool, default=True
///     Include Blatt-Weisskopf factors in the running width.
/// q_r : float, default=1.0
///     Barrier-radius momentum scale.
///
/// Returns
/// -------
/// Expr
///     Configured complex resonance amplitude.
///
/// Raises
/// ------
/// TypeError
///     If an expression or angular momentum cannot be converted.
/// LadduError
///     If the physical inputs are inconsistent.
pub fn relativistic_breit_wigner_custom(
    s: &Bound<'_, PyAny>,
    mass: &Bound<'_, PyAny>,
    width: &Bound<'_, PyAny>,
    mass1: &Bound<'_, PyAny>,
    mass2: &Bound<'_, PyAny>,
    l: &Bound<'_, PyAny>,
    barrier_factors: bool,
    q_r: f64,
) -> PyResult<PyExpr> {
    rust_relativistic_breit_wigner_custom(
        extract_expr(s)?,
        extract_expr(mass)?,
        extract_expr(width)?,
        extract_expr(mass1)?,
        extract_expr(mass2)?,
        extract_l(l)?,
        barrier_factors,
        q_r,
    )
    .map(PyExpr::from)
    .map_err(to_py_err)
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    channel_mass_1: "Expr | int | float",
    channel_mass_2: "Expr | int | float",
    pole_masses: "Expr | int | float",
    l: "int | float",
    *,
    q_r=1.0
))]
/// Build channel-by-pole Blatt-Weisskopf barrier factors.
///
/// Parameters
/// ----------
/// s : Expr
///     Squared invariant mass.
/// channel_mass_1, channel_mass_2 : Expr
///     Vectors of the two daughter masses for each channel.
/// pole_masses : Expr
///     Vector of pole masses.
/// l : L or int
///     Orbital angular momentum.
/// q_r : float, default=1.0
///     Barrier-radius momentum scale.
///
/// Returns
/// -------
/// Expr
///     Matrix whose rows are channels and columns are poles.
///
/// Raises
/// ------
/// TypeError
///     If an argument cannot be converted.
/// LadduError
///     If expression dimensions are incompatible.
pub fn blatt_weisskopf_barriers(
    s: &Bound<'_, PyAny>,
    channel_mass_1: &Bound<'_, PyAny>,
    channel_mass_2: &Bound<'_, PyAny>,
    pole_masses: &Bound<'_, PyAny>,
    l: &Bound<'_, PyAny>,
    q_r: f64,
) -> PyResult<PyExpr> {
    rust_blatt_weisskopf_barriers(
        extract_expr(s)?,
        extract_expr(channel_mass_1)?,
        extract_expr(channel_mass_2)?,
        extract_expr(pole_masses)?,
        extract_l(l)?,
        q_r,
    )
    .map(PyExpr::from)
    .map_err(to_py_err)
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    pole_masses: "Expr | int | float",
    couplings: "Expr | int | float",
    barriers: "Expr | int | float",
    *,
    background: "Expr | int | float | None" = None
))]
/// Construct a coupled-channel K-matrix expression.
///
/// Parameters
/// ----------
/// s : Expr
///     Squared invariant mass.
/// pole_masses : Expr
///     Vector of pole masses.
/// couplings : Expr
///     Channel-by-pole coupling matrix.
/// barriers : Expr
///     Channel-by-pole barrier-factor matrix.
/// background : Expr, optional
///     Symmetric channel-by-channel background matrix.
///
/// Returns
/// -------
/// Expr
///     Channel-by-channel K-matrix.
///
/// Raises
/// ------
/// TypeError
///     If an argument cannot be converted to an expression.
/// LadduError
///     If matrix and vector dimensions are incompatible.
pub fn k_matrix(
    s: &Bound<'_, PyAny>,
    pole_masses: &Bound<'_, PyAny>,
    couplings: &Bound<'_, PyAny>,
    barriers: &Bound<'_, PyAny>,
    background: Option<&Bound<'_, PyAny>>,
) -> PyResult<PyExpr> {
    let result = match background {
        Some(background) => rust_k_matrix_with_background(
            extract_expr(s)?,
            extract_expr(pole_masses)?,
            extract_expr(couplings)?,
            extract_expr(barriers)?,
            extract_expr(background)?,
        ),
        None => rust_k_matrix(
            extract_expr(s)?,
            extract_expr(pole_masses)?,
            extract_expr(couplings)?,
            extract_expr(barriers)?,
        ),
    };
    result.map(PyExpr::from).map_err(to_py_err)
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    pole_masses: "Expr | int | float",
    production: "Expr | int | float",
    couplings: "Expr | int | float",
    barriers: "Expr | int | float",
    *,
    background: "Expr | int | float | None" = None
))]
/// Construct the production vector for a coupled-channel model.
///
/// Parameters
/// ----------
/// s : Expr
///     Squared invariant mass.
/// pole_masses : Expr
///     Vector of pole masses.
/// production : Expr
///     Vector of complex production strengths for the poles.
/// couplings, barriers : Expr
///     Channel-by-pole coupling and barrier matrices.
/// background : Expr, optional
///     Nonresonant channel production vector.
///
/// Returns
/// -------
/// Expr
///     Channel production-vector expression.
///
/// Raises
/// ------
/// TypeError
///     If an argument cannot be converted to an expression.
/// LadduError
///     If matrix and vector dimensions are incompatible.
pub fn p_vector(
    s: &Bound<'_, PyAny>,
    pole_masses: &Bound<'_, PyAny>,
    production: &Bound<'_, PyAny>,
    couplings: &Bound<'_, PyAny>,
    barriers: &Bound<'_, PyAny>,
    background: Option<&Bound<'_, PyAny>>,
) -> PyResult<PyExpr> {
    let result = match background {
        Some(background) => rust_p_vector_with_background(
            extract_expr(s)?,
            extract_expr(pole_masses)?,
            extract_expr(production)?,
            extract_expr(couplings)?,
            extract_expr(barriers)?,
            extract_expr(background)?,
        ),
        None => rust_p_vector(
            extract_expr(s)?,
            extract_expr(pole_masses)?,
            extract_expr(production)?,
            extract_expr(couplings)?,
            extract_expr(barriers)?,
        ),
    };
    result.map(PyExpr::from).map_err(to_py_err)
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    pole_masses: "Expr | int | float",
    k: "Expr | int | float",
    p: "Expr | int | float",
    phase_space: "Expr | int | float"
))]
/// Unitarize a production vector with a K-matrix and phase space.
///
/// Parameters
/// ----------
/// s : Expr
///     Squared invariant mass.
/// pole_masses : Expr
///     Pole-mass vector used to remove spurious singularities.
/// k : Expr
///     Channel-by-channel K-matrix.
/// p : Expr
///     Bare production vector.
/// phase_space : Expr
///     Channel phase-space vector or diagonal matrix.
///
/// Returns
/// -------
/// Expr
///     Unitarized channel amplitude vector.
///
/// Raises
/// ------
/// TypeError
///     If an argument cannot be converted to an expression.
/// LadduError
///     If expression dimensions are incompatible.
pub fn f_vector(
    s: &Bound<'_, PyAny>,
    pole_masses: &Bound<'_, PyAny>,
    k: &Bound<'_, PyAny>,
    p: &Bound<'_, PyAny>,
    phase_space: &Bound<'_, PyAny>,
) -> PyResult<PyExpr> {
    rust_f_vector(
        extract_expr(s)?,
        extract_expr(pole_masses)?,
        extract_expr(k)?,
        extract_expr(p)?,
        extract_expr(phase_space)?,
    )
    .map(PyExpr::from)
    .map_err(to_py_err)
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    production: "list[Expr | int | float] | tuple[Expr | int | float, ...]"
))]
/// Construct the published two-pole Kopf rho amplitude.
///
/// Parameters
/// ----------
/// s : Expr or number
///     Squared invariant mass.
/// production : sequence of Expr
///     Exactly two complex production amplitudes.
///
/// Returns
/// -------
/// Expr
///     Coupled-channel rho amplitude vector.
///
/// Raises
/// ------
/// ValueError
///     If `production` does not contain exactly two entries.
/// TypeError
///     If an entry cannot be converted to an expression.
pub fn kopf_rho(s: &Bound<'_, PyAny>, production: Vec<Bound<'_, PyAny>>) -> PyResult<PyExpr> {
    let production: [laddu_expr::Expr; 2] = production
        .iter()
        .map(extract_expr)
        .collect::<PyResult<Vec<_>>>()?
        .try_into()
        .map_err(|_| {
            pyo3::exceptions::PyValueError::new_err("kopf_rho requires 2 production amplitudes")
        })?;
    rust_kopf_rho(extract_expr(s)?, production)
        .map(PyExpr::from)
        .map_err(to_py_err)
}

#[pyfunction]
#[pyo3(signature = (
    s: "Expr | int | float",
    production: "list[Expr | int | float] | tuple[Expr | int | float, ...]"
))]
/// Construct the published one-pole Kopf pi1 amplitude.
///
/// Parameters
/// ----------
/// s : Expr or number
///     Squared invariant mass.
/// production : sequence of Expr
///     Exactly one complex production amplitude.
///
/// Returns
/// -------
/// Expr
///     Coupled-channel pi1 amplitude vector.
///
/// Raises
/// ------
/// ValueError
///     If `production` does not contain exactly one entry.
/// TypeError
///     If the entry cannot be converted to an expression.
pub fn kopf_pi1(s: &Bound<'_, PyAny>, production: Vec<Bound<'_, PyAny>>) -> PyResult<PyExpr> {
    let production: [laddu_expr::Expr; 1] = production
        .iter()
        .map(extract_expr)
        .collect::<PyResult<Vec<_>>>()?
        .try_into()
        .map_err(|_| {
            pyo3::exceptions::PyValueError::new_err("kopf_pi1 requires 1 production amplitude")
        })?;
    rust_kopf_pi1(extract_expr(s)?, production)
        .map(PyExpr::from)
        .map_err(to_py_err)
}

#[pymodule]
/// Standard resonance and coupled-channel amplitude constructors.
pub mod amplitudes {
    #[pymodule_export]
    use super::{
        blatt_weisskopf_barriers, breit_wigner, f_vector, k_matrix, kopf_pi1, kopf_rho, p_vector,
        relativistic_breit_wigner, relativistic_breit_wigner_custom,
    };
}