vsl icon indicating copy to clipboard operation
vsl copied to clipboard

la.jacobi: fix incorrect eigen values/vectors

Open PottierLoic opened this issue 9 months ago • 1 comments

This PR resolves issue #202, where the Jacobi eigenvalue algorithm in vsl.la produced incorrect results. The original rotation logic failed to zero off-diagonal elements and broke the symmetry of the matrix a, resulting in incorrect eigenvalues (v) and eigenvectors (q).

I retained the initial test with a diagonal matrix:

mut a := la.Matrix.deep2([
  [2.0, 0.0, 0.0],
  [0.0, 2.0, 0.0],
  [0.0, 0.0, 2.0],
])

Expected : v = [2, 2, 2], q = identity, a unchanged. It still passes.

I added a new test for a more complex symmetric matrix, which was failing prior to the fix:

mut a := la.Matrix.deep2([
  [4.0, 1.0, 1.0],
  [1.0, 3.0, 0.0],
  [1.0, 0.0, 2.0],
])	

Expected : v = [4.879385241571816, 2.652703644666139, 1.467911113762045], q matches computed eigenvectors, a diagonalized. It now passes.

I compared the outputs against an online eigenvalue calculator. v match exactly and q_values are matching the results from the solver once normalized.

I re-ran the code from #202 and it still doesn't return the expected values from the issue. I think there may be a problem in the code snippet provided too.

Summary by CodeRabbit

  • Refactor
    • Improved the matrix algorithm’s logic by revising convergence checks and updating numerical computations for enhanced clarity and correctness.
  • Tests
    • Expanded test coverage by introducing a new case for a symmetric 3x3 matrix and enhancing existing validations to ensure precise outputs.

PottierLoic avatar Mar 25 '25 15:03 PottierLoic

Walkthrough

The Jacobi method implementation has been updated to adjust its control flow and numerical updates. The convergence check now uses a break statement instead of an immediate return, and the computation of the difference variable has been reversed. Additional tolerance conditions have been added before processing further calculations. The matrix update logic has been refactored using a single loop, and the final matrix assembly now explicitly sets both diagonal and off-diagonal elements. In parallel, a new test case for a 3x3 symmetric matrix and additional assertions in an existing test have been introduced.

Changes

File Change Summary
la/jacobi.v Modified the jacobi function: convergence check now uses a break; reversed the computation of h; added extra tolerance checks; refactored matrix update loops; explicitly set diagonal/off-diagonal values; updated convergence validation.
la/jacobi_test.v Added a new test function test_jacobi_3x3_symmetric() for a 3x3 symmetric matrix; enhanced test_jacobi01() with additional assertions for updated matrix output.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Caller/Test
    participant Jacobi as jacobi()
    participant Matrix as Matrix (a, v, q)
    
    Caller->>Jacobi: Call jacobi(q, v, a)
    Note over Jacobi: Start iterations for Jacobi rotation
    
    Jacobi->>Jacobi: Compute h = v[j] - v[i]
    alt Tolerance Condition Met & Convergence Check
        Jacobi->>Jacobi: Break loop
    else Continue Calculations
        Jacobi->>Jacobi: Calculate t based on h and a.get(i, j)
        Jacobi->>Matrix: Update matrix values (aii, ajj, aij) and set a[i][j] to 0.0
    end
    
    Jacobi->>Matrix: Update remaining matrix entries (excluding i and j)
    Jacobi->>Matrix: Set diagonal elements to v and off-diagonals to zero
    Jacobi-->>Caller: Return updated matrices or error if not converged

Poem

I'm a bunny with a coder's delight,
Hopping through changes both day and night.
Jacobi's loop now twists and turns,
With each new check, my code burns.
Matrices dance with precision and cheer,
For in this code field, I’m the rabbit engineer!
🥕🐇 Happy coding!


🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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 Mar 25 '25 15:03 coderabbitai[bot]