Skip to content

Commit 9e5d9bf

Browse files
authored
Merge pull request #115 from isuruf/series
Use symengine in series
2 parents e2a348e + 26b2986 commit 9e5d9bf

File tree

4 files changed

+34
-37
lines changed

4 files changed

+34
-37
lines changed

symengine/lib/symengine.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ cdef extern from "<symengine/ntheory.h>" namespace "SymEngine":
703703

704704
cdef extern from "<symengine/visitor.h>" namespace "SymEngine":
705705
bool has_symbol(const Basic &b, const Symbol &x) nogil except +
706+
RCP[const Basic] coeff(const Basic &b, const Basic &x, const Basic &n) nogil except +
706707
set_basic free_symbols(const Basic &b) nogil except +
707708

708709
cdef extern from "<utility>" namespace "std":

symengine/lib/symengine_wrapper.pyx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,15 @@ cdef class Basic(object):
605605
d[self] = 1
606606
return d
607607

608-
def series(ex, x=None, x0=0, n=6, method='sympy', removeO=False):
608+
def coeff(self, x, n=1):
609+
cdef Symbol _x = sympify(x)
610+
cdef Basic _n = sympify(n)
611+
return c2py(symengine.coeff(deref(self.thisptr), deref(_x.thisptr), deref(_n.thisptr)))
612+
613+
def has(self, *symbols):
614+
return any([has_symbol(self, symbol) for symbol in symbols])
615+
616+
def series(ex, x=None, x0=0, n=6, as_deg_coef_pair=False):
609617
# TODO: check for x0 an infinity, see sympy/core/expr.py
610618
# TODO: nonzero x0
611619
# underscored local vars are of symengine.py type
@@ -622,35 +630,31 @@ def series(ex, x=None, x0=0, n=6, method='sympy', removeO=False):
622630
if not _x in syms:
623631
return _ex
624632

625-
if len(syms) > 1 or method == 'sympy':
626-
from sympy import series as sy_series
627-
return sy_series(_ex._sympy_(), _x._sympy_(), x0, n)
628-
elif method == 'ring_series':
629-
from sympy.polys.ring_series import rs_series
630-
return rs_series(_ex._sympy_(), _x._sympy_(), n).as_expr().subs(x,x-x0)
631-
elif method != 'symengine':
632-
raise ValueError('unknown method in series()')
633+
if x0 != 0:
634+
_ex = _ex.subs({_x: _x + x0})
633635

634636
cdef RCP[const symengine.Symbol] X = symengine.rcp_static_cast_Symbol(_x.thisptr)
635-
cdef unsigned int N = n
636637
cdef umap_int_basic umap
637638
cdef umap_int_basic_iterator iter, iterend
638-
cdef Basic coef
639639

640-
umap = deref(symengine.series(_ex.thisptr, X, N)).as_dict()
640+
if not as_deg_coef_pair:
641+
b = c2py(<symengine.RCP[const symengine.Basic]>deref(symengine.series(_ex.thisptr, X, n)).as_basic())
642+
if x0 != 0:
643+
b = b.subs({_x: _x - x0})
644+
return b
645+
646+
umap = deref(symengine.series(_ex.thisptr, X, n)).as_dict()
641647

642-
from sympy import Add as sAdd, Pow as sPow, O as sO
643648
iter = umap.begin()
644649
iterend = umap.end()
645650
poly = 0
646651
l = []
647652
while iter != iterend:
648-
coef = c2py(<symengine.RCP[const symengine.Basic]>(deref(iter).second))
649-
l.append(sPow(_x,(deref(iter).first)) * coef)
653+
l.append([deref(iter).first, c2py(<symengine.RCP[const symengine.Basic]>(deref(iter).second))])
650654
inc(iter)
651-
if removeO is False:
652-
l.append(sO(sPow(_x, n)))
653-
return sAdd(*l)
655+
if as_deg_coef_pair:
656+
return l
657+
return add(*l)
654658

655659

656660
cdef class Symbol(Basic):
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
from symengine.utilities import raises
2-
3-
from sympy import Symbol, Integer, sin, cos, exp, sqrt, E
4-
from symengine.lib.symengine_wrapper import series, have_piranha, have_flint, Symbol as SESymbol
2+
from symengine.lib.symengine_wrapper import (series, have_piranha, have_flint,
3+
Symbol, Integer, sin, cos, exp, sqrt, E)
54

65
def test_series_expansion():
7-
if have_piranha:
8-
x = SESymbol('x')
9-
ex = series(sin(1+x), x, n=10, method='symengine')
10-
assert ex.coeff(x,7) == -cos(1)/5040
11-
12-
if not have_flint and not have_piranha:
13-
return
6+
x = Symbol('x')
7+
ex = series(sin(1+x), x, n=10)
8+
assert ex.coeff(x,7) == -cos(1)/5040
149

1510
x = Symbol('x')
16-
ex = series(1/(1-x), x, n=10, method='symengine')
17-
assert ex.coeff(x,9) == 1
18-
x = SESymbol('x')
19-
ex = series(1/(1-x), x, n=10, method='symengine')
11+
ex = series(1/(1-x), x, n=10)
2012
assert ex.coeff(x,9) == 1
21-
ex = series(sin(x)*cos(x), x, n=10, method='symengine')
13+
ex = series(sin(x)*cos(x), x, n=10)
2214
assert ex.coeff(x,8) == 0
2315
assert ex.coeff(x,9) == Integer(2)/Integer(2835)
2416

25-
ex = series(E**x, x, n=10, method='symengine')
17+
ex = series(E**x, x, n=10)
2618
assert ex.coeff(x,9) == Integer(1)/Integer(362880)
27-
ex1 = series(1/sqrt(4-x), x, n=50, method='symengine')
28-
ex2 = series((4-x)**(Integer(-1)/Integer(2)), x, n=50, method='symengine')
19+
ex1 = series(1/sqrt(4-x), x, n=50)
20+
ex2 = series((4-x)**(Integer(-1)/Integer(2)), x, n=50)
2921
assert ex1.coeff(x,49) == ex2.coeff(x,49)
3022

symengine_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d44e20c7437f6c6795a97354a213983abc77c9fe
1+
0172e47349b7d158ee8a7186eb0a19a7f155a225

0 commit comments

Comments
 (0)