Skip to content

Commit d6fc03d

Browse files
committed
Add tests for numbers and matrices and trace
1 parent 08afb47 commit d6fc03d

File tree

5 files changed

+189
-2
lines changed

5 files changed

+189
-2
lines changed

symengine/lib/symengine.pxd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ cdef extern from "<symengine/number.h>" namespace "SymEngine":
348348
cdef cppclass NumberWrapper(Basic):
349349
pass
350350
cdef int is_zero(const Basic &x) nogil
351+
cdef int is_positive(const Basic &x) nogil
352+
cdef int is_negative(const Basic &x) nogil
353+
cdef int is_nonnegative(const Basic &x) nogil
354+
cdef int is_nonpositive(const Basic &x) nogil
355+
cdef int is_real(const Basic &x) nogil
351356

352357
cdef extern from "pywrapper.h" namespace "SymEngine":
353358
cdef cppclass PyNumber(NumberWrapper):
@@ -814,6 +819,14 @@ cdef extern from "<symengine/matrix.h>" namespace "SymEngine":
814819
void col_insert(const DenseMatrix &B, unsigned pos) nogil
815820
void row_del(unsigned k) nogil
816821
void col_del(unsigned k) nogil
822+
rcp_const_basic trace() nogil
823+
int is_zero() nogil
824+
int is_real() nogil
825+
int is_diagonal() nogil
826+
int is_symmetric() nogil
827+
int is_hermitian() nogil
828+
int is_weakly_diagonally_dominant() nogil
829+
int is_strictly_diagonally_dominant() nogil
817830

818831
bool is_a_DenseMatrix "SymEngine::is_a<SymEngine::DenseMatrix>"(const MatrixBase &b) nogil
819832
DenseMatrix* static_cast_DenseMatrix "static_cast<SymEngine::DenseMatrix*>"(const MatrixBase *a)

symengine/lib/symengine_wrapper.pyx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,26 @@ cdef class Basic(object):
10721072
def is_zero(self):
10731073
return is_zero(self)
10741074

1075+
@property
1076+
def is_positive(self):
1077+
return is_positive(self)
1078+
1079+
@property
1080+
def is_negative(self):
1081+
return is_negative(self)
1082+
1083+
@property
1084+
def is_nonpositive(self):
1085+
return is_nonpositive(self)
1086+
1087+
@property
1088+
def is_nonnegative(self):
1089+
return is_nonnegative(self)
1090+
1091+
@property
1092+
def is_real(self):
1093+
return is_real(self)
1094+
10751095
def copy(self):
10761096
return self
10771097

@@ -3576,6 +3596,37 @@ cdef class DenseMatrixBase(MatrixBase):
35763596
def H(self):
35773597
return self.conjugate_transpose()
35783598

3599+
def trace(self):
3600+
return c2py(deref(symengine.static_cast_DenseMatrix(self.thisptr)).trace())
3601+
3602+
@property
3603+
def is_zero_matrix(self):
3604+
return tribool(deref(symengine.static_cast_DenseMatrix(self.thisptr)).is_zero())
3605+
3606+
@property
3607+
def is_real_matrix(self):
3608+
return tribool(deref(symengine.static_cast_DenseMatrix(self.thisptr)).is_real())
3609+
3610+
@property
3611+
def is_diagonal(self):
3612+
return tribool(deref(symengine.static_cast_DenseMatrix(self.thisptr)).is_diagonal())
3613+
3614+
@property
3615+
def is_symmetric(self):
3616+
return tribool(deref(symengine.static_cast_DenseMatrix(self.thisptr)).is_symmetric())
3617+
3618+
@property
3619+
def is_hermitian(self):
3620+
return tribool(deref(symengine.static_cast_DenseMatrix(self.thisptr)).is_hermitian())
3621+
3622+
@property
3623+
def is_weakly_diagonally_dominant(self):
3624+
return tribool(deref(symengine.static_cast_DenseMatrix(self.thisptr)).is_weakly_diagonally_dominant())
3625+
3626+
@property
3627+
def is_strongly_diagonally_dominant(self):
3628+
return tribool(deref(symengine.static_cast_DenseMatrix(self.thisptr)).is_strictly_diagonally_dominant())
3629+
35793630
@property
35803631
def T(self):
35813632
return self.transpose()
@@ -5159,6 +5210,36 @@ def is_zero(expr):
51595210
return tribool(tbool)
51605211

51615212

5213+
def is_positive(expr):
5214+
cdef Basic expr_ = sympify(expr)
5215+
cdef int tbool = symengine.is_positive(deref(expr_.thisptr))
5216+
return tribool(tbool)
5217+
5218+
5219+
def is_negative(expr):
5220+
cdef Basic expr_ = sympify(expr)
5221+
cdef int tbool = symengine.is_negative(deref(expr_.thisptr))
5222+
return tribool(tbool)
5223+
5224+
5225+
def is_nonpositive(expr):
5226+
cdef Basic expr_ = sympify(expr)
5227+
cdef int tbool = symengine.is_nonpositive(deref(expr_.thisptr))
5228+
return tribool(tbool)
5229+
5230+
5231+
def is_nonnegative(expr):
5232+
cdef Basic expr_ = sympify(expr)
5233+
cdef int tbool = symengine.is_nonnegative(deref(expr_.thisptr))
5234+
return tribool(tbool)
5235+
5236+
5237+
def is_real(expr):
5238+
cdef Basic expr_ = sympify(expr)
5239+
cdef int tbool = symengine.is_real(deref(expr_.thisptr))
5240+
return tribool(tbool)
5241+
5242+
51625243
def set_union(*args):
51635244
cdef symengine.set_set s
51645245
cdef Set e_

symengine/tests/test_matrices.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,70 @@ def test_conjugate_transpose():
321321
assert A.conjugate_transpose() == DenseMatrix(2, 2, [1, 3, 2, -I])
322322

323323

324+
def test_trace():
325+
A = DenseMatrix(2, 2, [1, 2, 3, 4])
326+
assert A.trace() == 5
327+
328+
329+
def test_is_zero_matrix():
330+
A = DenseMatrix(2, 2, [1, 2, 3, I])
331+
assert not A.is_zero_matrix
332+
B = DenseMatrix(1, 1, [Symbol('x')])
333+
assert B.is_zero_matrix is None
334+
C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 0, 0])
335+
assert C.is_zero_matrix
336+
337+
338+
def test_is_real_matrix():
339+
A = DenseMatrix(2, 2, [1, 2, 3, I])
340+
assert not A.is_real_matrix
341+
B = DenseMatrix(1, 1, [Symbol('x')])
342+
assert B.is_real_matrix is None
343+
C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 0, 0])
344+
assert C.is_real_matrix
345+
346+
347+
def test_is_diagonal():
348+
A = DenseMatrix(2, 2, [1, 0, 0, I])
349+
assert A.is_diagonal
350+
B = DenseMatrix(1, 1, [Symbol('x')])
351+
assert B.is_diagonal
352+
C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 2, 0])
353+
assert not C.is_diagonal
354+
355+
356+
def test_is_symmetric():
357+
A = DenseMatrix(2, 2, [1, 3, 2, I])
358+
assert not A.is_symmetric
359+
B = DenseMatrix(1, 1, [Symbol('x')])
360+
assert B.is_symmetric
361+
C = DenseMatrix(3, 3, [0, 0, 0, 0, 0, 0, 0, 2, 0])
362+
assert not C.is_symmetric
363+
364+
365+
def test_is_hermitian():
366+
A = DenseMatrix(2, 2, [1, 3, 2, I])
367+
assert not A.is_hermitian
368+
B = DenseMatrix(1, 1, [Symbol('x')])
369+
assert B.is_hermitian is None
370+
C = DenseMatrix(3, 3, [0, I, 0, 0, 0, 0, 0, 2, 0])
371+
assert not C.is_hermitian
372+
373+
374+
def test_is_weakly_diagonally_dominant():
375+
A = DenseMatrix(2, 2, [2, 1, 1, 2])
376+
assert A.is_weakly_diagonally_dominant
377+
C = DenseMatrix(3, 3, [Symbol('x'), 0, 0, 0, 3, 0, 0, 0, 4])
378+
assert C.is_weakly_diagonally_dominant is None
379+
380+
381+
def test_is_strongly_diagonally_dominant():
382+
A = DenseMatrix(2, 2, [2, 1, 1, 2])
383+
assert A.is_strongly_diagonally_dominant
384+
C = DenseMatrix(3, 3, [Symbol('x'), 2, 0, 0, 4, 0, 0, 0, 4])
385+
assert C.is_strongly_diagonally_dominant is None
386+
387+
324388
def test_LU():
325389
A = DenseMatrix(3, 3, [1, 3, 5, 2, 5, 6, 8, 3, 1])
326390
L, U = A.LU()

symengine/tests/test_number.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from symengine.utilities import raises
22

3-
from symengine import Integer, I, S, Symbol, pi
3+
from symengine import Integer, I, S, Symbol, pi, Rational
44
from symengine.lib.symengine_wrapper import (perfect_power, is_square, integer_nthroot)
55

66

@@ -155,3 +155,32 @@ def test_integer_nthroot():
155155

156156
def test_is_zero():
157157
assert Symbol('x').is_zero is None
158+
159+
160+
def test_is_positive():
161+
assert Rational(1, 2).is_positive
162+
assert not Rational(-2, 3).is_positive
163+
assert Symbol('x').is_positive is None
164+
165+
166+
def test_is_negative():
167+
assert not Rational(1, 2).is_negative
168+
assert Rational(-2, 3).is_negative
169+
assert Symbol('x').is_negative is None
170+
171+
172+
def test_is_nonpositive():
173+
assert not Rational(1, 2).is_nonpositive
174+
assert Rational(-2, 3).is_nonpositive
175+
assert Symbol('x').is_nonpositive is None
176+
177+
178+
def test_is_nonnegative():
179+
assert Rational(1, 2).is_nonnegative
180+
assert not Rational(-2, 3).is_nonnegative
181+
assert Symbol('x').is_nonnegative is None
182+
183+
184+
def test_is_real():
185+
assert Rational(1, 2).is_real
186+
assert Symbol('x').is_real is None

symengine_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
46090cfd20b92e73b6ddd8c203e3173b106adabb
1+
44eb47e3bbfa7e06718f2f65f3f41a0a9d133b70

0 commit comments

Comments
 (0)