Skip to content

Commit

Permalink
fix(poly): merge @suleyman-kaya changes
Browse files Browse the repository at this point in the history
Merging with @suleyman-kaya changes
removed println and useless math import in poly_test.v
  • Loading branch information
PottierLoic committed Sep 10, 2024
1 parent 80d32ad commit 2f05eb1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 41 deletions.
2 changes: 1 addition & 1 deletion examples/poly_operations/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 0 additions & 33 deletions poly/poly.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 0 additions & 7 deletions poly/poly_test.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module poly

import math

fn test_eval() {
x := 4
coef := [4.0, 5, 2]
Expand Down Expand Up @@ -61,15 +59,13 @@ 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]
}

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]
}

Expand All @@ -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]
}

Expand All @@ -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).
}

0 comments on commit 2f05eb1

Please sign in to comment.