From 6e57a00b5dfdd599b19a41e6b7a52a6449059e56 Mon Sep 17 00:00:00 2001 From: PottierLoic Date: Mon, 15 Jul 2024 23:34:59 +0200 Subject: [PATCH] poly: add examples for eval and basic operations --- examples/poly_eval/README.md | 17 +++++++++++++++++ examples/poly_eval/main.v | 11 +++++++++++ examples/poly_operations/README.md | 17 +++++++++++++++++ examples/poly_operations/main.v | 23 +++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 examples/poly_eval/README.md create mode 100644 examples/poly_eval/main.v create mode 100644 examples/poly_operations/README.md create mode 100644 examples/poly_operations/main.v diff --git a/examples/poly_eval/README.md b/examples/poly_eval/README.md new file mode 100644 index 000000000..f4162db0b --- /dev/null +++ b/examples/poly_eval/README.md @@ -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! 😊 diff --git a/examples/poly_eval/main.v b/examples/poly_eval/main.v new file mode 100644 index 000000000..546f11179 --- /dev/null +++ b/examples/poly_eval/main.v @@ -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}') +} diff --git a/examples/poly_operations/README.md b/examples/poly_operations/README.md new file mode 100644 index 000000000..79e818955 --- /dev/null +++ b/examples/poly_operations/README.md @@ -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! 😊 diff --git a/examples/poly_operations/main.v b/examples/poly_operations/main.v new file mode 100644 index 000000000..adf818861 --- /dev/null +++ b/examples/poly_operations/main.v @@ -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}') +}