Coverage for src/cvx/linalg/decomposition/qr.py: 100%

8 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-03 18:56 +0000

1"""QR decomposition helpers.""" 

2 

3from __future__ import annotations 

4 

5import numpy as np 

6 

7from ..core.exceptions import NotAMatrixError 

8from ..core.types import Matrix 

9 

10 

11def qr(matrix: Matrix) -> tuple[Matrix, Matrix]: 

12 """Compute the reduced QR decomposition of a 2-D matrix. 

13 

14 Args: 

15 matrix: Input matrix with shape ``(m, n)``. 

16 

17 Returns: 

18 A tuple ``(Q, R)`` matching ``np.linalg.qr(matrix, mode="reduced")``. 

19 

20 Raises: 

21 NotAMatrixError: If ``matrix`` is not two-dimensional. 

22 

23 Example: 

24 >>> import numpy as np 

25 >>> from cvx.linalg import qr 

26 >>> q, r = qr(np.array([[1.0, 2.0], [3.0, 4.0]])) 

27 >>> np.allclose(q @ r, np.array([[1.0, 2.0], [3.0, 4.0]])) 

28 True 

29 """ 

30 if matrix.ndim != 2: 

31 raise NotAMatrixError(matrix.ndim, func="qr") 

32 

33 return np.linalg.qr(matrix, mode="reduced")