Skip to content

[doc] Detailed docstring for functions in linalg module#32

Merged
chaoming0625 merged 4 commits intomainfrom
more-docstring-linalg
May 11, 2025
Merged

[doc] Detailed docstring for functions in linalg module#32
chaoming0625 merged 4 commits intomainfrom
more-docstring-linalg

Conversation

@Routhleck
Copy link
Collaborator

@Routhleck Routhleck commented May 11, 2025

closes: #24

This pull request introduces updates to the saiunit library's linear algebra functions to enhance support for handling quantities with units. The changes include modifying function signatures, return types, and examples to reflect the use of Quantity objects, as well as improving consistency in documentation and examples.

Updates to Linear Algebra Functions for Quantity Support

General Updates:

  • Updated function descriptions and argument types to reflect the use of Quantity objects instead of raw arrays. This change applies to the following functions: cholesky, solve, tensorsolve, lstsq, inv, pinv, tensorinv, norm, matrix_norm, and vector_norm. [1] [2] [3] [4] [5] [6] [7] [8] [9]

Documentation and Examples:

  • Enhanced examples with the inclusion of saiunit imports and usage of Quantity objects, showcasing unit-aware computations. Examples now demonstrate the correct handling of units in operations like Cholesky decomposition, solving linear systems, and computing norms. [1] [2] [3] [4] [5]

Function-Specific Changes:

  1. eigh Function:

    • Fixed the order of returned values to align with the expected (eigenvectors, eigenvalues) format.
  2. Norm Functions (norm, matrix_norm, vector_norm):

    • Updated return types to Quantity and adjusted examples to reflect unit-aware outputs. [1] [2] [3]

These updates ensure that the library is consistent in its handling of quantities with units, improving usability and correctness for scientific and engineering applications.

Summary by Sourcery

Update linear algebra module documentation and examples to consistently use and demonstrate unit-aware Quantity objects, improving clarity and correctness for scientific applications.

Enhancements:

  • Revise function docstrings in linalg modules to reflect Quantity-based arguments and return types instead of raw arrays.
  • Update and expand examples to use saiunit imports and unit-aware computations throughout the documentation.
  • Correct the order of returned values in the eigh function to match expected output.

Documentation:

  • Enhance user-facing documentation for all major linear algebra functions to illustrate unit handling and provide clearer, unit-aware examples.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 11, 2025

Reviewer's Guide

This pull request refactors and enhances the documentation for all linear algebra functions in the saiunit library to consistently support and demonstrate unit-aware computations using Quantity objects, updates function signatures and return types, and improves example code to reflect these changes. It also fixes the return value order for the eigh function and ensures all docstrings and examples are consistent with the new unit-handling paradigm.

File-Level Changes

Change Details Files
Refactored all linalg function docstrings and examples to use Quantity objects and unit-aware computations.
  • Updated function argument and return type descriptions from raw arrays to Quantity objects.
  • Rewrote all examples to import saiunit and use Quantity objects, demonstrating unit-aware results.
  • Clarified and standardized function descriptions to reflect SaiUnit implementations.
saiunit/linalg/_linalg_keep_unit.py
saiunit/linalg/_linalg_change_unit.py
saiunit/linalg/_linalg_remove_unit.py
Fixed the order of return values for the eigh function to match the expected (eigenvectors, eigenvalues) format.
  • Swapped the order of eigenvectors and eigenvalues in the return statement for eigh.
  • Updated docstrings and examples to match the corrected return order.
saiunit/lax/_lax_linalg.py
Updated norm, matrix_norm, and vector_norm functions to return Quantity objects and updated their examples accordingly.
  • Changed return type documentation from array to Quantity.
  • Modified examples to show unit-aware outputs for norm computations.
saiunit/linalg/_linalg_keep_unit.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Routhleck Routhleck requested a review from chaoming0625 May 11, 2025 03:17
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Routhleck - I've reviewed your changes - here's some feedback:

  • The implementation of eig and eigh now returns (eigenvectors, eigenvalues) instead of the more common (eigenvalues, eigenvectors) order; consider clarifying this in the docstrings to avoid confusion for users.
  • In the docstring for eigvalsh, the comment '# TODO: Seems wrong implementation' is present; please either address the concern or remove the comment if resolved.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

>>> import jax.numpy as jnp

>>> a = jnp.array([[1, -2j],
... [2j, 1]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Avoid unnecessary eigenvector computation when only eigenvalues are needed

Re-add compute_left_eigenvectors=False and compute_right_eigenvectors=False to skip eigenvector computation and avoid unnecessary work.

Suggested change
... [2j, 1]])
return eig(a, compute_left_eigenvectors=False,
compute_right_eigenvectors=False)[0]

Comment on lines 166 to 172
if isinstance(x, Quantity):
w, v = lax.linalg.eigh(x.mantissa, lower=lower, symmetrize_input=symmetrize_input,
v, w = lax.linalg.eigh(x.mantissa, lower=lower, symmetrize_input=symmetrize_input,
sort_eigenvalues=sort_eigenvalues, subset_by_index=subset_by_index)
return maybe_decimal(Quantity(w, unit=x.unit)), v
return v, maybe_decimal(Quantity(w, unit=x.unit))
else:
return lax.linalg.eigh(x, lower=lower, symmetrize_input=symmetrize_input,
sort_eigenvalues=sort_eigenvalues, subset_by_index=subset_by_index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): We've found these issues:

@chaoming0625 chaoming0625 merged commit cec6686 into main May 11, 2025
24 checks passed
@chaoming0625 chaoming0625 deleted the more-docstring-linalg branch May 11, 2025 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[doc] Detailed docstring for functions in linalg module

2 participants