clean(*): prep 42
This commit is contained in:
parent
8260e7fc92
commit
0628e54d6e
|
@ -94,7 +94,6 @@ fn solve_degree_2(equation: Vec<f64>) {
|
||||||
println!("Discriminant is strictly positive, the two solutions are:");
|
println!("Discriminant is strictly positive, the two solutions are:");
|
||||||
println!("{x1}");
|
println!("{x1}");
|
||||||
println!("{x2}");
|
println!("{x2}");
|
||||||
//(-b +- sqrt(delta)) / 2a
|
|
||||||
} else if delta < 0. {
|
} else if delta < 0. {
|
||||||
let sqrt_delta = sqrt(delta);
|
let sqrt_delta = sqrt(delta);
|
||||||
let a = -equation[1] / (2. * equation[2]);
|
let a = -equation[1] / (2. * equation[2]);
|
||||||
|
@ -102,12 +101,10 @@ fn solve_degree_2(equation: Vec<f64>) {
|
||||||
println!("Discriminant is strictly negative, the two complex solutions are:");
|
println!("Discriminant is strictly negative, the two complex solutions are:");
|
||||||
println!("{a} + {b}i");
|
println!("{a} + {b}i");
|
||||||
println!("{a} - {b}i");
|
println!("{a} - {b}i");
|
||||||
//(-b +- i sqrt(-delta)) / 2a
|
|
||||||
} else {
|
} else {
|
||||||
let x = -equation[1] / (2. * equation[2]);
|
let x = -equation[1] / (2. * equation[2]);
|
||||||
println!("Discriminant is zero, the solution is:");
|
println!("Discriminant is zero, the solution is:");
|
||||||
println!("{x}");
|
println!("{x}");
|
||||||
//-b / 2a
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
22
src/maths.rs
22
src/maths.rs
|
@ -43,7 +43,6 @@ fn get_prime_factors(mut n: i128) -> Vec<i128> {
|
||||||
prime_factors
|
prime_factors
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO slow ? check Stein's algorithm (binaryGCD) + tests
|
|
||||||
fn gcd(a: i128, b: i128) -> i128 {
|
fn gcd(a: i128, b: i128) -> i128 {
|
||||||
if b == 0 {
|
if b == 0 {
|
||||||
a
|
a
|
||||||
|
@ -86,7 +85,6 @@ impl Rational {
|
||||||
fn format_fract(&self) -> String {
|
fn format_fract(&self) -> String {
|
||||||
let mut copy = self.clone();
|
let mut copy = self.clone();
|
||||||
let factors = get_prime_factors(self.denominator);
|
let factors = get_prime_factors(self.denominator);
|
||||||
dbg!(&factors);
|
|
||||||
let mut two_count = 0;
|
let mut two_count = 0;
|
||||||
let mut five_count = 0;
|
let mut five_count = 0;
|
||||||
for factor in factors {
|
for factor in factors {
|
||||||
|
@ -111,7 +109,6 @@ impl Rational {
|
||||||
format!("{}.{}", copy.numerator / copy.denominator, copy.numerator % copy.denominator)
|
format!("{}.{}", copy.numerator / copy.denominator, copy.numerator % copy.denominator)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO return string
|
|
||||||
pub fn format(&self) -> String {
|
pub fn format(&self) -> String {
|
||||||
match (self.numerator, self.denominator) {
|
match (self.numerator, self.denominator) {
|
||||||
(_, 1) => format!("{}", self.numerator),
|
(_, 1) => format!("{}", self.numerator),
|
||||||
|
@ -119,7 +116,6 @@ impl Rational {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO rename or move to better scope, only used in complex div ?
|
|
||||||
pub fn simplify(lhs: Rational, rhs: Rational) -> Self {
|
pub fn simplify(lhs: Rational, rhs: Rational) -> Self {
|
||||||
let res = Rational::new(lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
|
let res = Rational::new(lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
|
||||||
res.reduce()
|
res.reduce()
|
||||||
|
@ -253,7 +249,6 @@ impl GaussianRational {
|
||||||
GaussianRational::new(self.real, -1 * self.imaginary)
|
GaussianRational::new(self.real, -1 * self.imaginary)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO return string, avoid printing real or imag part if zero
|
|
||||||
pub fn format(&self) -> String {
|
pub fn format(&self) -> String {
|
||||||
match (self.real().is_zero(), self.imaginary.is_zero()) {
|
match (self.real().is_zero(), self.imaginary.is_zero()) {
|
||||||
(true, true) => format!(""),
|
(true, true) => format!(""),
|
||||||
|
@ -394,7 +389,6 @@ mod tests {
|
||||||
assert_eq!(rational.left_shift(), Rational::new(7, 30));
|
assert_eq!(rational.left_shift(), Rational::new(7, 30));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simplify() {
|
fn simplify() {
|
||||||
let a = Rational::new(10, 3);
|
let a = Rational::new(10, 3);
|
||||||
|
@ -524,19 +518,3 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
pub struct SquareRoot {
|
|
||||||
rational: Rational,
|
|
||||||
irrational: Rational,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Algebraic {
|
|
||||||
rational: Rational,
|
|
||||||
algebraic: SquareRoot,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct GaussianAlgebraic {
|
|
||||||
real: Algebraic,
|
|
||||||
imaginary: Algebraic,
|
|
||||||
}*/
|
|
|
@ -341,18 +341,9 @@ fn degree_two_real(a: Rational, b: Rational, c: Rational) {
|
||||||
|
|
||||||
left_part.denominator *= sqrt_delta.denominator;
|
left_part.denominator *= sqrt_delta.denominator;
|
||||||
left_part.numerator *= sqrt_delta.denominator;
|
left_part.numerator *= sqrt_delta.denominator;
|
||||||
|
|
||||||
sqrt_delta.numerator_natural *= tmp;
|
sqrt_delta.numerator_natural *= tmp;
|
||||||
sqrt_delta.denominator *= tmp;
|
sqrt_delta.denominator *= tmp;
|
||||||
/*
|
|
||||||
let gcd = gcd(gcd(sqrt_delta.numerator_natural, left_part.numerator), left_part.denominator);
|
|
||||||
|
|
||||||
left_part.denominator /= gcd;
|
|
||||||
left_part.numerator /= gcd;
|
|
||||||
|
|
||||||
sqrt_delta.numerator_natural /= gcd;
|
|
||||||
sqrt_delta.denominator /= gcd;
|
|
||||||
*/
|
|
||||||
print_degree_two_real(left_part, sqrt_delta, imaginary);
|
print_degree_two_real(left_part, sqrt_delta, imaginary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue