Skip to content

Fix NormalCG shape bug with rectangular matrices#186

Closed
jpbrodrick89 wants to merge 1 commit intopatrick-kidger:mainfrom
jpbrodrick89:jpb/fixNormalCG
Closed

Fix NormalCG shape bug with rectangular matrices#186
jpbrodrick89 wants to merge 1 commit intopatrick-kidger:mainfrom
jpbrodrick89:jpb/fixNormalCG

Conversation

@jpbrodrick89
Copy link
Contributor

@jpbrodrick89 jpbrodrick89 commented Jan 17, 2026

The bug was introduced in commit 6893255 which moved the preconditioner_and_y0() call before the vector transformation for NormalCG mode. This caused y0 to be initialized in the wrong space. This was not caught previously because NormalCG was omitted from rectangular matrix tests! I realise this is fixed on dev with super clean and awesome lx.Normal implementation! ❤️ But this provides a temporary fix on main until dev is merged in, these changes can be ignored at that point.

For rectangular matrices (m×n where m>n), NormalCG transforms the vector from output space (m,) to input space (n,) via A^T. The y0 initial guess must be in this transformed space, but was being initialized before the transformation.

This fix:

  • Moves preconditioner_and_y0() to after vector transformation
  • Preserves the preconditioner transformation logic added in 6893255
  • Ensures y0 has the correct shape for both square and rectangular matrices
  • Adds NormalCG to rectangular matrix tests to prevent regression

Fixes issue with NormalCG failing on overdetermined systems with: TypeError: dot_general requires contracting dimensions to have the same shape, got (3,) and (6,).

The bug was introduced in commit 6893255 which moved the
preconditioner_and_y0() call before the vector transformation for
NormalCG mode. This caused y0 to be initialized in the wrong space.

For rectangular matrices (m×n where m>n), NormalCG transforms the
vector from output space (m,) to input space (n,) via A^T. The y0
initial guess must be in this transformed space, but was being
initialized before the transformation.

This fix:
- Moves preconditioner_and_y0() to after vector transformation
- Preserves the preconditioner transformation logic added in 6893255
- Ensures y0 has the correct shape for both square and rectangular matrices
- Adds NormalCG to rectangular matrix tests to prevent regression

Fixes issue with NormalCG failing on overdetermined systems with:
TypeError: dot_general requires contracting dimensions to have the
same shape, got (3,) and (6,).

All 3587 tests pass.
@patrick-kidger
Copy link
Owner

Thanks for the fix! I am actually (finally...) about to do a new release over in #189, which I think will supersede this.

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