use num_bigint::BigInt;
use num_complex::Complex64;
use crate::core::EvalResult;
#[derive(Debug, Clone, PartialEq)]
pub struct Matrix {
rows: Vec<Vec<f64>>,
}
impl Matrix {
pub fn from_rows(rows: &[&[f64]]) -> Self {
Self {
rows: rows.iter().map(|r| r.to_vec()).collect(),
}
}
pub fn nrows(&self) -> usize {
self.rows.len()
}
pub fn ncols(&self) -> usize {
self.rows.first().map_or(0, |r| r.len())
}
pub fn as_rows(&self) -> &[Vec<f64>] {
&self.rows
}
}
impl From<Matrix> for EvalResult {
fn from(m: Matrix) -> Self {
EvalResult::Matrix(m.rows)
}
}
impl TryFrom<EvalResult> for Matrix {
type Error = &'static str;
fn try_from(result: EvalResult) -> Result<Self, Self::Error> {
match result {
EvalResult::Matrix(rows) => Ok(Matrix { rows }),
_ => Err("expected Matrix result"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Vector {
data: Vec<f64>,
}
impl Vector {
pub fn new(data: &[f64]) -> Self {
Self {
data: data.to_vec(),
}
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn as_slice(&self) -> &[f64] {
&self.data
}
}
impl From<Vector> for EvalResult {
fn from(v: Vector) -> Self {
EvalResult::Vector(v.data)
}
}
impl TryFrom<EvalResult> for Vector {
type Error = &'static str;
fn try_from(result: EvalResult) -> Result<Self, Self::Error> {
match result {
EvalResult::Vector(data) => Ok(Vector { data }),
_ => Err("expected Vector result"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Complex {
value: Complex64,
}
impl Complex {
pub fn new(re: f64, im: f64) -> Self {
Self {
value: Complex64::new(re, im),
}
}
pub fn re(&self) -> f64 {
self.value.re
}
pub fn im(&self) -> f64 {
self.value.im
}
pub fn value(&self) -> Complex64 {
self.value
}
}
impl From<Complex> for EvalResult {
fn from(c: Complex) -> Self {
EvalResult::Complex(c.re(), c.im())
}
}
impl TryFrom<EvalResult> for Complex {
type Error = &'static str;
fn try_from(result: EvalResult) -> Result<Self, Self::Error> {
match result {
EvalResult::Complex(re, im) => Ok(Complex::new(re, im)),
_ => Err("expected Complex result"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Polynomial {
coeffs: Vec<f64>,
}
impl Polynomial {
pub fn new(coeffs: &[f64]) -> Self {
Self {
coeffs: coeffs.to_vec(),
}
}
pub fn coeffs(&self) -> &[f64] {
&self.coeffs
}
pub fn degree(&self) -> usize {
self.coeffs.len().saturating_sub(1)
}
}
impl From<Polynomial> for EvalResult {
fn from(p: Polynomial) -> Self {
EvalResult::Polynomial(p.coeffs)
}
}
impl TryFrom<EvalResult> for Polynomial {
type Error = &'static str;
fn try_from(result: EvalResult) -> Result<Self, Self::Error> {
match result {
EvalResult::Polynomial(coeffs) => Ok(Polynomial { coeffs }),
_ => Err("expected Polynomial result"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigNumber {
value: BigInt,
}
impl BigNumber {
pub fn new(value: BigInt) -> Self {
Self { value }
}
pub fn from_i64(v: i64) -> Self {
Self {
value: BigInt::from(v),
}
}
pub fn value(&self) -> &BigInt {
&self.value
}
}
impl From<BigNumber> for EvalResult {
fn from(b: BigNumber) -> Self {
EvalResult::BigInt(b.value)
}
}
impl TryFrom<EvalResult> for BigNumber {
type Error = &'static str;
fn try_from(result: EvalResult) -> Result<Self, Self::Error> {
match result {
EvalResult::BigInt(value) => Ok(BigNumber { value }),
_ => Err("expected BigInt result"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_matrix_from_rows() {
let m = Matrix::from_rows(&[&[1.0, 2.0], &[3.0, 4.0]]);
assert_eq!(m.nrows(), 2);
assert_eq!(m.ncols(), 2);
}
#[test]
fn test_matrix_roundtrip() {
let m = Matrix::from_rows(&[&[1.0, 2.0], &[3.0, 4.0]]);
let result: EvalResult = m.clone().into();
let m2 = Matrix::try_from(result).unwrap();
assert_eq!(m, m2);
}
#[test]
fn test_matrix_try_from_wrong_variant() {
let result = EvalResult::Scalar(1.0);
assert!(Matrix::try_from(result).is_err());
}
#[test]
fn test_vector_new() {
let v = Vector::new(&[1.0, 2.0, 3.0]);
assert_eq!(v.len(), 3);
assert!(!v.is_empty());
}
#[test]
fn test_vector_roundtrip() {
let v = Vector::new(&[1.0, 2.0, 3.0]);
let result: EvalResult = v.clone().into();
let v2 = Vector::try_from(result).unwrap();
assert_eq!(v, v2);
}
#[test]
fn test_complex_new() {
let c = Complex::new(3.0, 4.0);
assert_eq!(c.re(), 3.0);
assert_eq!(c.im(), 4.0);
}
#[test]
fn test_complex_roundtrip() {
let c = Complex::new(3.0, 4.0);
let result: EvalResult = c.into();
let c2 = Complex::try_from(result).unwrap();
assert_eq!(c, c2);
}
#[test]
fn test_polynomial_new() {
let p = Polynomial::new(&[1.0, 2.0, 3.0]);
assert_eq!(p.degree(), 2);
assert_eq!(p.coeffs(), &[1.0, 2.0, 3.0]);
}
#[test]
fn test_polynomial_roundtrip() {
let p = Polynomial::new(&[1.0, 2.0, 3.0]);
let result: EvalResult = p.clone().into();
let p2 = Polynomial::try_from(result).unwrap();
assert_eq!(p, p2);
}
#[test]
fn test_bignumber_from_i64() {
let b = BigNumber::from_i64(42);
assert_eq!(b.value(), &BigInt::from(42));
}
#[test]
fn test_bignumber_roundtrip() {
let b = BigNumber::from_i64(42);
let result: EvalResult = b.clone().into();
let b2 = BigNumber::try_from(result).unwrap();
assert_eq!(b, b2);
}
#[test]
fn test_bignumber_try_from_wrong_variant() {
let result = EvalResult::Scalar(1.0);
assert!(BigNumber::try_from(result).is_err());
}
}