You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
7.3 KiB
221 lines
7.3 KiB
//
|
|
// Test our fractional crate / module...
|
|
//
|
|
// Georg Hopp <georg@steffers.org>
|
|
//
|
|
// Copyright © 2019 Georg Hopp
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
//
|
|
use std::convert::{TryFrom, TryInto, Into};
|
|
use std::num::TryFromIntError;
|
|
use std::f64::consts::PI as FPI;
|
|
|
|
use fractional::fractional::{Fractional, from_vector, Continuous};
|
|
use fractional::trigonometry::{sin, cos, tan, PI};
|
|
use fractional::vector::{Vector};
|
|
use fractional::transform::{translate, rotate_x, rotate_y, rotate_z, rotate_v};
|
|
|
|
fn mean(v: &Vec<i64>) -> Result<Fractional, TryFromIntError> {
|
|
let r = v.iter().fold(0, |acc, x| acc + x);
|
|
let l = i64::try_from(v.len())?;
|
|
Ok(Fractional(r, l))
|
|
}
|
|
|
|
fn common_fractional() {
|
|
let a = vec![3, 6, 1, 9];
|
|
let b = from_vector(&a);
|
|
let c = mean(&a).unwrap(); // This might fail if the len of the
|
|
// vector (usize) does not fit into i32.
|
|
let cr :f64 = c.try_into().unwrap();
|
|
|
|
println!(" [i32] : {:?}", a);
|
|
println!(" [Fractional] : {:?}", b);
|
|
println!(" mean of [i32] : {}" , c);
|
|
println!(" as f64 : {}" , cr);
|
|
println!(" again as f64 : {}" , TryInto::<f64>::try_into(c).unwrap());
|
|
}
|
|
|
|
fn continuous() {
|
|
let d = Fractional(45, 16);
|
|
let e = Fractional(16, 45);
|
|
|
|
let dc :Continuous = d.into();
|
|
let ec :Continuous = e.into();
|
|
|
|
println!("cont frac of d : {} => {:?}", d, dc);
|
|
println!("cont frac of e : {} => {:?}", e, ec);
|
|
println!(" reverted dc : {:?} {}", dc, Into::<Fractional>::into(&dc));
|
|
println!(" reverted ec : {:?} {}", ec, Into::<Fractional>::into(&ec));
|
|
}
|
|
|
|
fn sqrt() {
|
|
let f = Fractional(-9, 4);
|
|
let fr :f64 = f.try_into().unwrap();
|
|
let sq = f.sqrt();
|
|
let _sq = fr.sqrt();
|
|
|
|
println!("{:>14} : {:?} / {}", format!("sqrt {}", f), sq, _sq);
|
|
|
|
for f in [ Fractional(9, 4)
|
|
, Fractional(45, 16)
|
|
, Fractional(16, 45)
|
|
, Fractional(9, 3) ].iter() {
|
|
let fr :f64 = (*f).try_into().unwrap();
|
|
let sq = f.sqrt().unwrap();
|
|
let sqr :f64 = sq.try_into().unwrap();
|
|
let _sqr = fr.sqrt();
|
|
|
|
println!("{:>14} : {} {} / {}", format!("sqrt {}", f), sq, sqr, _sqr);
|
|
}
|
|
}
|
|
|
|
fn pi() {
|
|
let pir :f64 = PI.try_into().unwrap();
|
|
let pit :(i32, i32) = PI.try_into().unwrap();
|
|
let pi2r :f64 = (PI * PI).try_into().unwrap();
|
|
|
|
println!(" Rust π : {}" , FPI);
|
|
println!(" π : {} {}" , PI, pir);
|
|
println!(" π as tuple : {:?}" , pit);
|
|
println!(" Rust π² : {}" , FPI * FPI);
|
|
println!(" π² : {} {}" , PI * PI, pi2r);
|
|
}
|
|
|
|
fn _sin() {
|
|
for d in [ 0, 45, 90, 135, 180, 225, 270, 315
|
|
, 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
|
|
let s = sin(*d as i32);
|
|
let sr :f64 = s.try_into().unwrap();
|
|
let _s = f64::sin(*d as f64 * FPI / 180.0);
|
|
|
|
println!("{:>14} : {} {} / {}", format!("sin {}", d), s, sr, _s);
|
|
}
|
|
}
|
|
|
|
fn _tan() {
|
|
for d in [ 0, 45, 90, 135, 180, 225, 270, 315
|
|
, 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
|
|
let s = tan(*d as i32);
|
|
let sr :f64 = s.try_into().unwrap();
|
|
let _s = f64::tan(*d as f64 * FPI / 180.0);
|
|
|
|
println!("{:>14} : {} {} / {}", format!("tan {}", d), s, sr, _s);
|
|
}
|
|
}
|
|
|
|
fn _cos() {
|
|
for d in [ 0, 45, 90, 135, 180, 225, 270, 315
|
|
, 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
|
|
let s = cos(*d as i32);
|
|
let sr :f64 = s.try_into().unwrap();
|
|
let _s = f64::cos(*d as f64 * FPI / 180.0);
|
|
|
|
println!("{:>14} : {} {} / {}", format!("cos {}", d), s, sr, _s);
|
|
}
|
|
}
|
|
|
|
fn _vector() {
|
|
let v1 = Vector(1.into(), 2.into(), 3.into());
|
|
let v2 = Vector(2.into(), 2.into(), 3.into());
|
|
let s :Fractional = 3.into();
|
|
|
|
println!("{:>14} : {}", "Vector v1", v1);
|
|
println!("{:>14} : {}", "Vector v2", v2);
|
|
println!("{:>14} : {}", "abs v1", v1.abs());
|
|
println!("{:>14} : {}", "-v1", -v1);
|
|
println!("{:>14} : {}", "v1 + v1", v1 + v1);
|
|
println!("{:>14} : {}", "v1 - v1", v1 - v1);
|
|
println!("{:>14} : {}", "v2 - v1", v2 - v1);
|
|
println!("{:>14} : {}", format!("v1 * {}", s), v1.mul(&s));
|
|
println!("{:>14} : {}", "norm v1", v1.norm());
|
|
println!("{:>14} : {}", "abs norm v1", v1.norm().abs());
|
|
println!("{:>14} : {}", "abs v1", v1.abs());
|
|
println!("{:>14} : {}", "norm * abs", v1.norm().mul(&v1.abs()));
|
|
println!("{:>14} : {}", "distance v1 v2", v1.distance(v2));
|
|
println!("{:>14} : {}", "distance v2 v1", v2.distance(v1));
|
|
println!("{:>14} : {}", "v1 dot v2", v1.dot(v2));
|
|
println!("{:>14} : {}", "v2 dot v1", v2.dot(v1));
|
|
println!("{:>14} : {}", "v1 * v2", v1 * v2);
|
|
println!("{:>14} : {}", "v2 * v1", v2 * v1);
|
|
}
|
|
|
|
fn _transform() {
|
|
let v = Vector(1.into(), 1.into(), 1.into());
|
|
let v1 = Vector(1.into(), 2.into(), 3.into());
|
|
let mt = translate(v);
|
|
|
|
println!("{:>14} : {}", "Vector v1", v1);
|
|
println!("{:>14} : {}", "translate v1", mt.apply(&v1));
|
|
println!();
|
|
|
|
let v2 = Vector(1.into(), 1.into(), 0.into());
|
|
println!("{:>14} : {}", "Vector v2", v2);
|
|
for d in [ 30, 45, 60, 90, 120, 135, 150, 180
|
|
, 210, 225, 240, 270, 300, 315, 330 ].iter() {
|
|
let m = rotate_x(*d as i32);
|
|
println!("{:>14} : {}", format!("rot_x {} v2", d), m.apply(&v2));
|
|
}
|
|
println!();
|
|
|
|
println!("{:>14} : {}", "Vector v2", v2);
|
|
for d in [ 30, 45, 60, 90, 120, 135, 150, 180
|
|
, 210, 225, 240, 270, 300, 315, 330 ].iter() {
|
|
let m = rotate_y(*d as i32);
|
|
println!("{:>14} : {}", format!("rot_y {} v2", d), m.apply(&v2));
|
|
}
|
|
println!();
|
|
|
|
for d in [ 30, 45, 60, 90, 120, 135, 150, 180
|
|
, 210, 225, 240, 270, 300, 315, 330 ].iter() {
|
|
let m = rotate_x(*d as i32) * rotate_y(*d as i32);
|
|
println!("{:>14} : {}", format!("rot_xy {} v2", d), m.apply(&v2));
|
|
}
|
|
println!();
|
|
|
|
let v3 = Vector(1.into(), 0.into(), 1.into());
|
|
println!("{:>14} : {}", "Vector v3", v3);
|
|
for d in [ 30, 45, 60, 90, 120, 135, 150, 180
|
|
, 210, 225, 240, 270, 300, 315, 330 ].iter() {
|
|
let m = rotate_z(*d as i32);
|
|
println!("{:>14} : {}", format!("rot_z {} v3", d), m.apply(&v3));
|
|
}
|
|
println!();
|
|
|
|
for d in [ 30, 45, 60, 90, 120, 135, 150, 180
|
|
, 210, 225, 240, 270, 300, 315, 330 ].iter() {
|
|
let m = rotate_v(&v, *d as i32);
|
|
println!("{:>14} : {}", format!("rot_v {} v2", d), m.apply(&v2));
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
common_fractional();
|
|
println!();
|
|
continuous();
|
|
println!();
|
|
sqrt();
|
|
println!();
|
|
pi();
|
|
println!();
|
|
_sin();
|
|
println!();
|
|
_cos();
|
|
println!();
|
|
_tan();
|
|
println!();
|
|
_vector();
|
|
println!();
|
|
_transform();
|
|
}
|