Is this a bug?

34 views
Skip to first unread message

Chengpu Wang

unread,
Jul 8, 2024, 11:59:20 PMJul 8
to sympy
Hi, I am a new user of SymPy.  I wonder if the following result is a bug?  Anyway, the following code is a nuance.   I am using the latest SymPy

class TestSymPy (unittest.TestCase):
    def testSin(self):
        '''
        The expansion is not ordered by x**n: x**7 before x**5
        '''
        x = sympy.Symbol('x')
        sin = sympy.sin(x)
        self.assertTupleEqual(sin.args, (x,))
        e = sin.series(x, 0, 10)
        self.assertTupleEqual(e.args, (x, -x**3/6, -x**7/5040, x**5/120, x**9/362880, sympy.O(x**10)))


emanuel.c...@gmail.com

unread,
Jul 9, 2024, 5:41:50 AMJul 9
to sympy
Doesn't matter :

```
>>> from sympy import *
>>> x=symbols("x")
>>> sin(x).series(x,0,10).args
(x, -x**3/6, -x**7/5040, x**5/120, x**9/362880, O(x**10))
```

The arguments tuple doesn't order `x` powers (and this isn't a problem, since (complex) multiplication is commutative. But

```
>>> sin(x).series(x,0,10)
x - x**3/6 + x**5/120 - x**7/5040 + x**9/362880 + O(x**10)
```

The expression is still printed with ordered powers of `x`.

HTH,

Chengpu Wang

unread,
Jul 9, 2024, 6:28:01 AMJul 9
to sympy
How do I detect which order of x?

Oscar Benjamin

unread,
Jul 9, 2024, 6:51:24 AMJul 9
to sy...@googlegroups.com
Ideally series would produce something more like a Poly object rather
than just an Add with an O() term. You can use Poly to get the
coefficients in order:

In [6]: sin(x).series(x,0,10).removeO().as_poly(x).coeffs()[::-1]
Out[6]: [1, -1/6, 1/120, -1/5040, 1/362880]

In [7]: sin(x).series(x,0,10).removeO().as_poly(x).all_coeffs()[::-1]
Out[7]: [0, 1, 0, -1/6, 0, 1/120, 0, -1/5040, 0, 1/362880]

--
Oscar
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2e33ae1e-9c71-4e58-9887-ea5dffe08e3an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages