Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add polynomial odd-even coefficient sum and degree functions #217

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions poly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,44 @@ Divides two polynomials:

Uses polynomial long division algorithm. Returns `(q, r)` where `q` is the quotient and `r`
is the remainder such that `a(x) = b(x) * q(x) + r(x)` and `degree(r) < degree(b)`.

## Polynomial Degree and Coefficient Sums

```v ignore
fn degree(c []f64) int
```

This function returns the degree of the given polynomial.
The polynomial is represented by the array `c`:

```console
P(x) = c[0] + c[1] x + c[2] x^2 + ... + c[n] x^n
```

The function returns the index of the highest non-zero term.

```v ignore
fn sum_odd_coeffs(c []f64) f64
```

This function calculates the sum of the coefficients at odd indices
in the given polynomial. The polynomial is represented by the array `c`:

```console
P(x) = c[0] + c[1] x + c[2] x^2 + ... + c[n] x^n
```

The function returns the sum of coefficients `c[1]`, `c[3]`, `c[5]`, etc.

```v ignore
fn sum_even_coeffs(c []f64) f64
```

This function calculates the sum of the coefficients at even indices
in the given polynomial. The polynomial is represented by the array `c`:

```console
P(x) = c[0] + c[1] x + c[2] x^2 + ... + c[n] x^n
```

The function returns the sum of coefficients `c[0]`, `c[2]`, `c[4]`, etc.
29 changes: 29 additions & 0 deletions poly/poly.v
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,32 @@ pub fn divide(a []f64, b []f64) ([]f64, []f64) {

return quotient, remainder
}

// degree returns the degree of the given polynomial
// Input: c = [a_0, a_1, ..., a_n]
// Output: Degree of the polynomial as an integer
pub fn degree(c []f64) int {
return c.len - 1
}
Comment on lines +317 to +319
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle zero-coefficient case in degree.

The function should handle the case where all coefficients are zero, returning -1 as the degree.

Apply this diff to handle the zero-coefficient case:

 pub fn degree(c []f64) int {
-  return c.len - 1
+  for i := c.len - 1; i >= 0; i-- {
+    if c[i] != 0.0 {
+      return i
+    }
+  }
+  return -1
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub fn degree(c []f64) int {
return c.len - 1
}
pub fn degree(c []f64) int {
for i := c.len - 1; i >= 0; i-- {
if c[i] != 0.0 {
return i
}
}
return -1
}


// sum_odd_coeffs calculates the sum of the coefficients at odd indices
// Input: c = [a_0, a_1, ..., a_n]
// Output: Sum of the coefficients at odd indices as a floating-point number
pub fn sum_odd_coeffs(c []f64) f64 {
mut sum := 0.0
for i := 1; i < c.len; i += 2 {
sum += c[i]
}
return sum
}

// sum_even_coeffs calculates the sum of the coefficients at even indices
// Input: c = [a_0, a_1, ..., a_n]
// Output: Sum of the coefficients at even indices as a floating-point number
pub fn sum_even_coeffs(c []f64) f64 {
mut sum := 0.0
for i := 0; i < c.len; i += 2 {
sum += c[i]
}
return sum
}
20 changes: 19 additions & 1 deletion poly/poly_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,23 @@ fn test_divide() {
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).
assert remainder == [] // The empty set indicates that the two polynomials divide each other exactly (without remainder).
}

fn test_degree() {
assert degree([4.0, 5.0, 2.0]) == 2 // Degree should be 2
assert degree([1.0]) == 0 // Degree should be 0 for a single coefficient polynomial
assert degree([]) == -1 // Degree should be -1 for an empty coefficient array
}

fn test_sum_odd_coeffs() {
assert sum_odd_coeffs([4.0, 5.0, 2.0]) == 5.0 // 5 is at an odd index
assert sum_odd_coeffs([7.0, 456.0, 21.0, 87.0]) == 543.0 // 456 and 87 are at odd indices
assert sum_odd_coeffs([]) == 0.0 // The sum should be 0 for an empty coefficient array
}

fn test_sum_even_coeffs() {
assert sum_even_coeffs([4.0, 5.0, 2.0]) == 6.0 // 4.0 and 2 are at even indices
assert sum_even_coeffs([7.0, 456.0, 21.0, 87.0]) == 28.0 // 7 and 21 are at even indices
assert sum_even_coeffs([]) == 0 // The sum should be 0 for an empty coefficient array
}
Loading