-
-
Notifications
You must be signed in to change notification settings - Fork 47
poly: improvements, tests and examples #208
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
Open
PottierLoic
wants to merge
12
commits into
vlang:main
Choose a base branch
from
PottierLoic:fix_poly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c41c75
poly: fix eval and sorted_3_ + fix tests
PottierLoic e736526
poly: fmt + complete test
PottierLoic 6e57a00
poly: add examples for eval and basic operations
PottierLoic cc2d76c
poly: fix typo
PottierLoic 9277af1
poly: add divide function, example and tests
PottierLoic 9695393
poly: add tests and examples
PottierLoic 8c08e42
noise: add tests/examples for companion matrix
PottierLoic eae3bf6
noise: fmt
PottierLoic 80d32ad
Merge branch 'main' into fix_poly
PottierLoic 2f05eb1
fix(poly): merge @suleyman-kaya changes
PottierLoic d0b4fd5
run fmt
PottierLoic 6ebfb97
fix divide function
PottierLoic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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! 😊 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| 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('Evaluated value: ${result}') | ||
|
|
||
| // base polynomial: 2 * x^2 + 5 * x + 4 = 56 with x = 4 | ||
| // 1st derivative: 4 * x + 5 = 21 with x = 4 | ||
| result_derivs := poly.eval_derivs(coef, 4, 2) | ||
| println('Evaluated values: ${result_derivs}') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Example - poly_matrix 📘 | ||
|
|
||
| This example demonstrates the usage of the V Scientific Library for constructing companion matrix. | ||
|
|
||
| ## 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! 😊 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| module main | ||
|
|
||
| import vsl.poly | ||
|
|
||
| fn main() { | ||
| // Polynomial coefficients for p(x) = 2x^3 -4x^2 + 3x - 5 | ||
| coef := [2.0, -4.0, 3.0, -5.0] | ||
|
|
||
| // For the polynomial p(x) = a_n x^n + a_{n-1} x^{n-1} + ... + a_1 x + a_0 | ||
| // The companion matrix C will be: | ||
| // [ 0 0 0 -a_0/a_n ] | ||
| // [ 1 0 0 -a_1/a_n ] | ||
| // [ 0 1 0 -a_2/a_n ] | ||
| // [ 0 0 1 -a_3/a_n ] | ||
| comp_matrix := poly.companion_matrix(coef) | ||
| println('Companion matrix:') | ||
| for row in comp_matrix { | ||
| println(row) | ||
| } | ||
|
|
||
| // Balancing matrix if needed | ||
| balanced_matrix := poly.balance_companion_matrix(comp_matrix) | ||
| println('Balanced companion matrix:') | ||
| for row in balanced_matrix { | ||
| println(row) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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! 😊 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| 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] // 1 + 12x + 3x^2 | ||
| poly_2 := [3.0, 2, 7] // 3 + 2x + 7x^2 | ||
| result_add := poly.add(poly_1, poly_2) | ||
| println('Addition result: ${result_add}') // Expected: [4.0, 14.0, 10.0] (4 + 14x + 10x^2) | ||
|
|
||
| // Subtraction | ||
| // Degree is not modified unless highest coefficients cancel each other out | ||
| result_sub := poly.subtract(poly_1, poly_2) | ||
| println('Subtraction result: ${result_sub}') // Expected: [-2.0, 10.0, -4.0] (-2 + 10x - 4x^2) | ||
|
|
||
| // 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}') // Expected: [3.0, 38.0, 40.0, 90.0, 21.0] (3 + 38x + 400x^2 + 90x^3 + 21x^4) | ||
|
|
||
| // Division | ||
| // 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 | ||
| poly_divisor := [-2.0, 1.0] // -2 + x | ||
| quotient, remainder := poly.divide(poly_dividend, poly_divisor) | ||
| println('Division quotient: ${quotient}') // Expected quotient: [-8.0, -2.0, 1.0] (-8 - 2x + x^2) | ||
| println('Division remainder: ${remainder}') // Expected remainder: [-14.0] | ||
| // Real remainder: -14 / (-2 + x) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.