fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.02k stars 161 forks source link

linalg: Eigenvalues and Eigenvectors #816

Closed perazz closed 6 days ago

perazz commented 1 month ago

Computing eigenvalues and eigenvectors of a square matrix: $A \cdot \bar{v} - \lambda \cdot \bar{v} = 0$ . Based on LAPACK General (*GEEV) or real symmetric (*SYEV) / complex Hermitian (*HEEV) operations.

General matrix

Prior art

Proposed implementation

Real Symmetric / Complex Hermitian

Prior art

Proposed implementation

Note: the LAPACK backends are not pure due to functions with intent(out) arguments. The current proposed interfaces are ready to be made pure (e.g., function interface with all intent(in) arguments) to make it easy when the backend will be pure.

I believe this is ready for consideration, thank you @jvdp1 @jalvesz @fortran-lang/stdlib

perazz commented 1 month ago

From Fortran Monthly call:

For example, NumPy returns real eigenvalues when all imaginary parts are zero:

    if not isComplexType(t) and all(w.imag == 0.0):
        w = w.real
        vt = vt.real
        result_t = _realType(result_t)
    else:
        result_t = _complexType(result_t)

Thank you @jvdp1 for the reviews. As pointed out during our last Monthly call with @everythingfunctional @jalvesz, I've added the option to return the real part of the eigenvalues only. In this latest case, we must check that there are no meaningful imaginary components, and raise an error if so:

https://github.com/fortran-lang/stdlib/blob/e6aa0f50e95eefe96cd5e6ade2c50e42fa158cdc/src/stdlib_linalg_eigenvalues.fypp#L346-L350

perazz commented 2 weeks ago

Thanks a lot @jvdp1 @jalvesz for the reviews - this PR looks well polished now. You will find references for each of your comments to the related commit.

jalvesz commented 1 week ago

LGTM @perazz, I think this is ready to go :)

perazz commented 1 week ago

Thank you both @jvdp1 @jalvesz, I would say let's wait a couple more days, and then merge if there are no further comments.

perazz commented 6 days ago

Thank you, I will merge this one.