From 2f05eb11a6b0a1617ca4cb9a2d1066d9ebd75ce6 Mon Sep 17 00:00:00 2001 From: PottierLoic Date: Tue, 10 Sep 2024 19:23:06 +0200 Subject: [PATCH] fix(poly): merge @suleyman-kaya changes Merging with @suleyman-kaya changes removed println and useless math import in poly_test.v --- examples/poly_operations/main.v | 2 +- poly/poly.v | 33 --------------------------------- poly/poly_test.v | 7 ------- 3 files changed, 1 insertion(+), 41 deletions(-) diff --git a/examples/poly_operations/main.v b/examples/poly_operations/main.v index cbba520e5..47b9fb6e4 100644 --- a/examples/poly_operations/main.v +++ b/examples/poly_operations/main.v @@ -21,7 +21,7 @@ fn main() { result_mult := poly.multiply(poly_1, poly_2) println('Multplication result: ${result_mult}') // Expected: [3.0, 38.0, 40.0, 90.0, 21.0] (3 + 38x + 400x^2 + 90x^3 + 21x^4) - // Division + // Division TODO:Fix this // Result includes the quotient and the remainder // To get the real remainder, divide it by the divisor. poly_dividend := [2.0, -4.0, -4.0, 1.0] // 2 - 4x - 4x^2 + x^3 diff --git a/poly/poly.v b/poly/poly.v index 697c8de86..da10bf14c 100644 --- a/poly/poly.v +++ b/poly/poly.v @@ -312,36 +312,3 @@ pub fn divide(a []f64, b []f64) ([]f64, []f64) { return quotient, remainder } - -pub fn divide(dividend []f64, divisor []f64) ([]f64, []f64) { - if divisor.len == 0 { - panic('divisor cannot be an empty polynomial') - } - if dividend.len == 0 { - return []f64{len: 0}, []f64{len: 0} - } - - mut quotient := []f64{len: dividend.len - divisor.len + 1, init: 0.0} - mut remainder := dividend.clone() - - divisor_degree := divisor.len - 1 - divisor_lead_coeff := divisor[divisor_degree] - - for remainder.len >= divisor.len { - remainder_degree := remainder.len - 1 - lead_coeff := remainder[remainder_degree] - - quotient_term := lead_coeff / divisor_lead_coeff - quotient_idx := remainder_degree - divisor_degree - quotient[quotient_idx] = quotient_term - - for i in 0 .. divisor.len { - remainder[quotient_idx + i] -= quotient_term * divisor[i] - } - - for remainder.len > 0 && remainder[remainder.len - 1] == 0.0 { - remainder = remainder[0..remainder.len - 1].clone() - } - } - return quotient, remainder -} diff --git a/poly/poly_test.v b/poly/poly_test.v index 13573b3c0..d28a2fb40 100644 --- a/poly/poly_test.v +++ b/poly/poly_test.v @@ -1,7 +1,5 @@ module poly -import math - fn test_eval() { x := 4 coef := [4.0, 5, 2] @@ -61,7 +59,6 @@ fn test_add() { a := [1.0, 2.0, 3.0] b := [6.0, 20.0, -10.0] result := add(a, b) - println('Add result: ${result}') assert result == [7.0, 22.0, -7.0] } @@ -69,7 +66,6 @@ fn test_subtract() { a := [6.0, 1532.0, -4.0] b := [1.0, -1.0, -5.0] result := subtract(a, b) - println('Subtract result: ${result}') assert result == [5.0, 1533.0, 1.0] } @@ -78,7 +74,6 @@ fn test_multiply() { a := [2.0, 3.0, 4.0] b := [0.0, -3.0, 2.0] result := multiply(a, b) - println('Multiply result: ${result}') assert result == [0.0, -6.0, -5.0, -6.0, 8.0] } @@ -87,8 +82,6 @@ fn test_divide() { a := [1.0, 2.0, 1.0] b := [1.0, 1.0] quotient, remainder := divide(a, b) - println('Divide quotient: ${quotient}') - println('Divide remainder: ${remainder}') assert quotient == [1.0, 1.0] assert remainder == [] // The empty set indicates that two polynomials divide each other exactly (without remainder). }