use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::Zero;
use super::check_players;
use super::coalition::{lexicographic_order, players_from_len};
use crate::error::{Error, Result};
use crate::game::{Coalition, ExplicitGame};
pub type Rational = BigRational;
pub(crate) fn to_rational(value: f64) -> Result<Rational> {
Rational::from_float(value)
.ok_or_else(|| Error::InvalidArgument(format!("{value} は有限の数ではない")))
}
pub const MAX_DECIMAL_EXPONENT: i32 = 4096;
pub fn parse_rational(text: &str) -> Result<Rational> {
let text = text.trim();
let bad = || Error::InvalidArgument(format!("有理数として読めない: {text:?}"));
if let Some((numerator, denominator)) = text.split_once('/') {
let numerator: BigInt = numerator.trim().parse().map_err(|_| bad())?;
let denominator: BigInt = denominator.trim().parse().map_err(|_| bad())?;
if denominator.is_zero() {
return Err(bad());
}
return Ok(Rational::new(numerator, denominator));
}
let (mantissa, exponent) = match text.split_once(['e', 'E']) {
Some((m, e)) => (m, e.parse::<i32>().map_err(|_| bad())?),
None => (text, 0),
};
let (integer, fraction) = mantissa.split_once('.').unwrap_or((mantissa, ""));
let digits: BigInt = format!("{integer}{fraction}").parse().map_err(|_| bad())?;
if exponent.unsigned_abs() > MAX_DECIMAL_EXPONENT.unsigned_abs() {
return Err(Error::InvalidArgument(format!(
"有理数として読めない: {text:?} (指数の絶対値が上限 {MAX_DECIMAL_EXPONENT} を超える)"
)));
}
let shift = i64::from(exponent) - i64::try_from(fraction.len()).map_err(|_| bad())?;
let magnitude = usize::try_from(shift.unsigned_abs()).map_err(|_| bad())?;
let power = num_traits::pow(BigInt::from(10), magnitude);
Ok(if shift >= 0 {
Rational::from_integer(digits * power)
} else {
Rational::new(digits, power)
})
}
pub fn parse_rational_values(text: &str) -> Result<Vec<Rational>> {
text.lines()
.enumerate()
.map(|(index, line)| (index, line.trim()))
.filter(|(_, line)| !line.is_empty() && !line.starts_with('#'))
.map(|(index, line)| {
parse_rational(line).map_err(|err| Error::Parse {
line: index + 1,
message: err.to_string(),
})
})
.collect()
}
pub fn format_rational(value: &Rational) -> String {
if value.is_integer() {
value.numer().to_string()
} else {
format!("{}/{}", value.numer(), value.denom())
}
}
#[derive(Clone, Debug)]
pub struct ExactGame {
players: usize,
values: Vec<Rational>,
}
impl ExactGame {
pub fn from_explicit(game: &ExplicitGame) -> Result<ExactGame> {
let values = game
.values()
.iter()
.map(|v| to_rational(*v))
.collect::<Result<_>>()?;
Ok(ExactGame {
players: game.players(),
values,
})
}
pub fn from_binary(values: Vec<Rational>) -> Result<ExactGame> {
let players = players_from_len(values.len())?;
check_players(players)?;
let mut all = Vec::with_capacity(values.len() + 1);
all.push(Rational::zero());
all.extend(values);
Ok(ExactGame {
players,
values: all,
})
}
pub fn from_lex(values: Vec<Rational>) -> Result<ExactGame> {
let n = players_from_len(values.len())?;
let mut binary = vec![Rational::zero(); values.len()];
for (value, coalition) in values.into_iter().zip(lexicographic_order(n)) {
binary[coalition.index() - 1] = value;
}
ExactGame::from_binary(binary)
}
pub fn players(&self) -> usize {
self.players
}
pub fn value(&self, coalition: Coalition) -> &Rational {
&self.values[coalition.index()]
}
pub fn values(&self) -> &[Rational] {
&self.values
}
pub fn to_explicit(&self) -> Result<ExplicitGame> {
ExplicitGame::from_binary(&to_f64(&self.values[1..]))
}
}
pub(crate) fn to_f64(values: &[Rational]) -> Vec<f64> {
use num_traits::ToPrimitive;
values
.iter()
.map(|v| v.to_f64().unwrap_or(f64::NAN))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn r(numerator: i64, denominator: i64) -> Rational {
Rational::new(numerator.into(), denominator.into())
}
#[test]
fn parses_and_formats_rationals() {
assert_eq!(parse_rational("8/3").unwrap(), r(8, 3));
assert_eq!(parse_rational("-4").unwrap(), r(-4, 1));
assert_eq!(parse_rational("2.5").unwrap(), r(5, 2));
assert_eq!(parse_rational("1.25e1").unwrap(), r(25, 2));
assert_eq!(format_rational(&r(16, 6)), "8/3");
assert_eq!(format_rational(&r(-4, 1)), "-4");
}
}