Skip to content

Commit

Permalink
poly: add examples for eval and basic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
PottierLoic committed Jul 15, 2024
1 parent e736526 commit 6e57a00
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
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}')

// 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}')
}

0 comments on commit 6e57a00

Please sign in to comment.