pub(crate) mod simplex;
use num_traits::{One, Zero};
use crate::game::Coalition;
use crate::game::exact::Rational;
#[derive(Clone, Debug, Default)]
pub(crate) struct ExactEchelon {
pub(crate) rows: Vec<(usize, Vec<Rational>)>,
}
pub(crate) enum Inserted {
Independent,
Dependent,
Inconsistent,
}
impl ExactEchelon {
pub(crate) fn reduce(&self, mut row: Vec<Rational>) -> Vec<Rational> {
for (pivot, existing) in &self.rows {
if !row[*pivot].is_zero() {
let factor = row[*pivot].clone();
for (a, e) in row.iter_mut().zip(existing) {
*a -= &factor * e;
}
}
}
row
}
pub(crate) fn insert(&mut self, row: Vec<Rational>, coefficients: usize) -> Inserted {
let reduced = self.reduce(row);
let Some(pivot) = (0..coefficients).find(|&j| !reduced[j].is_zero()) else {
return if reduced[coefficients..].iter().all(Zero::is_zero) {
Inserted::Dependent
} else {
Inserted::Inconsistent
};
};
let scale = reduced[pivot].clone();
let normalized: Vec<Rational> = reduced.iter().map(|a| a / &scale).collect();
for (_, existing) in &mut self.rows {
if !existing[pivot].is_zero() {
let factor = existing[pivot].clone();
for (e, a) in existing.iter_mut().zip(&normalized) {
*e -= &factor * a;
}
}
}
self.rows.push((pivot, normalized));
Inserted::Independent
}
pub(crate) fn rank(&self) -> usize {
self.rows.len()
}
}
pub(crate) fn indicator(coalition: Coalition, n: usize) -> Vec<Rational> {
(0..n)
.map(|i| {
if coalition.contains(i) {
Rational::one()
} else {
Rational::zero()
}
})
.collect()
}