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

poly: improvements, tests and examples #208

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions examples/poly_eval/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example - poly_eval 📘

This example demonstrates the usage of the V Scientific Library for evaluating polynomial at given
value of x.

## Instructions

1. Ensure you have the V compiler installed. You can download it from [here](https://vlang.io).
2. Ensure you have the VSL installed. You can do it following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)!
3. Navigate to this directory.
4. Run the example using the following command:

```sh
v run main.v
```

Enjoy exploring the capabilities of the V Scientific Library! 😊
11 changes: 11 additions & 0 deletions examples/poly_eval/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module main

import vsl.poly

fn main() {
// represent the polynomial 2 * x^2 + 5 * x + 4
// with x = 4, result is 2 * 4^2 + 5 * 4 + 4 = 56
coef := [4.0, 5, 2]
result := poly.eval(coef, 4)
println('Evalutated value: ${result}')
}
17 changes: 17 additions & 0 deletions examples/poly_operations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example - poly_eval 📘

This example demonstrates the usage of the V Scientific Library for additioning, substracting and
multiplying polynomials.

## Instructions

1. Ensure you have the V compiler installed. You can download it from [here](https://vlang.io).
2. Ensure you have the VSL installed. You can do it following the [installation guide](https://github.com/vlang/vsl?tab=readme-ov-file#-installation)!
3. Navigate to this directory.
4. Run the example using the following command:

```sh
v run main.v
```

Enjoy exploring the capabilities of the V Scientific Library! 😊
23 changes: 23 additions & 0 deletions examples/poly_operations/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module main

import vsl.poly

fn main() {
// Addition
// Degree is not modified unless highest coefficients cancel each other out
poly_1 := [1.0, 12, 3]
poly_2 := [3.0, 2, 7]
result_add := poly.add(poly_1, poly_2)
println('Addition result: ${result_add}')

// Substraction
// Degree is not modified unless highest coefficients cancel each other out
result_sub := poly.substract(poly_1, poly_2)
println('Substraction result: ${result_sub}')
PottierLoic marked this conversation as resolved.
Show resolved Hide resolved

// Multiplication
// with given degree n and m for poly_1 and poly_2
// resulting polynomial will be of degree n + m
result_mult := poly.multiply(poly_1, poly_2)
println('Multplication result: ${result_mult}')
}
18 changes: 10 additions & 8 deletions poly/poly.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ pub fn eval(c []f64, x f64) f64 {
panic('coeficients can not be empty')
}
len := c.len
mut ans := c[len - 1]
for e in c[..len - 1] {
ans = e + x * ans
mut ans := 0.0
mut i := len - 1
for i >= 0 {
ans = c[i] + x * ans
i--
}
return ans
}
Expand Down Expand Up @@ -131,13 +133,13 @@ fn sorted_3_(x_ f64, y_ f64, z_ f64) (f64, f64, f64) {
mut y := y_
mut z := z_
if x > y {
y, x = swap_(x, y)
x, y = swap_(x, y)
}
if y > z {
z, y = swap_(y, z)
if x > z {
x, z = swap_(x, z)
}
if x > y {
y, x = swap_(x, y)
if y > z {
y, z = swap_(y, z)
}
return x, y, z
}
Expand Down
30 changes: 15 additions & 15 deletions poly/poly_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module poly

fn test_eval() {
// ans = 2
// ans = 4.0 + 4 * 2 = 12
// ans = 5 + 4 * 12 = 53
// ans = 5 + 4 * 2 = 13
// ans = 4 + 4 * 13 = 56
x := 4
cof := [4.0, 5, 2]
assert eval(cof, 4) == 53
assert eval(cof, 4) == 56
}

fn test_swap() {
Expand All @@ -16,13 +16,13 @@ fn test_swap() {
assert a == 202.0 && b == 101.0
}

// fn test_sorted_3_(){
// a := 5.0
// b := 7.0
// c := -8.0
// x, y, z := sorted_3_(a, b, c)
// assert y == 7.0
// }
fn test_sorted_3_() {
a := 5.0
b := 7.0
c := -8.0
x, y, z := sorted_3_(a, b, c)
assert x == -8.0 && y == 5.0 && z == 7.0
}

fn test_add() {
a := [6.0, 777, -3]
Expand All @@ -36,9 +36,9 @@ fn test_substract() {
assert substract(a, b) == [5.0, 1532, 1]
}

// fn test_multiply(){
// a := [9.0, -1, 5]
// b := [0.0, -1, 7]
fn test_multiply() {
a := [9.0, -1, 5]
b := [0.0, -1, 7]

// assert multiply(a, b) == [0.0, -9, 73, -12, 35, 0]
// }
assert multiply(a, b) == [0.0, -9, 64, -12, 35, 0]
}
Loading