Preparse is the problem? Sage not compute FFT

22 views
Skip to first unread message

Rafael Costa

unread,
Jul 25, 2009, 4:29:01 PM7/25/09
to sage-devel
Why Sage not execute the python code?

from scipy import *
from pylab import *

sample_rate=1000.00
t=r_[0:0.6:1/sample_rate]
N=len(t)
s=sin(2*pi*50*t)+sin(2*pi*70*t+pi/4)
S=fft(s)
f=sample_rate*r_[0:(N/2)]/N
n=len(f)
plot(f,abs(S[0:n])/N)


Thank!


Rafael

Rafael Cardoso Dias Costa

unread,
Jul 25, 2009, 4:38:17 PM7/25/09
to sage-devel
error message:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/sage/sagenb/sage_notebook/worksheets/rfcard/0/code/12.py", line 15, in <module>
S=fft(s)
File "", line 1, in <module>

File "/home/sage/sage_install/sage/local/lib/python2.6/site-packages/numpy/fft/fftpack.py", line 159, in fft
return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache)
File "/home/sage/sage_install/sage/local/lib/python2.6/site-packages/numpy/fft/fftpack.py", line 66, in _raw_fft
r = work_function(a, wsave)
TypeError: array cannot be safely cast to required type
--
Rafael Cardoso Dias Costa
Engenheiro de Controle e Automação - UFMG
Mestrando em Engenharia Química - Controle de Processos - UFRGS
+55 51 8541-0926 :: Porto Alegre - RS
+55 51 8430-1421 :: Porto Alegre - RS
+55 31 9165-5488 :: Belo Horizonte - MG
+55 31 3466-0146 :: Residência em Belo Horizonte - MG

Rafael Cardoso Dias Costa

unread,
Jul 25, 2009, 4:40:49 PM7/25/09
to sage-devel
My problem is sage-support!

Thank!

David Joyner

unread,
Jul 25, 2009, 4:59:17 PM7/25/09
to sage-...@googlegroups.com
This work in pure python.

The problem in sage is with the line

s = sin(2*pi*50*t)+sin(2*pi*70*t+pi/4)

However, I don't know how to get Sage to
evaluate the sin of a numpy.ndarray.
For example,

sage: sample_rate=1000.00
sage: import numpy
sage: s=numpy.sin(2*numpy.pi*50*t)+numpy.sin(2*numpy.pi*70*t+numpy.pi/4)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)

/home/wdj/.sage/temp/hera/27292/_home_wdj__sage_init_sage_0.py in <module>()

AttributeError: sin




On Sat, Jul 25, 2009 at 4:40 PM, Rafael Cardoso Dias

Tim Lahey

unread,
Jul 25, 2009, 5:24:52 PM7/25/09
to sage-...@googlegroups.com

On Jul 25, 2009, at 4:59 PM, David Joyner wrote:

>
> This work in pure python.
>
> The problem in sage is with the line
>
> s = sin(2*pi*50*t)+sin(2*pi*70*t+pi/4)
>
> However, I don't know how to get Sage to
> evaluate the sin of a numpy.ndarray.
> For example,
>
> sage: sample_rate=1000.00
> sage: import numpy
> sage: s=numpy.sin(2*numpy.pi*50*t)+numpy.sin(2*numpy.pi*70*t
> +numpy.pi/4)
> ---------------------------------------------------------------------------
> AttributeError Traceback (most recent
> call last)
>
> /home/wdj/.sage/temp/hera/27292/_home_wdj__sage_init_sage_0.py in
> <module>()
>
> AttributeError: sin


Won't Sage preparse it so the 2, 50, 70, and 4 become Sage integers?

If I recall correctly, putting r after the appropriate constants
should fix it.

Cheers,

Tim.

---
Tim Lahey
PhD Candidate, Systems Design Engineering
University of Waterloo
http://www.linkedin.com/in/timlahey

Minh Nguyen

unread,
Jul 25, 2009, 5:30:25 PM7/25/09
to sage-...@googlegroups.com
Hi Rafael,

Well, I get this far:

----------------------------------------------------------------------
| Sage Version 4.1, Release Date: 2009-07-09 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: from scipy import *
sage: from pylab import *
sage: sample_rate = 1000.0
sage: t = r_[0:0.6:1/sample_rate]
sage: N = len(t)
sage: s = [sin(2*pi*50*elem) + sin(2*pi*70*elem + (pi/4)) for elem in t]
sage: S = fft(s)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/home/mvngu/.sage/temp/sage.math.washington.edu/6349/_home_mvngu__sage_init_sage_0.py
in <module>()

/usr/local/sage/local/lib/python2.6/site-packages/numpy/fft/fftpack.pyc
in fft(a, n, axis)
157 """
158
--> 159 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf,
_fft_cache)
160
161

/usr/local/sage/local/lib/python2.6/site-packages/numpy/fft/fftpack.pyc
in _raw_fft(a, n, axis, init_function, work_function, fft_cache)
64 if axis != -1:
65 a = swapaxes(a, axis, -1)
---> 66 r = work_function(a, wsave)
67 if axis != -1:
68 r = swapaxes(r, axis, -1)

TypeError: array cannot be safely cast to required type


The problem is that Sage and NumPy do not yet talk to each other very
well. The relevant tickets to accomplish this are:

#6497
http://trac.sagemath.org/sage_trac/ticket/6497

#5081
http://trac.sagemath.org/sage_trac/ticket/5081

#6506
http://trac.sagemath.org/sage_trac/ticket/6506

--
Regards
Minh Van Nguyen

Jason Grout

unread,
Jul 25, 2009, 9:30:13 PM7/25/09
to sage-...@googlegroups.com
Minh Nguyen wrote:

>
> The problem is that Sage and NumPy do not yet talk to each other very
> well. The relevant tickets to accomplish this are:
>
> #6497
> http://trac.sagemath.org/sage_trac/ticket/6497
>
> #5081
> http://trac.sagemath.org/sage_trac/ticket/5081
>
> #6506
> http://trac.sagemath.org/sage_trac/ticket/6506
>


These tickets need reviews, so if anyone really wants to see them in the
next version of Sage (which has a feature-freeze of tonight, I think),
please review them!

Jason

Minh Nguyen

unread,
Jul 25, 2009, 9:40:18 PM7/25/09
to sage-...@googlegroups.com

That's right. As of 27th July 2009, there will be a feature freeze
window until the release of Sage 4.1.1. Of course, the timeline for
merging features is not as rigid as one thinks, but is open to
negotiation.

Jason Grout

unread,
Jul 25, 2009, 9:40:49 PM7/25/09
to sage-...@googlegroups.com


See http://sagenb.org/home/pub/676/ for a working version:

from scipy import *
from pylab import *
sample_rate = 1000.0

t = r_[0:0.6:1/sample_rate].astype(float)
N = len(t)
s = sin(2*pi*50*t)+sin(2*pi*70*t+pi/4)
S = fft(s)


f=sample_rate*r_[0:(N/2)]/N
n=len(f)

line(zip(f,abs(S[0:n])/N))


As Minh points out, the need for the "astype" above will go away once
the three tickets (that are up for review!) are reviewed and merged;
hopefully in the next few days.

Thanks,

Jason

Rafael Cardoso Dias Costa

unread,
Jul 26, 2009, 3:23:32 PM7/26/09
to sage-...@googlegroups.com
The problem is solved!

http://www.sagenb.org/home/pub/677/

Thanks, Jason!
Thanks, everyone!

Maurizio

unread,
Jul 26, 2009, 5:36:16 PM7/26/09
to sage-devel
that is wonderful!!

by the way, I am looking forward to see those LTI and Fourier
Transform features applied to our powerful symbolic system :D

Maurizio

On Jul 26, 9:23 pm, Rafael Cardoso Dias Costa <rfc...@gmail.com>
wrote:
> The problem is solved!
>
> http://www.sagenb.org/home/pub/677/
>
> Thanks, Jason!
> Thanks, everyone!
>
> On Sat, Jul 25, 2009 at 10:40 PM, Jason Grout
> <jason-s...@creativetrax.com>wrote:
>
>
>
>
>
> > Rafael Costa wrote:
> > > Why Sage not execute the python code?
>
> > > from scipy import *
> > > from pylab import *
>
> > > sample_rate=1000.00
> > > t=r_[0:0.6:1/sample_rate]
> > > N=len(t)
> > > s=sin(2*pi*50*t)+sin(2*pi*70*t+pi/4)
> > > S=fft(s)
> > > f=sample_rate*r_[0:(N/2)]/N
> > > n=len(f)
> > > plot(f,abs(S[0:n])/N)
>
> > Seehttp://sagenb.org/home/pub/676/for a working version:
Reply all
Reply to author
Forward
0 new messages