From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,47fc49812a5e8e38 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!d37g2000yqm.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: small example, using complex variables in Ada Date: Wed, 9 Jun 2010 04:26:54 -0700 (PDT) Organization: http://groups.google.com Message-ID: <6e70b80b-3030-479f-8378-d1281d1fa847@d37g2000yqm.googlegroups.com> References: NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1276082814 13549 127.0.0.1 (9 Jun 2010 11:26:54 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 9 Jun 2010 11:26:54 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d37g2000yqm.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:11504 Date: 2010-06-09T04:26:54-07:00 List-Id: On Jun 9, 12:49=A0pm, "Nasser M. Abbasi" wrote: > I never used complex variables before in Ada, so for the last 3 hrs I > was learning how to do it. I wrote a simple program, to find the DFT of > an array of 3 elements {1,2,3} (DFT=3Ddiscrete Fourier transform). > > The definition of DFT is one equation with summation, here it is, first > equation you'll see: > > http://en.wikipedia.org/wiki/Discrete_Fourier_transform > > Since I have not programmed in Ada nor in FORTRAN for a looong time, I > am very rusty, I program in Mathematica and sometimes in matlab these > days, but I wanted to try Ada on complex numbers. > > I also wrote a FORTRAN equivalent of the small Ada function. =A0Here is > below the Ada code, and the FORTRAN code. =A0Again, do not scream too muc= h > if it is not good code, I just learned this now, I am sure this can be > improved a lot. > > And for comparing, I show the Matlab and the Mathematica output just to > make sure. > > =3D=3D=3D=3D=3D=3D START ADA =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > -- > -- dtf.adb, compiled with GNAT 4.3.4 20090804 (release) 1 > -- under CYGWIN 1.7.5 > -- gnatmake dft.adb > -- > -- ./dft.exe > -- ( 6.00000E+00, 0.00000E+00) > -- (-1.50000E+00, 8.66026E-01) > -- (-1.50000E+00,-8.66025E-01) > -- $ > > with Ada.Text_IO; use Ada.Text_IO; > with Ada.Numerics.Complex_Types; use =A0Ada.Numerics.Complex_Types; > > with Ada.Numerics; use =A0Ada.Numerics; > > with Ada.Numerics.Complex_Elementary_Functions; > use =A0Ada.Numerics.Complex_Elementary_Functions; > > with Ada.Complex_Text_IO; use Ada.Complex_Text_IO; > > procedure dft is > =A0 =A0 =A0N : positive :=3D 3; > =A0 =A0 =A0J : constant complex :=3D(0.0,1.0); =A0-- SQRT(-1) > =A0 =A0 =A0X : array(0 .. N-1) of Complex =A0:=3D (others=3D>(0.0,0.0)); > =A0 =A0 =A0data : array(0 .. N-1) of float :=3D(1.0,2.0,3.0); > > begin > =A0 =A0 =A0FOR k in X'range LOOP > =A0 =A0 =A0 =A0 =A0FOR m in data'range LOOP > =A0 =A0 =A0 =A0 =A0 =A0 =A0X(k) :=3D X(k) + data(m) * exp(- J*(2.0*Pi)/fl= oat(N) * > float(m*k) ); > =A0 =A0 =A0 =A0 =A0END LOOP; > =A0 =A0 =A0 =A0 =A0put(X(k)); new_line; > =A0 =A0 =A0END LOOP; > > end dft; > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D END ADA =3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > =3D=3D=3D=3D=3D=3D=3D FORTRAN code =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > ! dtf.f90, compiled with GCC 4.3.4 > ! under CYGWIN 1.7.5 > ! gfortran -Wall dft.f90 > ! ./a.exe > ! ( =A06.0000000 =A0 =A0, =A00.0000000 =A0 =A0) > ! ( -1.4999999 =A0 =A0, 0.86602557 =A0 =A0) > ! ( -1.5000005 =A0 =A0,-0.86602497 =A0 =A0) > ! > > PROGRAM dft > > =A0 =A0IMPLICIT NONE > > =A0 =A0INTEGER, PARAMETER :: N =3D 3 > =A0 =A0COMPLEX, parameter :: J =3D(0,1) > > =A0 =A0REAL, parameter :: Pi =3D ACOS(-1.0) > =A0 =A0INTEGER :: k,m > =A0 =A0COMPLEX, dimension(N) :: X > =A0 =A0REAL, dimension(N) :: data=3D(/1.0,2.0,3.0/) > > DO k=3D1,N > =A0 =A0 X(k)=3D(0,0) > =A0 =A0 DO m=3D1,N > =A0 =A0 =A0 =A0X(k) =3D X(k) + data(m) * EXP(-1.0*J*2.0*Pi/N *(m-1)*(k-1)= ) > =A0 =A0 END DO > =A0 =A0 print *,X(k) > > END DO > > END PROGRAM dft > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > =3D=3D=3D=3D Matlab code =3D=3D=3D=3D > EDU>> fft([1,2,3])' > > ans =3D > > =A0 =A0 6.0000 > =A0 =A0-1.5000 - 0.8660i > =A0 =A0-1.5000 + 0.8660i > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D > > =3D=3D=3D Mathematica =3D=3D=3D=3D > In[5]:=3D Chop[Fourier[{1, 2, 3}, FourierParameters -> {1, -1}]] > > Out[5]=3D {6., -1.5 + 0.8660254037844386*I, =A0-1.5 - 0.8660254037844386*= I} > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D > > Conclusion: > I actually liked the Ada implementation more than FORTRAN because: > > 1. In Ada, I did not have to change the index of m and k in the > summation to reflect the 1-off per the definition of DFT. > DFT starts from 0 to N-1. In Ada, using 'Range and defining the arrays > to go from 0 .. N-1 solved the problem. > > 2. In Ada, the compiler complained more times more about types being > mixed up. I placed float() around the places it complained about. > > 3. It actually took me less time to do the Ada function than the FORTRAN > one, even though I am equally not familiar with both at this time :) > > ok, this was a fun learning exercise You should use constants and named numbers instead of variables wherever possible; this simplifies your Ada program. Also, i and j are predefined so you do not need to declare a "new" value for J: with Ada.Text_IO; use Ada.Text_IO; with Ada.Numerics.Complex_Types; use Ada.Numerics.Complex_Types; with Ada.Numerics; use Ada.Numerics; with Ada.Numerics.Complex_Elementary_Functions; use Ada.Numerics.Complex_Elementary_Functions; with Ada.Complex_Text_IO; use Ada.Complex_Text_IO; procedure dft is N : constant :=3D 3; -- named number, no conversion to Float needed X : array(0 .. N-1) of Complex :=3D (others=3D>(0.0,0.0)); data : constant array(0 .. N-1) of float :=3D(1.0,2.0,3.0); Two_Pi_Over_N : constant :=3D 2 * Pi / N; -- named number, outside the loop, like in ARM 3.3.2(9) begin FOR k in X'range LOOP FOR m in data'range LOOP X(k) :=3D X(k) + data(m) * exp(- J*Two_Pi_Over_N * float(m*k) ); END LOOP; put(X(k)); new_line; END LOOP; end dft; -- Ludovic Brenta.