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

7 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-19 05:40 +0000

1"""QR decomposition helpers.""" 

2 

3from __future__ import annotations 

4 

5import numpy as np 

6 

7from .exceptions import DimensionMismatchError 

8 

9 

10def qr(matrix: np.ndarray) -> tuple[np.ndarray, np.ndarray]: 

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

12 

13 Args: 

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

15 

16 Returns: 

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

18 

19 Raises: 

20 DimensionMismatchError: If ``matrix`` is not two-dimensional. 

21 

22 Example: 

23 >>> import numpy as np 

24 >>> from cvx.linalg import qr 

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

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

27 True 

28 """ 

29 if matrix.ndim != 2: 

30 raise DimensionMismatchError(matrix.ndim, 2) 

31 

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