comp.lang.ada
 help / color / mirror / Atom feed
From: Vincent LAFAGE <lafage@ipno.in2p3.fr>
Subject: Re: small example, using complex variables in Ada
Date: Mon, 14 Jun 2010 11:33:26 +0200
Date: 2010-06-14T11:33:26+02:00	[thread overview]
Message-ID: <hv4t13$5qe$1@ccpntc8.in2p3.fr> (raw)
In-Reply-To: <hunrki$ph7$1@speranza.aioe.org>

Hi,

I would like to give some comments on your conclusion.
In fact, I have to disagree with this conclusion.

 > 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.

It is a cliche that Fortran's index have to start at 1.
It was already possible in Fortran 77 to start indexes for where you 
want it to start.
look for instance at 
http://www.physics.nau.edu/~bowman/PHY520/F77tutor/10_arrays.html
       real b(0:19), weird(-162:237)

In your case, it would lead to
   COMPLEX, dimension (0:N-1) :: X
   REAL, dimension (0:N-1) :: data=(/1.0,2.0,3.0/)

In Fortran 90, you do not have the very convenient X'range
but you can use the following statement to keep generality
   DO k = lbound (X, 1), ubound (X, 1)

When speaking about Fortran, we should not forget to specify which one,
Fortran 90 being a completely different beast.
As far as we can compare, the writer of Fortran 90 have drawn a lot from 
Ada 83.

 > 2. In Ada, the compiler complained more times more about types being
 > mixed up. I placed float() around the places it complained about.

We can certainly complain about the implicit type promotion of Fortran.
Still modern compiler provides the same safe-guard against the implicit 
type promotion of Fortran.
For instance,
  $ gfortran-4.3 -Wall -Wsurprising -Wconversion dft.f90 -o dft
will reveal 11 surprising implicit conversion such as
  dft.f90:14.29:
   COMPLEX, parameter :: J =(0,1)
                              1
  Warning: Conversion from INTEGER(4) to REAL(4) at (1)

So "-Wall" is not the last word as far as warning are concerned.

 > 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 :)

A 17 Statement Line Of Code example is not really anything close to 
realistic example for scaling.
Not only the sample size is small, but what is more, it doesn't scale 
linearly, or in the same way.
Besides, you did not tell us how long it took in either case. But that 
would be telling... ;)

I am also an Ada enthusiast, but it does not prevent my being a Fortran 
enthusiast as well.

Best regards,
Vincent

Le 09/06/2010 12:49, Nasser M. Abbasi a �crit :
> 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=discrete 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. Here is
> below the Ada code, and the FORTRAN code. Again, do not scream too much
> 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.
>
>
> ====== START ADA ============
> --
> -- 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 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 : positive := 3;
> J : constant complex :=(0.0,1.0); -- SQRT(-1)
> X : array(0 .. N-1) of Complex := (others=>(0.0,0.0));
> data : array(0 .. N-1) of float :=(1.0,2.0,3.0);
>
> begin
> FOR k in X'range LOOP
> FOR m in data'range LOOP
> X(k) := X(k) + data(m) * exp(- J*(2.0*Pi)/float(N) * float(m*k) );
> END LOOP;
> put(X(k)); new_line;
> END LOOP;
>
> end dft;
> ================== END ADA ==============
>
> ======= FORTRAN code ===========
> ! dtf.f90, compiled with GCC 4.3.4
> ! under CYGWIN 1.7.5
> ! gfortran -Wall dft.f90
> ! ./a.exe
> ! ( 6.0000000 , 0.0000000 )
> ! ( -1.4999999 , 0.86602557 )
> ! ( -1.5000005 ,-0.86602497 )
> !
>
> PROGRAM dft
>
> IMPLICIT NONE
>
> INTEGER, PARAMETER :: N = 3
> COMPLEX, parameter :: J =(0,1)
>
> REAL, parameter :: Pi = ACOS(-1.0)
> INTEGER :: k,m
> COMPLEX, dimension(N) :: X
> REAL, dimension(N) :: data=(/1.0,2.0,3.0/)
>
> DO k=1,N
> X(k)=(0,0)
> DO m=1,N
> X(k) = X(k) + data(m) * EXP(-1.0*J*2.0*Pi/N *(m-1)*(k-1) )
> END DO
> print *,X(k)
>
> END DO
>
> END PROGRAM dft
> ==================================
>
> ==== Matlab code ====
> EDU>> fft([1,2,3])'
>
> ans =
>
> 6.0000
> -1.5000 - 0.8660i
> -1.5000 + 0.8660i
> ===============================
>
> === Mathematica ====
> In[5]:= Chop[Fourier[{1, 2, 3}, FourierParameters -> {1, -1}]]
>
> Out[5]= {6., -1.5 + 0.8660254037844386*I, -1.5 - 0.8660254037844386*I}
> =========================
>
> 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
>
>
> --Nasser




  parent reply	other threads:[~2010-06-14  9:33 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-09 10:49 small example, using complex variables in Ada Nasser M. Abbasi
2010-06-09 11:26 ` Ludovic Brenta
2010-06-09 23:50   ` Jerry
2010-06-10  1:03     ` Jeffrey R. Carter
2010-06-10 15:48   ` Colin Paul Gloster
2010-06-10 14:54     ` Ludovic Brenta
2010-06-10 16:21       ` Colin Paul Gloster
2010-06-10 17:37         ` Adam Beneschan
2010-06-10 17:57         ` Jeffrey R. Carter
2010-06-10 22:32           ` Randy Brukardt
2010-06-11 12:42             ` Colin Paul Gloster
2010-06-11 18:59               ` Randy Brukardt
2010-06-14 19:19                 ` Colin Paul Gloster
2010-06-14 19:48                   ` Nasser M. Abbasi
2010-06-17  7:44     ` Gautier write-only
2010-06-17 10:33       ` Colin Paul Gloster
2010-06-17 14:39       ` Yannick Duchêne (Hibou57)
2010-06-17 16:36         ` Colin Paul Gloster
2010-06-09 12:43 ` Niklas Holsti
2010-06-10  7:23 ` Stephen Leake
2010-06-10  9:12   ` J-P. Rosen
2010-06-10 11:03     ` Yannick Duchêne (Hibou57)
2010-06-10 13:27       ` J-P. Rosen
2010-06-10 21:15         ` Yannick Duchêne (Hibou57)
2010-06-11  7:22           ` Dmitry A. Kazakov
2010-06-11  8:48           ` J-P. Rosen
2010-06-11 12:00             ` Brian Drummond
2010-06-10  9:34   ` Nasser M. Abbasi
2010-06-10 20:12     ` Simon Wright
2010-06-14  9:33 ` Vincent LAFAGE [this message]
2010-06-14 12:29   ` Nasser M. Abbasi
2010-06-14 13:00     ` Vincent LAFAGE
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox