Re-enable the CGS (Conjugate Gradient Squared) iterative linear system solver that was commented out in PR #88 during -Wall -Werror compliance work.
-
cgsInit(lines 917-920): Initializes the CGS solver statecgsInit :: LinearVectorSpace (SpVector a) => MatrixType (SpVector a) -> SpVector a -> SpVector a -> CGS a cgsInit aa b x0 = CGS x0 r0 r0 r0 where r0 = b ^-^ (aa #> x0) -- residual of initial guess solution
-
cgsStep(lines 922-933): Performs one CGS iteration stepcgsStep :: (V (SpVector a), Fractional (Scalar (SpVector a))) => MatrixType (SpVector a) -> SpVector a -> CGS a -> CGS a cgsStep aa rhat (CGS x r p u) = CGS xj1 rj1 pj1 uj1 where aap = aa #> p alphaj = (r `dot` rhat) / (aap `dot` rhat) q = u ^-^ (alphaj .* aap) xj1 = x ^+^ (alphaj .* (u ^+^ q)) -- updated solution rj1 = r ^-^ (alphaj .* (aa #> (u ^+^ q))) -- updated residual betaj = (rj1 `dot` rhat) / (r `dot` rhat) uj1 = rj1 ^+^ (betaj .* q) pj1 = uj1 ^+^ (betaj .* (q ^+^ (betaj .* p)))
-
Show instance (lines 936-940): Display CGS state for debugging
Added to the export list:
CGS(..)- The CGS data type with all fieldscgsInit- Initialization functioncgsStep- Iteration function
Comprehensive test suite with multiple levels of verification:
Unit Tests:
-
Initial State Test: Verifies
cgsInitcreates correct initial state- Checks residual r₀ = b - Ax₀
- Verifies p₀ = u₀ = r₀
-
Iteration Test: Verifies
cgsStepexecutes without errors- Confirms state is updated
- Validates dimensions are preserved
Integration Tests: 3. 2x2 Dense System: Tests convergence on small dense system
- Matrix:
aa0(2×2) - Known solution:
x0true - Iterations: 50
- 3x3 SPD System: Tests convergence on sparse symmetric positive definite system
- Matrix:
aa2(3×3 tridiagonal) - Known solution:
x2 - Iterations: 50
- Matrix:
Property-Based Tests: 5. Random SPD Systems: QuickCheck property test
- Generates random SPD matrices via
m #^# m - Tests convergence for various sizes
- Iterations: 100
-
checkCGS: Runs CGS for n iterations and verifies convergence- Takes matrix, RHS, expected solution, and iteration count
- Returns boolean indicating if residual is near zero
- Used by all convergence tests
-
prop_cgs: Property test function- Tests CGS on random SPD systems
- Guards against tiny systems (< 2 dimensions)
- Updated to mark CGS functions as ✅ RE-ENABLED
- Added status notes about tests
- Updated summary statistics:
- Originally commented out: 10 functions
- Re-enabled: 2 (CGS)
- Still commented: 8
Created comprehensive build and test documentation including:
- Build steps with stack/make commands
- Expected test outcomes
- Troubleshooting guide for common issues
- Performance notes about CGS behavior
- Verification checklist
- Addressed all review feedback
- Removed unnecessary
MonadIOconstraint - Improved code comments for clarity
- Verified test descriptions match test data
- Ran CodeQL checker
- No security vulnerabilities detected
The CGS implementation follows the standard algorithm from: Y. Saad, "Iterative Methods for Sparse Linear Systems", 2nd ed., 2000
-
Initialization:
- r₀ = b - Ax₀ (initial residual)
- r̂ = r₀ (fixed shadow residual)
- p₀ = u₀ = r₀
-
Iteration:
- α = (r·r̂) / (Ap·r̂)
- q = u - αAp
- x_{j+1} = x + α(u + q)
- r_{j+1} = r - αA(u + q)
- β = (r_{j+1}·r̂) / (r·r̂)
- u_{j+1} = r_{j+1} + βq
- p_{j+1} = u_{j+1} + β(q + βp)
All mathematical operations in the implementation match this reference.
src/Numeric/LinearAlgebra/Sparse.hs- Uncommented and exported CGS functionstest/LibSpec.hs- Added comprehensive test suiteCOMMENTED_OUT_FUNCTIONS.md- Updated documentationBUILD_AND_TEST.md- Created build/test guide (new file)IMPLEMENTATION_SUMMARY_CGS.md- This summary (new file)
- Build Verification: Run
stack buildto ensure no compilation errors - Test Execution: Run
stack testto verify all tests pass - Test Results Review: Verify all CGS tests pass consistently
-
Integration with
<\>operator: Currently, the high-levellinSolve0function is commented out. To fully integrate CGS into the user-facing API,linSolve0would need to be re-enabled. -
Preconditioner Support: For better performance on ill-conditioned systems, consider adding preconditioner support.
-
Convergence Monitoring: Add optional convergence monitoring/logging for debugging.
-
Complex Number Support: Test and verify CGS works with Complex Double types.
-
Performance Benchmarking: Compare CGS performance against other solvers (GMRES, BiCGSTAB, etc.).
- Stability: CGS can exhibit irregular convergence and may fail on ill-conditioned systems
- Residual Behavior: Residual may not decrease monotonically
- No Preconditioner: Currently no preconditioner support (future enhancement)
stack test --test-arguments "-m CGS"stack testAll tests should pass with output similar to:
Numeric.LinearAlgebra.Sparse : CGS (Conjugate Gradient Squared) (Real)
cgsInit creates initial CGS state
cgsStep performs one iteration
CGS converges on 2x2 system
CGS converges on 3x3 SPD system
QuickCheck properties for CGS:
prop_cgs : CGS converges for SPD systems
+++ OK, passed 100 tests.
The CGS solver has been successfully re-enabled with:
- ✅ Correct algorithm implementation
- ✅ Comprehensive test coverage (unit, integration, property-based)
- ✅ Updated documentation
- ✅ Code review completed
- ✅ Security scan passed
- ⏳ Pending: Build and test verification (requires network access)
Once build and tests are verified, the PR is ready for final review and merge.