Skip to content

Commit a5bf6ee

Browse files
committed
Add dgetrf_
1 parent fd47cc6 commit a5bf6ee

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The package is structured as follows:
3939
- `LAPACK` - LAPACK functions:
4040
- [x] `dgesv_` - Solve a system of linear equations
4141
- [x] `dgesvd_` - Singular Value Decomposition
42-
- [ ] `dgetrf_` - LU Decomposition
42+
- [x] `dgetrf_` - LU Decomposition
4343
- [ ] `dgetri_` - Inverse of a matrix
4444
- [ ] `dgeev_` - Eigenvalues and eigenvectors
4545
- [ ] `dpotrf_` - Cholesky decomposition

Sources/AccelerateLinux/MatrixOps/LAPACK.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,24 @@ public func dgesvd_(
5454
_ __lwork: UnsafeMutablePointer<__CLPK_integer>!,
5555
_ __info: UnsafeMutablePointer<__CLPK_integer>!
5656
)
57+
58+
/// DGETRF computes an LU factorization of a general M-by-N matrix A
59+
/// using partial pivoting with row interchanges.
60+
///
61+
/// The factorization has the form
62+
/// A = P * L * U
63+
/// where P is a permutation matrix, L is lower triangular with unit
64+
/// diagonal elements (lower trapezoidal if m > n), and U is upper
65+
/// triangular (upper trapezoidal if m < n).
66+
///
67+
/// This is the right-looking Level 3 BLAS version of the algorithm.
68+
@_silgen_name("dgetrf_")
69+
public func dgetrf_(
70+
_ __m: UnsafeMutablePointer<__CLPK_integer>!,
71+
_ __n: UnsafeMutablePointer<__CLPK_integer>!,
72+
_ __a: UnsafeMutablePointer<__CLPK_doublereal>!,
73+
_ __lda: UnsafeMutablePointer<__CLPK_integer>!,
74+
_ __ipiv: UnsafeMutablePointer<__CLPK_integer>!,
75+
_ __info: UnsafeMutablePointer<__CLPK_integer>!
76+
) -> Int32
5777
#endif // canImport(Accelerate)

Tests/AccelerateLinuxTests/MatrixTests/LAPACKTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,39 @@ struct LAPACKTests {
113113

114114
// Note: if we care about orthogonality of U and V we should check those too
115115
}
116+
117+
// https://numericalalgorithmsgroup.github.io/LAPACK_Examples/examples/doc/dgetrf_example.html
118+
@Test("dgetrf_")
119+
func test_dgetrf_() {
120+
let M = 4
121+
let N = 4
122+
let LDA = M
123+
124+
var m = __CLPK_integer(M)
125+
var n = __CLPK_integer(N)
126+
var lda = __CLPK_integer(LDA)
127+
var info = __CLPK_integer(0)
128+
129+
var ipiv = [__CLPK_integer](repeating: 0, count: Int(min(m, n)))
130+
131+
var a: [__CLPK_doublereal] = [
132+
1.80, 5.25, 1.58, -1.11,
133+
2.88, -2.95, -2.69, -0.66,
134+
2.05, -0.95, -2.90, -0.59,
135+
-0.89, -3.80, -1.04, 0.80,
136+
]
137+
138+
_ = dgetrf_(&m, &n, &a, &lda, &ipiv, &info)
139+
140+
#expect(
141+
a.map {
142+
($0 * pow(10, 2)).rounded() / pow(10, 2)
143+
} == [
144+
5.25, 0.34, 0.30, -0.21,
145+
-2.95, 3.89, -0.46, -0.33,
146+
-0.95, 2.38, -1.51, 0.00,
147+
-3.8, 0.41, 0.29, 0.13,
148+
]
149+
)
150+
}
116151
}

0 commit comments

Comments
 (0)