#![allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_possible_wrap
)]
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::One;
use pyo3::exceptions::{PyOverflowError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::PyAny;
use crate::coeff::{Integral, Ring};
use crate::convert::{convert, FromSchur, ToSchur};
use crate::guard::{guarded, Guarded, GuardedRat};
use crate::hopf::{self, SkewBy};
use crate::lr::{LrBackend, NaiveLr};
use crate::ops;
use crate::partition::Partition;
use crate::permutation::{Perm, MAX_SUPPORT};
use crate::schubert::Schubert;
use crate::sym::{Elementary, Forgotten, Homogeneous, Ht, Monomial, PowerSum, Schur, St, SymFn};
#[derive(Clone, Debug)]
enum Coeff {
Small(i128),
Big(BigInt),
}
impl<'py> IntoPyObject<'py> for Coeff {
type Target = PyAny;
type Output = Bound<'py, PyAny>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(match self {
Coeff::Small(v) => v.into_pyobject(py)?.into_any(),
Coeff::Big(v) => v.into_pyobject(py)?.into_any(),
})
}
}
impl<'a, 'py> FromPyObject<'a, 'py> for Coeff {
type Error = PyErr;
fn extract(ob: pyo3::Borrowed<'a, 'py, PyAny>) -> Result<Self, PyErr> {
match ob.extract::<i128>() {
Ok(v) => Ok(Coeff::Small(v)),
Err(_) => Ok(Coeff::Big(ob.extract::<BigInt>()?)),
}
}
}
impl Coeff {
fn negated(&self) -> Coeff {
match self {
Coeff::Small(v) => match v.checked_neg() {
Some(n) => Coeff::Small(n),
None => Coeff::Big(-BigInt::from(*v)),
},
Coeff::Big(v) => Coeff::Big(-v.clone()),
}
}
fn to_big(&self) -> BigInt {
match self {
Coeff::Small(v) => BigInt::from(*v),
Coeff::Big(v) => v.clone(),
}
}
fn as_i128(&self) -> Option<i128> {
match self {
Coeff::Small(v) => Some(*v),
Coeff::Big(v) => i128::try_from(v).ok(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct Key(Vec<u32>);
impl<'py> IntoPyObject<'py> for Key {
type Target = pyo3::types::PyTuple;
type Output = Bound<'py, pyo3::types::PyTuple>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
pyo3::types::PyTuple::new(py, self.0)
}
}
impl<'a, 'py> FromPyObject<'a, 'py> for Key {
type Error = PyErr;
fn extract(ob: pyo3::Borrowed<'a, 'py, PyAny>) -> Result<Self, PyErr> {
Ok(Key(ob.extract::<Vec<u32>>()?))
}
}
impl From<Vec<u32>> for Key {
fn from(v: Vec<u32>) -> Self {
Key(v)
}
}
impl std::ops::Deref for Key {
type Target = [u32];
fn deref(&self) -> &[u32] {
&self.0
}
}
type Terms = Vec<(Key, Coeff)>;
type RatTerms = Vec<(Key, (Coeff, Coeff))>;
fn part_arg(p: &[u32]) -> PyResult<Partition> {
let end = p.iter().rposition(|&x| x != 0).map_or(0, |i| i + 1);
let body = &p[..end];
if let Some(i) = body.iter().position(|&x| x == 0) {
return Err(PyValueError::new_err(format!(
"not a partition: {p:?} has a zero part at index {i}, before a positive one; \
only trailing zeros are padding"
)));
}
if let Some(i) = body.windows(2).position(|w| w[0] < w[1]) {
return Err(PyValueError::new_err(format!(
"not a partition: {p:?} increases at index {i} ({} < {}); \
parts must be weakly decreasing",
body[i],
body[i + 1]
)));
}
Ok(Partition::new(body.iter().copied()))
}
fn same_degree(named: &[(&str, &Partition)]) -> PyResult<()> {
let (first_name, first) = named[0];
for (name, p) in &named[1..] {
if p.size() != first.size() {
return Err(PyValueError::new_err(format!(
"these are not all partitions of one integer: {first_name} = {first} has \
degree {}, but {name} = {p} has degree {}",
first.size(),
p.size()
)));
}
}
Ok(())
}
fn level_arg(k: u32) -> PyResult<u32> {
if k < 1 {
return Err(PyValueError::new_err("a ribbon level needs k >= 1, got 0"));
}
Ok(k)
}
fn abacus_arg(lambda: &Partition, k: u32) -> PyResult<()> {
let reach = crate::llt::abacus_reach(lambda, k);
if reach >= crate::llt::ABACUS_REACH_LIMIT {
return Err(PyValueError::new_err(format!(
"the shape {lambda} at level {k} needs beta-numbers up to {reach}, \
past this abacus's {} bits",
crate::llt::ABACUS_REACH_LIMIT
)));
}
Ok(())
}
fn abacus_table_arg(n: u32, k: u32) -> PyResult<()> {
let reach = crate::llt::abacus_reach_table(n, k);
if reach >= crate::llt::ABACUS_REACH_LIMIT {
return Err(PyValueError::new_err(format!(
"every shape of size {} at level {k} needs beta-numbers up to {reach}, \
past this abacus's {} bits",
k * n,
crate::llt::ABACUS_REACH_LIMIT
)));
}
Ok(())
}
fn cells_arg(cells: usize, what: &str) -> PyResult<()> {
if cells > crate::llt::MAX_CELLS {
return Err(PyValueError::new_err(format!(
"{what} is {cells} cells, past the {} a SkewTuple holds; \
this is a representation limit, not a mathematical one",
crate::llt::MAX_CELLS
)));
}
Ok(())
}
trait Boundary: Ring + Integral + Sized + ToCoeff {
fn from_coeff(v: &Coeff) -> Option<Self>;
}
trait ToCoeff {
fn to_coeff(&self) -> Coeff;
}
impl ToCoeff for i128 {
fn to_coeff(&self) -> Coeff {
Coeff::Small(*self)
}
}
impl ToCoeff for Guarded {
fn to_coeff(&self) -> Coeff {
Coeff::Small(self.0)
}
}
impl ToCoeff for BigInt {
fn to_coeff(&self) -> Coeff {
Coeff::Big(self.clone())
}
}
impl Boundary for Guarded {
fn from_coeff(v: &Coeff) -> Option<Self> {
v.as_i128().map(Guarded)
}
}
impl Boundary for BigInt {
fn from_coeff(v: &Coeff) -> Option<Self> {
Some(v.to_big())
}
}
trait BoundaryRat: Ring + Integral + Sized {
fn from_coeff(v: &Coeff) -> Option<Self>;
fn split(&self) -> (Coeff, Coeff);
}
impl BoundaryRat for GuardedRat {
fn from_coeff(v: &Coeff) -> Option<Self> {
v.as_i128()
.filter(|&n| n != i128::MIN)
.map(<GuardedRat as Ring>::from_i128)
}
fn split(&self) -> (Coeff, Coeff) {
(Coeff::Small(self.numer()), Coeff::Small(self.denom()))
}
}
impl BoundaryRat for BigRational {
fn from_coeff(v: &Coeff) -> Option<Self> {
Some(BigRational::from(v.to_big()))
}
fn split(&self) -> (Coeff, Coeff) {
(
Coeff::Big(self.numer().clone()),
Coeff::Big(self.denom().clone()),
)
}
}
trait Wide: Boundary {
fn from_coeff_wide(v: &Coeff) -> Self;
}
impl Wide for BigInt {
fn from_coeff_wide(v: &Coeff) -> Self {
v.to_big()
}
}
trait WideRat: BoundaryRat {
fn from_coeff_wide(v: &Coeff) -> Self;
}
impl WideRat for BigRational {
fn from_coeff_wide(v: &Coeff) -> Self {
BigRational::from(v.to_big())
}
}
type Parsed<'a> = Vec<(Partition, &'a Coeff)>;
fn terms_arg(t: &Terms) -> PyResult<Parsed<'_>> {
t.iter().map(|(p, c)| Ok((part_arg(p)?, c))).collect()
}
fn build<C: Boundary, B: SymFn<C>>(terms: &Parsed) -> Option<B> {
let mut x = B::zero();
for (p, c) in terms {
x.add_term(p.clone(), C::from_coeff(c)?);
}
Some(x)
}
fn build_wide<C: Wide, B: SymFn<C>>(terms: &Parsed) -> B {
let mut x = B::zero();
for (p, c) in terms {
x.add_term(p.clone(), C::from_coeff_wide(c));
}
x
}
fn build_rat<C: BoundaryRat, B: SymFn<C>>(terms: &Parsed) -> Option<B> {
let mut x = B::zero();
for (p, c) in terms {
x.add_term(p.clone(), C::from_coeff(c)?);
}
Some(x)
}
fn build_rat_wide<C: WideRat, B: SymFn<C>>(terms: &Parsed) -> B {
let mut x = B::zero();
for (p, c) in terms {
x.add_term(p.clone(), C::from_coeff_wide(c));
}
x
}
fn dump<C: Boundary, S: SymFn<C>>(x: &S) -> Terms {
x.terms()
.iter()
.map(|(p, c)| (p.parts().to_vec().into(), c.to_coeff()))
.collect()
}
fn dump_integral<C: BoundaryRat, S: SymFn<C>>(x: &S, what: &str) -> PyResult<Terms> {
let mut out = Vec::with_capacity(x.terms().len());
for (p, c) in x.terms() {
let (num, den) = c.split();
if !den.to_big().is_one() {
let (n, d) = (num.to_big(), den.to_big());
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"non-integral {what} coefficient {n}/{d}"
)));
}
out.push((p.parts().to_vec().into(), num));
}
Ok(out)
}
fn escalate<T>(fast: impl FnOnce() -> Option<T>, slow: impl FnOnce() -> T) -> T {
match fast() {
Some(v) => v,
None => slow(),
}
}
fn q_analog_wall(la: &Partition, n: u32) -> PyErr {
PyOverflowError::new_err(format!(
"s_{la}(1, q, .., q^{}) exceeds the fixed-width computation",
n.saturating_sub(1)
))
}
thread_local! {
static PENDING: std::cell::RefCell<Option<PyErr>> = const { std::cell::RefCell::new(None) };
}
fn check_python_signals() -> bool {
Python::attach(|py| match py.check_signals() {
Ok(()) => false,
Err(e) => {
PENDING.with(|p| *p.borrow_mut() = Some(e));
true
}
})
}
fn interruptible<T>(f: impl FnOnce() -> PyResult<T>) -> PyResult<T> {
match crate::interrupt::catch_interrupt(f) {
Ok(r) => r,
Err(crate::interrupt::Interrupted) => Err(PENDING
.with(|p| p.borrow_mut().take())
.unwrap_or_else(|| pyo3::exceptions::PyKeyboardInterrupt::new_err("interrupted"))),
}
}
fn install_quiet_cancellation_hook() {
let previous = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
if !crate::interrupt::is_interrupt(info.payload()) {
previous(info);
}
}));
}
#[pyfunction]
fn schur_multiply(a: Terms, b: Terms) -> PyResult<Terms> {
interruptible(move || {
let (a, b) = (terms_arg(&a)?, terms_arg(&b)?);
Ok(escalate(
|| {
let (x, y): (Schur<Guarded>, Schur<Guarded>) = (build(&a)?, build(&b)?);
Some(dump(&guarded(|| x.mul(&y))?))
},
|| {
let (x, y): (Schur<BigInt>, Schur<BigInt>) = (build_wide(&a), build_wide(&b));
dump(&x.mul(&y))
},
))
})
}
#[pyfunction]
fn st_multiply(a: Terms, b: Terms) -> PyResult<Terms> {
interruptible(move || {
let (a, b) = (terms_arg(&a)?, terms_arg(&b)?);
Ok(escalate(
|| {
let (x, y): (St<Guarded>, St<Guarded>) = (build(&a)?, build(&b)?);
Some(dump(&guarded(|| x.mul(&y))?))
},
|| {
let (x, y): (St<BigInt>, St<BigInt>) = (build_wide(&a), build_wide(&b));
dump(&x.mul(&y))
},
))
})
}
#[pyfunction]
fn reduced_kronecker_product(la: Vec<u32>, mu: Vec<u32>) -> PyResult<Terms> {
interruptible(move || {
let (l, m) = (part_arg(&la)?, part_arg(&mu)?);
Ok(escalate(
|| {
let x: St<Guarded> =
guarded(|| crate::reduced_kronecker_product::<Guarded>(&l, &m))?;
Some(dump(&x))
},
|| dump(&crate::reduced_kronecker_product::<BigInt>(&l, &m)),
))
})
}
#[pyfunction]
fn reduced_kronecker(la: Vec<u32>, mu: Vec<u32>, nu: Vec<u32>) -> PyResult<Coeff> {
interruptible(move || {
let (l, m, n) = (part_arg(&la)?, part_arg(&mu)?, part_arg(&nu)?);
Ok(escalate(
|| {
let v: Guarded = guarded(|| crate::reduced_kronecker::<Guarded>(&l, &m, &n))?;
Some(v.to_coeff())
},
|| crate::reduced_kronecker::<BigInt>(&l, &m, &n).to_coeff(),
))
})
}
#[pyfunction]
fn schur_to_st(a: Terms) -> PyResult<Terms> {
interruptible(move || {
let a = terms_arg(&a)?;
Ok(escalate(
|| {
let x: Schur<Guarded> = build(&a)?;
Some(dump(&guarded(|| St::from_schur(&x))?))
},
|| {
let x: Schur<BigInt> = build_wide(&a);
dump(&St::from_schur(&x))
},
))
})
}
#[pyfunction]
fn st_to_schur(a: Terms) -> PyResult<Terms> {
interruptible(move || {
let a = terms_arg(&a)?;
Ok(escalate(
|| {
let x: St<Guarded> = build(&a)?;
Some(dump(&guarded(|| x.to_schur())?))
},
|| {
let x: St<BigInt> = build_wide(&a);
dump(&x.to_schur())
},
))
})
}
#[pyfunction]
fn ht_multiply(a: Terms, b: Terms) -> PyResult<Option<Terms>> {
interruptible(move || {
let (a, b) = (terms_arg(&a)?, terms_arg(&b)?);
for (l, _) in &a {
for (m, _) in &b {
if crate::character_basis::ht_product_terms(l, m).is_none() {
return Ok(None);
}
}
}
Ok(Some(escalate(
|| {
let (x, y): (Ht<Guarded>, Ht<Guarded>) = (build(&a)?, build(&b)?);
Some(dump(&guarded(|| x.mul(&y))?))
},
|| {
let (x, y): (Ht<BigInt>, Ht<BigInt>) = (build_wide(&a), build_wide(&b));
dump(&x.mul(&y))
},
)))
})
}
#[pyfunction]
fn schur_to_ht(a: Terms) -> PyResult<Terms> {
interruptible(move || {
let a = terms_arg(&a)?;
Ok(escalate(
|| {
let x: Schur<Guarded> = build(&a)?;
Some(dump(&guarded(|| Ht::from_schur(&x))?))
},
|| {
let x: Schur<BigInt> = build_wide(&a);
dump(&Ht::from_schur(&x))
},
))
})
}
#[pyfunction]
fn ht_to_schur(a: Terms) -> PyResult<Terms> {
interruptible(move || {
let a = terms_arg(&a)?;
Ok(escalate(
|| {
let x: Ht<Guarded> = build(&a)?;
Some(dump(&guarded(|| x.to_schur())?))
},
|| {
let x: Ht<BigInt> = build_wide(&a);
dump(&x.to_schur())
},
))
})
}
type SchubTerms = Vec<(Key, Coeff)>;
type SchubParsed<'a> = Vec<(Perm, &'a Coeff)>;
fn schub_terms(t: &SchubTerms) -> PyResult<SchubParsed<'_>> {
t.iter().map(|(w, c)| Ok((perm_arg(w)?, c))).collect()
}
fn build_schubert<C: Boundary>(t: &SchubParsed) -> Option<Schubert<C>> {
let mut out = Schubert::zero();
for (p, c) in t {
out.add_term(*p, &C::from_coeff(c)?);
}
Some(out)
}
fn build_schubert_wide<C: Wide>(t: &SchubParsed) -> Schubert<C> {
let mut out = Schubert::zero();
for (p, c) in t {
out.add_term(*p, &C::from_coeff_wide(c));
}
out
}
fn dump_schubert<C: Boundary>(f: &Schubert<C>) -> SchubTerms {
f.terms()
.iter()
.map(|(w, c)| {
(
w.one_line()
.iter()
.map(|&x| x as u32)
.collect::<Vec<_>>()
.into(),
c.to_coeff(),
)
})
.collect()
}
fn perm_arg(w: &[u32]) -> PyResult<Perm> {
Perm::new(w.iter().copied())
.map_err(|e| PyValueError::new_err(format!("not a permutation: {e:?}")))
}
fn variable_arg(i: u32) -> PyResult<u32> {
if i < 1 {
return Err(PyValueError::new_err("variable index is 1-based"));
}
if i as usize >= MAX_SUPPORT {
return Err(PyValueError::new_err(format!(
"variable index {i} is past this permutation representation's \
ceiling of {} points",
MAX_SUPPORT
)));
}
Ok(i)
}
fn in_flag(t: &SchubParsed, n: u32, which: &str) -> PyResult<()> {
if let Some((w, _)) = t.iter().find(|(w, _)| w.support_len() > n) {
return Err(PyValueError::new_err(format!(
"{which} has a term {w} outside S_{n}: it moves {} points, so the \
pairing on Fl({n}) is not defined for it",
w.support_len()
)));
}
Ok(())
}
fn rank_arg(n: u32) -> PyResult<u32> {
if n as usize > MAX_SUPPORT {
return Err(PyValueError::new_err(format!(
"Fl({n}) needs {n} points, past this permutation representation's \
ceiling of {}",
MAX_SUPPORT
)));
}
Ok(n)
}
fn code_arg(e: &[u32]) -> PyResult<()> {
let end = e.iter().rposition(|&x| x != 0).map_or(0, |i| i + 1);
let mut m = end as u32;
for (i, &c) in e.iter().enumerate() {
m = m.max(i as u32 + 1 + c);
}
if m as usize > MAX_SUPPORT {
return Err(PyValueError::new_err(format!(
"the exponent vector {e:?} is the code of a permutation of {m} points, \
past this representation's ceiling of {}",
MAX_SUPPORT
)));
}
Ok(())
}
#[pyfunction]
fn schubert_multiply(a: SchubTerms, b: SchubTerms) -> PyResult<SchubTerms> {
interruptible(move || {
let (a, b) = (schub_terms(&a)?, schub_terms(&b)?);
Ok(escalate(
|| {
let (x, y): (Schubert<Guarded>, Schubert<Guarded>) =
(build_schubert(&a)?, build_schubert(&b)?);
Some(dump_schubert(&guarded(|| x.mul(&y))?))
},
|| {
let (x, y): (Schubert<BigInt>, Schubert<BigInt>) =
(build_schubert_wide(&a), build_schubert_wide(&b));
dump_schubert(&x.mul(&y))
},
))
})
}
#[pyfunction]
fn schubert_multiply_variable(a: SchubTerms, i: u32) -> PyResult<SchubTerms> {
interruptible(move || {
let (a, i) = (schub_terms(&a)?, variable_arg(i)?);
Ok(escalate(
|| {
let x: Schubert<Guarded> = build_schubert(&a)?;
Some(dump_schubert(&guarded(|| x.mul_variable(i))?))
},
|| {
let x: Schubert<BigInt> = build_schubert_wide(&a);
dump_schubert(&x.mul_variable(i))
},
))
})
}
#[pyfunction]
fn schubert_divided_difference(a: SchubTerms, i: u32) -> PyResult<SchubTerms> {
interruptible(move || {
let (a, i) = (schub_terms(&a)?, variable_arg(i)?);
Ok(escalate(
|| {
let x: Schubert<Guarded> = build_schubert(&a)?;
Some(dump_schubert(&guarded(|| x.divided_difference(i))?))
},
|| {
let x: Schubert<BigInt> = build_schubert_wide(&a);
dump_schubert(&x.divided_difference(i))
},
))
})
}
#[pyfunction]
fn schubert_divided_difference_perm(a: SchubTerms, w: Vec<u32>) -> PyResult<SchubTerms> {
interruptible(move || {
let (a, p) = (schub_terms(&a)?, perm_arg(&w)?);
Ok(escalate(
|| {
let x: Schubert<Guarded> = build_schubert(&a)?;
Some(dump_schubert(&guarded(|| x.divided_difference_perm(&p))?))
},
|| {
let x: Schubert<BigInt> = build_schubert_wide(&a);
dump_schubert(&x.divided_difference_perm(&p))
},
))
})
}
#[pyfunction]
fn schubert_expand(a: SchubTerms) -> PyResult<Terms> {
interruptible(move || {
let a = schub_terms(&a)?;
Ok(escalate(
|| {
let x: Schubert<Guarded> = build_schubert(&a)?;
let e = guarded(|| x.expand())?;
Some(
e.into_iter()
.map(|(v, c)| (v.into(), c.to_coeff()))
.collect(),
)
},
|| {
let x: Schubert<BigInt> = build_schubert_wide(&a);
x.expand()
.into_iter()
.map(|(v, c)| (v.into(), c.to_coeff()))
.collect()
},
))
})
}
#[pyfunction]
fn polynomial_to_schubert(terms: Terms) -> PyResult<SchubTerms> {
interruptible(move || {
for (e, _) in &terms {
code_arg(e)?;
}
Ok(escalate(
|| {
let t: Vec<(Vec<u32>, Guarded)> = terms
.iter()
.map(|(e, c)| Guarded::from_coeff(c).map(|g| (e.to_vec(), g)))
.collect::<Option<_>>()?;
Some(dump_schubert(&guarded(|| Schubert::from_polynomial(&t))?))
},
|| {
let t: Vec<(Vec<u32>, BigInt)> = terms
.iter()
.map(|(e, c)| (e.to_vec(), BigInt::from_coeff_wide(c)))
.collect();
dump_schubert(&Schubert::from_polynomial(&t))
},
))
})
}
#[pyfunction]
fn schubert_pairing(a: SchubTerms, b: SchubTerms, n: u32) -> PyResult<Coeff> {
interruptible(move || {
let (a, b, n) = (schub_terms(&a)?, schub_terms(&b)?, rank_arg(n)?);
in_flag(&a, n, "a")?;
in_flag(&b, n, "b")?;
Ok(escalate(
|| {
let (x, y): (Schubert<Guarded>, Schubert<Guarded>) =
(build_schubert(&a)?, build_schubert(&b)?);
Some(guarded(|| x.pairing(&y, n))?.to_coeff())
},
|| {
let (x, y): (Schubert<BigInt>, Schubert<BigInt>) =
(build_schubert_wide(&a), build_schubert_wide(&b));
x.pairing(&y, n).to_coeff()
},
))
})
}
#[pyfunction]
fn schubert_scalar_product(a: SchubTerms, b: SchubTerms, n: u32) -> PyResult<SchubTerms> {
interruptible(move || {
let (a, b, n) = (schub_terms(&a)?, schub_terms(&b)?, rank_arg(n)?);
Ok(escalate(
|| {
let (x, y): (Schubert<Guarded>, Schubert<Guarded>) =
(build_schubert(&a)?, build_schubert(&b)?);
Some(dump_schubert(&guarded(|| x.scalar_product(&y, n))?))
},
|| {
let (x, y): (Schubert<BigInt>, Schubert<BigInt>) =
(build_schubert_wide(&a), build_schubert_wide(&b));
dump_schubert(&x.scalar_product(&y, n))
},
))
})
}
#[pyfunction]
fn schubert_dimension(w: Vec<u32>) -> PyResult<u128> {
interruptible(move || {
let p = perm_arg(&w)?;
crate::schubert::dimension(&p).ok_or_else(|| {
PyOverflowError::new_err(format!("the pipe-dream count of {p} exceeds u128"))
})
})
}
#[pyfunction]
fn schubert_coefficient(u: Vec<u32>, v: Vec<u32>, w: Vec<u32>) -> PyResult<Coeff> {
interruptible(move || {
let (pu, pv, pw) = (perm_arg(&u)?, perm_arg(&v)?, perm_arg(&w)?);
Ok(escalate(
|| {
guarded(|| crate::schubert::schubert_coeff::<Guarded>(&pu, &pv, &pw))
.map(|c| c.to_coeff())
},
|| crate::schubert::schubert_coeff::<BigInt>(&pu, &pv, &pw).to_coeff(),
))
})
}
#[pyfunction]
fn schubert_to_stanley_schur(w: Vec<u32>) -> PyResult<Terms> {
interruptible(move || {
let p = perm_arg(&w)?;
Ok(escalate(
|| {
let f = guarded(|| crate::schubert::stanley::<Guarded>(&p))?;
Some(dump(&f))
},
|| dump(&crate::schubert::stanley::<BigInt>(&p)),
))
})
}
#[pyfunction]
fn schubert_monomial_mass(u: Vec<u32>, v: Vec<u32>) -> PyResult<u128> {
interruptible(move || {
let (pu, pv) = (perm_arg(&u)?, perm_arg(&v)?);
Ok(crate::schubert::schubert_monomial_mass_of(&pu, &pv))
})
}
#[pyfunction]
fn clear_caches() -> PyResult<()> {
interruptible(move || {
crate::memo::clear_caches();
Ok(())
})
}
#[pyfunction]
fn cache_stats() -> PyResult<Vec<(String, u8, usize, usize)>> {
Ok(crate::memo::cache_stats()
.into_iter()
.map(|r| (r.name.to_string(), r.tier, r.entries, r.bytes))
.collect())
}
#[pyfunction]
fn cache_budget() -> PyResult<Option<usize>> {
Ok(crate::memo::cache_budget())
}
#[pyfunction]
fn set_cache_budget(budget: Option<usize>) -> PyResult<()> {
crate::memo::set_cache_budget(budget);
Ok(())
}
#[pyfunction]
fn lr_coefficient(la: Vec<u32>, mu: Vec<u32>, nu: Vec<u32>) -> PyResult<u128> {
interruptible(move || Ok(NaiveLr.lr_coeff(&part_arg(&la)?, &part_arg(&mu)?, &part_arg(&nu)?)))
}
fn relay(t: &Terms) -> Parsed<'_> {
t.iter()
.map(|(p, c)| (Partition::new(p.iter().copied()), c))
.collect()
}
macro_rules! out_of_schur {
($(#[$doc:meta])* $name:ident, $inner:ident, $basis:ident) => {
fn $inner(a: &Parsed) -> Terms {
escalate(
|| {
let s: Schur<Guarded> = build(a)?;
Some(dump(&guarded(|| $basis::<Guarded>::from_schur(&s))?))
},
|| {
let s: Schur<BigInt> = build_wide(a);
dump(&$basis::<BigInt>::from_schur(&s))
},
)
}
$(#[$doc])*
#[pyfunction]
fn $name(a: Terms) -> PyResult<Terms> {
Ok($inner(&terms_arg(&a)?))
}
};
}
out_of_schur!(
schur_to_homogeneous,
s_to_h,
Homogeneous
);
out_of_schur!(
schur_to_elementary,
s_to_e,
Elementary
);
out_of_schur!(
schur_to_monomial,
s_to_m,
Monomial
);
out_of_schur!(
schur_to_forgotten,
s_to_f,
Forgotten
);
fn split<C: BoundaryRat>(p: &PowerSum<C>) -> RatTerms {
p.terms()
.iter()
.map(|(part, c)| (part.parts().to_vec().into(), c.split()))
.collect()
}
macro_rules! into_power {
($inner:ident, $basis:ident) => {
fn $inner(a: &Parsed) -> RatTerms {
escalate(
|| {
let x: $basis<GuardedRat> = build_rat(a)?;
Some(split(&guarded(|| {
convert::<GuardedRat, _, PowerSum<GuardedRat>>(&x)
})?))
},
|| {
let x: $basis<BigRational> = build_rat_wide(a);
split(&convert::<BigRational, _, PowerSum<BigRational>>(&x))
},
)
}
};
}
into_power!(s_to_p, Schur);
into_power!(h_to_p, Homogeneous);
into_power!(e_to_p, Elementary);
#[pyfunction]
fn to_power(a: Terms, src: &str) -> PyResult<RatTerms> {
interruptible(move || {
let a = terms_arg(&a)?;
Ok(match Basis::parse(src)? {
Basis::Schur => s_to_p(&a),
Basis::Homogeneous => h_to_p(&a),
Basis::Elementary => e_to_p(&a),
Basis::PowerSum => a
.iter()
.map(|(p, c)| (p.parts().to_vec().into(), ((*c).clone(), Coeff::Small(1))))
.collect(),
Basis::Monomial => s_to_p(&relay(&m_to_s(&a))),
Basis::Forgotten => s_to_p(&relay(&f_to_s(&a))),
})
})
}
macro_rules! into_schur {
($(#[$doc:meta])* $name:ident, $inner:ident, $basis:ident) => {
fn $inner(a: &Parsed) -> Terms {
escalate(
|| {
let x: $basis<Guarded> = build(a)?;
Some(dump(&guarded(|| x.to_schur())?))
},
|| {
let x: $basis<BigInt> = build_wide(a);
dump(&x.to_schur())
},
)
}
$(#[$doc])*
#[pyfunction]
fn $name(a: Terms) -> PyResult<Terms> {
Ok($inner(&terms_arg(&a)?))
}
};
}
into_schur!(
homogeneous_to_schur,
h_to_s,
Homogeneous
);
into_schur!(
elementary_to_schur,
e_to_s,
Elementary
);
into_schur!(
monomial_to_schur,
m_to_s,
Monomial
);
into_schur!(
power_to_schur,
p_to_s,
PowerSum
);
into_schur!(
forgotten_to_schur,
f_to_s,
Forgotten
);
macro_rules! direct_route {
($inner:ident, $from:ident, $to:ident) => {
fn $inner(a: &Parsed) -> Terms {
escalate(
|| {
let x: $from<Guarded> = build(a)?;
Some(dump(&guarded(|| convert::<Guarded, _, $to<Guarded>>(&x))?))
},
|| {
let x: $from<BigInt> = build_wide(a);
dump(&convert::<BigInt, _, $to<BigInt>>(&x))
},
)
}
};
}
direct_route!(p_to_h, PowerSum, Homogeneous);
direct_route!(p_to_e, PowerSum, Elementary);
direct_route!(h_to_e, Homogeneous, Elementary);
direct_route!(e_to_h, Elementary, Homogeneous);
direct_route!(h_to_m, Homogeneous, Monomial);
direct_route!(e_to_m, Elementary, Monomial);
direct_route!(h_to_f, Homogeneous, Forgotten);
direct_route!(e_to_f, Elementary, Forgotten);
#[pyfunction]
fn plethysm(f: Terms, g: Terms) -> PyResult<Terms> {
interruptible(move || {
let (f, g) = (terms_arg(&f)?, terms_arg(&g)?);
escalate(
|| {
let (x, y): (Schur<GuardedRat>, Schur<GuardedRat>) =
(build_rat(&f)?, build_rat(&g)?);
let r = guarded(|| crate::plethysm::plethysm(&x, &y))?;
Some(dump_integral(&r, "plethysm"))
},
|| {
let (x, y): (Schur<BigRational>, Schur<BigRational>) =
(build_rat_wide(&f), build_rat_wide(&g));
dump_integral(&crate::plethysm::plethysm(&x, &y), "plethysm")
},
)
})
}
#[pyfunction]
#[pyo3(signature = (f, g, basis = "s"))]
fn skew_by(f: Terms, g: Terms, basis: &str) -> PyResult<Terms> {
interruptible(move || {
let b = Basis::parse(basis)?;
fn fast(f: &Parsed, g: &Parsed, b: Basis) -> Option<Schur<Guarded>> {
let sf: Schur<Guarded> = build(f)?;
Some(match b {
Basis::Schur => SkewBy::skew_by(&sf, &build::<_, Schur<Guarded>>(g)?),
Basis::Homogeneous => SkewBy::skew_by(&sf, &build::<_, Homogeneous<Guarded>>(g)?),
Basis::Elementary => SkewBy::skew_by(&sf, &build::<_, Elementary<Guarded>>(g)?),
Basis::PowerSum => SkewBy::skew_by(&sf, &build::<_, PowerSum<Guarded>>(g)?),
Basis::Monomial => SkewBy::skew_by(&sf, &build::<_, Monomial<Guarded>>(g)?),
Basis::Forgotten => SkewBy::skew_by(&sf, &build::<_, Forgotten<Guarded>>(g)?),
})
}
fn wide(f: &Parsed, g: &Parsed, b: Basis) -> Schur<BigInt> {
let sf: Schur<BigInt> = build_wide(f);
match b {
Basis::Schur => SkewBy::skew_by(&sf, &build_wide::<_, Schur<BigInt>>(g)),
Basis::Homogeneous => {
SkewBy::skew_by(&sf, &build_wide::<_, Homogeneous<BigInt>>(g))
}
Basis::Elementary => SkewBy::skew_by(&sf, &build_wide::<_, Elementary<BigInt>>(g)),
Basis::PowerSum => SkewBy::skew_by(&sf, &build_wide::<_, PowerSum<BigInt>>(g)),
Basis::Monomial => SkewBy::skew_by(&sf, &build_wide::<_, Monomial<BigInt>>(g)),
Basis::Forgotten => SkewBy::skew_by(&sf, &build_wide::<_, Forgotten<BigInt>>(g)),
}
}
let (f, g) = (terms_arg(&f)?, terms_arg(&g)?);
Ok(escalate(
|| Some(dump(&guarded(|| fast(&f, &g, b))??)),
|| dump(&wide(&f, &g, b)),
))
})
}
#[pyfunction]
fn evaluate_schur(a: Terms, xs: Vec<Coeff>) -> PyResult<Coeff> {
interruptible(move || {
let a = terms_arg(&a)?;
Ok(escalate(
|| {
let s: Schur<Guarded> = build(&a)?;
let alphabet: Option<Vec<Guarded>> =
xs.iter().map(<Guarded as Boundary>::from_coeff).collect();
let alphabet = alphabet?;
Some(guarded(|| s.eval(&alphabet))?.to_coeff())
},
|| {
let s: Schur<BigInt> = build_wide(&a);
let alphabet: Vec<BigInt> = xs.iter().map(Coeff::to_big).collect();
Coeff::Big(s.eval(&alphabet))
},
))
})
}
#[pyfunction]
fn expand_alphabet(a: Terms, src: &str, n: usize) -> PyResult<Vec<(Key, Coeff)>> {
interruptible(move || {
let a = terms_arg(&a)?;
let terms = match Basis::parse(src)? {
Basis::Monomial => return rows_of(&a, n),
Basis::Schur => s_to_m(&a),
Basis::Homogeneous => s_to_m(&relay(&h_to_s(&a))),
Basis::Elementary => s_to_m(&relay(&e_to_s(&a))),
Basis::PowerSum => s_to_m(&relay(&p_to_s(&a))),
Basis::Forgotten => s_to_m(&relay(&f_to_s(&a))),
};
rows_of(&relay(&terms), n)
})
}
fn rows_of(terms: &Parsed, n: usize) -> PyResult<Vec<(Key, Coeff)>> {
fn rows<C: Boundary>(terms: &Parsed, n: usize) -> Option<Vec<(Key, Coeff)>> {
let m: Monomial<C> = build(terms)?;
Some(expand_rows(&m, n))
}
fn rows_wide<C: Wide>(terms: &Parsed, n: usize) -> Vec<(Key, Coeff)> {
let m: Monomial<C> = build_wide(terms);
expand_rows(&m, n)
}
fn expand_rows<C: Ring + Boundary>(m: &Monomial<C>, n: usize) -> Vec<(Key, Coeff)> {
m.expand(n)
.into_iter()
.map(|(alpha, c)| (alpha.into(), c.to_coeff()))
.collect()
}
Ok(escalate(
|| rows::<Guarded>(terms, n),
|| rows_wide::<BigInt>(terms, n),
))
}
#[pyfunction]
fn monomial_multiply(a: Terms, b: Terms) -> PyResult<Terms> {
interruptible(move || {
let (a, b) = (terms_arg(&a)?, terms_arg(&b)?);
Ok(escalate(
|| {
let (x, y): (Monomial<Guarded>, Monomial<Guarded>) = (build(&a)?, build(&b)?);
Some(dump(&guarded(|| x.mul(&y))?))
},
|| {
let (x, y): (Monomial<BigInt>, Monomial<BigInt>) = (build_wide(&a), build_wide(&b));
dump(&x.mul(&y))
},
))
})
}
#[pyfunction]
fn semistandard_tableaux(la: Vec<u32>, mu: Vec<u32>) -> PyResult<Vec<Vec<Vec<u32>>>> {
interruptible(move || Ok(crate::kostka::semistandard_tableaux(&part_arg(&la)?, &mu)))
}
#[pyfunction]
fn dimension(la: Vec<u32>) -> PyResult<Option<u128>> {
interruptible(move || Ok(crate::eval::dimension(&part_arg(&la)?)))
}
#[pyfunction]
fn principal_specialization(la: Vec<u32>, n: u32) -> PyResult<Option<u128>> {
interruptible(move || Ok(crate::eval::principal_specialization(&part_arg(&la)?, n)))
}
#[pyfunction]
fn principal_specialization_q(la: Vec<u32>, n: u32) -> PyResult<Vec<i128>> {
interruptible(move || {
let l = part_arg(&la)?;
crate::eval::principal_specialization_q(&l, n).ok_or_else(|| {
PyOverflowError::new_err(format!(
"s_{l}(1, q, .., q^{}) exceeds the fixed-width computation",
n.saturating_sub(1)
))
})
})
}
#[pyfunction]
fn kostka_number(la: Vec<u32>, mu: Vec<u32>) -> PyResult<u128> {
interruptible(move || {
let mut mu = mu;
mu.sort_unstable_by(|a, b| b.cmp(a));
let (l, m) = (part_arg(&la)?, part_arg(&mu)?);
crate::kostka::try_kostka(&l, &m)
.ok_or_else(|| PyOverflowError::new_err(format!("K_{{{l},{m}}} exceeds u128")))
})
}
#[pyfunction]
fn character_value(la: Vec<u32>, mu: Vec<u32>) -> PyResult<Coeff> {
interruptible(move || {
let (l, m) = (part_arg(&la)?, part_arg(&mu)?);
same_degree(&[("la", &l), ("mu", &m)])?;
Ok(match crate::character::try_character(&l, &m) {
Some(v) => Coeff::Small(v),
None => Coeff::Big(crate::character::character_in::<BigInt>(&l, &m)),
})
})
}
#[pyfunction]
fn internal_product(a: Terms, b: Terms) -> PyResult<Terms> {
interruptible(move || {
let (a, b) = (terms_arg(&a)?, terms_arg(&b)?);
escalate(
|| {
let (x, y): (Schur<GuardedRat>, Schur<GuardedRat>) =
(build_rat(&a)?, build_rat(&b)?);
let r = guarded(|| ops::internal(&x, &y))?;
Some(dump_integral(&r, "Kronecker"))
},
|| {
let (x, y): (Schur<BigRational>, Schur<BigRational>) =
(build_rat_wide(&a), build_rat_wide(&b));
dump_integral(&ops::internal(&x, &y), "Kronecker")
},
)
})
}
#[pyfunction]
fn kronecker_coefficient(la: Vec<u32>, mu: Vec<u32>, nu: Vec<u32>) -> PyResult<Coeff> {
interruptible(move || {
let (l, m, n) = (part_arg(&la)?, part_arg(&mu)?, part_arg(&nu)?);
same_degree(&[("la", &l), ("mu", &m), ("nu", &n)])?;
Ok(escalate(
|| {
let v: GuardedRat = guarded(|| ops::kronecker_via_characters(&l, &m, &n))?;
(v.denom() == 1).then(|| Coeff::Small(v.numer()))
},
|| {
let v: BigRational = ops::kronecker_via_characters(&l, &m, &n);
assert!(
v.is_integer(),
"Kronecker coefficient is not an integer over BigRational, \
which is a bug rather than an overflow"
);
Coeff::Big(v.to_integer())
},
))
})
}
#[pyfunction]
fn convert_indexed(a: Terms, src: &str, dst: &str) -> PyResult<Vec<(u32, usize, Coeff)>> {
interruptible(move || {
Ok(routed(&terms_arg(&a)?, src, dst)?
.into_iter()
.map(|(p, c)| {
let n: u32 = p.iter().sum();
(n, index_of(n, &p), c)
})
.collect())
})
}
#[pyfunction]
fn convert_terms(a: Terms, src: &str, dst: &str) -> PyResult<Terms> {
interruptible(move || routed(&terms_arg(&a)?, src, dst))
}
fn routed(a: &Parsed, src: &str, dst: &str) -> PyResult<Terms> {
let src = Basis::parse(src)?;
let dst = Basis::parse(dst).map_err(|_| bad_dst_basis(dst))?;
if dst == Basis::PowerSum {
return Err(bad_dst_basis("powersum"));
}
match (src, dst) {
(Basis::PowerSum, Basis::Homogeneous) => return Ok(p_to_h(a)),
(Basis::PowerSum, Basis::Elementary) => return Ok(p_to_e(a)),
(Basis::Homogeneous, Basis::Elementary) => return Ok(h_to_e(a)),
(Basis::Elementary, Basis::Homogeneous) => return Ok(e_to_h(a)),
(Basis::Homogeneous, Basis::Monomial) => return Ok(h_to_m(a)),
(Basis::Elementary, Basis::Monomial) => return Ok(e_to_m(a)),
(Basis::Homogeneous, Basis::Forgotten) => return Ok(h_to_f(a)),
(Basis::Elementary, Basis::Forgotten) => return Ok(e_to_f(a)),
_ => {}
}
let terms = match src {
Basis::Schur => dump_parsed(a),
Basis::Monomial => m_to_s(a),
Basis::Homogeneous => h_to_s(a),
Basis::Elementary => e_to_s(a),
Basis::PowerSum => p_to_s(a),
Basis::Forgotten => f_to_s(a),
};
Ok(match dst {
Basis::Schur => terms,
Basis::Monomial => s_to_m(&relay(&terms)),
Basis::Homogeneous => s_to_h(&relay(&terms)),
Basis::Elementary => s_to_e(&relay(&terms)),
Basis::Forgotten => s_to_f(&relay(&terms)),
Basis::PowerSum => return Err(bad_dst_basis("powersum")),
})
}
fn dump_parsed(a: &Parsed) -> Terms {
a.iter()
.map(|(p, c)| (p.parts().to_vec().into(), (*c).clone()))
.collect()
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Basis {
Schur,
Homogeneous,
Elementary,
PowerSum,
Monomial,
Forgotten,
}
impl Basis {
fn parse(name: &str) -> PyResult<Self> {
Ok(match name {
"Schur" | "s" => Basis::Schur,
"homogeneous" | "h" => Basis::Homogeneous,
"elementary" | "e" => Basis::Elementary,
"powersum" | "p" => Basis::PowerSum,
"monomial" | "m" => Basis::Monomial,
"forgotten" | "f" => Basis::Forgotten,
other => return Err(bad_basis(other)),
})
}
fn code(self) -> &'static str {
match self {
Basis::Schur => "s",
Basis::Homogeneous => "h",
Basis::Elementary => "e",
Basis::PowerSum => "p",
Basis::Monomial => "m",
Basis::Forgotten => "f",
}
}
}
fn bad_basis(other: &str) -> PyErr {
PyValueError::new_err(format!(
"unknown basis {other:?}; expected one of Schur, homogeneous, elementary, powersum, monomial, forgotten \
or the one-letter codes s, h, e, p, m, f"
))
}
fn bad_dst_basis(other: &str) -> PyErr {
PyValueError::new_err(format!(
"unknown target basis {other:?}; expected one of Schur, homogeneous, elementary, monomial, forgotten \
or the codes s, h, e, m, f. \
The power-sum basis is not a target here because that conversion is rational; use to_power(a, src)"
))
}
thread_local! {
static INDEX: std::cell::RefCell<std::collections::HashMap<u32, std::collections::HashMap<Vec<u32>, usize>>> =
std::cell::RefCell::new(std::collections::HashMap::new());
}
fn index_of(n: u32, parts: &[u32]) -> usize {
INDEX.with(|cell| {
let mut m = cell.borrow_mut();
let table = m.entry(n).or_insert_with(|| {
crate::memo::partitions_cached(n)
.iter()
.enumerate()
.map(|(i, p)| (p.parts().to_vec(), i))
.collect()
});
table[parts]
})
}
#[pyfunction]
fn partitions(n: u32) -> PyResult<Vec<Key>> {
interruptible(move || {
Ok({
crate::memo::partitions_cached(n)
.iter()
.map(|p| p.parts().to_vec().into())
.collect()
})
})
}
#[pyfunction]
fn character_table(n: u32) -> PyResult<Vec<Vec<i128>>> {
interruptible(move || Ok(crate::character::character_table(n)))
}
#[pyfunction]
fn kostka_table(n: u32) -> PyResult<Vec<Vec<u128>>> {
interruptible(move || Ok(crate::kostka::kostka_table(n)))
}
#[pyfunction]
fn omega(a: Terms) -> PyResult<Terms> {
interruptible(move || {
let s: Schur<BigInt> = build_wide(&terms_arg(&a)?);
Ok(dump(&s.omega()))
})
}
#[pyfunction]
fn hall_inner_product(a: Terms, b: Terms) -> PyResult<Coeff> {
interruptible(move || {
let (a, b) = (terms_arg(&a)?, terms_arg(&b)?);
Ok(escalate(
|| {
let (x, y): (Schur<Guarded>, Schur<Guarded>) = (build(&a)?, build(&b)?);
Some(guarded(|| ops::hall::<Guarded, _, _>(&x, &y))?.to_coeff())
},
|| {
let (x, y): (Schur<BigInt>, Schur<BigInt>) = (build_wide(&a), build_wide(&b));
Coeff::Big(ops::hall::<BigInt, _, _>(&x, &y))
},
))
})
}
#[pyfunction]
fn skew_schur(la: Vec<u32>, mu: Vec<u32>) -> PyResult<Terms> {
interruptible(move || {
let s: Schur<BigInt> = hopf::skew_schur(&part_arg(&la)?, &part_arg(&mu)?);
Ok(dump(&s))
})
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn coproduct(a: Terms) -> PyResult<Vec<((Key, Key), Coeff)>> {
interruptible(move || {
fn split<C: Boundary>(x: &Schur<C>) -> Vec<((Key, Key), Coeff)> {
hopf::coproduct(x)
.terms()
.iter()
.map(|((m, n), c)| {
(
(m.parts().to_vec().into(), n.parts().to_vec().into()),
c.to_coeff(),
)
})
.collect()
}
let a = terms_arg(&a)?;
Ok(escalate(
|| {
let s: Schur<Guarded> = build(&a)?;
guarded(|| split(&s))
},
|| {
let s: Schur<BigInt> = build_wide(&a);
split(&s)
},
))
})
}
#[pyfunction]
fn antipode(a: Terms) -> PyResult<Terms> {
interruptible(move || {
let s: Schur<BigInt> = build_wide(&terms_arg(&a)?);
Ok(dump(&hopf::antipode(&s)))
})
}
#[pyfunction]
fn hall_littlewood(la: Vec<u32>) -> PyResult<Vec<(Key, Vec<(u32, Coeff)>)>> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(escalate(
|| Some(hl_rows(&guarded(|| crate::hall_littlewood::<Guarded>(&l))?)),
|| hl_rows(&crate::hall_littlewood::<BigInt>(&l)),
))
})
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn hall_littlewood_table(n: u32) -> PyResult<Vec<(Key, Vec<(Key, Vec<(u32, Coeff)>)>)>> {
interruptible(move || {
Ok({
crate::hall_littlewood_table::<i128>(n)
.into_iter()
.map(|(lambda, hl)| (lambda.parts().to_vec().into(), hl_rows(&hl)))
.collect()
})
})
}
#[pyfunction]
fn kostka_foulkes(la: Vec<u32>, mu: Vec<u32>) -> PyResult<Vec<(u32, Coeff)>> {
interruptible(move || {
let (l, m) = (part_arg(&la)?, part_arg(&mu)?);
same_degree(&[("la", &l), ("mu", &m)])?;
Ok(t_poly(&crate::kostka_foulkes::<i128>(&l, &m)))
})
}
#[pyfunction]
fn kostka_foulkes_column(mu: Vec<u32>) -> PyResult<Vec<(Key, Vec<(u32, Coeff)>)>> {
interruptible(move || {
Ok(crate::kostka_foulkes_column::<i128>(&part_arg(&mu)?)
.into_iter()
.map(|(lambda, k)| (lambda.parts().to_vec().into(), t_poly(&k)))
.collect())
})
}
#[pyfunction]
fn hall_littlewood_p(la: Vec<u32>) -> PyResult<Vec<(Key, Vec<(u32, Coeff)>)>> {
interruptible(move || Ok(hl_rows(&crate::hall_littlewood_p::<i128>(&part_arg(&la)?))))
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn hall_littlewood_p_table(n: u32) -> PyResult<Vec<(Key, Vec<(Key, Vec<(u32, Coeff)>)>)>> {
interruptible(move || {
Ok({
crate::hall_littlewood_p_table::<i128>(n)
.into_iter()
.map(|(lambda, hl)| (lambda.parts().to_vec().into(), hl_rows(&hl)))
.collect()
})
})
}
type TSchur = Vec<(Key, Vec<(u32, Coeff)>)>;
type TParsed<'a> = Vec<(Partition, &'a [(u32, Coeff)])>;
fn t_terms_arg(rows: &TSchur) -> PyResult<TParsed<'_>> {
rows.iter()
.map(|(p, c)| Ok((part_arg(p)?, c.as_slice())))
.collect()
}
fn build_t<C: Boundary>(rows: &TParsed) -> Option<Schur<crate::QtPoly<C>>> {
let mut x = Schur::zero();
for (p, terms) in rows {
let mut c = crate::QtPoly::zero();
for (b, v) in *terms {
c.add_term(0, *b, C::from_coeff(v)?);
}
x.add_term(p.clone(), c);
}
Some(x)
}
fn build_t_wide<C: Wide>(rows: &TParsed) -> Schur<crate::QtPoly<C>> {
let mut x = Schur::zero();
for (p, terms) in rows {
let mut c = crate::QtPoly::zero();
for (b, v) in *terms {
c.add_term(0, *b, C::from_coeff_wide(v));
}
x.add_term(p.clone(), c);
}
x
}
fn t_map_rows<C: Ring + ToCoeff>(
m: &std::collections::BTreeMap<Partition, crate::QtPoly<C>>,
) -> TSchur {
m.iter()
.map(|(la, c)| (la.parts().to_vec().into(), t_poly(c)))
.collect()
}
#[pyfunction]
fn schur_to_hall_littlewood_p(f: TSchur) -> PyResult<TSchur> {
interruptible(move || {
let rows = t_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_t::<Guarded>(&rows)?;
guarded(|| t_map_rows(&crate::schur_to_hall_littlewood_p(&x)))
},
|| {
t_map_rows(&crate::schur_to_hall_littlewood_p(&build_t_wide::<BigInt>(
&rows,
)))
},
))
})
}
#[pyfunction]
fn schur_to_hall_littlewood_qp(f: TSchur) -> PyResult<TSchur> {
interruptible(move || {
let rows = t_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_t::<Guarded>(&rows)?;
guarded(|| t_map_rows(&crate::schur_to_hall_littlewood_qp(&x)))
},
|| {
t_map_rows(&crate::schur_to_hall_littlewood_qp(
&build_t_wide::<BigInt>(&rows),
))
},
))
})
}
#[pyfunction]
fn hall_littlewood_p_to_schur(f: TSchur) -> PyResult<TSchur> {
interruptible(move || {
let rows = t_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_t::<Guarded>(&rows)?;
guarded(|| t_map_rows(crate::hall_littlewood_p_to_schur(x.terms()).terms()))
},
|| {
let x = build_t_wide::<BigInt>(&rows);
t_map_rows(crate::hall_littlewood_p_to_schur(x.terms()).terms())
},
))
})
}
#[pyfunction]
fn hall_littlewood_qp_to_schur(f: TSchur) -> PyResult<TSchur> {
interruptible(move || {
let rows = t_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_t::<Guarded>(&rows)?;
guarded(|| t_map_rows(crate::hall_littlewood_qp_to_schur(x.terms()).terms()))
},
|| {
let x = build_t_wide::<BigInt>(&rows);
t_map_rows(crate::hall_littlewood_qp_to_schur(x.terms()).terms())
},
))
})
}
#[pyfunction]
fn kostka_foulkes_table(n: u32) -> PyResult<Vec<Vec<Vec<(u32, Coeff)>>>> {
interruptible(move || {
Ok({
crate::kostka_foulkes_table::<i128>(n)
.into_iter()
.map(|row| row.iter().map(t_poly).collect())
.collect()
})
})
}
fn hl_rows<C: Ring + ToCoeff>(hl: &Schur<crate::QtPoly<C>>) -> Vec<(Key, Vec<(u32, Coeff)>)> {
hl.terms()
.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), t_poly(c)))
.collect()
}
fn t_poly<C: Ring + ToCoeff>(p: &crate::QtPoly<C>) -> Vec<(u32, Coeff)> {
p.terms()
.map(|((a, b), v)| {
debug_assert_eq!(*a, 0, "Hall-Littlewood must not involve q");
(*b, v.to_coeff())
})
.collect()
}
type MacTerms = Vec<(Key, Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>)>;
fn mac_terms<C: Ring + ToCoeff>(f: &Monomial<crate::Frac<C>>) -> MacTerms {
f.terms()
.iter()
.map(|(mu, c)| {
let (num, den) = c.parts();
(
mu.parts().to_vec().into(),
num.terms()
.map(|((a, b), v)| (*a, *b, v.to_coeff()))
.collect(),
den.map(|(&(a, b), &m)| (a, b, m)).collect(),
)
})
.collect()
}
#[pyfunction]
fn macdonald_p(la: Vec<u32>) -> PyResult<MacTerms> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(escalate(
|| Some(mac_terms(&guarded(|| crate::macdonald_p::<Guarded>(&l))?)),
|| mac_terms(&crate::macdonald_p::<BigInt>(&l)),
))
})
}
#[pyfunction]
fn macdonald_q(la: Vec<u32>) -> PyResult<MacTerms> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(escalate(
|| Some(mac_terms(&guarded(|| crate::macdonald_q::<Guarded>(&l))?)),
|| mac_terms(&crate::macdonald_q::<BigInt>(&l)),
))
})
}
#[pyfunction]
fn macdonald_j(la: Vec<u32>) -> PyResult<MacTerms> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(escalate(
|| Some(mac_terms(&guarded(|| crate::macdonald_j::<Guarded>(&l))?)),
|| mac_terms(&crate::macdonald_j::<BigInt>(&l)),
))
})
}
#[allow(clippy::type_complexity)]
fn mac_cell<C: BoundaryRat>(
c: &crate::Frac<C>,
what: &str,
) -> PyResult<(Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>)> {
let (num, den) = c.parts();
let mut terms = Vec::with_capacity(num.len());
for (&(a, b), v) in num.terms() {
let (numer, denom) = v.split();
if !denom.to_big().is_one() {
let (x, y) = (numer.to_big(), denom.to_big());
return Err(PyValueError::new_err(format!(
"non-integral {what} coefficient {x}/{y}"
)));
}
terms.push((a, b, numer));
}
Ok((terms, den.map(|(&(a, b), &m)| (a, b, m)).collect()))
}
#[pyfunction]
fn schur_in_macdonald_j(n: u32) -> PyResult<Vec<(Key, MacTerms)>> {
interruptible(move || {
fn rows<C: BoundaryRat>(
table: &[Vec<crate::Frac<C>>],
n: u32,
) -> PyResult<Vec<(Key, MacTerms)>> {
let parts = crate::partitions_of(n);
let mut out = Vec::with_capacity(parts.len());
for (i, lambda) in parts.iter().enumerate() {
let mut row = MacTerms::new();
for (j, mu) in parts.iter().enumerate() {
if table[i][j].is_zero() {
continue;
}
let (terms, den) = mac_cell(&table[i][j], &format!("s_{lambda} in J_{mu}"))?;
row.push((mu.parts().to_vec().into(), terms, den));
}
out.push((lambda.parts().to_vec().into(), row));
}
Ok(out)
}
escalate(
|| {
guarded(|| crate::schur_in_j_table::<GuardedRat>(n))
.map(|table| rows::<GuardedRat>(&table, n))
},
|| rows::<BigRational>(&crate::schur_in_j_table::<BigRational>(n), n),
)
})
}
type QtParsed<'a> = Vec<(Partition, &'a [(u32, u32, Coeff)])>;
fn qt_terms_arg(rows: &QtSchur) -> PyResult<QtParsed<'_>> {
rows.iter()
.map(|(p, c)| Ok((part_arg(p)?, c.as_slice())))
.collect()
}
fn build_qt<C: BoundaryRat>(rows: &QtParsed) -> Option<Schur<crate::QtPoly<C>>> {
let mut x = Schur::zero();
for (p, terms) in rows {
let mut c = crate::QtPoly::zero();
for (a, b, v) in *terms {
c.add_term(*a, *b, C::from_coeff(v)?);
}
x.add_term(p.clone(), c);
}
Some(x)
}
fn build_qt_wide<C: WideRat>(rows: &QtParsed) -> Schur<crate::QtPoly<C>> {
let mut x = Schur::zero();
for (p, terms) in rows {
let mut c = crate::QtPoly::zero();
for (a, b, v) in *terms {
c.add_term(*a, *b, C::from_coeff_wide(v));
}
x.add_term(p.clone(), c);
}
x
}
type QtMap<C> = std::collections::BTreeMap<Partition, crate::QtPoly<C>>;
fn build_qt_map<C: Boundary>(rows: &QtParsed) -> Option<QtMap<C>> {
let mut x = QtMap::new();
for (p, terms) in rows {
let mut c = crate::QtPoly::zero();
for (a, b, v) in *terms {
c.add_term(*a, *b, C::from_coeff(v)?);
}
if !c.is_zero() {
x.insert(p.clone(), c);
}
}
Some(x)
}
fn build_qt_map_wide<C: Wide>(rows: &QtParsed) -> QtMap<C> {
let mut x = QtMap::new();
for (p, terms) in rows {
let mut c = crate::QtPoly::zero();
for (a, b, v) in *terms {
c.add_term(*a, *b, C::from_coeff_wide(v));
}
if !c.is_zero() {
x.insert(p.clone(), c);
}
}
x
}
fn qt_map_rows<C: Ring + ToCoeff>(m: &QtMap<C>) -> QtSchur {
m.iter()
.map(|(la, c)| (la.parts().to_vec().into(), qt_poly(c)))
.collect()
}
fn convert_pair(src: &str, dst: &str) -> PyResult<(Basis, Basis)> {
let source = Basis::parse(src)?;
let target = Basis::parse(dst).map_err(|_| bad_dst_basis(dst))?;
if target == Basis::PowerSum {
return Err(bad_dst_basis("powersum"));
}
Ok((source, target))
}
fn routed_ring<C: Ring>(
m: &std::collections::BTreeMap<Partition, C>,
src: Basis,
dst: Basis,
) -> std::collections::BTreeMap<Partition, C> {
crate::convert_named(m, src.code(), dst.code())
.expect("a parsed Basis names a route convert_named resolves")
}
fn routed_ring_to_power<C: crate::coeff::QAlgebra>(
m: &std::collections::BTreeMap<Partition, C>,
src: Basis,
) -> std::collections::BTreeMap<Partition, C> {
crate::convert_named_to_power(m, src.code())
.expect("a parsed Basis names a route convert_named_to_power resolves")
}
#[pyfunction]
fn schur_multiply_qt(a: QtSchur, b: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
let (a, b) = (qt_terms_arg(&a)?, qt_terms_arg(&b)?);
Ok(escalate(
|| {
let x: Schur<crate::QtPoly<Guarded>> =
Schur::from_terms(build_qt_map::<Guarded>(&a)?);
let y = Schur::from_terms(build_qt_map::<Guarded>(&b)?);
Some(qt_map_rows(guarded(|| x.mul(&y))?.terms()))
},
|| {
let x: Schur<crate::QtPoly<BigInt>> =
Schur::from_terms(build_qt_map_wide::<BigInt>(&a));
let y = Schur::from_terms(build_qt_map_wide::<BigInt>(&b));
qt_map_rows(x.mul(&y).terms())
},
))
})
}
#[pyfunction]
fn schur_multiply_macdonald(a: MacElement, b: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
let (a, b) = (mac_terms_arg(&a)?, mac_terms_arg(&b)?);
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
let y = build_mac::<Guarded>(&b)?;
let out = guarded(|| schur_of(x.terms()).mul(&schur_of(y.terms())))?;
Some(mac_out(out.terms()))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
let y = build_mac_wide::<BigInt>(&b);
mac_out(schur_of(x.terms()).mul(&schur_of(y.terms())).terms())
},
))
})
}
#[pyfunction]
fn schur_multiply_jack(a: JackElement, b: JackElement) -> PyResult<JackTerms> {
interruptible(move || {
let (a, b) = (jack_terms_arg(&a)?, jack_terms_arg(&b)?);
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
let y = build_jack::<Guarded>(&b)?;
let out = guarded(|| schur_of(x.terms()).mul(&schur_of(y.terms())))?;
Some(jack_out(out.terms()))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
let y = build_jack_wide::<BigInt>(&b);
jack_out(schur_of(x.terms()).mul(&schur_of(y.terms())).terms())
},
))
})
}
#[pyfunction]
fn schur_multiply_ht(a: HtElement, b: HtElement) -> PyResult<HtTerms> {
interruptible(move || {
let (x, y) = (ht_terms_arg(&a)?, ht_terms_arg(&b)?);
ht_out(schur_of(&x).mul(&schur_of(&y)).terms())
})
}
fn schur_of<C: Ring>(m: &std::collections::BTreeMap<Partition, C>) -> Schur<C> {
Schur::from_terms(m.clone())
}
#[pyfunction]
fn omega_qt_terms(a: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
Ok(qt_terms_arg(&a)?
.into_iter()
.map(|(la, c)| (la.conjugate().parts().to_vec().into(), c.to_vec()))
.collect())
})
}
#[pyfunction]
fn antipode_qt_terms(a: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
Ok(qt_terms_arg(&a)?
.into_iter()
.map(|(la, c)| {
let odd = la.size() % 2 == 1;
let row = c
.iter()
.map(|(x, y, v)| (*x, *y, if odd { v.negated() } else { v.clone() }))
.collect();
(la.conjugate().parts().to_vec().into(), row)
})
.collect())
})
}
fn hopf_of<C: Ring>(
m: &std::collections::BTreeMap<Partition, C>,
sign: bool,
) -> std::collections::BTreeMap<Partition, C> {
m.iter()
.map(|(la, c)| {
let v = if sign && la.size() % 2 == 1 {
c.neg()
} else {
c.clone()
};
(la.conjugate(), v)
})
.collect()
}
fn mac_hopf(a: MacElement, sign: bool) -> PyResult<MacTerms> {
interruptible(move || {
let rows = mac_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&rows)?;
Some(mac_out(&guarded(|| hopf_of(x.terms(), sign))?))
},
|| mac_out(&hopf_of(build_mac_wide::<BigInt>(&rows).terms(), sign)),
))
})
}
#[pyfunction]
fn omega_macdonald_terms(a: MacElement) -> PyResult<MacTerms> {
mac_hopf(a, false)
}
#[pyfunction]
fn antipode_macdonald_terms(a: MacElement) -> PyResult<MacTerms> {
mac_hopf(a, true)
}
fn jack_hopf(a: JackElement, sign: bool) -> PyResult<JackTerms> {
interruptible(move || {
let rows = jack_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&rows)?;
Some(jack_out(&guarded(|| hopf_of(x.terms(), sign))?))
},
|| jack_out(&hopf_of(build_jack_wide::<BigInt>(&rows).terms(), sign)),
))
})
}
#[pyfunction]
fn omega_jack_terms(a: JackElement) -> PyResult<JackTerms> {
jack_hopf(a, false)
}
#[pyfunction]
fn antipode_jack_terms(a: JackElement) -> PyResult<JackTerms> {
jack_hopf(a, true)
}
fn ht_hopf(a: HtElement, sign: bool) -> PyResult<HtTerms> {
interruptible(move || ht_out(&hopf_of(&ht_terms_arg(&a)?, sign)))
}
#[pyfunction]
fn omega_ht_terms(a: HtElement) -> PyResult<HtTerms> {
ht_hopf(a, false)
}
#[pyfunction]
fn antipode_ht_terms(a: HtElement) -> PyResult<HtTerms> {
ht_hopf(a, true)
}
fn skew_ring<C: Ring>(
f: &std::collections::BTreeMap<Partition, C>,
g: &std::collections::BTreeMap<Partition, C>,
b: Basis,
) -> std::collections::BTreeMap<Partition, C> {
let sf: Schur<C> = Schur::from_terms(f.clone());
let out = match b {
Basis::Schur => SkewBy::skew_by(&sf, &Schur::from_terms(g.clone())),
Basis::Homogeneous => SkewBy::skew_by(&sf, &Homogeneous::from_terms(g.clone())),
Basis::Elementary => SkewBy::skew_by(&sf, &Elementary::from_terms(g.clone())),
Basis::PowerSum => SkewBy::skew_by(&sf, &PowerSum::from_terms(g.clone())),
Basis::Monomial => SkewBy::skew_by(&sf, &Monomial::from_terms(g.clone())),
Basis::Forgotten => SkewBy::skew_by(&sf, &Forgotten::from_terms(g.clone())),
};
out.terms().clone()
}
#[pyfunction]
#[pyo3(signature = (f, g, basis = "s"))]
fn skew_by_qt(f: QtSchur, g: QtSchur, basis: &str) -> PyResult<QtSchur> {
interruptible(move || {
let b = Basis::parse(basis)?;
let (f, g) = (qt_terms_arg(&f)?, qt_terms_arg(&g)?);
Ok(escalate(
|| {
let x = build_qt_map::<Guarded>(&f)?;
let y = build_qt_map::<Guarded>(&g)?;
Some(qt_map_rows(&guarded(|| skew_ring(&x, &y, b))?))
},
|| {
let x = build_qt_map_wide::<BigInt>(&f);
let y = build_qt_map_wide::<BigInt>(&g);
qt_map_rows(&skew_ring(&x, &y, b))
},
))
})
}
#[pyfunction]
#[pyo3(signature = (f, g, basis = "s"))]
fn skew_by_macdonald(f: MacElement, g: MacElement, basis: &str) -> PyResult<MacTerms> {
interruptible(move || {
let b = Basis::parse(basis)?;
let (f, g) = (mac_terms_arg(&f)?, mac_terms_arg(&g)?);
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&f)?;
let y = build_mac::<Guarded>(&g)?;
Some(mac_out(&guarded(|| skew_ring(x.terms(), y.terms(), b))?))
},
|| {
let x = build_mac_wide::<BigInt>(&f);
let y = build_mac_wide::<BigInt>(&g);
mac_out(&skew_ring(x.terms(), y.terms(), b))
},
))
})
}
#[pyfunction]
#[pyo3(signature = (f, g, basis = "s"))]
fn skew_by_jack(f: JackElement, g: JackElement, basis: &str) -> PyResult<JackTerms> {
interruptible(move || {
let b = Basis::parse(basis)?;
let (f, g) = (jack_terms_arg(&f)?, jack_terms_arg(&g)?);
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&f)?;
let y = build_jack::<Guarded>(&g)?;
Some(jack_out(&guarded(|| skew_ring(x.terms(), y.terms(), b))?))
},
|| {
let x = build_jack_wide::<BigInt>(&f);
let y = build_jack_wide::<BigInt>(&g);
jack_out(&skew_ring(x.terms(), y.terms(), b))
},
))
})
}
#[pyfunction]
#[pyo3(signature = (f, g, basis = "s"))]
fn skew_by_ht(f: HtElement, g: HtElement, basis: &str) -> PyResult<HtTerms> {
interruptible(move || {
let b = Basis::parse(basis)?;
let (x, y) = (ht_terms_arg(&f)?, ht_terms_arg(&g)?);
ht_out(&skew_ring(&x, &y, b))
})
}
type MacCell = (Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>);
type HtCell = (Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32, u32)>);
fn mac_coeff<C: Ring + ToCoeff>(c: &crate::Frac<C>) -> MacCell {
let (num, den) = c.parts();
(
num.terms()
.map(|(&(a, b), v)| (a, b, v.to_coeff()))
.collect(),
den.map(|(&(a, b), &k)| (a, b, k)).collect(),
)
}
fn ht_coeff(c: &crate::Ratio<crate::Rational>, what: &str) -> PyResult<HtCell> {
let (num, den) = c.parts();
Ok((
qt_poly_int(num, what)?,
den.map(|(&atom, &k)| atom_row(atom, k)).collect(),
))
}
#[pyfunction]
fn hall_inner_product_qt(a: QtSchur, b: QtSchur) -> PyResult<Vec<(u32, u32, Coeff)>> {
interruptible(move || {
let (a, b) = (qt_terms_arg(&a)?, qt_terms_arg(&b)?);
Ok(escalate(
|| {
let x: Schur<crate::QtPoly<Guarded>> =
Schur::from_terms(build_qt_map::<Guarded>(&a)?);
let y = Schur::from_terms(build_qt_map::<Guarded>(&b)?);
Some(qt_poly(&guarded(|| {
ops::hall::<crate::QtPoly<Guarded>, _, _>(&x, &y)
})?))
},
|| {
let x: Schur<crate::QtPoly<BigInt>> =
Schur::from_terms(build_qt_map_wide::<BigInt>(&a));
let y = Schur::from_terms(build_qt_map_wide::<BigInt>(&b));
qt_poly(&ops::hall::<crate::QtPoly<BigInt>, _, _>(&x, &y))
},
))
})
}
#[pyfunction]
fn hall_inner_product_macdonald(a: MacElement, b: MacElement) -> PyResult<MacCell> {
interruptible(move || {
let (a, b) = (mac_terms_arg(&a)?, mac_terms_arg(&b)?);
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
let y = build_mac::<Guarded>(&b)?;
let v = guarded(|| {
ops::hall::<crate::Frac<Guarded>, _, _>(
&schur_of(x.terms()),
&schur_of(y.terms()),
)
})?;
Some(mac_coeff(&v))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
let y = build_mac_wide::<BigInt>(&b);
mac_coeff(&ops::hall::<crate::Frac<BigInt>, _, _>(
&schur_of(x.terms()),
&schur_of(y.terms()),
))
},
))
})
}
#[pyfunction]
fn hall_inner_product_jack(a: JackElement, b: JackElement) -> PyResult<JackCell> {
interruptible(move || {
let (a, b) = (jack_terms_arg(&a)?, jack_terms_arg(&b)?);
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
let y = build_jack::<Guarded>(&b)?;
let v = guarded(|| {
ops::hall::<crate::AFrac<Guarded>, _, _>(
&schur_of(x.terms()),
&schur_of(y.terms()),
)
})?;
Some(jack_cell(&v))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
let y = build_jack_wide::<BigInt>(&b);
jack_cell(&ops::hall::<crate::AFrac<BigInt>, _, _>(
&schur_of(x.terms()),
&schur_of(y.terms()),
))
},
))
})
}
#[pyfunction]
fn hall_inner_product_ht(a: HtElement, b: HtElement) -> PyResult<HtCell> {
interruptible(move || {
let (x, y) = (ht_terms_arg(&a)?, ht_terms_arg(&b)?);
let v = ops::hall::<crate::Ratio<crate::Rational>, _, _>(&schur_of(&x), &schur_of(&y));
ht_coeff(&v, "the Hall inner product")
})
}
#[pyfunction]
fn scalar_t(a: MacElement, b: MacElement) -> PyResult<MacCell> {
interruptible(move || {
let (a, b) = (mac_terms_arg(&a)?, mac_terms_arg(&b)?);
escalate(
|| {
let x = build_mac_rat::<GuardedRat>(&a)?;
let y = build_mac_rat::<GuardedRat>(&b)?;
let v = guarded(|| crate::scalar_t(&schur_of(x.terms()), &schur_of(y.terms())))?;
Some(mac_coeff_integral(&v, "scalar_t"))
},
|| {
let x = build_mac_rat::<BigRational>(&a)
.expect("BigRational accepts every coefficient");
let y = build_mac_rat::<BigRational>(&b)
.expect("BigRational accepts every coefficient");
mac_coeff_integral(
&crate::scalar_t(&schur_of(x.terms()), &schur_of(y.terms())),
"scalar_t",
)
},
)
})
}
#[pyfunction]
fn scalar_qt(a: MacElement, b: MacElement) -> PyResult<MacCell> {
interruptible(move || {
let (a, b) = (mac_terms_arg(&a)?, mac_terms_arg(&b)?);
escalate(
|| {
let x = build_mac_rat::<GuardedRat>(&a)?;
let y = build_mac_rat::<GuardedRat>(&b)?;
let v = guarded(|| crate::scalar_qt(&schur_of(x.terms()), &schur_of(y.terms())))?;
Some(mac_coeff_integral(&v, "scalar_qt"))
},
|| {
let x = build_mac_rat::<BigRational>(&a)
.expect("BigRational accepts every coefficient");
let y = build_mac_rat::<BigRational>(&b)
.expect("BigRational accepts every coefficient");
mac_coeff_integral(
&crate::scalar_qt(&schur_of(x.terms()), &schur_of(y.terms())),
"scalar_qt",
)
},
)
})
}
#[pyfunction]
fn scalar_qt_ht(a: HtElement, b: HtElement) -> PyResult<HtCell> {
interruptible(move || {
let (x, y) = (ht_terms_arg(&a)?, ht_terms_arg(&b)?);
let v = crate::scalar_qt_ratio(&schur_of(&x), &schur_of(&y));
ht_coeff(&v, "scalar_qt")
})
}
fn coproduct_ring<C: Ring>(m: &std::collections::BTreeMap<Partition, C>) -> Vec<((Key, Key), C)> {
hopf::coproduct(&Schur::from_terms(m.clone()))
.terms()
.iter()
.map(|((mu, nu), c)| {
(
(mu.parts().to_vec().into(), nu.parts().to_vec().into()),
c.clone(),
)
})
.collect()
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn coproduct_qt(a: QtSchur) -> PyResult<Vec<((Key, Key), Vec<(u32, u32, Coeff)>)>> {
interruptible(move || {
let a = qt_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_qt_map::<Guarded>(&a)?;
let out = guarded(|| coproduct_ring(&x))?;
Some(out.iter().map(|(k, c)| (k.clone(), qt_poly(c))).collect())
},
|| {
let x = build_qt_map_wide::<BigInt>(&a);
coproduct_ring(&x)
.iter()
.map(|(k, c)| (k.clone(), qt_poly(c)))
.collect()
},
))
})
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn coproduct_macdonald(
a: MacElement,
) -> PyResult<Vec<((Key, Key), Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>)>> {
interruptible(move || {
let a = mac_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
let out = guarded(|| coproduct_ring(x.terms()))?;
Some(mac_pairs(&out))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
mac_pairs(&coproduct_ring(x.terms()))
},
))
})
}
#[allow(clippy::type_complexity)]
fn mac_pairs<C: Ring + ToCoeff>(
rows: &[((Key, Key), crate::Frac<C>)],
) -> Vec<((Key, Key), Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>)> {
rows.iter()
.map(|(k, c)| {
let (num, den) = mac_coeff(c);
(k.clone(), num, den)
})
.collect()
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn coproduct_jack(
a: JackElement,
) -> PyResult<
Vec<(
(Key, Key),
Vec<Coeff>,
Vec<(u32, u32, u32)>,
u128,
Vec<Coeff>,
)>,
> {
interruptible(move || {
let a = jack_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
let out = guarded(|| coproduct_ring(x.terms()))?;
Some(jack_pairs(&out))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
jack_pairs(&coproduct_ring(x.terms()))
},
))
})
}
#[allow(clippy::type_complexity)]
fn jack_pairs<C: Boundary>(
rows: &[((Key, Key), crate::AFrac<C>)],
) -> Vec<(
(Key, Key),
Vec<Coeff>,
Vec<(u32, u32, u32)>,
u128,
Vec<Coeff>,
)> {
rows.iter()
.map(|(k, c)| {
let (n, d, s, t) = jack_cell(c);
(k.clone(), n, d, s, t)
})
.collect()
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn coproduct_ht(
a: HtElement,
) -> PyResult<
Vec<(
(Key, Key),
Vec<(u32, u32, Coeff)>,
Vec<(u32, u32, u32, u32)>,
)>,
> {
interruptible(move || {
let x = ht_terms_arg(&a)?;
coproduct_ring(&x)
.iter()
.map(|(k, c)| {
let (num, den) = ht_coeff(c, "a coproduct coefficient")?;
Ok((k.clone(), num, den))
})
.collect()
})
}
fn expand_ring<C: Ring>(m: &std::collections::BTreeMap<Partition, C>, n: usize) -> Vec<(Key, C)> {
Monomial::from_terms(m.clone())
.expand(n)
.into_iter()
.map(|(alpha, c)| (alpha.into(), c))
.collect()
}
fn evaluate_ring<C: Ring>(m: &std::collections::BTreeMap<Partition, C>, xs: &[i64]) -> C {
let alphabet: Vec<C> = xs.iter().map(|&x| C::from_i64(x)).collect();
Schur::from_terms(m.clone()).eval(&alphabet)
}
#[pyfunction]
fn expand_qt(a: QtSchur, n: usize) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>)>> {
interruptible(move || {
let a = qt_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_qt_map::<Guarded>(&a)?;
Some(
expand_ring(&x, n)
.iter()
.map(|(k, c)| (k.clone(), qt_poly(c)))
.collect(),
)
},
|| {
let x = build_qt_map_wide::<BigInt>(&a);
expand_ring(&x, n)
.iter()
.map(|(k, c)| (k.clone(), qt_poly(c)))
.collect()
},
))
})
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn expand_macdonald(
a: MacElement,
n: usize,
) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>)>> {
interruptible(move || {
let a = mac_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
Some(mac_indexed(&expand_ring(x.terms(), n)))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
mac_indexed(&expand_ring(x.terms(), n))
},
))
})
}
#[allow(clippy::type_complexity)]
fn mac_indexed<C: Ring + ToCoeff>(
rows: &[(Key, crate::Frac<C>)],
) -> Vec<(Key, Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>)> {
rows.iter()
.map(|(k, c)| {
let (num, den) = mac_coeff(c);
(k.clone(), num, den)
})
.collect()
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn expand_jack(
a: JackElement,
n: usize,
) -> PyResult<Vec<(Key, Vec<Coeff>, Vec<(u32, u32, u32)>, u128, Vec<Coeff>)>> {
interruptible(move || {
let a = jack_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
Some(jack_indexed(&expand_ring(x.terms(), n)))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
jack_indexed(&expand_ring(x.terms(), n))
},
))
})
}
#[allow(clippy::type_complexity)]
fn jack_indexed<C: Boundary>(
rows: &[(Key, crate::AFrac<C>)],
) -> Vec<(Key, Vec<Coeff>, Vec<(u32, u32, u32)>, u128, Vec<Coeff>)> {
rows.iter()
.map(|(k, c)| {
let (num, den, scale, tail) = jack_cell(c);
(k.clone(), num, den, scale, tail)
})
.collect()
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn expand_ht(
a: HtElement,
n: usize,
) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32, u32)>)>> {
interruptible(move || {
let x = ht_terms_arg(&a)?;
expand_ring(&x, n)
.iter()
.map(|(k, c)| {
let (num, den) = ht_coeff(c, "an expansion coefficient")?;
Ok((k.clone(), num, den))
})
.collect()
})
}
#[pyfunction]
fn evaluate_qt(a: QtSchur, xs: Vec<i64>) -> PyResult<Vec<(u32, u32, Coeff)>> {
interruptible(move || {
let a = qt_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_qt_map::<Guarded>(&a)?;
Some(qt_poly(&guarded(|| evaluate_ring(&x, &xs))?))
},
|| {
let x = build_qt_map_wide::<BigInt>(&a);
qt_poly(&evaluate_ring(&x, &xs))
},
))
})
}
#[pyfunction]
fn evaluate_macdonald(a: MacElement, xs: Vec<i64>) -> PyResult<MacCell> {
interruptible(move || {
let a = mac_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
Some(mac_coeff(&guarded(|| evaluate_ring(x.terms(), &xs))?))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
mac_coeff(&evaluate_ring(x.terms(), &xs))
},
))
})
}
#[pyfunction]
fn evaluate_jack(a: JackElement, xs: Vec<i64>) -> PyResult<JackCell> {
interruptible(move || {
let a = jack_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
Some(jack_cell(&guarded(|| evaluate_ring(x.terms(), &xs))?))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
jack_cell(&evaluate_ring(x.terms(), &xs))
},
))
})
}
#[pyfunction]
fn evaluate_ht(a: HtElement, xs: Vec<i64>) -> PyResult<HtCell> {
interruptible(move || {
let x = ht_terms_arg(&a)?;
ht_coeff(&evaluate_ring(&x, &xs), "the value at the alphabet")
})
}
fn shape_weights(
parts: impl Iterator<Item = Partition>,
w: impl Fn(&Partition) -> Option<u128>,
what: &str,
) -> PyResult<std::collections::BTreeMap<Partition, u128>> {
let mut out = std::collections::BTreeMap::new();
for la in parts {
match w(&la) {
Some(v) => {
out.insert(la, v);
}
None => {
return Err(PyOverflowError::new_err(format!(
"{what} at {la} exceeds the fixed-width computation"
)))
}
}
}
Ok(out)
}
fn combine_ring<C: Ring>(
m: &std::collections::BTreeMap<Partition, C>,
w: &std::collections::BTreeMap<Partition, u128>,
) -> C {
let mut total = C::zero();
for (la, c) in m {
total.add_assign(&C::from_u128(w[la]).mul(c));
}
total
}
#[pyfunction]
fn dimension_qt(a: QtSchur) -> PyResult<Vec<(u32, u32, Coeff)>> {
interruptible(move || {
let a = qt_terms_arg(&a)?;
let w = shape_weights(
a.iter().map(|r| r.0.clone()),
crate::eval::dimension,
"the dimension",
)?;
Ok(escalate(
|| {
let x = build_qt_map::<Guarded>(&a)?;
Some(qt_poly(&guarded(|| combine_ring(&x, &w))?))
},
|| {
let x = build_qt_map_wide::<BigInt>(&a);
qt_poly(&combine_ring(&x, &w))
},
))
})
}
#[pyfunction]
fn dimension_macdonald(a: MacElement) -> PyResult<MacCell> {
interruptible(move || {
let a = mac_terms_arg(&a)?;
let w = shape_weights(
a.iter().map(|r| r.0.clone()),
crate::eval::dimension,
"the dimension",
)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
Some(mac_coeff(&guarded(|| combine_ring(x.terms(), &w))?))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
mac_coeff(&combine_ring(x.terms(), &w))
},
))
})
}
#[pyfunction]
fn dimension_jack(a: JackElement) -> PyResult<JackCell> {
interruptible(move || {
let a = jack_terms_arg(&a)?;
let w = shape_weights(
a.iter().map(|r| r.0.clone()),
crate::eval::dimension,
"the dimension",
)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
Some(jack_cell(&guarded(|| combine_ring(x.terms(), &w))?))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
jack_cell(&combine_ring(x.terms(), &w))
},
))
})
}
#[pyfunction]
fn principal_specialization_qt(a: QtSchur, n: u32) -> PyResult<Vec<(u32, u32, Coeff)>> {
interruptible(move || {
let a = qt_terms_arg(&a)?;
let w = shape_weights(
a.iter().map(|r| r.0.clone()),
|la| crate::eval::principal_specialization(la, n),
"the value at 1^n",
)?;
Ok(escalate(
|| {
let x = build_qt_map::<Guarded>(&a)?;
Some(qt_poly(&guarded(|| combine_ring(&x, &w))?))
},
|| {
let x = build_qt_map_wide::<BigInt>(&a);
qt_poly(&combine_ring(&x, &w))
},
))
})
}
#[pyfunction]
fn principal_specialization_macdonald(a: MacElement, n: u32) -> PyResult<MacCell> {
interruptible(move || {
let a = mac_terms_arg(&a)?;
let w = shape_weights(
a.iter().map(|r| r.0.clone()),
|la| crate::eval::principal_specialization(la, n),
"the value at 1^n",
)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
Some(mac_coeff(&guarded(|| combine_ring(x.terms(), &w))?))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
mac_coeff(&combine_ring(x.terms(), &w))
},
))
})
}
#[pyfunction]
fn principal_specialization_jack(a: JackElement, n: u32) -> PyResult<JackCell> {
interruptible(move || {
let a = jack_terms_arg(&a)?;
let w = shape_weights(
a.iter().map(|r| r.0.clone()),
|la| crate::eval::principal_specialization(la, n),
"the value at 1^n",
)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
Some(jack_cell(&guarded(|| combine_ring(x.terms(), &w))?))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
jack_cell(&combine_ring(x.terms(), &w))
},
))
})
}
#[pyfunction]
fn dimension_ht(a: HtElement) -> PyResult<HtCell> {
interruptible(move || {
let x = ht_terms_arg(&a)?;
let w = shape_weights(x.keys().cloned(), crate::eval::dimension, "the dimension")?;
ht_coeff(&combine_ring(&x, &w), "the dimension")
})
}
#[pyfunction]
fn principal_specialization_ht(a: HtElement, n: u32) -> PyResult<HtCell> {
interruptible(move || {
let x = ht_terms_arg(&a)?;
let w = shape_weights(
x.keys().cloned(),
|la| crate::eval::principal_specialization(la, n),
"the value at 1^n",
)?;
ht_coeff(&combine_ring(&x, &w), "the value at 1^n")
})
}
#[pyfunction]
fn principal_specialization_q_qt(a: QtSchur, n: u32) -> PyResult<Vec<(u32, u32, Coeff)>> {
interruptible(move || {
let a = qt_terms_arg(&a)?;
for (la, row) in &a {
if let Some((x, y, _)) = row.iter().find(|(x, _, _)| *x != 0) {
return Err(PyValueError::new_err(format!(
"the coefficient of q^{x}t^{y} at {la} already carries q, \
which this specialization introduces; pass a t-only \
element, or evaluate at an alphabet you name yourself"
)));
}
}
escalate(
|| {
let x = build_qt_map::<Guarded>(&a)?;
let p = guarded(|| ps_q_ring(&x, n))?;
Some(p.map(|p| qt_poly(&p)))
},
|| {
let x = build_qt_map_wide::<BigInt>(&a);
ps_q_ring(&x, n).map(|p| qt_poly(&p))
},
)
})
}
fn ps_q_ring<C: Ring>(
m: &std::collections::BTreeMap<Partition, crate::QtPoly<C>>,
n: u32,
) -> PyResult<crate::QtPoly<C>> {
let mut total = crate::QtPoly::zero();
for (la, c) in m {
let q =
crate::eval::principal_specialization_q(la, n).ok_or_else(|| q_analog_wall(la, n))?;
for (k, v) in q.iter().enumerate() {
if *v == 0 {
continue;
}
let mut mono = crate::QtPoly::zero();
let count = u128::try_from(*v)
.expect("a q-analog coefficient counts tableaux and is non-negative");
mono.add_term(k as u32, 0, C::from_u128(count));
total.add_assign(&c.mul(&mono));
}
}
Ok(total)
}
fn ps_at_ring<C: Ring>(m: &std::collections::BTreeMap<Partition, C>, n: u32, z: &C) -> PyResult<C> {
let mut powers = vec![C::one()];
let mut total = C::zero();
for (la, c) in m {
let q =
crate::eval::principal_specialization_q(la, n).ok_or_else(|| q_analog_wall(la, n))?;
while powers.len() < q.len() {
let next = powers[powers.len() - 1].mul(z);
powers.push(next);
}
let mut value = C::zero();
for (k, v) in q.iter().enumerate() {
if *v == 0 {
continue;
}
let count = u128::try_from(*v)
.expect("a q-analog coefficient counts tableaux and is non-negative");
value.add_assign(&C::from_u128(count).mul(&powers[k]));
}
total.add_assign(&c.mul(&value));
}
Ok(total)
}
#[pyfunction]
fn principal_specialization_at_qt(
a: QtSchur,
n: u32,
z: Vec<(u32, u32, Coeff)>,
) -> PyResult<Vec<(u32, u32, Coeff)>> {
interruptible(move || {
let rows = qt_terms_arg(&a)?;
let cell: QtSchur = vec![(Vec::new().into(), z)];
let cell = qt_terms_arg(&cell)?;
escalate(
|| {
let x = build_qt_map::<Guarded>(&rows)?;
let z = one_coefficient(build_qt_map::<Guarded>(&cell)?.into_values());
let v = guarded(|| ps_at_ring(&x, n, &z))?;
Some(v.map(|v| qt_poly(&v)))
},
|| {
let x = build_qt_map_wide::<BigInt>(&rows);
let z = one_coefficient(build_qt_map_wide::<BigInt>(&cell).into_values());
ps_at_ring(&x, n, &z).map(|v| qt_poly(&v))
},
)
})
}
fn one_coefficient<C: Ring>(values: impl IntoIterator<Item = C>) -> C {
values.into_iter().next().unwrap_or_else(C::zero)
}
#[pyfunction]
fn principal_specialization_at_macdonald(
a: MacElement,
n: u32,
z: (Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>),
) -> PyResult<MacCell> {
interruptible(move || {
let rows = mac_terms_arg(&a)?;
let cell: MacElement = vec![(Vec::new().into(), z.0, z.1)];
let cell = mac_terms_arg(&cell)?;
escalate(
|| {
let x = build_mac::<Guarded>(&rows)?;
let z = one_coefficient(build_mac::<Guarded>(&cell)?.terms().values().cloned());
let v = guarded(|| ps_at_ring(x.terms(), n, &z))?;
Some(v.map(|v| mac_coeff(&v)))
},
|| {
let x = build_mac_wide::<BigInt>(&rows);
let z = one_coefficient(build_mac_wide::<BigInt>(&cell).terms().values().cloned());
ps_at_ring(x.terms(), n, &z).map(|v| mac_coeff(&v))
},
)
})
}
#[pyfunction]
fn principal_specialization_at_jack(
a: JackElement,
n: u32,
z: (Vec<Coeff>, Vec<(u32, u32, u32)>, u128, Vec<Coeff>),
) -> PyResult<JackCell> {
interruptible(move || {
let rows = jack_terms_arg(&a)?;
let cell: JackElement = vec![(Vec::new().into(), z.0, z.1, z.2, z.3)];
let cell = jack_terms_arg(&cell)?;
escalate(
|| {
let x = build_jack::<Guarded>(&rows)?;
let z = one_coefficient(build_jack::<Guarded>(&cell)?.terms().values().cloned());
let v = guarded(|| ps_at_ring(x.terms(), n, &z))?;
Some(v.map(|v| jack_cell(&v)))
},
|| {
let x = build_jack_wide::<BigInt>(&rows);
let z = one_coefficient(build_jack_wide::<BigInt>(&cell).terms().values().cloned());
ps_at_ring(x.terms(), n, &z).map(|v| jack_cell(&v))
},
)
})
}
#[pyfunction]
fn principal_specialization_at_ht(
a: HtElement,
n: u32,
z: (Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32, u32)>),
) -> PyResult<HtCell> {
interruptible(move || {
let x = ht_terms_arg(&a)?;
let cell: HtElement = vec![(Vec::new().into(), z.0, z.1)];
let z = one_coefficient(ht_terms_arg(&cell)?.into_values());
ht_coeff(&ps_at_ring(&x, n, &z)?, "the value at 1, z, ...")
})
}
#[pyfunction]
fn convert_qt_terms(a: QtSchur, src: &str, dst: &str) -> PyResult<QtSchur> {
interruptible(move || {
let (source, target) = convert_pair(src, dst)?;
let rows = qt_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_qt_map::<Guarded>(&rows)?;
guarded(|| qt_map_rows(&routed_ring(&x, source, target)))
},
|| {
qt_map_rows(&routed_ring(
&build_qt_map_wide::<BigInt>(&rows),
source,
target,
))
},
))
})
}
fn qt_poly_integral<C: BoundaryRat>(
p: &crate::QtPoly<C>,
what: &str,
) -> PyResult<Vec<(u32, u32, Coeff)>> {
p.terms()
.map(|(&(a, b), v)| {
let (num, den) = v.split();
if den.to_big().is_one() {
Ok((a, b, num))
} else {
let (n, d) = (num.to_big(), den.to_big());
Err(PyValueError::new_err(format!(
"non-integral {what} coefficient {n}/{d} at q^{a}t^{b}"
)))
}
})
.collect()
}
fn build_mac_rat<C: BoundaryRat>(rows: &MacParsed) -> Option<Monomial<crate::Frac<C>>> {
let mut x = Monomial::zero();
for (p, num, den) in rows {
let mut poly = crate::QtPoly::zero();
for (a, b, v) in *num {
poly.add_term(*a, *b, C::from_coeff(v)?);
}
x.add_term(p.clone(), crate::Frac::from_poly(poly).mul_factors(den));
}
Some(x)
}
fn mac_coeff_integral<C: BoundaryRat>(c: &crate::Frac<C>, what: &str) -> PyResult<MacCell> {
let (num, den) = c.parts();
Ok((
qt_poly_integral(num, what)?,
den.map(|(&(a, b), &k)| (a, b, k)).collect(),
))
}
#[pyfunction]
fn internal_product_qt(a: QtSchur, b: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
let (a, b) = (qt_terms_arg(&a)?, qt_terms_arg(&b)?);
escalate(
|| {
let x: Schur<crate::QtPoly<GuardedRat>> = build_qt(&a)?;
let y: Schur<crate::QtPoly<GuardedRat>> = build_qt(&b)?;
let r = guarded(|| ops::internal(&x, &y))?;
Some(qt_schur_integral(&r, "Kronecker"))
},
|| {
let x: Schur<crate::QtPoly<BigRational>> = build_qt_wide(&a);
let y: Schur<crate::QtPoly<BigRational>> = build_qt_wide(&b);
qt_schur_integral(&ops::internal(&x, &y), "Kronecker")
},
)
})
}
fn qt_schur_integral<C: BoundaryRat>(x: &Schur<crate::QtPoly<C>>, what: &str) -> PyResult<QtSchur> {
x.terms()
.iter()
.map(|(la, c)| Ok((la.parts().to_vec().into(), qt_poly_integral(c, what)?)))
.collect()
}
#[pyfunction]
fn internal_product_macdonald(a: MacElement, b: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
let (a, b) = (mac_terms_arg(&a)?, mac_terms_arg(&b)?);
escalate(
|| {
let x = build_mac_rat::<GuardedRat>(&a)?;
let y = build_mac_rat::<GuardedRat>(&b)?;
let r = guarded(|| ops::internal(&schur_of(x.terms()), &schur_of(y.terms())))?;
Some(mac_terms_integral(&r, "Kronecker"))
},
|| {
let x = build_mac_rat::<BigRational>(&a)
.expect("BigRational accepts every coefficient");
let y = build_mac_rat::<BigRational>(&b)
.expect("BigRational accepts every coefficient");
mac_terms_integral(
&ops::internal(&schur_of(x.terms()), &schur_of(y.terms())),
"Kronecker",
)
},
)
})
}
fn mac_terms_integral<C: BoundaryRat>(x: &Schur<crate::Frac<C>>, what: &str) -> PyResult<MacTerms> {
x.terms()
.iter()
.map(|(la, c)| {
let (num, den) = mac_coeff_integral(c, what)?;
Ok((la.parts().to_vec().into(), num, den))
})
.collect()
}
#[pyfunction]
fn internal_product_jack(a: JackElement, b: JackElement) -> PyResult<JackTerms> {
interruptible(move || {
let (a, b) = (jack_terms_arg(&a)?, jack_terms_arg(&b)?);
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
let y = build_jack::<Guarded>(&b)?;
let r = guarded(|| ops::internal(&schur_of(x.terms()), &schur_of(y.terms())))?;
Some(jack_out(r.terms()))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
let y = build_jack_wide::<BigInt>(&b);
jack_out(ops::internal(&schur_of(x.terms()), &schur_of(y.terms())).terms())
},
))
})
}
#[pyfunction]
fn internal_product_ht(a: HtElement, b: HtElement) -> PyResult<HtTerms> {
interruptible(move || {
let (x, y) = (ht_terms_arg(&a)?, ht_terms_arg(&b)?);
ht_out(ops::internal(&schur_of(&x), &schur_of(&y)).terms())
})
}
#[pyfunction]
fn plethysm_qt(f: QtSchur, g: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
let (f, g) = (qt_terms_arg(&f)?, qt_terms_arg(&g)?);
escalate(
|| {
let x: Schur<crate::QtPoly<GuardedRat>> = build_qt(&f)?;
let y: Schur<crate::QtPoly<GuardedRat>> = build_qt(&g)?;
let r = guarded(|| crate::plethysm::plethysm(&x, &y))?;
Some(qt_schur_integral(&r, "plethysm"))
},
|| {
let x: Schur<crate::QtPoly<BigRational>> = build_qt_wide(&f);
let y: Schur<crate::QtPoly<BigRational>> = build_qt_wide(&g);
qt_schur_integral(&crate::plethysm::plethysm(&x, &y), "plethysm")
},
)
})
}
#[pyfunction]
fn plethysm_macdonald(f: MacElement, g: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
let (f, g) = (mac_terms_arg(&f)?, mac_terms_arg(&g)?);
escalate(
|| {
let x = build_mac_rat::<GuardedRat>(&f)?;
let y = build_mac_rat::<GuardedRat>(&g)?;
let r = guarded(|| {
crate::plethysm::plethysm(&schur_of(x.terms()), &schur_of(y.terms()))
})?;
Some(mac_terms_integral(&r, "plethysm"))
},
|| {
let x = build_mac_rat::<BigRational>(&f)
.expect("BigRational accepts every coefficient");
let y = build_mac_rat::<BigRational>(&g)
.expect("BigRational accepts every coefficient");
mac_terms_integral(
&crate::plethysm::plethysm(&schur_of(x.terms()), &schur_of(y.terms())),
"plethysm",
)
},
)
})
}
#[pyfunction]
fn plethysm_jack(f: JackElement, g: JackElement) -> PyResult<JackTerms> {
interruptible(move || {
let (f, g) = (jack_terms_arg(&f)?, jack_terms_arg(&g)?);
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&f)?;
let y = build_jack::<Guarded>(&g)?;
let r = guarded(|| {
crate::plethysm::plethysm(&schur_of(x.terms()), &schur_of(y.terms()))
})?;
Some(jack_out(r.terms()))
},
|| {
let x = build_jack_wide::<BigInt>(&f);
let y = build_jack_wide::<BigInt>(&g);
jack_out(
crate::plethysm::plethysm(&schur_of(x.terms()), &schur_of(y.terms())).terms(),
)
},
))
})
}
#[pyfunction]
fn plethysm_ht(f: HtElement, g: HtElement) -> PyResult<HtTerms> {
interruptible(move || {
let (x, y) = (ht_terms_arg(&f)?, ht_terms_arg(&g)?);
ht_out(crate::plethysm::plethysm(&schur_of(&x), &schur_of(&y)).terms())
})
}
#[pyfunction]
fn schur_to_macdonald_j(f: QtSchur) -> PyResult<MacTerms> {
interruptible(move || {
let rows = qt_terms_arg(&f)?;
fn out<C: BoundaryRat>(
m: &std::collections::BTreeMap<Partition, crate::Frac<C>>,
) -> PyResult<MacTerms> {
m.iter()
.map(|(mu, c)| {
let (terms, den) = mac_cell(c, &format!("the J_{mu} coefficient"))?;
Ok((mu.parts().to_vec().into(), terms, den))
})
.collect()
}
escalate(
|| {
let x = build_qt::<GuardedRat>(&rows)?;
guarded(|| crate::schur_to_macdonald_j(&x)).map(|m| out::<GuardedRat>(&m))
},
|| {
out::<BigRational>(&crate::schur_to_macdonald_j(&build_qt_wide::<BigRational>(
&rows,
)))
},
)
})
}
type MacElement = Vec<(Key, Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32)>)>;
type MacParsed<'a> = Vec<(Partition, &'a [(u32, u32, Coeff)], Factors)>;
type Factors = std::collections::BTreeMap<(u32, u32), i32>;
fn mac_terms_arg(rows: &MacElement) -> PyResult<MacParsed<'_>> {
rows.iter()
.map(|(p, num, den)| {
let mut factors = Factors::new();
for &(a, b, m) in den {
if a == 0 && b == 0 {
return Err(PyValueError::new_err(
"not a denominator factor: (0, 0) is 1 - q^0 t^0 = 0",
));
}
let m = i32::try_from(m).map_err(|_| {
PyValueError::new_err(format!("denominator multiplicity {m} is too large"))
})?;
*factors.entry((a, b)).or_insert(0) -= m;
}
Ok((part_arg(p)?, num.as_slice(), factors))
})
.collect()
}
fn build_mac<C: Boundary>(rows: &MacParsed) -> Option<Monomial<crate::Frac<C>>> {
let mut x = Monomial::zero();
for (p, num, den) in rows {
let mut poly = crate::QtPoly::zero();
for (a, b, v) in *num {
poly.add_term(*a, *b, C::from_coeff(v)?);
}
x.add_term(p.clone(), crate::Frac::from_poly(poly).mul_factors(den));
}
Some(x)
}
fn build_mac_wide<C: Wide>(rows: &MacParsed) -> Monomial<crate::Frac<C>> {
let mut x = Monomial::zero();
for (p, num, den) in rows {
let mut poly = crate::QtPoly::zero();
for (a, b, v) in *num {
poly.add_term(*a, *b, C::from_coeff_wide(v));
}
x.add_term(p.clone(), crate::Frac::from_poly(poly).mul_factors(den));
}
x
}
fn mac_out<C: Ring + ToCoeff>(
m: &std::collections::BTreeMap<Partition, crate::Frac<C>>,
) -> MacTerms {
m.iter()
.map(|(mu, c)| {
let (num, den) = c.parts();
(
mu.parts().to_vec().into(),
num.terms()
.map(|(&(a, b), v)| (a, b, v.to_coeff()))
.collect(),
den.map(|(&(a, b), &k)| (a, b, k)).collect(),
)
})
.collect()
}
#[pyfunction]
fn convert_macdonald_terms(a: MacElement, src: &str, dst: &str) -> PyResult<MacTerms> {
interruptible(move || {
let (source, target) = convert_pair(src, dst)?;
let rows = mac_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&rows)?;
let out = guarded(|| routed_ring(x.terms(), source, target))?;
Some(mac_out(&out))
},
|| {
let x = build_mac_wide::<BigInt>(&rows);
mac_out(&routed_ring(x.terms(), source, target))
},
))
})
}
#[pyfunction]
fn convert_jack_terms(a: JackElement, src: &str, dst: &str) -> PyResult<JackTerms> {
interruptible(move || {
let (source, target) = convert_pair(src, dst)?;
let rows = jack_terms_arg(&a)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&rows)?;
let out = guarded(|| routed_ring(x.terms(), source, target))?;
Some(jack_out(&out))
},
|| {
let x = build_jack_wide::<BigInt>(&rows);
jack_out(&routed_ring(x.terms(), source, target))
},
))
})
}
#[pyfunction]
fn convert_ht_terms(a: HtElement, src: &str, dst: &str) -> PyResult<HtTerms> {
interruptible(move || {
let (source, target) = convert_pair(src, dst)?;
let x = ht_terms_arg(&a)?;
ht_out(&routed_ring(&x, source, target))
})
}
fn qt_poly_rat<C: BoundaryRat>(p: &crate::QtPoly<C>) -> Vec<(u32, u32, Coeff, Coeff)> {
p.terms()
.map(|(&(a, b), v)| {
let (n, d) = v.split();
(a, b, n, d)
})
.collect()
}
type RatQtTerms = Vec<(Key, Vec<(u32, u32, Coeff, Coeff)>)>;
type RatMacTerms = Vec<(Key, Vec<(u32, u32, Coeff, Coeff)>, Vec<(u32, u32, u32)>)>;
type RatHtTerms = Vec<(
Key,
Vec<(u32, u32, Coeff, Coeff)>,
Vec<(u32, u32, u32, u32)>,
)>;
#[pyfunction]
fn to_power_qt(f: QtSchur, src: &str) -> PyResult<RatQtTerms> {
interruptible(move || {
let b = Basis::parse(src)?;
let rows = qt_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_qt::<GuardedRat>(&rows)?;
let out = guarded(|| routed_ring_to_power(x.terms(), b))?;
Some(
out.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), qt_poly_rat(c)))
.collect(),
)
},
|| {
let x = build_qt_wide::<BigRational>(&rows);
routed_ring_to_power(x.terms(), b)
.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), qt_poly_rat(c)))
.collect()
},
))
})
}
fn mac_rat_out<C: BoundaryRat>(
m: &std::collections::BTreeMap<Partition, crate::Frac<C>>,
) -> RatMacTerms {
m.iter()
.map(|(mu, c)| {
let (num, den) = c.parts();
(
mu.parts().to_vec().into(),
qt_poly_rat(num),
den.map(|(&(a, b), &k)| (a, b, k)).collect(),
)
})
.collect()
}
#[pyfunction]
fn to_power_macdonald(f: MacElement, src: &str) -> PyResult<RatMacTerms> {
interruptible(move || {
let b = Basis::parse(src)?;
let rows = mac_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_mac_rat::<GuardedRat>(&rows)?;
let mut out = guarded(|| routed_ring_to_power(x.terms(), b))?;
out.values_mut().for_each(crate::Frac::reduce);
Some(mac_rat_out(&out))
},
|| {
let x = build_mac_rat::<BigRational>(&rows)
.expect("BigRational accepts every coefficient");
let mut out = routed_ring_to_power(x.terms(), b);
out.values_mut().for_each(crate::Frac::reduce);
mac_rat_out(&out)
},
))
})
}
#[pyfunction]
fn to_power_jack(f: JackElement, src: &str) -> PyResult<JackTerms> {
interruptible(move || {
let b = Basis::parse(src)?;
let rows = jack_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&rows)?;
let mut out = guarded(|| routed_ring_to_power(x.terms(), b))?;
out.values_mut().for_each(crate::AFrac::reduce);
Some(jack_out(&out))
},
|| {
let x = build_jack_wide::<BigInt>(&rows);
let mut out = routed_ring_to_power(x.terms(), b);
out.values_mut().for_each(crate::AFrac::reduce);
jack_out(&out)
},
))
})
}
#[pyfunction]
fn to_power_ht(f: HtElement, src: &str) -> PyResult<RatHtTerms> {
interruptible(move || {
let b = Basis::parse(src)?;
let x = ht_terms_arg(&f)?;
let mut out = routed_ring_to_power(&x, b);
out.values_mut().for_each(crate::Ratio::reduce);
Ok(out
.iter()
.map(|(mu, c)| {
let (num, den) = c.parts();
(
mu.parts().to_vec().into(),
num.terms()
.map(|(&(a, b), v)| {
(a, b, Coeff::Small(v.numer()), Coeff::Small(v.denom()))
})
.collect(),
den.map(|(&atom, &k)| atom_row(atom, k)).collect(),
)
})
.collect())
})
}
#[pyfunction]
fn monomial_to_macdonald_p(f: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
let rows = mac_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&rows)?;
Some(mac_out(&guarded(|| crate::monomial_to_macdonald_p(&x))?))
},
|| {
mac_out(&crate::monomial_to_macdonald_p(&build_mac_wide::<BigInt>(
&rows,
)))
},
))
})
}
#[pyfunction]
fn monomial_to_macdonald_q(f: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
let rows = mac_terms_arg(&f)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&rows)?;
Some(mac_out(&guarded(|| crate::monomial_to_macdonald_q(&x))?))
},
|| {
mac_out(&crate::monomial_to_macdonald_q(&build_mac_wide::<BigInt>(
&rows,
)))
},
))
})
}
#[pyfunction]
fn macdonald_element_add(f: MacElement, g: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
let a = mac_terms_arg(&f)?;
let b = mac_terms_arg(&g)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&a)?;
let y = build_mac::<Guarded>(&b)?;
let out =
guarded(|| crate::macdonald::macdonald_element_add(x.terms(), y.terms()))?;
Some(mac_out(&out))
},
|| {
let x = build_mac_wide::<BigInt>(&a);
let y = build_mac_wide::<BigInt>(&b);
mac_out(&crate::macdonald::macdonald_element_add(
x.terms(),
y.terms(),
))
},
))
})
}
#[pyfunction]
fn macdonald_element_scale(
f: MacElement,
num: Vec<(u32, u32, Coeff)>,
den: Vec<(u32, u32, u32)>,
) -> PyResult<MacTerms> {
interruptible(move || {
let rows = mac_terms_arg(&f)?;
let one_row: MacElement = vec![(vec![].into(), num, den)];
let scalar = mac_terms_arg(&one_row)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&rows)?;
let c = build_mac::<Guarded>(&scalar)?;
let c = c.coeff(&Partition::new([]));
Some(mac_out(&guarded(|| {
crate::macdonald::macdonald_element_scale(x.terms(), &c)
})?))
},
|| {
let x = build_mac_wide::<BigInt>(&rows);
let c = build_mac_wide::<BigInt>(&scalar).coeff(&Partition::new([]));
mac_out(&crate::macdonald::macdonald_element_scale(x.terms(), &c))
},
))
})
}
fn mac_forward(
f: &MacElement,
fast: fn(
&std::collections::BTreeMap<Partition, crate::Frac<Guarded>>,
) -> Monomial<crate::Frac<Guarded>>,
slow: fn(
&std::collections::BTreeMap<Partition, crate::Frac<BigInt>>,
) -> Monomial<crate::Frac<BigInt>>,
) -> PyResult<MacTerms> {
let rows = mac_terms_arg(f)?;
Ok(escalate(
|| {
let x = build_mac::<Guarded>(&rows)?;
Some(mac_out(guarded(|| fast(x.terms()))?.terms()))
},
|| {
let x = build_mac_wide::<BigInt>(&rows);
mac_out(slow(x.terms()).terms())
},
))
}
#[pyfunction]
fn macdonald_p_to_monomial(f: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
mac_forward(
&f,
crate::macdonald_p_to_monomial,
crate::macdonald_p_to_monomial,
)
})
}
#[pyfunction]
fn macdonald_q_to_monomial(f: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
mac_forward(
&f,
crate::macdonald_q_to_monomial,
crate::macdonald_q_to_monomial,
)
})
}
#[pyfunction]
fn macdonald_j_to_monomial(f: MacElement) -> PyResult<MacTerms> {
interruptible(move || {
mac_forward(
&f,
crate::macdonald_j_to_monomial,
crate::macdonald_j_to_monomial,
)
})
}
type JackCell = (Vec<Coeff>, Vec<(u32, u32, u32)>, u128, Vec<Coeff>);
type JackTerms = Vec<(Key, Vec<Coeff>, Vec<(u32, u32, u32)>, u128, Vec<Coeff>)>;
fn jack_cell<C: Boundary>(c: &crate::AFrac<C>) -> JackCell {
let (num, den, scale) = c.parts();
(
num.iter().map(ToCoeff::to_coeff).collect(),
den.map(|(&(u, v), &m)| (u, v, m)).collect(),
scale,
c.tail().iter().map(ToCoeff::to_coeff).collect(),
)
}
fn jack_terms<C: Boundary>(f: &Monomial<crate::AFrac<C>>) -> JackTerms {
f.terms()
.iter()
.map(|(mu, c)| {
let (n, d, s, t) = jack_cell(c);
(mu.parts().to_vec().into(), n, d, s, t)
})
.collect()
}
fn jack_terms_p<C: Boundary>(f: &PowerSum<crate::AFrac<C>>) -> JackTerms {
f.terms()
.iter()
.map(|(mu, c)| {
let (n, d, s, t) = jack_cell(c);
(mu.parts().to_vec().into(), n, d, s, t)
})
.collect()
}
fn jack_escalate(
fast: impl FnOnce() -> crate::AFrac<Guarded>,
slow: impl FnOnce() -> crate::AFrac<BigInt>,
) -> JackCell {
escalate(|| guarded(|| jack_cell(&fast())), || jack_cell(&slow()))
}
fn jack_escalate_m(
fast: impl FnOnce() -> Monomial<crate::AFrac<Guarded>>,
slow: impl FnOnce() -> Monomial<crate::AFrac<BigInt>>,
) -> JackTerms {
escalate(|| guarded(|| jack_terms(&fast())), || jack_terms(&slow()))
}
#[pyfunction]
fn jack_p(la: Vec<u32>) -> PyResult<JackTerms> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(jack_escalate_m(|| crate::jack_p(&l), || crate::jack_p(&l)))
})
}
#[pyfunction]
fn jack_q(la: Vec<u32>) -> PyResult<JackTerms> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(jack_escalate_m(|| crate::jack_q(&l), || crate::jack_q(&l)))
})
}
#[pyfunction]
fn jack_j(la: Vec<u32>) -> PyResult<JackTerms> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(jack_escalate_m(|| crate::jack_j(&l), || crate::jack_j(&l)))
})
}
type JackElement = Vec<(Key, Vec<Coeff>, Vec<(u32, u32, u32)>, u128, Vec<Coeff>)>;
type JackParsed<'a> = Vec<(
Partition,
&'a [Coeff],
crate::afrac::Linears,
u128,
&'a [Coeff],
)>;
fn jack_terms_arg(rows: &JackElement) -> PyResult<JackParsed<'_>> {
rows.iter()
.map(|(p, num, den, scale, tail)| {
if *scale == 0 {
return Err(PyValueError::new_err("a scale of 0 is a division by zero"));
}
let mut factors = crate::afrac::Linears::new();
for &(u, v, m) in den {
if u == 0 && v == 0 {
return Err(PyValueError::new_err(
"not a denominator factor: (0, 0) is the zero linear form",
));
}
let m = i32::try_from(m).map_err(|_| {
PyValueError::new_err(format!("denominator multiplicity {m} is too large"))
})?;
*factors.entry((u, v)).or_insert(0) -= m;
}
Ok((
part_arg(p)?,
num.as_slice(),
factors,
*scale,
tail.as_slice(),
))
})
.collect()
}
fn build_jack<C: Boundary>(rows: &JackParsed) -> Option<Monomial<crate::AFrac<C>>> {
let mut x = Monomial::zero();
for (p, num, den, scale, tail) in rows {
let coeffs: Option<Vec<C>> = num.iter().map(C::from_coeff).collect();
let mut c = crate::AFrac::from_coeffs(coeffs?)
.mul_factors(den)
.div_int(*scale);
if !tail.is_empty() {
let t: Option<Vec<C>> = tail.iter().map(C::from_coeff).collect();
c = c.div_tail(&t?);
}
x.add_term(p.clone(), c);
}
Some(x)
}
fn build_jack_wide<C: Wide>(rows: &JackParsed) -> Monomial<crate::AFrac<C>> {
let mut x = Monomial::zero();
for (p, num, den, scale, tail) in rows {
let coeffs: Vec<C> = num.iter().map(C::from_coeff_wide).collect();
let mut c = crate::AFrac::from_coeffs(coeffs)
.mul_factors(den)
.div_int(*scale);
if !tail.is_empty() {
let t: Vec<C> = tail.iter().map(C::from_coeff_wide).collect();
c = c.div_tail(&t);
}
x.add_term(p.clone(), c);
}
x
}
fn jack_out<C: Boundary>(m: &std::collections::BTreeMap<Partition, crate::AFrac<C>>) -> JackTerms {
m.iter()
.map(|(mu, c)| {
let (n, d, s, t) = jack_cell(c);
(mu.parts().to_vec().into(), n, d, s, t)
})
.collect()
}
fn jack_inverse(
f: &JackElement,
fast: fn(
&Monomial<crate::AFrac<Guarded>>,
) -> std::collections::BTreeMap<Partition, crate::AFrac<Guarded>>,
slow: fn(
&Monomial<crate::AFrac<BigInt>>,
) -> std::collections::BTreeMap<Partition, crate::AFrac<BigInt>>,
) -> PyResult<JackTerms> {
let rows = jack_terms_arg(f)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&rows)?;
Some(jack_out(&guarded(|| fast(&x))?))
},
|| jack_out(&slow(&build_jack_wide::<BigInt>(&rows))),
))
}
#[pyfunction]
fn monomial_to_jack_p(f: JackElement) -> PyResult<JackTerms> {
interruptible(move || jack_inverse(&f, crate::monomial_to_jack_p, crate::monomial_to_jack_p))
}
#[pyfunction]
fn monomial_to_jack_q(f: JackElement) -> PyResult<JackTerms> {
interruptible(move || jack_inverse(&f, crate::monomial_to_jack_q, crate::monomial_to_jack_q))
}
#[pyfunction]
fn monomial_to_jack_j(f: JackElement) -> PyResult<JackTerms> {
interruptible(move || jack_inverse(&f, crate::monomial_to_jack_j, crate::monomial_to_jack_j))
}
#[pyfunction]
fn jack_element_add(f: JackElement, g: JackElement) -> PyResult<JackTerms> {
interruptible(move || {
let a = jack_terms_arg(&f)?;
let b = jack_terms_arg(&g)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
let y = build_jack::<Guarded>(&b)?;
let out = guarded(|| crate::jack::jack_element_add(x.terms(), y.terms()))?;
Some(jack_out(&out))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
let y = build_jack_wide::<BigInt>(&b);
jack_out(&crate::jack::jack_element_add(x.terms(), y.terms()))
},
))
})
}
#[pyfunction]
#[pyo3(signature = (f, num, den, scale, tail = Vec::new()))]
fn jack_element_scale(
f: JackElement,
num: Vec<Coeff>,
den: Vec<(u32, u32, u32)>,
scale: u128,
tail: Vec<Coeff>,
) -> PyResult<JackTerms> {
interruptible(move || {
let rows = jack_terms_arg(&f)?;
let one_row: JackElement = vec![(vec![].into(), num, den, scale, tail)];
let scalar = jack_terms_arg(&one_row)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&rows)?;
let c = build_jack::<Guarded>(&scalar)?.coeff(&Partition::new([]));
Some(jack_out(&guarded(|| {
crate::jack::jack_element_scale(x.terms(), &c)
})?))
},
|| {
let x = build_jack_wide::<BigInt>(&rows);
let c = build_jack_wide::<BigInt>(&scalar).coeff(&Partition::new([]));
jack_out(&crate::jack::jack_element_scale(x.terms(), &c))
},
))
})
}
fn jack_forward(
f: &JackElement,
fast: fn(
&std::collections::BTreeMap<Partition, crate::AFrac<Guarded>>,
) -> Monomial<crate::AFrac<Guarded>>,
slow: fn(
&std::collections::BTreeMap<Partition, crate::AFrac<BigInt>>,
) -> Monomial<crate::AFrac<BigInt>>,
) -> PyResult<JackTerms> {
let rows = jack_terms_arg(f)?;
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&rows)?;
Some(jack_out(guarded(|| fast(x.terms()))?.terms()))
},
|| {
let x = build_jack_wide::<BigInt>(&rows);
jack_out(slow(x.terms()).terms())
},
))
}
#[pyfunction]
fn jack_p_to_monomial(f: JackElement) -> PyResult<JackTerms> {
interruptible(move || jack_forward(&f, crate::jack_p_to_monomial, crate::jack_p_to_monomial))
}
#[pyfunction]
fn jack_q_to_monomial(f: JackElement) -> PyResult<JackTerms> {
interruptible(move || jack_forward(&f, crate::jack_q_to_monomial, crate::jack_q_to_monomial))
}
#[pyfunction]
fn jack_j_to_monomial(f: JackElement) -> PyResult<JackTerms> {
interruptible(move || jack_forward(&f, crate::jack_j_to_monomial, crate::jack_j_to_monomial))
}
#[pyfunction]
fn jack_table(n: u32) -> PyResult<Vec<(Key, JackTerms)>> {
interruptible(move || {
Ok({
crate::partitions_of(n)
.into_iter()
.map(|l| {
let rows = jack_escalate_m(|| crate::jack_p(&l), || crate::jack_p(&l));
(l.parts().to_vec().into(), rows)
})
.collect()
})
})
}
#[pyfunction]
fn jack_j_powersum(la: Vec<u32>) -> PyResult<JackTerms> {
interruptible(move || {
let l = part_arg(&la)?;
Ok(escalate(
|| guarded(|| jack_terms_p(&crate::jack_j_powersum::<Guarded>(&l))),
|| jack_terms_p(&crate::jack_j_powersum::<BigInt>(&l)),
))
})
}
#[pyfunction]
fn jack_norm_j(la: Vec<u32>) -> PyResult<Vec<(u32, u32, u32)>> {
interruptible(move || {
Ok(crate::jack_norm_j(&part_arg(&la)?)
.into_iter()
.map(|((u, v), m)| (u, v, m as u32))
.collect())
})
}
#[pyfunction]
fn jack_structure_constant(la: Vec<u32>, mu: Vec<u32>, nu: Vec<u32>) -> PyResult<JackCell> {
interruptible(move || {
let (a, b, c) = (part_arg(&la)?, part_arg(&mu)?, part_arg(&nu)?);
Ok(jack_escalate(
|| crate::jack_structure_constant(&a, &b, &c),
|| crate::jack_structure_constant(&a, &b, &c),
))
})
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn stanley_table(
k: u32,
) -> PyResult<
Vec<(
Vec<u32>,
Vec<u32>,
Vec<u32>,
Vec<Coeff>,
Vec<(u32, u32, u32)>,
u128,
)>,
> {
interruptible(move || {
Ok({
escalate(
|| {
guarded(|| {
crate::stanley_table::<Guarded>(k)
.iter()
.map(|(la, mu, nu, g)| {
let (n, d, s, _) = jack_cell(g);
(
la.parts().to_vec(),
mu.parts().to_vec(),
nu.parts().to_vec(),
n,
d,
s,
)
})
.collect()
})
},
|| {
crate::stanley_table::<BigInt>(k)
.iter()
.map(|(la, mu, nu, g)| {
let (n, d, s, _) = jack_cell(g);
(
la.parts().to_vec(),
mu.parts().to_vec(),
nu.parts().to_vec(),
n,
d,
s,
)
})
.collect()
},
)
})
})
}
#[pyfunction]
fn jack_scalar(f: JackElement, g: JackElement) -> PyResult<JackCell> {
interruptible(move || {
let (a, b) = (jack_terms_arg(&f)?, jack_terms_arg(&g)?);
Ok(escalate(
|| {
let x = build_jack::<Guarded>(&a)?;
let y = build_jack::<Guarded>(&b)?;
let v = guarded(|| crate::jack_scalar(&x, &y))?;
Some(jack_cell(&v))
},
|| {
let x = build_jack_wide::<BigInt>(&a);
let y = build_jack_wide::<BigInt>(&b);
jack_cell(&crate::jack_scalar(&x, &y))
},
))
})
}
#[pyfunction]
fn zonal(la: Vec<u32>, integral_form: bool) -> PyResult<Vec<(Key, Coeff, Coeff)>> {
interruptible(move || {
let l = part_arg(&la)?;
let f = if integral_form {
crate::zonal_j(&l)
} else {
crate::zonal_p(&l)
};
Ok(f.terms()
.iter()
.map(|(mu, c)| {
(
mu.parts().to_vec().into(),
Coeff::Small(c.numer()),
Coeff::Small(c.denom()),
)
})
.collect())
})
}
#[pyfunction]
#[allow(clippy::type_complexity)]
fn gj_connection_tables(
n: u32,
) -> PyResult<(
Vec<(Vec<u32>, Vec<u32>, Vec<u32>, Vec<Coeff>, u128)>,
Vec<(Vec<u32>, Vec<u32>, Vec<u32>, Vec<Coeff>, u128)>,
)> {
interruptible(move || {
let t = crate::gj_connection_tables(n);
if !t.laws_hold() {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"a PROVEN law failed at n = {n}: not polynomial at {:?}, c not integral at {:?}",
t.not_polynomial, t.c_not_integral
)));
}
let rows = |m: &std::collections::BTreeMap<crate::gj::Key, crate::BPoly>| {
m.iter()
.map(|((la, mu, nu), p)| {
(
la.parts().to_vec(),
mu.parts().to_vec(),
nu.parts().to_vec(),
p.num.iter().map(|&v| Coeff::Small(v)).collect(),
p.den,
)
})
.collect()
};
Ok((rows(&t.c), rows(&t.h)))
})
}
#[pyfunction]
fn class_algebra_coefficient(la: Vec<u32>, mu: Vec<u32>, nu: Vec<u32>) -> PyResult<Coeff> {
interruptible(move || {
let (l, m, n) = (part_arg(&la)?, part_arg(&mu)?, part_arg(&nu)?);
same_degree(&[("la", &l), ("mu", &m), ("nu", &n)])?;
crate::try_class_algebra_coefficient(&l, &m, &n)
.map(Coeff::Small)
.ok_or_else(|| {
PyOverflowError::new_err(format!(
"a^{l}_{{{m},{n}}} exceeds the fixed-width computation at n = {}",
l.size()
))
})
})
}
fn qt_poly<C: Ring + ToCoeff>(p: &crate::QtPoly<C>) -> Vec<(u32, u32, Coeff)> {
p.terms().map(|(&(a, b), v)| (a, b, v.to_coeff())).collect()
}
#[pyfunction]
fn qt_kostka(la: Vec<u32>, mu: Vec<u32>) -> PyResult<Vec<(u32, u32, Coeff)>> {
interruptible(move || {
let (l, m) = (part_arg(&la)?, part_arg(&mu)?);
same_degree(&[("la", &l), ("mu", &m)])?;
Ok(qt_poly(&crate::qt_kostka::<i128>(&l, &m)))
})
}
#[pyfunction]
fn qt_kostka_column(mu: Vec<u32>) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>)>> {
interruptible(move || {
Ok(crate::qt_kostka_column::<i128>(&part_arg(&mu)?)
.iter()
.map(|(lambda, k)| (lambda.parts().to_vec().into(), qt_poly(k)))
.collect())
})
}
#[pyfunction]
fn macdonald_ht(mu: Vec<u32>) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>)>> {
interruptible(move || {
Ok(crate::macdonald_ht::<i128>(&part_arg(&mu)?)
.terms()
.iter()
.map(|(lambda, k)| (lambda.parts().to_vec().into(), qt_poly(k)))
.collect())
})
}
type HtTerms = Vec<(Key, Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32, u32)>)>;
type HtElement = Vec<(Key, Vec<(u32, u32, Coeff)>, Vec<(u32, u32, u32, u32)>)>;
fn atom_row(atom: crate::Atom, m: u32) -> (u32, u32, u32, u32) {
match atom {
crate::Atom::Unit(a, b) => (0, a, b, m),
crate::Atom::Diff(a, b) => (1, a, b, m),
}
}
fn atom_arg(row: (u32, u32, u32, u32)) -> PyResult<(crate::Atom, u32)> {
let (kind, a, b, m) = row;
match kind {
0 => {
if a == 0 && b == 0 {
return Err(PyValueError::new_err(
"not a denominator atom: 1 - q^0 t^0 is zero",
));
}
Ok((crate::Atom::Unit(a, b), m))
}
1 => {
if a == 0 || b == 0 {
return Err(PyValueError::new_err(format!(
"q^{a} - t^{b} is not of kind 1: with a zero exponent it is \
1 - q^a t^b up to sign, which is kind 0"
)));
}
Ok((crate::Atom::Diff(a, b), m))
}
_ => Err(PyValueError::new_err(format!(
"unknown atom kind {kind}; 0 is 1 - q^a t^b and 1 is q^a - t^b"
))),
}
}
fn ht_terms_arg(
rows: &HtElement,
) -> PyResult<std::collections::BTreeMap<Partition, crate::Ratio<crate::Rational>>> {
let mut out = std::collections::BTreeMap::new();
for (la, num, den) in rows {
let mut atoms = std::collections::BTreeMap::new();
for &row in den {
let (atom, m) = atom_arg(row)?;
*atoms.entry(atom).or_insert(0) += m;
}
let c = crate::Ratio::from_poly(qt_coeffs(num)?).div_atoms(&atoms);
out.insert(part_arg(la)?, c);
}
Ok(out)
}
fn ht_out(
m: &std::collections::BTreeMap<Partition, crate::Ratio<crate::Rational>>,
) -> PyResult<HtTerms> {
m.iter()
.map(|(mu, c)| {
let (num, den) = c.parts();
Ok((
mu.parts().to_vec().into(),
qt_poly_int(num, &format!("the coefficient at {mu}"))?,
den.map(|(&atom, &k)| atom_row(atom, k)).collect(),
))
})
.collect()
}
fn qt_poly_int(p: &crate::QtPoly<crate::Rational>, what: &str) -> PyResult<Vec<(u32, u32, Coeff)>> {
p.terms()
.map(|(&(a, b), v)| {
if v.denom() != 1 {
return Err(PyValueError::new_err(format!(
"{what}: the coefficient of q^{a}t^{b} is {v:?}, not an integer"
)));
}
Ok((a, b, Coeff::Small(v.numer())))
})
.collect()
}
#[pyfunction]
fn schur_to_macdonald_ht(f: QtSchur) -> PyResult<HtTerms> {
interruptible(move || {
let x = qt_schur_in_any(&f)?;
ht_out(&crate::schur_to_macdonald_ht(&x))
})
}
#[pyfunction]
fn macdonald_ht_to_schur(f: HtElement) -> PyResult<HtTerms> {
interruptible(move || {
let x = ht_terms_arg(&f)?;
ht_out(crate::macdonald_ht_to_schur(&x).terms())
})
}
#[pyfunction]
fn macdonald_ht_element_add(f: HtElement, g: HtElement) -> PyResult<HtTerms> {
interruptible(move || {
let a = ht_terms_arg(&f)?;
let b = ht_terms_arg(&g)?;
ht_out(&crate::deltaop::htilde_element_add(&a, &b))
})
}
#[pyfunction]
fn macdonald_ht_element_scale(
f: HtElement,
num: Vec<(u32, u32, Coeff)>,
den: Vec<(u32, u32, u32, u32)>,
) -> PyResult<HtTerms> {
interruptible(move || {
let rows = ht_terms_arg(&f)?;
let one_row: HtElement = vec![(vec![].into(), num, den)];
let c = ht_terms_arg(&one_row)?;
let c = c
.get(&Partition::new([]))
.cloned()
.unwrap_or_else(<crate::Ratio<crate::Rational> as Ring>::zero);
ht_out(&crate::deltaop::htilde_element_scale(&rows, &c))
})
}
#[pyfunction]
fn qt_kostka_table(n: u32) -> PyResult<Vec<Vec<Vec<(u32, u32, Coeff)>>>> {
interruptible(move || {
Ok({
crate::qt_kostka_table::<i128>(n)
.into_iter()
.map(|row| row.iter().map(qt_poly).collect())
.collect()
})
})
}
type QtSchur = Vec<(Key, Vec<(u32, u32, Coeff)>)>;
fn qt_schur_out<C: Ring + ToCoeff>(f: &Schur<crate::QtPoly<C>>) -> QtSchur {
f.terms()
.iter()
.map(|(lambda, c)| (lambda.parts().to_vec().into(), qt_poly(c)))
.collect()
}
fn qt_schur_out_rat(f: &Schur<crate::QtPoly<crate::Rational>>, what: &str) -> PyResult<QtSchur> {
let mut out = QtSchur::new();
for (lambda, c) in f.terms() {
let mut row = Vec::with_capacity(c.len());
for (&(a, b), v) in c.terms() {
if v.denom() != 1 {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"{what} is not integral at {lambda}: coefficient of q^{a}t^{b} is {v:?}"
)));
}
row.push((a, b, Coeff::Small(v.numer())));
}
out.push((lambda.parts().to_vec().into(), row));
}
Ok(out)
}
fn qt_schur_in(rows: &QtSchur) -> PyResult<Schur<crate::QtPoly<crate::Rational>>> {
let mut out = Schur::zero();
let mut degree: Option<(u32, Partition)> = None;
for (lambda, terms) in rows {
let lambda = part_arg(lambda)?;
let n = lambda.size();
match °ree {
Some((d, first)) if *d != n => {
return Err(PyValueError::new_err(format!(
"a Macdonald operator requires a homogeneous argument: \
{first} has degree {d} but {lambda} has degree {n}"
)))
}
Some(_) => {}
None => degree = Some((n, lambda.clone())),
}
out.add_term(lambda, qt_coeffs(terms)?);
}
Ok(out)
}
fn qt_schur_in_any(rows: &QtSchur) -> PyResult<Schur<crate::QtPoly<crate::Rational>>> {
let mut out = Schur::zero();
for (lambda, terms) in rows {
out.add_term(part_arg(lambda)?, qt_coeffs(terms)?);
}
Ok(out)
}
fn qt_coeffs(terms: &[(u32, u32, Coeff)]) -> PyResult<crate::QtPoly<crate::Rational>> {
let mut c = crate::QtPoly::zero();
for (a, b, v) in terms {
let v = v.as_i128().filter(|&v| v != i128::MIN).ok_or_else(|| {
pyo3::exceptions::PyValueError::new_err(
"coefficient does not fit the fixed-width (q,t) arithmetic",
)
})?;
c.add_term(*a, *b, crate::Rational::from_int(v));
}
Ok(c)
}
#[pyfunction]
fn nabla_e(n: u32) -> PyResult<QtSchur> {
interruptible(move || Ok(qt_schur_out(&crate::nabla_e::<i128>(n))))
}
#[pyfunction]
fn delta_prime_e(k: u32, n: u32) -> PyResult<QtSchur> {
interruptible(move || Ok(qt_schur_out(&crate::delta_prime_e::<i128>(k, n))))
}
#[pyfunction]
fn nabla(f: QtSchur) -> PyResult<QtSchur> {
interruptible(move || qt_schur_out_rat(&crate::nabla(&qt_schur_in(&f)?), "nabla"))
}
#[pyfunction]
fn nabla_power(f: QtSchur, r: u32) -> PyResult<QtSchur> {
interruptible(move || {
qt_schur_out_rat(&crate::nabla_power(&qt_schur_in(&f)?, r), "nabla_power")
})
}
#[pyfunction]
fn delta_ek(k: u32, f: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
let ek = crate::deltaop::elementary(k);
qt_schur_out_rat(&crate::delta(&ek, &qt_schur_in(&f)?), "delta")
})
}
#[pyfunction]
fn delta_prime_ek(k: u32, f: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
let ek = crate::deltaop::elementary(k);
qt_schur_out_rat(&crate::delta_prime(&ek, &qt_schur_in(&f)?), "delta_prime")
})
}
#[pyfunction]
fn theta_ek(k: u32, f: QtSchur) -> PyResult<QtSchur> {
interruptible(move || {
let ek = crate::deltaop::elementary(k);
qt_schur_out_rat(&crate::theta(&ek, &qt_schur_in(&f)?), "theta")
})
}
#[pyfunction]
fn big_pi(f: QtSchur) -> PyResult<QtSchur> {
interruptible(move || qt_schur_out_rat(&crate::big_pi(&qt_schur_in(&f)?), "big_pi"))
}
#[pyfunction]
fn delta_conjecture_side(n: u32, side: &str) -> PyResult<Vec<QtSchur>> {
interruptible(move || {
let which = match side {
"rise" => crate::Side::Rise,
"valley" => crate::Side::Valley,
other => {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"side must be \"rise\" or \"valley\", got {other:?}"
)))
}
};
Ok(crate::ladder::<i128>(n, which)
.iter()
.map(|f| {
f.terms()
.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), qt_poly(c)))
.collect()
})
.collect())
})
}
type QtMon = Vec<(Key, Vec<(u32, u32, Coeff)>)>;
fn qt_mon_out<C: Ring + ToCoeff>(f: &Monomial<crate::QtPoly<C>>) -> QtMon {
f.terms()
.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), qt_poly(c)))
.collect()
}
#[derive(FromPyObject)]
enum LltShape {
Skew((Vec<u32>, Vec<u32>)),
Straight(Vec<u32>),
}
fn skew_tuple(shapes: &[LltShape], offsets: Option<Vec<i32>>) -> PyResult<crate::llt::SkewTuple> {
let skews: Vec<(Partition, Partition)> = shapes
.iter()
.map(|s| match s {
LltShape::Straight(v) => Ok((part_arg(v)?, Partition::default())),
LltShape::Skew((o, i)) => {
let (outer, inner) = (part_arg(o)?, part_arg(i)?);
if !outer.contains(&inner) {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"a skew shape needs inner contained in outer; \
{inner} is not contained in {outer}"
)));
}
Ok((outer, inner))
}
})
.collect::<PyResult<_>>()?;
let offs = match offsets {
None => vec![0i32; skews.len()],
Some(o) if o.len() == skews.len() => o,
Some(o) => {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"got {} offsets for {} components",
o.len(),
skews.len()
)))
}
};
let cells: u32 = skews.iter().map(|(o, i)| o.size() - i.size()).sum();
cells_arg(cells as usize, "this tuple")?;
Ok(crate::llt::SkewTuple::from_skews(&skews, &offs))
}
fn decorated_graph(
n: u32,
weak: Vec<(u32, u32)>,
strict: Vec<(u32, u32)>,
) -> PyResult<crate::llt::DecoratedGraph> {
if let Some(&(u, v)) = strict.iter().find(|&&(u, v)| u >= v) {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"a strict edge is oriented u < v (the constraint is κ(u) < κ(v)); got ({u}, {v})"
)));
}
if let Some(&(u, v)) = weak.iter().chain(&strict).find(|&&(u, v)| u >= n || v >= n) {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"edge ({u}, {v}) lands outside 0..{n}"
)));
}
let un = |&(u, v): &(u32, u32)| (u.min(v), u.max(v));
if let Some(e) = weak
.iter()
.map(un)
.find(|e| strict.iter().map(un).any(|s| s == *e))
{
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"edge {e:?} is both weak (counted) and strict (constraining); it must be one or the other"
)));
}
Ok(crate::llt::DecoratedGraph::new(n, &weak, &strict))
}
#[pyfunction]
#[pyo3(signature = (la, k))]
fn llt_gtilde(la: Vec<u32>, k: u32) -> PyResult<QtMon> {
interruptible(move || {
let (l, k) = (part_arg(&la)?, level_arg(k)?);
abacus_arg(&l, k)?;
Ok(escalate(
|| {
Some(qt_mon_out(&guarded(|| {
crate::llt::llt_gtilde::<Guarded>(&l, k)
})?))
},
|| qt_mon_out(&crate::llt::llt_gtilde::<BigInt>(&l, k)),
))
})
}
#[pyfunction]
#[pyo3(signature = (mu, k))]
fn llt_h(mu: Vec<u32>, k: u32) -> PyResult<QtMon> {
interruptible(move || {
let (m, k) = (part_arg(&mu)?, level_arg(k)?);
abacus_arg(&m, k)?;
Ok(escalate(
|| {
Some(qt_mon_out(&guarded(|| {
crate::llt::llt_h::<Guarded>(&m, k)
})?))
},
|| qt_mon_out(&crate::llt::llt_h::<BigInt>(&m, k)),
))
})
}
#[pyfunction]
#[pyo3(signature = (mu, k))]
fn llt_h_tilde(mu: Vec<u32>, k: u32) -> PyResult<QtMon> {
interruptible(move || {
let (m, k) = (part_arg(&mu)?, level_arg(k)?);
abacus_arg(&m, k)?;
Ok(escalate(
|| {
Some(qt_mon_out(&guarded(|| {
crate::llt::llt_h_tilde::<Guarded>(&m, k)
})?))
},
|| qt_mon_out(&crate::llt::llt_h_tilde::<BigInt>(&m, k)),
))
})
}
#[pyfunction]
#[pyo3(signature = (la, k))]
fn llt_g_lt(la: Vec<u32>, k: u32) -> PyResult<QtMon> {
interruptible(move || {
let (l, k) = (part_arg(&la)?, level_arg(k)?);
abacus_arg(&l, k)?;
Ok(escalate(
|| {
Some(qt_mon_out(&guarded(|| {
crate::llt::llt_g_lt::<Guarded>(&l, k)
})?))
},
|| qt_mon_out(&crate::llt::llt_g_lt::<BigInt>(&l, k)),
))
})
}
#[pyfunction]
#[pyo3(signature = (n, k))]
fn llt_h_table(n: u32, k: u32) -> PyResult<Vec<(Key, QtMon)>> {
interruptible(move || {
Ok(crate::llt::llt_h_table::<i128>(n, level_arg(k)?)
.iter()
.map(|(mu, f)| (mu.parts().to_vec().into(), qt_mon_out(f)))
.collect())
})
}
fn llt_inverse_rows<C: Ring + ToCoeff>(table: &[(Partition, QtMap<C>)]) -> Vec<(Key, QtMon)> {
table
.iter()
.map(|(nu, row)| {
let row = row
.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), qt_poly(c)))
.collect();
(nu.parts().to_vec().into(), row)
})
.collect()
}
#[pyfunction]
#[pyo3(signature = (n, k))]
fn monomial_in_llt_h_table(n: u32, k: u32) -> PyResult<Vec<(Key, QtMon)>> {
interruptible(move || {
let k = level_arg(k)?;
if n > 0 {
abacus_arg(&Partition::new([n]), k)?;
}
Ok(escalate(
|| guarded(|| llt_inverse_rows(&crate::llt::monomial_in_llt_h_table::<Guarded>(n, k))),
|| llt_inverse_rows(&crate::llt::monomial_in_llt_h_table::<BigInt>(n, k)),
))
})
}
fn llt_tilde_inverse_rows<C: Ring + ToCoeff>(
table: &[(Partition, u32, QtMap<C>)],
) -> Vec<(Key, u32, QtMon)> {
table
.iter()
.map(|(nu, shift, row)| {
let row = row
.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), qt_poly(c)))
.collect();
(nu.parts().to_vec().into(), *shift, row)
})
.collect()
}
#[pyfunction]
#[pyo3(signature = (n, k))]
fn monomial_in_llt_h_tilde_table(n: u32, k: u32) -> PyResult<Vec<(Key, u32, QtMon)>> {
interruptible(move || {
let k = level_arg(k)?;
if n > 0 {
abacus_arg(&Partition::new([n]), k)?;
}
Ok(escalate(
|| {
guarded(|| {
llt_tilde_inverse_rows(&crate::llt::monomial_in_llt_h_tilde_table::<Guarded>(
n, k,
))
})
},
|| llt_tilde_inverse_rows(&crate::llt::monomial_in_llt_h_tilde_table::<BigInt>(n, k)),
))
})
}
#[pyfunction]
#[pyo3(signature = (n, k))]
fn llt_gtilde_table(n: u32, k: u32) -> PyResult<Vec<(Key, QtMon)>> {
interruptible(move || {
let k = level_arg(k)?;
abacus_table_arg(n, k)?;
Ok(crate::llt::llt_gtilde_table::<i128>(n, k)
.iter()
.map(|(lambda, f)| (lambda.parts().to_vec().into(), qt_mon_out(f)))
.collect())
})
}
#[pyfunction]
#[pyo3(signature = (la, k))]
fn llt_schur(la: Vec<u32>, k: u32) -> PyResult<QtSchur> {
interruptible(move || {
let (l, k) = (part_arg(&la)?, level_arg(k)?);
abacus_arg(&l, k)?;
Ok(escalate(
|| {
Some(qt_schur_out(&guarded(|| {
crate::llt::llt_schur::<Guarded>(&l, k)
})?))
},
|| qt_schur_out(&crate::llt::llt_schur::<BigInt>(&l, k)),
))
})
}
#[pyfunction]
#[pyo3(signature = (shapes, offsets=None))]
fn llt_g(shapes: Vec<LltShape>, offsets: Option<Vec<i32>>) -> PyResult<QtMon> {
interruptible(move || {
Ok(qt_mon_out(&crate::llt::llt_g::<i128>(&skew_tuple(
&shapes, offsets,
)?)))
})
}
#[pyfunction]
#[pyo3(signature = (shapes, offsets=None))]
fn llt_min_inv(shapes: Vec<LltShape>, offsets: Option<Vec<i32>>) -> PyResult<u32> {
interruptible(move || Ok(crate::llt::llt_min_inv(&skew_tuple(&shapes, offsets)?)))
}
#[pyfunction]
#[pyo3(signature = (shapes, offsets=None))]
fn llt_fundamental(
shapes: Vec<LltShape>,
offsets: Option<Vec<i32>>,
) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>)>> {
interruptible(move || {
Ok(
crate::llt::llt_fundamental::<i128>(&skew_tuple(&shapes, offsets)?)
.iter()
.map(|(comp, c)| (comp.clone().into(), qt_poly(c)))
.collect(),
)
})
}
#[pyfunction]
#[pyo3(signature = (la, k))]
fn k_core_quotient(la: Vec<u32>, k: u32) -> PyResult<(Key, Vec<Key>)> {
interruptible(move || {
let (l, k) = (part_arg(&la)?, level_arg(k)?);
Ok((
l.k_core(k).parts().to_vec().into(),
l.k_quotient(k)
.iter()
.map(|p| p.parts().to_vec().into())
.collect(),
))
})
}
#[pyfunction]
fn nabla_e_by_path(n: u32) -> PyResult<Vec<(Key, QtMon)>> {
interruptible(move || {
cells_arg(n as usize, &format!("a degree-{n} path tuple"))?;
Ok(crate::llt::nabla_e_by_path::<i128>(n)
.iter()
.map(|(area, g)| (area.clone().into(), qt_mon_out(g)))
.collect())
})
}
#[pyfunction]
#[pyo3(signature = (la, k))]
fn llt_kl_column(la: Vec<u32>, k: u32) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>)>> {
interruptible(move || {
let (l, k) = (part_arg(&la)?, level_arg(k)?);
abacus_arg(&l, k)?;
Ok(crate::llt::llt_kl_column::<i128>(&l, k)
.iter()
.map(|(mu, c)| (mu.parts().to_vec().into(), qt_poly(c)))
.collect())
})
}
#[pyfunction]
#[pyo3(signature = (n, weak, strict))]
fn llt_graph(n: u32, weak: Vec<(u32, u32)>, strict: Vec<(u32, u32)>) -> PyResult<QtMon> {
interruptible(move || {
Ok(qt_mon_out(&crate::llt::llt_graph::<i128>(
&decorated_graph(n, weak, strict)?,
)))
})
}
#[pyfunction]
#[pyo3(signature = (n, weak, strict))]
fn chromatic_from_llt(n: u32, weak: Vec<(u32, u32)>, strict: Vec<(u32, u32)>) -> PyResult<QtMon> {
interruptible(move || {
let g = decorated_graph(n, weak, strict)?;
let f = crate::llt::chromatic_from_llt::<crate::Rational>(&g);
let mut out = QtMon::new();
for (mu, c) in f.terms() {
let mut row = Vec::with_capacity(c.len());
for (&(a, b), v) in c.terms() {
if v.denom() != 1 {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"the chromatic function is not integral at {mu}: \
coefficient of q^{a}t^{b} is {v:?}"
)));
}
row.push((a, b, Coeff::Small(v.numer())));
}
out.push((mu.parts().to_vec().into(), row));
}
Ok(out)
})
}
#[pyfunction]
#[pyo3(signature = (n, weak, strict))]
fn llt_e_expansion(
n: u32,
weak: Vec<(u32, u32)>,
strict: Vec<(u32, u32)>,
) -> PyResult<Vec<(Key, Vec<(u32, u32, Coeff)>)>> {
interruptible(move || {
let g = decorated_graph(n, weak, strict)?;
let free = crate::llt::free_edges(&g).len();
if free >= crate::llt::MAX_FREE_EDGES {
return Err(PyValueError::new_err(format!(
"the orientation sum over {free} free edges is 2^{free} terms, \
past the {} this mask holds",
crate::llt::MAX_FREE_EDGES
)));
}
Ok(crate::llt::llt_e_expansion::<i128>(&g)
.iter()
.map(|(lambda, c)| (lambda.parts().to_vec().into(), qt_poly(c)))
.collect())
})
}
#[pyfunction]
fn htilde_by_llt(mu: Vec<u32>) -> PyResult<QtMon> {
interruptible(move || {
let m = part_arg(&mu)?;
cells_arg(m.size() as usize, &format!("the shape {m}"))?;
Ok(qt_mon_out(&crate::llt::htilde_by_llt::<i128>(&m)))
})
}
const WHEEL_CACHE_BUDGET: Option<usize> = Some(1 << 30);
fn initial_cache_budget() -> PyResult<Option<usize>> {
match std::env::var("SYMFN_CACHE_BUDGET") {
Err(_) => Ok(WHEEL_CACHE_BUDGET),
Ok(v) => match v.trim().parse::<usize>() {
Ok(0) => Ok(None),
Ok(b) => Ok(Some(b)),
Err(_) => Err(pyo3::exceptions::PyValueError::new_err(format!(
"SYMFN_CACHE_BUDGET must be a byte count (0 for unbounded), not {v:?}"
))),
},
}
}
#[pymodule]
fn symfn(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
crate::interrupt::set_checker(check_python_signals);
install_quiet_cancellation_hook();
crate::memo::set_cache_budget(initial_cache_budget()?);
m.add_function(wrap_pyfunction!(hall_littlewood, m)?)?;
m.add_function(wrap_pyfunction!(hall_littlewood_table, m)?)?;
m.add_function(wrap_pyfunction!(kostka_foulkes, m)?)?;
m.add_function(wrap_pyfunction!(kostka_foulkes_column, m)?)?;
m.add_function(wrap_pyfunction!(kostka_foulkes_table, m)?)?;
m.add_function(wrap_pyfunction!(hall_littlewood_p, m)?)?;
m.add_function(wrap_pyfunction!(hall_littlewood_p_table, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_hall_littlewood_p, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_hall_littlewood_qp, m)?)?;
m.add_function(wrap_pyfunction!(hall_littlewood_p_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(hall_littlewood_qp_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_p, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_q, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_j, m)?)?;
m.add_function(wrap_pyfunction!(schur_in_macdonald_j, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_macdonald_j, m)?)?;
m.add_function(wrap_pyfunction!(monomial_to_macdonald_p, m)?)?;
m.add_function(wrap_pyfunction!(monomial_to_macdonald_q, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_p_to_monomial, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_q_to_monomial, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_j_to_monomial, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_element_add, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_element_scale, m)?)?;
m.add_function(wrap_pyfunction!(jack_p, m)?)?;
m.add_function(wrap_pyfunction!(jack_q, m)?)?;
m.add_function(wrap_pyfunction!(jack_j, m)?)?;
m.add_function(wrap_pyfunction!(monomial_to_jack_p, m)?)?;
m.add_function(wrap_pyfunction!(monomial_to_jack_q, m)?)?;
m.add_function(wrap_pyfunction!(monomial_to_jack_j, m)?)?;
m.add_function(wrap_pyfunction!(jack_p_to_monomial, m)?)?;
m.add_function(wrap_pyfunction!(jack_q_to_monomial, m)?)?;
m.add_function(wrap_pyfunction!(jack_j_to_monomial, m)?)?;
m.add_function(wrap_pyfunction!(jack_element_add, m)?)?;
m.add_function(wrap_pyfunction!(jack_element_scale, m)?)?;
m.add_function(wrap_pyfunction!(jack_table, m)?)?;
m.add_function(wrap_pyfunction!(jack_j_powersum, m)?)?;
m.add_function(wrap_pyfunction!(jack_norm_j, m)?)?;
m.add_function(wrap_pyfunction!(jack_scalar, m)?)?;
m.add_function(wrap_pyfunction!(jack_structure_constant, m)?)?;
m.add_function(wrap_pyfunction!(stanley_table, m)?)?;
m.add_function(wrap_pyfunction!(zonal, m)?)?;
m.add_function(wrap_pyfunction!(gj_connection_tables, m)?)?;
m.add_function(wrap_pyfunction!(class_algebra_coefficient, m)?)?;
m.add_function(wrap_pyfunction!(qt_kostka, m)?)?;
m.add_function(wrap_pyfunction!(qt_kostka_column, m)?)?;
m.add_function(wrap_pyfunction!(qt_kostka_table, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_ht, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_macdonald_ht, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_ht_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_ht_element_add, m)?)?;
m.add_function(wrap_pyfunction!(macdonald_ht_element_scale, m)?)?;
m.add_function(wrap_pyfunction!(nabla_e, m)?)?;
m.add_function(wrap_pyfunction!(delta_prime_e, m)?)?;
m.add_function(wrap_pyfunction!(nabla, m)?)?;
m.add_function(wrap_pyfunction!(nabla_power, m)?)?;
m.add_function(wrap_pyfunction!(delta_ek, m)?)?;
m.add_function(wrap_pyfunction!(delta_prime_ek, m)?)?;
m.add_function(wrap_pyfunction!(theta_ek, m)?)?;
m.add_function(wrap_pyfunction!(big_pi, m)?)?;
m.add_function(wrap_pyfunction!(delta_conjecture_side, m)?)?;
m.add_function(wrap_pyfunction!(llt_gtilde, m)?)?;
m.add_function(wrap_pyfunction!(llt_h, m)?)?;
m.add_function(wrap_pyfunction!(llt_h_tilde, m)?)?;
m.add_function(wrap_pyfunction!(llt_g_lt, m)?)?;
m.add_function(wrap_pyfunction!(llt_h_table, m)?)?;
m.add_function(wrap_pyfunction!(monomial_in_llt_h_table, m)?)?;
m.add_function(wrap_pyfunction!(monomial_in_llt_h_tilde_table, m)?)?;
m.add_function(wrap_pyfunction!(llt_gtilde_table, m)?)?;
m.add_function(wrap_pyfunction!(llt_schur, m)?)?;
m.add_function(wrap_pyfunction!(llt_g, m)?)?;
m.add_function(wrap_pyfunction!(llt_min_inv, m)?)?;
m.add_function(wrap_pyfunction!(llt_fundamental, m)?)?;
m.add_function(wrap_pyfunction!(llt_kl_column, m)?)?;
m.add_function(wrap_pyfunction!(llt_graph, m)?)?;
m.add_function(wrap_pyfunction!(chromatic_from_llt, m)?)?;
m.add_function(wrap_pyfunction!(llt_e_expansion, m)?)?;
m.add_function(wrap_pyfunction!(htilde_by_llt, m)?)?;
m.add_function(wrap_pyfunction!(nabla_e_by_path, m)?)?;
m.add_function(wrap_pyfunction!(k_core_quotient, m)?)?;
m.add_function(wrap_pyfunction!(clear_caches, m)?)?;
m.add_function(wrap_pyfunction!(cache_stats, m)?)?;
m.add_function(wrap_pyfunction!(cache_budget, m)?)?;
m.add_function(wrap_pyfunction!(set_cache_budget, m)?)?;
m.add_function(wrap_pyfunction!(schur_multiply, m)?)?;
m.add_function(wrap_pyfunction!(st_multiply, m)?)?;
m.add_function(wrap_pyfunction!(reduced_kronecker_product, m)?)?;
m.add_function(wrap_pyfunction!(reduced_kronecker, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_st, m)?)?;
m.add_function(wrap_pyfunction!(st_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(ht_multiply, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_ht, m)?)?;
m.add_function(wrap_pyfunction!(ht_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(lr_coefficient, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_homogeneous, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_elementary, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_monomial, m)?)?;
m.add_function(wrap_pyfunction!(schur_to_forgotten, m)?)?;
m.add_function(wrap_pyfunction!(to_power, m)?)?;
m.add_function(wrap_pyfunction!(homogeneous_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(elementary_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(monomial_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(forgotten_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(power_to_schur, m)?)?;
m.add_function(wrap_pyfunction!(plethysm, m)?)?;
m.add_function(wrap_pyfunction!(kostka_number, m)?)?;
m.add_function(wrap_pyfunction!(evaluate_schur, m)?)?;
m.add_function(wrap_pyfunction!(expand_alphabet, m)?)?;
m.add_function(wrap_pyfunction!(monomial_multiply, m)?)?;
m.add_function(wrap_pyfunction!(semistandard_tableaux, m)?)?;
m.add_function(wrap_pyfunction!(dimension, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_q, m)?)?;
m.add_function(wrap_pyfunction!(character_value, m)?)?;
m.add_function(wrap_pyfunction!(internal_product, m)?)?;
m.add_function(wrap_pyfunction!(partitions, m)?)?;
m.add_function(wrap_pyfunction!(kronecker_coefficient, m)?)?;
m.add_function(wrap_pyfunction!(convert_indexed, m)?)?;
m.add_function(wrap_pyfunction!(convert_terms, m)?)?;
m.add_function(wrap_pyfunction!(convert_qt_terms, m)?)?;
m.add_function(wrap_pyfunction!(schur_multiply_qt, m)?)?;
m.add_function(wrap_pyfunction!(schur_multiply_ht, m)?)?;
m.add_function(wrap_pyfunction!(schur_multiply_jack, m)?)?;
m.add_function(wrap_pyfunction!(schur_multiply_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(omega_qt_terms, m)?)?;
m.add_function(wrap_pyfunction!(antipode_qt_terms, m)?)?;
m.add_function(wrap_pyfunction!(omega_macdonald_terms, m)?)?;
m.add_function(wrap_pyfunction!(antipode_macdonald_terms, m)?)?;
m.add_function(wrap_pyfunction!(omega_jack_terms, m)?)?;
m.add_function(wrap_pyfunction!(antipode_jack_terms, m)?)?;
m.add_function(wrap_pyfunction!(omega_ht_terms, m)?)?;
m.add_function(wrap_pyfunction!(antipode_ht_terms, m)?)?;
m.add_function(wrap_pyfunction!(skew_by_qt, m)?)?;
m.add_function(wrap_pyfunction!(skew_by_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(skew_by_jack, m)?)?;
m.add_function(wrap_pyfunction!(skew_by_ht, m)?)?;
m.add_function(wrap_pyfunction!(hall_inner_product_qt, m)?)?;
m.add_function(wrap_pyfunction!(hall_inner_product_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(hall_inner_product_jack, m)?)?;
m.add_function(wrap_pyfunction!(hall_inner_product_ht, m)?)?;
m.add_function(wrap_pyfunction!(scalar_t, m)?)?;
m.add_function(wrap_pyfunction!(scalar_qt, m)?)?;
m.add_function(wrap_pyfunction!(scalar_qt_ht, m)?)?;
m.add_function(wrap_pyfunction!(to_power_qt, m)?)?;
m.add_function(wrap_pyfunction!(to_power_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(to_power_jack, m)?)?;
m.add_function(wrap_pyfunction!(to_power_ht, m)?)?;
m.add_function(wrap_pyfunction!(coproduct_qt, m)?)?;
m.add_function(wrap_pyfunction!(coproduct_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(coproduct_jack, m)?)?;
m.add_function(wrap_pyfunction!(coproduct_ht, m)?)?;
m.add_function(wrap_pyfunction!(expand_qt, m)?)?;
m.add_function(wrap_pyfunction!(expand_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(expand_jack, m)?)?;
m.add_function(wrap_pyfunction!(expand_ht, m)?)?;
m.add_function(wrap_pyfunction!(evaluate_qt, m)?)?;
m.add_function(wrap_pyfunction!(evaluate_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(evaluate_jack, m)?)?;
m.add_function(wrap_pyfunction!(evaluate_ht, m)?)?;
m.add_function(wrap_pyfunction!(dimension_qt, m)?)?;
m.add_function(wrap_pyfunction!(dimension_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(dimension_jack, m)?)?;
m.add_function(wrap_pyfunction!(dimension_ht, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_qt, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_jack, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_ht, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_q_qt, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_at_qt, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_at_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_at_jack, m)?)?;
m.add_function(wrap_pyfunction!(principal_specialization_at_ht, m)?)?;
m.add_function(wrap_pyfunction!(internal_product_qt, m)?)?;
m.add_function(wrap_pyfunction!(internal_product_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(internal_product_jack, m)?)?;
m.add_function(wrap_pyfunction!(internal_product_ht, m)?)?;
m.add_function(wrap_pyfunction!(plethysm_qt, m)?)?;
m.add_function(wrap_pyfunction!(plethysm_macdonald, m)?)?;
m.add_function(wrap_pyfunction!(plethysm_jack, m)?)?;
m.add_function(wrap_pyfunction!(plethysm_ht, m)?)?;
m.add_function(wrap_pyfunction!(convert_macdonald_terms, m)?)?;
m.add_function(wrap_pyfunction!(convert_jack_terms, m)?)?;
m.add_function(wrap_pyfunction!(convert_ht_terms, m)?)?;
m.add_function(wrap_pyfunction!(character_table, m)?)?;
m.add_function(wrap_pyfunction!(kostka_table, m)?)?;
m.add_function(wrap_pyfunction!(omega, m)?)?;
m.add_function(wrap_pyfunction!(hall_inner_product, m)?)?;
m.add_function(wrap_pyfunction!(skew_schur, m)?)?;
m.add_function(wrap_pyfunction!(skew_by, m)?)?;
m.add_function(wrap_pyfunction!(coproduct, m)?)?;
m.add_function(wrap_pyfunction!(antipode, m)?)?;
m.add_function(wrap_pyfunction!(schubert_multiply, m)?)?;
m.add_function(wrap_pyfunction!(schubert_multiply_variable, m)?)?;
m.add_function(wrap_pyfunction!(schubert_divided_difference, m)?)?;
m.add_function(wrap_pyfunction!(schubert_divided_difference_perm, m)?)?;
m.add_function(wrap_pyfunction!(schubert_expand, m)?)?;
m.add_function(wrap_pyfunction!(polynomial_to_schubert, m)?)?;
m.add_function(wrap_pyfunction!(schubert_pairing, m)?)?;
m.add_function(wrap_pyfunction!(schubert_scalar_product, m)?)?;
m.add_function(wrap_pyfunction!(schubert_dimension, m)?)?;
m.add_function(wrap_pyfunction!(schubert_coefficient, m)?)?;
m.add_function(wrap_pyfunction!(schubert_monomial_mass, m)?)?;
m.add_function(wrap_pyfunction!(schubert_to_stanley_schur, m)?)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn llt_h_below_the_wall_is_unchanged_and_stays_narrow() {
let got = llt_h(vec![1; 8], 3).expect("(1^8) at level 3 is a valid abacus argument");
let want = qt_mon_out(&crate::llt::llt_h::<BigInt>(&Partition::new([1; 8]), 3));
assert_eq!(got.len(), want.len());
assert!(
got.iter()
.flat_map(|(_, p)| p.iter())
.all(|(_, _, c)| matches!(c, Coeff::Small(_))),
"below the wall nothing should have escalated"
);
}
}