vsl icon indicating copy to clipboard operation
vsl copied to clipboard

poly: improvements, tests and examples

Open PottierLoic opened this issue 1 year ago • 8 comments

This PR fix eval and sorted_3_ function, they returned wrong results.

The new eval function is now faster and return the correct result:

C:\Users\loicp\Documents\GitHub\vsl\poly>v run .
time for 10000 old eval: 219.500us
old_eval returned value: 53.0
time for 10000 new eval: 126.700us
new_eval returned value: 56.0

I'll add tests for the others functions soon and I can fill some examples to resolve this : #106

Summary by CodeRabbit

  • New Features

    • Added detailed examples for polynomial operations, including addition, subtraction, multiplication, and division.
    • Introduced new test functions to validate polynomial operations and derivatives.
    • Implemented a module for constructing and balancing companion matrices from polynomials.
  • Bug Fixes

    • Corrected the sorting logic to ensure accurate results in polynomial evaluations.
  • Improvements

    • Enhanced the polynomial evaluation function for better efficiency and clarity.
    • Updated test scenarios for improved coverage and accuracy, including adjustments to expected results.

PottierLoic avatar Jul 15 '24 20:07 PottierLoic

Walkthrough

The recent updates refine polynomial evaluation and manipulation in the poly/poly.v file, specifically enhancing the eval, sorted_3_, and divide functions. The poly_test.v file has seen significant improvements with new test cases and updated assertions, while a new main.v file demonstrates basic polynomial operations such as addition, subtraction, multiplication, and division. These changes collectively enhance the functionality and testing framework for polynomial mathematics.

Changes

Files Change Summary
poly/poly.v Major updates to the eval, sorted_3_, balance_companion_matrix, and divide functions for enhanced functionality and clarity.
poly/poly_test.v Improvements to test functions with updated calculations, assertions, and multiple new test additions for comprehensive coverage.
examples/poly_operations/main.v New file implementing polynomial operations: addition, subtraction, multiplication, and division.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Main
    participant Poly as vsl.poly
    
    User ->> Main: Run poly_operations example
    Main ->> Poly: Call add, subtract, multiply, divide functions
    Poly -->> Main: Return results
    Main -->> User: Display results

Possibly related PRs

  • #212: The changes in the eval function in both PRs involve modifications to the same function, focusing on error handling and logic improvements, indicating a direct relationship.
  • #215: This PR includes modifications to the divide function, which is also altered in the main PR, suggesting a connection through shared functionality in polynomial operations.

Poem

In the code where numbers play,
Polynomials dance and sway.
With functions crisp and tests so bright,
Math in V takes joyful flight.
A rabbit's hop, a cheerful tune,
Together we explore this boon!
🐇✨

[!TIP]

OpenAI O1 model for chat
  • We have deployed OpenAI's latest O1 model for chat.
  • OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model.
  • Please share any feedback with us in the discussions post.

Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL

Commits

Files that changed from the base of the PR and between d0b4fd567f51634a8863e3d2bac5a13516c2f103 and 6ebfb97404f308e6c554d5eb6e91270720f4dd25.

Files selected for processing (3)
  • examples/poly_operations/main.v (1 hunks)
  • poly/poly.v (4 hunks)
  • poly/poly_test.v (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • examples/poly_operations/main.v
Additional comments not posted (11)
poly/poly_test.v (7)

5-7: LGTM!

The test case has been updated with the correct expected value. The changes look good.


9-14: Great addition!

The new test case for eval_derivs function looks good. It enhances the test coverage of polynomial operations by validating the derivative evaluation.


16-21: Great addition!

The new test case for solve_quadratic function looks good. It enhances the test coverage of polynomial operations by validating the roots of a quadratic equation.


31-37: LGTM!

The test case for sorted_3_ function looks good. The assertions correctly verify the order of the sorted elements.

The past review comment is no longer valid as the assertions have been updated correctly.


39-46: Great addition!

The new test case for companion_matrix function looks good. It enhances the test coverage of polynomial operations by validating the construction of a companion matrix from polynomial coefficients.


48-56: Great addition!

The new test case for balance_companion_matrix function looks good. It enhances the test coverage of polynomial operations by validating the balancing of the companion matrix.


81-91: LGTM!

The changes in the test_divide function look good. The test cases correctly verify the quotient and remainder of polynomial division.

The past review comment regarding the remainder value is no longer valid as the user has verified the result using an online solver.

poly/poly.v (4)

18-22: LGTM!

The changes to the eval function improve its clarity and performance. Initializing ans to zero and using a decrementing index i to traverse the coefficients in reverse order is a cleaner and more efficient approach compared to the previous implementation.


149-155: LGTM!

The changes to the sorted_3_ function improve its readability. The revised order of variable assignments during the sorting process makes the logic clearer and easier to follow.


218-228: LGTM!

The changes to the balance_companion_matrix function improve its readability. Replacing references to poly.radix and poly.radix2 with radix and radix2, respectively, makes the code cleaner and easier to understand.


293-320: LGTM!

The changes to the divide function improve its robustness. The additional checks for empty input polynomials prevent division by an empty polynomial and handle cases where the dividend is empty. This ensures that the function behaves correctly in all scenarios.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Jul 15 '24 20:07 coderabbitai[bot]

I have a problem with the solve_cubic function, I don't understand why it has only 3 parameters, a cubic polynomial should be written as $ax^3+bx^2+cx+d$, with a, b, c and d as parameters.

PottierLoic avatar Jul 16 '24 15:07 PottierLoic

Sorry for the noise in the 2 previous commit messages, I confused my 2 PR...

PottierLoic avatar Jul 23 '24 13:07 PottierLoic

I have a problem with the solve_cubic function, I don't understand why it has only 3 parameters, a cubic polynomial should be written as a x 3 + b x 2 + c x + d , with a, b, c and d as parameters.

I read something about normalized cubic polynomials where 𝑎 is equal to 1. Is that what the function is supposed to take as parameters?

@ulises-jeremias can you explain me the goal behind this ?

PottierLoic avatar Aug 09 '24 22:08 PottierLoic

@suleyman-kaya I noticed an issue in the divide function It might be my mistake,but the result of the following code:

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)

should be : Division quotient: [1.0, -2.0, -8.0] Division remainder: [-14.0]

When I reverse the order of the parameters, I get the correct result. So I believe the comment above the divide function should be updated:

...
// Input: a = [a_0, ..., a_n], b = [b_0, ..., b_m]
...
pub fn divide(a []f64, b []f64) ([]f64, []f64) {

Alternatively, we could modify the function so that it accepts the parameters in the order specified in the comment. (It would be more consistent since all the functions in the poly module take the lower degree coefficient first, with divide being the only exception.)

PottierLoic avatar Sep 10 '24 17:09 PottierLoic

No problem, I'll take a look when I have some time. You couldn't catch it with test_poly because the test is done with a symmetric polynomial:

a := [1.0, 2.0, 1.0]
b := [1.0, 1.0]

Reversing the two lists wouldn't change anything, which is why it passed :)

PottierLoic avatar Sep 10 '24 23:09 PottierLoic

@ulises-jeremias could you take a look please ?

Also, a v fmt -w . would change A LOT of files.. I did it on the files I modified, but there are a lot of others and I don't understand why.

PottierLoic avatar Sep 13 '24 16:09 PottierLoic

@ulises-jeremias could you take a look please ?

Also, a v fmt -w . would change A LOT of files.. I did it on the files I modified, but there are a lot of others and I don't understand why.

vfmt has changed in a few places recently and maintainers hasn't had time to update the code base it seems.

larpon avatar Sep 13 '24 18:09 larpon