Skip to content

Add dgeev implementation for WASM#4

Merged
pedrovgs merged 3 commits intomasterfrom
add-dgeev-implementation-for-wasm
Feb 4, 2026
Merged

Add dgeev implementation for WASM#4
pedrovgs merged 3 commits intomasterfrom
add-dgeev-implementation-for-wasm

Conversation

@pedrovgs
Copy link
Member

@pedrovgs pedrovgs commented Feb 4, 2026

🎩 What is the goal?

This PR implements the missing dgeev_ (double-precision eigenvalue decomposition) function for WASM/WASI platform. The CLAPACK library used on WASM doesn't include dgeev_, so a pure Swift implementation using the QR algorithm was developed. This enables eigenvalue decomposition functionality on WASM, which was previously throwing a fatal error.

Additionally, this PR improves the CLAPACK wrapper layer by introducing a CLAPACKHelper C module that properly handles the ABI mismatch between CLAPACK's header declarations (returning int) and the actual f2c-generated implementation (returning void).

📄 How is it being implemented?

  1. Pure Swift Eigenvalue Decomposition (swiftEigenDecomposition):

    • Implements the classic QR algorithm with implicit shifts
    • Uses Householder reflections for Hessenberg reduction (_eigenHessenbergReduction)
    • Applies Wilkinson shifts in the QR iteration (_eigenQRStep)
    • Extracts eigenvalues from quasi-triangular Schur form (_eigenExtractEigenvalues)
    • Computes eigenvectors via back-substitution (_eigenComputeEigenvectors)
    • SIMD optimizations for vector/matrix operations to improve performance
  2. CLAPACKHelper Module:

    • New C wrapper module (Sources/CLAPACKHelper/) that provides correct function signatures
    • Wrapper functions (clapack_dgetrf_wrapper, clapack_dgetri_wrapper) that call CLAPACK with proper void return type
    • Eliminates linker warnings caused by ABI mismatch
  3. WASM-compatible dgeev_ wrapper:

    • The dgeev_ function on WASM now delegates to the pure Swift implementation
    • Maintains the same LAPACK-compatible API (workspace query support, job flags for left/right eigenvectors)
  4. Test Coverage Updates:

    • Updated LinAlgTest.swift to conditionally run tests based on platform capabilities
    • Added comprehensive tests in WASIFallbackTests.swift for:
      • Matrix inverse (2x2, 3x3, batched)
      • Determinant calculations
      • Eigenvalue decomposition (real and complex eigenvalues)
      • Direct tests for swiftEigenDecomposition function

Copy link

@Juanpe Juanpe left a comment

Choose a reason for hiding this comment

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

LGTM

@pedrovgs pedrovgs merged commit d86966e into master Feb 4, 2026
2 checks passed
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.

2 participants