Skip to content

Commit 64fa455

Browse files
authored
Merge pull request #298 from cbehan/master
Matrix factorization follow-up
2 parents 7643152 + c67e4dc commit 64fa455

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

symengine/lib/symengine.pxd

+3-1
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,9 @@ cdef extern from "<symengine/matrix.h>" namespace "SymEngine":
789789
void LDL(MatrixBase &L, MatrixBase &D) nogil
790790
void LU_solve(const MatrixBase &b, MatrixBase &x) nogil
791791
void FFLU(MatrixBase &LU) nogil
792-
void FFLDU(MatrixBase&L, MatrixBase &D, MatrixBase &U) nogil
792+
void FFLDU(MatrixBase &L, MatrixBase &D, MatrixBase &U) nogil
793+
void QR(MatrixBase &Q, MatrixBase &R) nogil
794+
void cholesky(MatrixBase &L) nogil
793795

794796
cdef cppclass DenseMatrix(MatrixBase):
795797
DenseMatrix()

symengine/lib/symengine_wrapper.pyx

+11
Original file line numberDiff line numberDiff line change
@@ -3604,6 +3604,17 @@ cdef class DenseMatrixBase(MatrixBase):
36043604
deref(self.thisptr).FFLDU(deref(L.thisptr), deref(D.thisptr), deref(U.thisptr))
36053605
return L, D, U
36063606

3607+
def QR(self):
3608+
cdef DenseMatrixBase Q = self.__class__(self.nrows(), self.ncols())
3609+
cdef DenseMatrixBase R = self.__class__(self.nrows(), self.ncols())
3610+
deref(self.thisptr).QR(deref(Q.thisptr), deref(R.thisptr))
3611+
return Q, R
3612+
3613+
def cholesky(self):
3614+
cdef DenseMatrixBase L = self.__class__(self.nrows(), self.ncols())
3615+
deref(self.thisptr).cholesky(deref(L.thisptr))
3616+
return L
3617+
36073618
def jacobian(self, x):
36083619
cdef DenseMatrixBase x_ = sympify(x)
36093620
cdef DenseMatrixBase R = self.__class__(self.nrows(), x.nrows())

symengine/tests/test_matrices.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from symengine import symbols
22
from symengine.lib.symengine_wrapper import (DenseMatrix, Symbol, Integer,
3-
function_symbol, I, NonSquareMatrixError, ShapeError, zeros, ones, eye,
4-
ImmutableMatrix)
3+
Rational, function_symbol, I, NonSquareMatrixError, ShapeError, zeros,
4+
ones, eye, ImmutableMatrix)
55
from symengine.utilities import raises
66

77

@@ -353,6 +353,25 @@ def test_FFLDU():
353353
assert U == DenseMatrix(3, 3, [1, 2, 3, 0, -13, -13, 0, 0, 91])
354354

355355

356+
def test_QR():
357+
A = DenseMatrix(3, 3, [12, -51, 4, 6, 167, -68, -4, 24, -41])
358+
Q, R = A.QR()
359+
360+
assert Q == DenseMatrix(3, 3, [Rational(6, 7), Rational(-69, 175),
361+
Rational(-58, 175), Rational(3, 7),
362+
Rational(158, 175), Rational(6, 175),
363+
Rational(-2, 7), Rational(6, 35),
364+
Rational(-33, 35)])
365+
assert R == DenseMatrix(3, 3, [14, 21, -14, 0, 175, -70, 0, 0, 35])
366+
367+
368+
def test_cholesky():
369+
A = DenseMatrix(3, 3, [4, 12, -16, 12, 37, -43, -16, -43, 98])
370+
L = A.cholesky()
371+
372+
assert L == DenseMatrix(3, 3, [2, 0, 0, 6, 1, 0, -8, 5, 3])
373+
374+
356375
def test_str_repr():
357376
d = DenseMatrix(3, 2, [1, 2, 3, 4, 5, 6])
358377
assert str(d) == '[1, 2]\n[3, 4]\n[5, 6]\n'

0 commit comments

Comments
 (0)