#![allow(dead_code)] #![allow(unused_imports)] mod common; use wmath::*; #[test] fn test_polynomial() { let coe = vec![1i128, 2, 3]; let poly = Polynomial::new(&coe); println!("{:?}", poly); print_expr!(poly); print_expr!(poly.latex()); print_expr!(poly.neg()); print_expr!(poly == Polynomial::new(&vec![1, 2, 3])); print_expr!(poly == Polynomial::new(&vec![1, 2, 4])); print_expr!(Polynomial::new(&vec![1.000000000001, 2.0, 3.0]) == Polynomial::new(&vec![1.0, 2.0, 3.0])); print_expr!(Polynomial::new(&vec![1.000000000001, 2.0, 3.0]).similar(&Polynomial::new(&vec![1.0, 2.0, 3.0]), 1e-8)); let poly1 = Polynomial::new(&vec![1i128, 2, 3]); print_expr!(poly1.pow(4)); let poly2 = Polynomial::new(&vec![1f64, 2.0, 3.0]); print_expr!(poly2.pow(4)); let poly1 = Polynomial::new(&vec![2.0, -9.2, 1.0]); let poly2 = Polynomial::new(&vec![9.8, -0.8, 3.8, -8.7, 0.0]); print_expr!(poly1.clone() + poly2.clone()); print_expr!(poly1.clone() - poly2.clone()); print_expr!(poly1.clone() * poly2.clone()); let (quotient, remainder) = poly2.clone() / poly1.clone(); let delta = poly1.clone() * quotient.clone() + remainder.clone() - poly2.clone(); print_expr!(delta, delta.similar_zero(1e-8)); let (quotient, remainder) = poly1.clone() / poly2.clone(); let delta = poly2.clone() * quotient.clone() + remainder.clone() - poly1.clone(); print_expr!(delta, delta.similar_zero(1e-8)); print_expr!(poly2.value(9.4)); print_expr!(poly2.value_left(9.4)); let poly = poly!(&vec![frac!(4, 6), frac!(9, 2), frac!(1, 8), frac!(0, 1)]); print_expr!(poly); print_expr!(poly.adjust()); print_expr!(poly.primitive()); let poly1 = poly!(&vec![1i128, 2]); let poly2 = poly!(&vec![5i128, 1]); let poly3 = poly!(&vec![4i128, 1]); let poly4 = poly!(&vec![3i128, 1]); let poly5 = poly!(&vec![2i128, 7]); let poly6 = poly!(&vec![0i128, 4]); let poly7 = poly!(&vec![4i128, 0]); let poly8 = poly!(&vec![7i128, 2]); let poly = poly1 * poly2 * poly3 * poly4 * poly5 * poly6 * poly7 * poly8; print_expr!(poly); println!("{:?}", poly.rational_roots()); let poly = poly!(&vec![3.4, -9.2, 9.4, -0.9, 1.8, 3.9, 0.0, 9.4, 2.9, 0.9, 1.2, -9.8, 0.0]); let vec = poly.real_roots(1e-8, 0.0); for i in vec.iter() { println!("{} => {}", i, poly.value(*i)); } let poly1 = poly!(&vec![3.0, 2.0, 9.0, -12.0, 8.0]); let poly2 = poly!(&vec![9.0, -2.0, 3.0, 1.0]); let poly3 = poly!(&vec![8.0, -2.0, 9.0]); let poly4 = poly1.clone() * poly2.clone(); let poly5 = poly1.clone() * poly3.clone(); let poly6 = poly4.similar_gcd(&poly5, 1e-10); print_expr!(poly6.similar_gcd(&poly1, 1e-10) == poly1); let (poly6, poly7, poly8) = poly4.similar_gcde(&poly5, 1e-10); print_expr!(poly6.similar_gcd(&poly1, 1e-10) == poly1); print_expr!((poly7 * poly4 + poly8 * poly5 - poly6).similar_zero(1e-10)); fn vec2frac(vec: Vec) -> Vec { vec.iter().map(|x| Fraction::from(*x)).collect() } let poly1 = poly!(&vec2frac(vec![3, 2, 9, -12, 8])); let poly2 = poly!(&vec2frac(vec![9, -2, 3, 1])); let poly3 = poly!(&vec2frac(vec![8, -2, 9])); let poly4 = poly1.clone() * poly2.clone(); let poly5 = poly1.clone() * poly3.clone(); let poly6 = poly4.gcd(&poly5); print_expr!(poly6.primitive()); print_expr!(poly6.gcd(&poly1) == poly1); let (poly6, poly7, poly8) = poly4.gcde(&poly5); print_expr!(poly6.gcd(&poly1) == poly1); print_expr!((poly7 * poly4 + poly8 * poly5 - poly6).eq_zero()); let poly1 = poly_from!(mat!(&vec![1.217333113417948, 0.26998337953114326, -0.8611873636503731, 0.2811466681445065], 2, 2), mat!(&vec![3.272064686686692, -4.604100407322709, -4.2197880812006066, -4.354597117121138], 2, 2), mat!(&vec![2.7764964649014043, 2.7788609367427295, 0.7194332588776877, -3.43018652645926], 2, 2), mat!(&vec![-0.9911457088294728, -4.158906605476865, -0.24911330335887172, -4.691242732779607], 2, 2), mat!(&vec![3.09521213357063, 0.9088259241921701, -3.24994747251231, 3.3988130324279364], 2, 2), mat!(&vec![-0.4373556723401606, 4.497003897250629, 1.1461488186517244, -3.3024332541505896], 2, 2), mat!(&vec![3.2800020950932716, 0.5002358684874526, -1.6471216137399103, -3.9164457759208062], 2, 2), mat!(&vec![3.307315585143371, 4.622287148377358, -0.016403323836067685, -2.891686771943477], 2, 2), mat!(&vec![3.1341740602098547, 3.3570554859784316, -3.013272946378378, 2.570361470532262], 2, 2)); let poly2 = poly_from!(mat!(&vec![-3.7287278843007354, -3.9850435502415515, 0.6523183316121095, -0.7963321860330197], 2, 2), mat!(&vec![2.166904153348776, -4.2207165225532535, -1.38170735586725, -4.845749895691194], 2, 2), mat!(&vec![-3.543670330112838, -3.059109353057221, 4.441167967534188, 2.867567761041675], 2, 2), mat!(&vec![1.13807076197668, -2.794044062273252, -2.9589529723790333, -1.0426299694423022], 2, 2), mat!(&vec![-3.857275257785906, -2.516008679797599, 4.869913477610815, 3.9834832963249163], 2, 2), mat!(&vec![4.073777241172392, 4.675037364151384, 0.8779181248387484, 0.41734848376834854], 2, 2), mat!(&vec![4.994369038174341, 3.4118584229944435, 3.9281372564853747, -4.188394275565615], 2, 2), mat!(&vec![-1.7910193200861881, 4.2641465638473, 3.8498370944491693, -4.565355784092205], 2, 2), mat!(&vec![-1.6058450426912096, 4.889840799240323, 3.9353874659731556, 4.6925809125859725], 2, 2)); print_expr!((poly1.clone() * poly2.clone()).latex()); // let (poly3, poly4) = poly1.clone() / poly2.clone(); // print_expr!(poly3, poly4); // print_expr!(poly1.pow(1000).latex()); let poly3 = poly1.multiply_by(mat!(&vec![1.0, 2.0, 3.0, 8.0], 2, 2), 0); let poly4 = poly1.multiply_left_by(mat!(&vec![1.0, 2.0, 3.0, 8.0], 2, 2), 0); let poly5 = poly1.divide_by(mat!(&vec![1.0, 2.0, 3.0, 8.0], 2, 2)); let poly6 = poly1.divide_left_by(mat!(&vec![1.0, 2.0, 3.0, 8.0], 2, 2)); print_expr!(poly3, poly4, poly5, poly6); } fn main() {}