Skip to content

Commit 990f7d3

Browse files
authored
Merge pull request #355 from isuruf/lu
wrap LUdecomposition
2 parents 7dc38eb + c969732 commit 990f7d3

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

symengine/lib/symengine.pxd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,6 @@ cdef extern from "<symengine/matrix.h>" namespace "SymEngine":
832832
DenseMatrix* static_cast_DenseMatrix "static_cast<SymEngine::DenseMatrix*>"(const MatrixBase *a)
833833
void inverse_FFLU "SymEngine::inverse_fraction_free_LU"(const DenseMatrix &A,
834834
DenseMatrix &B) nogil except +
835-
void pivoted_LU (const DenseMatrix &A, DenseMatrix &L, DenseMatrix &U, vector[int] &P) nogil except +
836835
void pivoted_LU_solve (const DenseMatrix &A, const DenseMatrix &b, DenseMatrix &x) nogil except +
837836
void inverse_GJ "SymEngine::inverse_gauss_jordan"(const DenseMatrix &A,
838837
DenseMatrix &B) nogil except +
@@ -857,6 +856,9 @@ cdef extern from "<symengine/matrix.h>" namespace "SymEngine":
857856
void dot(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &C) nogil
858857
void cross(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &C) nogil
859858

859+
cdef extern from "<symengine/matrix.h>":
860+
void pivoted_LU (const DenseMatrix &A, DenseMatrix &L, DenseMatrix &U, vector[pair[int, int]] &P) nogil except +
861+
860862
cdef extern from "<symengine/ntheory.h>" namespace "SymEngine":
861863
int probab_prime_p(const Integer &a, int reps)
862864
RCP[const Integer] nextprime (const Integer &a) nogil

symengine/lib/symengine_wrapper.pyx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,17 @@ cdef class DenseMatrixBase(MatrixBase):
36783678
deref(self.thisptr).LU(deref(L.thisptr), deref(U.thisptr))
36793679
return L, U
36803680

3681+
def LUdecomposition(self):
3682+
cdef DenseMatrixBase L = self.__class__(self.nrows(), self.ncols())
3683+
cdef DenseMatrixBase U = self.__class__(self.nrows(), self.ncols())
3684+
cdef vector[pair[int, int]] perm
3685+
symengine.pivoted_LU(
3686+
deref(symengine.static_cast_DenseMatrix(self.thisptr)),
3687+
deref(symengine.static_cast_DenseMatrix(L.thisptr)),
3688+
deref(symengine.static_cast_DenseMatrix(U.thisptr)),
3689+
perm)
3690+
return L, U, perm
3691+
36813692
def LDL(self):
36823693
cdef DenseMatrixBase L = self.__class__(self.nrows(), self.ncols())
36833694
cdef DenseMatrixBase D = self.__class__(self.nrows(), self.ncols())

symengine/tests/test_matrices.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,15 @@ def test_atoms():
685685
b = Symbol("b")
686686
X = DenseMatrix([[a, 2], [b, 4]])
687687
assert X.atoms(Symbol) == set([a, b])
688+
689+
690+
def test_LUdecomp():
691+
testmat = DenseMatrix([[0, 2, 5, 3],
692+
[3, 3, 7, 4],
693+
[8, 4, 0, 2],
694+
[-2, 6, 3, 4]])
695+
L, U, p = testmat.LUdecomposition()
696+
res = L*U
697+
for orig, new in p:
698+
res.row_swap(orig, new)
699+
assert res - testmat == zeros(4)

0 commit comments

Comments
 (0)