cot(0)=0? and x * cot(x) evaluated near 0

34 views
Skip to first unread message

Scott

unread,
Jun 2, 2010, 11:12:38 AM6/2/10
to sympy
What is the best way to evaluate x * cot(x) evaluated for x=0-pi/2
with sympy?

Is there a better option than coding the Taylor series approximation?

Also with the sympy that shipped with Ubuntu 10.04 sympy.cot(0) is 0
rather than infinity.

V/R

Scott

Aaron S. Meurer

unread,
Jun 2, 2010, 12:41:31 PM6/2/10
to sy...@googlegroups.com

On Jun 2, 2010, at 9:12 AM, Scott wrote:

> What is the best way to evaluate x * cot(x) evaluated for x=0-pi/2
> with sympy?

I am not too sure what you mean by 0-pi/2, but you could try limit():


In [3]: limit(x*cot(x), x, 0)
Out[3]: 1

In [4]: limit(x*cot(x), x, pi/2)
Out[4]: 0


>
> Is there a better option than coding the Taylor series approximation?

You wouldn't need to code the taylor series, it already is implemented:

In [7]: print (x*cot(x)).series(x)
1 - x**2/3 - x**4/45 - 2*x**6/945 + O(x**7)

>
> Also with the sympy that shipped with Ubuntu 10.04 sympy.cot(0) is 0
> rather than infinity.

This is a bug that still exists in master. Could you report it in the issues?

Aaron Meurer
>
> V/R
>
> Scott
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Scott

unread,
Jun 2, 2010, 2:02:29 PM6/2/10
to sympy
Aaron

Thanks for the tips.

Where are the "issues" located?

I am numerically evaluating x*cos(x)/sin(x) on [-pi/2,pi/2] and the
spurious singularity at x= 0 is giving me grief. x/sin(x)=1 at x=0.

After looking at my problem it seems that I should have asked if there
is and efficient way to embed sin(x)/x or x/sin(x) in a function that
is evaluated at 0. I will probably use a 7th order Taylor series
unless there another clever option.

The series for x/sin(x) has much better convergence than the series
for x*cot(x) in my range of interest (+- pi/2).

In [41]: (x/sin(x)).series(x, 0, 8)
Out[41]: 1 + x**2/6 + 7*x**4/360 + 31*x**6/15120 + O(x**7)

Aaron S. Meurer

unread,
Jun 2, 2010, 2:19:01 PM6/2/10
to sy...@googlegroups.com

On Jun 2, 2010, at 12:02 PM, Scott wrote:

> Aaron
>
> Thanks for the tips.
>
> Where are the "issues" located?

http://code.google.com/p/sympy/issues/


>
> I am numerically evaluating x*cos(x)/sin(x) on [-pi/2,pi/2] and the
> spurious singularity at x= 0 is giving me grief. x/sin(x)=1 at x=0.
>
> After looking at my problem it seems that I should have asked if there
> is and efficient way to embed sin(x)/x or x/sin(x) in a function that
> is evaluated at 0. I will probably use a 7th order Taylor series
> unless there another clever option.

You can look at lambdify. I don't know much about it, not being much of a numerical person, but it always seems to be the answer in these situations. Maybe someone else can be more concrete.

Aaron Meurer

wflynny

unread,
Jun 2, 2010, 3:03:32 PM6/2/10
to sympy
Lambdify definitely helps evaluate things numerically. For example:

In [1]: import sympy as sp
In [2]: x = sp.var('x')
In [3]: func = sp.lambdify(x, x/sp.sin(x))

In [4]: func(1)
Out[4]: 1.1883951057781212

In [5]: func(0.01)
Out[5]: 1.0000166668611132

However, the problem with x=0 still exists:

In [6]: func(0)
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call
last)

C:\Documents and Settings\wflynn\My Documents\Python\<ipython console>
in <module>()

C:\Documents and Settings\wflynn\My Documents\Python\<string> in
<lambda>(x)

ZeroDivisionError: float division

I am not sure if this helps but I thought I would at least show an
example of lambdify since I use it a lot.

Bill

Fredrik Johansson

unread,
Jun 2, 2010, 4:14:06 PM6/2/10
to sy...@googlegroups.com
On Wed, Jun 2, 2010 at 8:02 PM, Scott <scott...@yahoo.com> wrote:
Aaron

Thanks for the tips.

Where are the "issues" located?

I am numerically evaluating x*cos(x)/sin(x) on [-pi/2,pi/2] and the
spurious singularity at x= 0 is giving me grief. x/sin(x)=1 at x=0.

After looking at my problem it seems that I should have asked if there
is and efficient way to embed sin(x)/x or x/sin(x) in a function that
is evaluated at 0. I will probably use a 7th order Taylor series
unless there another clever option.

The series for x/sin(x) has much better convergence than the series
for x*cot(x) in my range of interest (+- pi/2).

In [41]: (x/sin(x)).series(x, 0, 8)
Out[41]: 1 + x**2/6 + 7*x**4/360 + 31*x**6/15120 + O(x**7)


SymPy is missing the sinc function. I created an issue: http://code.google.com/p/sympy/issues/detail?id=1952

If you want to have a go at implementing this function (it shouldn't be too hard), see sympy/functions/elementary/trigonometric.py

Fredrik

Scott

unread,
Jun 2, 2010, 4:37:07 PM6/2/10
to sympy

I submitted the issue for the cot(0)=0

In [41]: (x/sin(x)).series(x, 0, 8)
Out[41]: 1 + x**2/6 + 7*x**4/360 + 31*x**6/15120 + O(x**7)

How do I take the output from series extract the coefficient and use
it via poly1d ala taylor in mpmath?

Knowing that sin( x)/x= sinc(c) was a great tip.

Thanks

Scott

Scott

unread,
Jun 2, 2010, 8:48:10 PM6/2/10
to sympy

The sinc function in mpmath meets most of my needs. I inspected the
source code for sympy.sin but the algorithm for determining the
approximations of sin(X) escaped my attention.

I am still having trouble find examples of moving between polynomials
and their coefficient arrays.

Cheers

Scott

Ted Horst

unread,
Jun 2, 2010, 8:57:09 PM6/2/10
to sy...@googlegroups.com
If you are interested in numeric evaluation, sympy also includes
mpmath which does include sinc.

>>> sympy.mpmath.sinc(0)
mpf('1.0')

This shouldn't be necessary once sympy gets sinc itself since it will
use mpmath transparently for numeric calculations.

Ted

smichr

unread,
Jun 4, 2010, 2:40:31 AM6/4/10
to sympy
> > In [41]: (x/sin(x)).series(x, 0, 8)
> > Out[41]: 1 + x**2/6 + 7*x**4/360 + 31*x**6/15120 + O(x**7)
>
> > How do I take the output from series extract the coefficient and use
> > it via poly1d ala taylor in mpmath?
>
>>> (x/sin(x)).series(x, 0, 8)
1 + x**2/6 + 7*x**4/360 + 31*x**6/15120 + O(x**7)
>>> _.as_independent(p.getO())
(1 + x**2/6 + 7*x**4/360 + 31*x**6/15120, O(x**7))
>>> Poly(_[0]).all_coeffs()
[31/15120, 0, 7/360, 0, 1/6, 0, 1]
>>> list(reversed(_))
[1, 0, 1/6, 0, 7/360, 0, 31/15120]

I'm not sure what order you need them in...if you need them from
leading to constant, just don't reverse the all_coeffs() result.

smichr

unread,
Jun 4, 2010, 2:51:57 AM6/4/10
to sympy
Just after sending I see there is the removeO method, so the above can
be done as

>>> (x/sin(x)).series(x, 0, 8)
1 + x**2/6 + 7*x**4/360 + 31*x**6/15120 + O(x**7)
>>> Poly(_.removeO()).all_coeffs()
[31/15120, 0, 7/360, 0, 1/6, 0, 1]

and sorting as necessary.

Scott

unread,
Jun 5, 2010, 7:51:44 AM6/5/10
to sympy

In [74]: a=(x/sin(x)).series(x, 0, 8)

In [75]: Poly(a.removeO(),x).coeffs
Out[75]: (31/15120, 7/360, 1/6, 1)

The remove0 method is perfect.

The Poly.all_coeffs() does not exist in my sympy 0.6.6 but coeffs did
work.

Cheers Scott

smichr

unread,
Jun 6, 2010, 7:30:16 AM6/6/10
to sympy
coeffs will only give the non-zero coefficients, so if you had 3*x**2
- 2, you will not get a 0 in the coefficients given by coeffs:

>>> Poly(3*x**2-2).coeffs()
[3, -2]
>>>

So watch out for that.
Reply all
Reply to author
Forward
0 new messages